forked from scikit-learn/scikit-learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.pyx
487 lines (412 loc) · 18.7 KB
/
histogram.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
"""This module contains routines for building histograms."""
# Author: Nicolas Hug
cimport cython
from cython.parallel import prange
import numpy as np
from .common import HISTOGRAM_DTYPE
from .common cimport hist_struct
from .common cimport X_BINNED_DTYPE_C
from .common cimport G_H_DTYPE_C
# Notes:
# - IN views are read-only, OUT views are write-only
# - In a lot of functions here, we pass feature_idx and the whole 2d
# histograms arrays instead of just histograms[feature_idx]. This is because
# Cython generated C code will have strange Python interactions (likely
# related to the GIL release and the custom histogram dtype) when using 1d
# histogram arrays that come from 2d arrays.
# - The for loops are un-wrapped, for example:
#
# for i in range(n):
# array[i] = i
#
# will become
#
# for i in range(n // 4):
# array[i] = i
# array[i + 1] = i + 1
# array[i + 2] = i + 2
# array[i + 3] = i + 3
#
# This is to hint gcc that it can auto-vectorize these 4 operations and
# perform them all at once.
@cython.final
cdef class HistogramBuilder:
"""A Histogram builder... used to build histograms.
A histogram is an array with n_bins entries of type HISTOGRAM_DTYPE. Each
feature has its own histogram. A histogram contains the sum of gradients
and hessians of all the samples belonging to each bin.
There are different ways to build a histogram:
- by subtraction: hist(child) = hist(parent) - hist(sibling)
- from scratch. In this case we have routines that update the hessians
or not (not useful when hessians are constant for some losses e.g.
least squares). Also, there's a special case for the root which
contains all the samples, leading to some possible optimizations.
Overall all the implementations look the same, and are optimized for
cache hit.
Parameters
----------
X_binned : ndarray of int, shape (n_samples, n_features)
The binned input samples. Must be Fortran-aligned.
n_bins : int
The total number of bins, including the bin for missing values. Used
to define the shape of the histograms.
gradients : ndarray, shape (n_samples,)
The gradients of each training sample. Those are the gradients of the
loss w.r.t the predictions, evaluated at iteration i - 1.
hessians : ndarray, shape (n_samples,)
The hessians of each training sample. Those are the hessians of the
loss w.r.t the predictions, evaluated at iteration i - 1.
hessians_are_constant : bool
Whether hessians are constant.
"""
cdef public:
const X_BINNED_DTYPE_C [::1, :] X_binned
unsigned int n_features
unsigned int n_bins
G_H_DTYPE_C [::1] gradients
G_H_DTYPE_C [::1] hessians
G_H_DTYPE_C [::1] ordered_gradients
G_H_DTYPE_C [::1] ordered_hessians
unsigned char hessians_are_constant
int n_threads
def __init__(self, const X_BINNED_DTYPE_C [::1, :] X_binned,
unsigned int n_bins, G_H_DTYPE_C [::1] gradients,
G_H_DTYPE_C [::1] hessians,
unsigned char hessians_are_constant,
int n_threads):
self.X_binned = X_binned
self.n_features = X_binned.shape[1]
# Note: all histograms will have <n_bins> bins, but some of the
# bins may be unused if a feature has a small number of unique values.
self.n_bins = n_bins
self.gradients = gradients
self.hessians = hessians
# for root node, gradients and hessians are already ordered
self.ordered_gradients = gradients.copy()
self.ordered_hessians = hessians.copy()
self.hessians_are_constant = hessians_are_constant
self.n_threads = n_threads
def compute_histograms_brute(
HistogramBuilder self,
const unsigned int [::1] sample_indices): # IN
"""Compute the histograms of the node by scanning through all the data.
For a given feature, the complexity is O(n_samples)
Parameters
----------
sample_indices : array of int, shape (n_samples_at_node,)
The indices of the samples at the node to split.
Returns
-------
histograms : ndarray of HISTOGRAM_DTYPE, shape (n_features, n_bins)
The computed histograms of the current node.
"""
cdef:
int n_samples
int feature_idx
int i
# need local views to avoid python interactions
unsigned char hessians_are_constant = \
self.hessians_are_constant
int n_features = self.n_features
G_H_DTYPE_C [::1] ordered_gradients = self.ordered_gradients
G_H_DTYPE_C [::1] gradients = self.gradients
G_H_DTYPE_C [::1] ordered_hessians = self.ordered_hessians
G_H_DTYPE_C [::1] hessians = self.hessians
# Histograms will be initialized to zero later within a prange
hist_struct [:, ::1] histograms = np.empty(
shape=(self.n_features, self.n_bins),
dtype=HISTOGRAM_DTYPE
)
int n_threads = self.n_threads
with nogil:
n_samples = sample_indices.shape[0]
# Populate ordered_gradients and ordered_hessians. (Already done
# for root) Ordering the gradients and hessians helps to improve
# cache hit.
if sample_indices.shape[0] != gradients.shape[0]:
if hessians_are_constant:
for i in prange(n_samples, schedule='static',
num_threads=n_threads):
ordered_gradients[i] = gradients[sample_indices[i]]
else:
for i in prange(n_samples, schedule='static',
num_threads=n_threads):
ordered_gradients[i] = gradients[sample_indices[i]]
ordered_hessians[i] = hessians[sample_indices[i]]
for feature_idx in prange(n_features, schedule='static',
num_threads=n_threads):
# Compute histogram of each feature
self._compute_histogram_brute_single_feature(
feature_idx, sample_indices, histograms)
return histograms
cdef void _compute_histogram_brute_single_feature(
HistogramBuilder self,
const int feature_idx,
const unsigned int [::1] sample_indices, # IN
hist_struct [:, ::1] histograms) nogil: # OUT
"""Compute the histogram for a given feature."""
cdef:
unsigned int n_samples = sample_indices.shape[0]
const X_BINNED_DTYPE_C [::1] X_binned = \
self.X_binned[:, feature_idx]
unsigned int root_node = X_binned.shape[0] == n_samples
G_H_DTYPE_C [::1] ordered_gradients = \
self.ordered_gradients[:n_samples]
G_H_DTYPE_C [::1] ordered_hessians = \
self.ordered_hessians[:n_samples]
unsigned char hessians_are_constant = \
self.hessians_are_constant
unsigned int bin_idx = 0
for bin_idx in range(self.n_bins):
histograms[feature_idx, bin_idx].sum_gradients = 0.
histograms[feature_idx, bin_idx].sum_hessians = 0.
histograms[feature_idx, bin_idx].count = 0
if root_node:
if hessians_are_constant:
_build_histogram_root_no_hessian(feature_idx, X_binned,
ordered_gradients,
histograms)
else:
_build_histogram_root(feature_idx, X_binned,
ordered_gradients, ordered_hessians,
histograms)
else:
if hessians_are_constant:
_build_histogram_no_hessian(feature_idx,
sample_indices, X_binned,
ordered_gradients, histograms)
else:
_build_histogram(feature_idx, sample_indices,
X_binned, ordered_gradients,
ordered_hessians, histograms)
def compute_histograms_subtraction(
HistogramBuilder self,
hist_struct [:, ::1] parent_histograms, # IN
hist_struct [:, ::1] sibling_histograms): # IN
"""Compute the histograms of the node using the subtraction trick.
hist(parent) = hist(left_child) + hist(right_child)
For a given feature, the complexity is O(n_bins). This is much more
efficient than compute_histograms_brute, but it's only possible for one
of the siblings.
Parameters
----------
parent_histograms : ndarray of HISTOGRAM_DTYPE, \
shape (n_features, n_bins)
The histograms of the parent.
sibling_histograms : ndarray of HISTOGRAM_DTYPE, \
shape (n_features, n_bins)
The histograms of the sibling.
Returns
-------
histograms : ndarray of HISTOGRAM_DTYPE, shape(n_features, n_bins)
The computed histograms of the current node.
"""
cdef:
int feature_idx
int n_features = self.n_features
hist_struct [:, ::1] histograms = np.empty(
shape=(self.n_features, self.n_bins),
dtype=HISTOGRAM_DTYPE
)
int n_threads = self.n_threads
for feature_idx in prange(n_features, schedule='static', nogil=True,
num_threads=n_threads):
# Compute histogram of each feature
_subtract_histograms(feature_idx,
self.n_bins,
parent_histograms,
sibling_histograms,
histograms)
return histograms
cpdef void _build_histogram_naive(
const int feature_idx,
unsigned int [:] sample_indices, # IN
X_BINNED_DTYPE_C [:] binned_feature, # IN
G_H_DTYPE_C [:] ordered_gradients, # IN
G_H_DTYPE_C [:] ordered_hessians, # IN
hist_struct [:, :] out) nogil: # OUT
"""Build histogram in a naive way, without optimizing for cache hit.
Used in tests to compare with the optimized version."""
cdef:
unsigned int i
unsigned int n_samples = sample_indices.shape[0]
unsigned int sample_idx
unsigned int bin_idx
for i in range(n_samples):
sample_idx = sample_indices[i]
bin_idx = binned_feature[sample_idx]
out[feature_idx, bin_idx].sum_gradients += ordered_gradients[i]
out[feature_idx, bin_idx].sum_hessians += ordered_hessians[i]
out[feature_idx, bin_idx].count += 1
cpdef void _subtract_histograms(
const int feature_idx,
unsigned int n_bins,
hist_struct [:, ::1] hist_a, # IN
hist_struct [:, ::1] hist_b, # IN
hist_struct [:, ::1] out) nogil: # OUT
"""compute (hist_a - hist_b) in out"""
cdef:
unsigned int i = 0
for i in range(n_bins):
out[feature_idx, i].sum_gradients = (
hist_a[feature_idx, i].sum_gradients -
hist_b[feature_idx, i].sum_gradients
)
out[feature_idx, i].sum_hessians = (
hist_a[feature_idx, i].sum_hessians -
hist_b[feature_idx, i].sum_hessians
)
out[feature_idx, i].count = (
hist_a[feature_idx, i].count -
hist_b[feature_idx, i].count
)
cpdef void _build_histogram(
const int feature_idx,
const unsigned int [::1] sample_indices, # IN
const X_BINNED_DTYPE_C [::1] binned_feature, # IN
const G_H_DTYPE_C [::1] ordered_gradients, # IN
const G_H_DTYPE_C [::1] ordered_hessians, # IN
hist_struct [:, ::1] out) nogil: # OUT
"""Return histogram for a given feature."""
cdef:
unsigned int i = 0
unsigned int n_node_samples = sample_indices.shape[0]
unsigned int unrolled_upper = (n_node_samples // 4) * 4
unsigned int bin_0
unsigned int bin_1
unsigned int bin_2
unsigned int bin_3
unsigned int bin_idx
for i in range(0, unrolled_upper, 4):
bin_0 = binned_feature[sample_indices[i]]
bin_1 = binned_feature[sample_indices[i + 1]]
bin_2 = binned_feature[sample_indices[i + 2]]
bin_3 = binned_feature[sample_indices[i + 3]]
out[feature_idx, bin_0].sum_gradients += ordered_gradients[i]
out[feature_idx, bin_1].sum_gradients += ordered_gradients[i + 1]
out[feature_idx, bin_2].sum_gradients += ordered_gradients[i + 2]
out[feature_idx, bin_3].sum_gradients += ordered_gradients[i + 3]
out[feature_idx, bin_0].sum_hessians += ordered_hessians[i]
out[feature_idx, bin_1].sum_hessians += ordered_hessians[i + 1]
out[feature_idx, bin_2].sum_hessians += ordered_hessians[i + 2]
out[feature_idx, bin_3].sum_hessians += ordered_hessians[i + 3]
out[feature_idx, bin_0].count += 1
out[feature_idx, bin_1].count += 1
out[feature_idx, bin_2].count += 1
out[feature_idx, bin_3].count += 1
for i in range(unrolled_upper, n_node_samples):
bin_idx = binned_feature[sample_indices[i]]
out[feature_idx, bin_idx].sum_gradients += ordered_gradients[i]
out[feature_idx, bin_idx].sum_hessians += ordered_hessians[i]
out[feature_idx, bin_idx].count += 1
cpdef void _build_histogram_no_hessian(
const int feature_idx,
const unsigned int [::1] sample_indices, # IN
const X_BINNED_DTYPE_C [::1] binned_feature, # IN
const G_H_DTYPE_C [::1] ordered_gradients, # IN
hist_struct [:, ::1] out) nogil: # OUT
"""Return histogram for a given feature, not updating hessians.
Used when the hessians of the loss are constant (typically LS loss).
"""
cdef:
unsigned int i = 0
unsigned int n_node_samples = sample_indices.shape[0]
unsigned int unrolled_upper = (n_node_samples // 4) * 4
unsigned int bin_0
unsigned int bin_1
unsigned int bin_2
unsigned int bin_3
unsigned int bin_idx
for i in range(0, unrolled_upper, 4):
bin_0 = binned_feature[sample_indices[i]]
bin_1 = binned_feature[sample_indices[i + 1]]
bin_2 = binned_feature[sample_indices[i + 2]]
bin_3 = binned_feature[sample_indices[i + 3]]
out[feature_idx, bin_0].sum_gradients += ordered_gradients[i]
out[feature_idx, bin_1].sum_gradients += ordered_gradients[i + 1]
out[feature_idx, bin_2].sum_gradients += ordered_gradients[i + 2]
out[feature_idx, bin_3].sum_gradients += ordered_gradients[i + 3]
out[feature_idx, bin_0].count += 1
out[feature_idx, bin_1].count += 1
out[feature_idx, bin_2].count += 1
out[feature_idx, bin_3].count += 1
for i in range(unrolled_upper, n_node_samples):
bin_idx = binned_feature[sample_indices[i]]
out[feature_idx, bin_idx].sum_gradients += ordered_gradients[i]
out[feature_idx, bin_idx].count += 1
cpdef void _build_histogram_root(
const int feature_idx,
const X_BINNED_DTYPE_C [::1] binned_feature, # IN
const G_H_DTYPE_C [::1] all_gradients, # IN
const G_H_DTYPE_C [::1] all_hessians, # IN
hist_struct [:, ::1] out) nogil: # OUT
"""Compute histogram of the root node.
Unlike other nodes, the root node has to find the split among *all* the
samples from the training set. binned_feature and all_gradients /
all_hessians already have a consistent ordering.
"""
cdef:
unsigned int i = 0
unsigned int n_samples = binned_feature.shape[0]
unsigned int unrolled_upper = (n_samples // 4) * 4
unsigned int bin_0
unsigned int bin_1
unsigned int bin_2
unsigned int bin_3
unsigned int bin_idx
for i in range(0, unrolled_upper, 4):
bin_0 = binned_feature[i]
bin_1 = binned_feature[i + 1]
bin_2 = binned_feature[i + 2]
bin_3 = binned_feature[i + 3]
out[feature_idx, bin_0].sum_gradients += all_gradients[i]
out[feature_idx, bin_1].sum_gradients += all_gradients[i + 1]
out[feature_idx, bin_2].sum_gradients += all_gradients[i + 2]
out[feature_idx, bin_3].sum_gradients += all_gradients[i + 3]
out[feature_idx, bin_0].sum_hessians += all_hessians[i]
out[feature_idx, bin_1].sum_hessians += all_hessians[i + 1]
out[feature_idx, bin_2].sum_hessians += all_hessians[i + 2]
out[feature_idx, bin_3].sum_hessians += all_hessians[i + 3]
out[feature_idx, bin_0].count += 1
out[feature_idx, bin_1].count += 1
out[feature_idx, bin_2].count += 1
out[feature_idx, bin_3].count += 1
for i in range(unrolled_upper, n_samples):
bin_idx = binned_feature[i]
out[feature_idx, bin_idx].sum_gradients += all_gradients[i]
out[feature_idx, bin_idx].sum_hessians += all_hessians[i]
out[feature_idx, bin_idx].count += 1
cpdef void _build_histogram_root_no_hessian(
const int feature_idx,
const X_BINNED_DTYPE_C [::1] binned_feature, # IN
const G_H_DTYPE_C [::1] all_gradients, # IN
hist_struct [:, ::1] out) nogil: # OUT
"""Compute histogram of the root node, not updating hessians.
Used when the hessians of the loss are constant (typically LS loss).
"""
cdef:
unsigned int i = 0
unsigned int n_samples = binned_feature.shape[0]
unsigned int unrolled_upper = (n_samples // 4) * 4
unsigned int bin_0
unsigned int bin_1
unsigned int bin_2
unsigned int bin_3
unsigned int bin_idx
for i in range(0, unrolled_upper, 4):
bin_0 = binned_feature[i]
bin_1 = binned_feature[i + 1]
bin_2 = binned_feature[i + 2]
bin_3 = binned_feature[i + 3]
out[feature_idx, bin_0].sum_gradients += all_gradients[i]
out[feature_idx, bin_1].sum_gradients += all_gradients[i + 1]
out[feature_idx, bin_2].sum_gradients += all_gradients[i + 2]
out[feature_idx, bin_3].sum_gradients += all_gradients[i + 3]
out[feature_idx, bin_0].count += 1
out[feature_idx, bin_1].count += 1
out[feature_idx, bin_2].count += 1
out[feature_idx, bin_3].count += 1
for i in range(unrolled_upper, n_samples):
bin_idx = binned_feature[i]
out[feature_idx, bin_idx].sum_gradients += all_gradients[i]
out[feature_idx, bin_idx].count += 1