bin.h 17.5 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
#ifndef LIGHTGBM_BIN_H_
#define LIGHTGBM_BIN_H_

4
5
#include <LightGBM/utils/common.h>

Guolin Ke's avatar
Guolin Ke committed
6
7
8
9
#include <LightGBM/meta.h>

#include <vector>
#include <functional>
Guolin Ke's avatar
Guolin Ke committed
10
#include <unordered_map>
11
#include <sstream>
Guolin Ke's avatar
Guolin Ke committed
12
13
14

namespace LightGBM {

15
16
17
18
19
20
enum BinType {
  NumericalBin,
  CategoricalBin
};


Guolin Ke's avatar
Guolin Ke committed
21
22
23
24
/*! \brief Store data for one histogram bin */
struct HistogramBinEntry {
public:
  /*! \brief Sum of gradients on this bin */
Guolin Ke's avatar
Guolin Ke committed
25
  float sum_gradients = 0.0f;
Guolin Ke's avatar
Guolin Ke committed
26
  /*! \brief Sum of hessians on this bin */
Guolin Ke's avatar
Guolin Ke committed
27
  float sum_hessians = 0.0f;
Guolin Ke's avatar
Guolin Ke committed
28
29
30
  /*! \brief Number of data on this bin */
  data_size_t cnt = 0;
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
31
  * \brief Sum up (reducers) functions for histogram bin
Guolin Ke's avatar
Guolin Ke committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  */
  inline static void SumReducer(const char *src, char *dst, int len) {
    const int type_size = sizeof(HistogramBinEntry);
    int used_size = 0;
    const HistogramBinEntry* p1;
    HistogramBinEntry* p2;
    while (used_size < len) {
      // convert
      p1 = reinterpret_cast<const HistogramBinEntry*>(src);
      p2 = reinterpret_cast<HistogramBinEntry*>(dst);
      // add
      p2->cnt += p1->cnt;
      p2->sum_gradients += p1->sum_gradients;
      p2->sum_hessians += p1->sum_hessians;
      src += type_size;
      dst += type_size;
      used_size += type_size;
    }
  }
};

Qiwei Ye's avatar
Qiwei Ye committed
53
54
/*! \brief This class used to convert feature values into bin,
*          and store some meta information for bin*/
Guolin Ke's avatar
Guolin Ke committed
55
56
57
58
59
60
61
class BinMapper {
public:
  BinMapper();
  BinMapper(const BinMapper& other);
  explicit BinMapper(const void* memory);
  ~BinMapper();

62
63
64
65
  bool CheckAlign(const BinMapper& other) const {
    if (num_bin_ != other.num_bin_) {
      return false;
    }
66
67
68
69
70
71
72
73
74
75
76
    if (bin_type_ == BinType::NumericalBin) {
      for (int i = 0; i < num_bin_; ++i) {
        if (bin_upper_bound_[i] != other.bin_upper_bound_[i]) {
          return false;
        }
      }
    } else {
      for (int i = 0; i < num_bin_; i++) {
        if (bin_2_categorical_[i] != other.bin_2_categorical_[i]) {
          return false;
        }
77
78
79
80
81
      }
    }
    return true;
  }

Guolin Ke's avatar
Guolin Ke committed
82
83
  /*! \brief Get number of bins */
  inline int num_bin() const { return num_bin_; }
Qiwei Ye's avatar
Qiwei Ye committed
84
  /*! \brief True if bin is trival (contains only one bin) */
Guolin Ke's avatar
Guolin Ke committed
85
  inline bool is_trival() const { return is_trival_; }
Qiwei Ye's avatar
Qiwei Ye committed
86
  /*! \brief Sparsity of this bin ( num_zero_bins / num_data ) */
87
  inline double sparse_rate() const { return sparse_rate_; }
Guolin Ke's avatar
Guolin Ke committed
88
89
90
91
92
93
  /*!
  * \brief Save binary data to file
  * \param file File want to write
  */
  void SaveBinaryToFile(FILE* file) const;
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
94
  * \brief Mapping bin into feature value
Guolin Ke's avatar
Guolin Ke committed
95
  * \param bin
Qiwei Ye's avatar
Qiwei Ye committed
96
  * \return Feature value of this bin
Guolin Ke's avatar
Guolin Ke committed
97
  */
Guolin Ke's avatar
Guolin Ke committed
98
  inline double BinToValue(uint32_t bin) const {
99
100
101
102
103
    if (bin_type_ == BinType::NumericalBin) {
      return bin_upper_bound_[bin];
    } else {
      return bin_2_categorical_[bin];
    }
Guolin Ke's avatar
Guolin Ke committed
104
105
106
107
108
109
  }
  /*!
  * \brief Get sizes in byte of this object
  */
  size_t SizesInByte() const;
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
110
  * \brief Mapping feature value into bin 
Guolin Ke's avatar
Guolin Ke committed
111
112
113
  * \param value
  * \return bin for this feature value
  */
Guolin Ke's avatar
Guolin Ke committed
114
  inline uint32_t ValueToBin(double value) const;
Guolin Ke's avatar
Guolin Ke committed
115

Guolin Ke's avatar
Guolin Ke committed
116
  /*!
Guolin Ke's avatar
Guolin Ke committed
117
  * \brief Get the default bin when value is 0
Guolin Ke's avatar
Guolin Ke committed
118
119
120
  * \return default bin
  */
  inline uint32_t GetDefaultBin() const {
Guolin Ke's avatar
Guolin Ke committed
121
    return default_bin_;
Guolin Ke's avatar
Guolin Ke committed
122
  }
Guolin Ke's avatar
Guolin Ke committed
123
124
  /*!
  * \brief Construct feature value to bin mapper according feature values
125
  * \param values (Sampled) values of this feature, Note: not include zero. 
126
  * \param num_values number of values.
127
  * \param total_sample_cnt number of total sample count, equal with values.size() + num_zeros
Guolin Ke's avatar
Guolin Ke committed
128
  * \param max_bin The maximal number of bin
Guolin Ke's avatar
Guolin Ke committed
129
  * \param min_data_in_bin min number of data in one bin
zhangyafeikimi's avatar
zhangyafeikimi committed
130
  * \param min_split_data
131
  * \param bin_type Type of this bin
Guolin Ke's avatar
Guolin Ke committed
132
  */
133
  void FindBin(double* values, int num_values, size_t total_sample_cnt, int max_bin, int min_data_in_bin, int min_split_data, BinType bin_type);
Guolin Ke's avatar
Guolin Ke committed
134
135
136
137
138
139
140
141
142

  /*!
  * \brief Use specific number of bin to calculate the size of this class
  * \param bin The number of bin
  * \return Size
  */
  static int SizeForSpecificBin(int bin);

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
143
  * \brief Seirilizing this object to buffer
Guolin Ke's avatar
Guolin Ke committed
144
145
146
147
148
  * \param buffer The destination
  */
  void CopyTo(char* buffer);

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
149
  * \brief Deserilizing this object from buffer
Guolin Ke's avatar
Guolin Ke committed
150
151
152
  * \param buffer The source
  */
  void CopyFrom(const char* buffer);
153
154
155
156
157
158

  /*!
  * \brief Get bin types
  */
  inline BinType bin_type() const { return bin_type_; }

159
160
161
162
  /*!
  * \brief Get bin info
  */
  inline std::string bin_info() const {
163
164
165
166
167
168
169
170
    if (bin_type_ == BinType::CategoricalBin) {
      return Common::Join(bin_2_categorical_, ":");
    } else {
      std::stringstream str_buf;
      str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
      str_buf << '[' << min_val_ << ':' << max_val_ << ']';
      return str_buf.str();
    }
171
  }
Guolin Ke's avatar
Guolin Ke committed
172

Guolin Ke's avatar
Guolin Ke committed
173
174
175
176
private:
  /*! \brief Number of bins */
  int num_bin_;
  /*! \brief Store upper bound for each bin */
Guolin Ke's avatar
Guolin Ke committed
177
  std::vector<double> bin_upper_bound_;
Guolin Ke's avatar
Guolin Ke committed
178
179
180
  /*! \brief True if this feature is trival */
  bool is_trival_;
  /*! \brief Sparse rate of this bins( num_bin0/num_data ) */
181
  double sparse_rate_;
182
183
184
185
186
187
  /*! \brief Type of this bin */
  BinType bin_type_;
  /*! \brief Mapper from categorical to bin */
  std::unordered_map<int, unsigned int> categorical_2_bin_;
  /*! \brief Mapper from bin to categorical */
  std::vector<int> bin_2_categorical_;
188
189
190
191
  /*! \brief minimal feature vaule */
  double min_val_;
  /*! \brief maximum feature value */
  double max_val_;
Guolin Ke's avatar
Guolin Ke committed
192
193
  /*! \brief bin value of feature value 0 */
  uint32_t default_bin_;
Guolin Ke's avatar
Guolin Ke committed
194
195
196
};

/*!
Qiwei Ye's avatar
Qiwei Ye committed
197
198
199
200
201
202
* \brief Interface for ordered bin data. efficient for construct histogram, especially for sparse bin
*        There are 2 advantages by using ordered bin.
*        1. group the data by leafs to improve the cache hit.
*        2. only store the non-zero bin, which can speed up the histogram consturction for sparse features.
*        However it brings additional cost: it need re-order the bins after every split, which will cost much for dense feature.
*        So we only using ordered bin for sparse situations.
Guolin Ke's avatar
Guolin Ke committed
203
204
205
206
207
208
209
*/
class OrderedBin {
public:
  /*! \brief virtual destructor */
  virtual ~OrderedBin() {}

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
210
  * \brief Initialization logic.
Guolin Ke's avatar
Guolin Ke committed
211
  * \param used_indices If used_indices.size() == 0 means using all data, otherwise, used_indices[i] == true means i-th data is used
Qiwei Ye's avatar
Qiwei Ye committed
212
213
           (this logic was build for bagging logic)
  * \param num_leaves Number of leaves on this iteration
Guolin Ke's avatar
Guolin Ke committed
214
  */
zhangyafeikimi's avatar
zhangyafeikimi committed
215
  virtual void Init(const char* used_indices, data_size_t num_leaves) = 0;
Guolin Ke's avatar
Guolin Ke committed
216
217
218

  /*!
  * \brief Construct histogram by using this bin
Qiwei Ye's avatar
Qiwei Ye committed
219
220
  *        Note: Unlike Bin, OrderedBin doesn't use ordered gradients and ordered hessians.
  *        Because it is hard to know the relative index in one leaf for sparse bin, since we skipped zero bins.
Guolin Ke's avatar
Guolin Ke committed
221
222
223
  * \param leaf Using which leaf's data to construct
  * \param gradients Gradients, Note:non-oredered by leaf
  * \param hessians Hessians, Note:non-oredered by leaf
Guolin Ke's avatar
Guolin Ke committed
224
  * \param num_bin The number of bins
Guolin Ke's avatar
Guolin Ke committed
225
226
  * \param out Output Result
  */
Guolin Ke's avatar
Guolin Ke committed
227
228
  virtual void ConstructHistogram(int leaf, const float* gradients,
    const float* hessians, int num_bin, HistogramBinEntry* out) const = 0;
Guolin Ke's avatar
Guolin Ke committed
229

230
231
232
233
234
235
  /*!
  * \brief Construct histogram by using this bin
  *        Note: Unlike Bin, OrderedBin doesn't use ordered gradients and ordered hessians.
  *        Because it is hard to know the relative index in one leaf for sparse bin, since we skipped zero bins.
  * \param leaf Using which leaf's data to construct
  * \param gradients Gradients, Note:non-oredered by leaf
Guolin Ke's avatar
Guolin Ke committed
236
  * \param num_bin The number of bins
237
238
  * \param out Output Result
  */
Guolin Ke's avatar
Guolin Ke committed
239
  virtual void ConstructHistogram(int leaf, const float* gradients, int num_bin, HistogramBinEntry* out) const = 0;
240

Guolin Ke's avatar
Guolin Ke committed
241
242
243
244
  /*!
  * \brief Split current bin, and perform re-order by leaf
  * \param leaf Using which leaf's to split
  * \param right_leaf The new leaf index after perform this split
Guolin Ke's avatar
Guolin Ke committed
245
246
  * \param is_in_leaf is_in_leaf[i] == mark means the i-th data will be on left leaf after split
  * \param mark is_in_leaf[i] == mark means the i-th data will be on left leaf after split
Guolin Ke's avatar
Guolin Ke committed
247
  */
Guolin Ke's avatar
Guolin Ke committed
248
249
250
  virtual void Split(int leaf, int right_leaf, const char* is_in_leaf, char mark) = 0;

  virtual data_size_t NonZeroCount(int leaf) const = 0;
Guolin Ke's avatar
Guolin Ke committed
251
252
253
254
255
256
257
258
259
260
261
};

/*! \brief Iterator for one bin column */
class BinIterator {
public:
  /*!
  * \brief Get bin data on specific row index
  * \param idx Index of this data
  * \return Bin data
  */
  virtual uint32_t Get(data_size_t idx) = 0;
262
  virtual uint32_t RawGet(data_size_t idx) = 0;
Guolin Ke's avatar
Guolin Ke committed
263
  virtual void Reset(data_size_t idx) = 0;
264
  virtual ~BinIterator() = default;
Guolin Ke's avatar
Guolin Ke committed
265
266
267
268
};

/*!
* \brief Interface for bin data. This class will store bin data for one feature.
Qiwei Ye's avatar
Qiwei Ye committed
269
270
271
*        unlike OrderedBin, this class will store data by original order.
*        Note that it may cause cache misses when construct histogram,
*        but it doesn't need to re-order operation, So it will be faster than OrderedBin for dense feature
Guolin Ke's avatar
Guolin Ke committed
272
273
274
275
276
277
278
279
280
281
282
283
284
*/
class Bin {
public:
  /*! \brief virtual destructor */
  virtual ~Bin() {}
  /*!
  * \brief Push one record
  * \pram tid Thread id
  * \param idx Index of record
  * \param value bin value of record
  */
  virtual void Push(int tid, data_size_t idx, uint32_t value) = 0;

285
286

  virtual void CopySubset(const Bin* full_bin, const data_size_t* used_indices, data_size_t num_used_indices) = 0;
Guolin Ke's avatar
Guolin Ke committed
287
  /*!
zhangyafeikimi's avatar
zhangyafeikimi committed
288
  * \brief Get bin iterator of this bin for specific feature
Guolin Ke's avatar
Guolin Ke committed
289
290
  * \param min_bin min_bin of current used feature
  * \param max_bin max_bin of current used feature
zhangyafeikimi's avatar
zhangyafeikimi committed
291
  * \param default_bin default bin if bin not in [min_bin, max_bin]
Guolin Ke's avatar
Guolin Ke committed
292
293
  * \return Iterator of this bin
  */
Guolin Ke's avatar
Guolin Ke committed
294
  virtual BinIterator* GetIterator(uint32_t min_bin, uint32_t max_bin, uint32_t default_bin) const = 0;
Guolin Ke's avatar
Guolin Ke committed
295
296
297
298
299
300
301
302
303

  /*!
  * \brief Save binary data to file
  * \param file File want to write
  */
  virtual void SaveBinaryToFile(FILE* file) const = 0;

  /*!
  * \brief Load from memory
zhangyafeikimi's avatar
zhangyafeikimi committed
304
305
  * \param memory
  * \param local_used_indices
Guolin Ke's avatar
Guolin Ke committed
306
307
308
309
310
311
312
313
314
315
316
317
  */
  virtual void LoadFromMemory(const void* memory,
    const std::vector<data_size_t>& local_used_indices) = 0;

  /*!
  * \brief Get sizes in byte of this object
  */
  virtual size_t SizesInByte() const = 0;

  /*! \brief Number of all data */
  virtual data_size_t num_data() const = 0;

Guolin Ke's avatar
Guolin Ke committed
318
319
  virtual void ReSize(data_size_t num_data) = 0;

Guolin Ke's avatar
Guolin Ke committed
320
321
  /*!
  * \brief Construct histogram of this feature,
Qiwei Ye's avatar
Qiwei Ye committed
322
  *        Note: We use ordered_gradients and ordered_hessians to improve cache hit chance
zhangyafeikimi's avatar
zhangyafeikimi committed
323
  *        The naive solution is using gradients[data_indices[i]] for data_indices[i] to get gradients,
Qiwei Ye's avatar
Qiwei Ye committed
324
325
326
           which is not cache friendly, since the access of memory is not continuous.
  *        ordered_gradients and ordered_hessians are preprocessed, and they are re-ordered by data_indices.
  *        Ordered_gradients[i] is aligned with data_indices[i]'s gradients (same for ordered_hessians).
Guolin Ke's avatar
Guolin Ke committed
327
328
329
330
  * \param data_indices Used data indices in current leaf
  * \param num_data Number of used data
  * \param ordered_gradients Pointer to gradients, the data_indices[i]-th data's gradient is ordered_gradients[i]
  * \param ordered_hessians Pointer to hessians, the data_indices[i]-th data's hessian is ordered_hessians[i]
Guolin Ke's avatar
Guolin Ke committed
331
  * \param num_bin The number of bins
Guolin Ke's avatar
Guolin Ke committed
332
333
334
  * \param out Output Result
  */
  virtual void ConstructHistogram(
Guolin Ke's avatar
Guolin Ke committed
335
    const data_size_t* data_indices, data_size_t num_data,
Guolin Ke's avatar
Guolin Ke committed
336
337
338
339
340
    const float* ordered_gradients, const float* ordered_hessians, int num_bin,
    HistogramBinEntry* out) const = 0;

  virtual void ConstructHistogram(data_size_t num_data,
    const float* ordered_gradients, const float* ordered_hessians, int num_bin,
Guolin Ke's avatar
Guolin Ke committed
341
342
    HistogramBinEntry* out) const = 0;

343
344
345
346
347
348
349
350
351
352
  /*!
  * \brief Construct histogram of this feature,
  *        Note: We use ordered_gradients and ordered_hessians to improve cache hit chance
  *        The naive solution is using gradients[data_indices[i]] for data_indices[i] to get gradients,
  which is not cache friendly, since the access of memory is not continuous.
  *        ordered_gradients and ordered_hessians are preprocessed, and they are re-ordered by data_indices.
  *        Ordered_gradients[i] is aligned with data_indices[i]'s gradients (same for ordered_hessians).
  * \param data_indices Used data indices in current leaf
  * \param num_data Number of used data
  * \param ordered_gradients Pointer to gradients, the data_indices[i]-th data's gradient is ordered_gradients[i]
Guolin Ke's avatar
Guolin Ke committed
353
  * \param num_bin The number of bins
354
355
356
  * \param out Output Result
  */
  virtual void ConstructHistogram(const data_size_t* data_indices, data_size_t num_data,
Guolin Ke's avatar
Guolin Ke committed
357
358
359
360
                                  const float* ordered_gradients, int num_bin, HistogramBinEntry* out) const = 0;

  virtual void ConstructHistogram(data_size_t num_data,
                                  const float* ordered_gradients, int num_bin, HistogramBinEntry* out) const = 0;
361

Guolin Ke's avatar
Guolin Ke committed
362
363
  /*!
  * \brief Split data according to threshold, if bin <= threshold, will put into left(lte_indices), else put into right(gt_indices)
Guolin Ke's avatar
Guolin Ke committed
364
365
366
  * \param min_bin min_bin of current used feature
  * \param max_bin max_bin of current used feature
  * \param default_bin defualt bin if bin not in [min_bin, max_bin]
Guolin Ke's avatar
Guolin Ke committed
367
368
369
370
371
  * \param threshold The split threshold.
  * \param data_indices Used data indices. After called this function. The less than or equal data indices will store on this object.
  * \param num_data Number of used data
  * \param lte_indices After called this function. The less or equal data indices will store on this object.
  * \param gt_indices After called this function. The greater data indices will store on this object.
372
  * \param bin_type type of bin
Guolin Ke's avatar
Guolin Ke committed
373
374
  * \return The number of less than or equal data.
  */
Guolin Ke's avatar
Guolin Ke committed
375
376
  virtual data_size_t Split(uint32_t min_bin, uint32_t max_bin, 
    uint32_t default_bin, uint32_t threshold,
Guolin Ke's avatar
Guolin Ke committed
377
    data_size_t* data_indices, data_size_t num_data,
378
    data_size_t* lte_indices, data_size_t* gt_indices, BinType bin_type) const = 0;
Guolin Ke's avatar
Guolin Ke committed
379
380
381
382
383
384
385
386

  /*!
  * \brief Create the ordered bin for this bin
  * \return Pointer to ordered bin
  */
  virtual OrderedBin* CreateOrderedBin() const = 0;

  /*!
Hui Xue's avatar
Hui Xue committed
387
  * \brief After pushed all feature data, call this could have better refactor for bin data
Guolin Ke's avatar
Guolin Ke committed
388
389
390
391
392
393
394
395
396
  */
  virtual void FinishLoad() = 0;

  /*!
  * \brief Create object for bin data of one feature, will call CreateDenseBin or CreateSparseBin according to "is_sparse"
  * \param num_data Total number of data
  * \param num_bin Number of bin
  * \param sparse_rate Sparse rate of this bins( num_bin0/num_data )
  * \param is_enable_sparse True if enable sparse feature
397
  * \param sparse_threshold Threshold for treating a feature as a sparse feature
Guolin Ke's avatar
Guolin Ke committed
398
  * \param is_sparse Will set to true if this bin is sparse
Guolin Ke's avatar
Guolin Ke committed
399
  * \param default_bin Default bin for zeros value
Guolin Ke's avatar
Guolin Ke committed
400
401
402
  * \return The bin data object
  */
  static Bin* CreateBin(data_size_t num_data, int num_bin,
403
    double sparse_rate, bool is_enable_sparse, double sparse_threshold, bool* is_sparse);
Guolin Ke's avatar
Guolin Ke committed
404
405
406
407
408
409
410

  /*!
  * \brief Create object for bin data of one feature, used for dense feature
  * \param num_data Total number of data
  * \param num_bin Number of bin
  * \return The bin data object
  */
Guolin Ke's avatar
Guolin Ke committed
411
  static Bin* CreateDenseBin(data_size_t num_data, int num_bin);
Guolin Ke's avatar
Guolin Ke committed
412
413
414
415
416
417
418

  /*!
  * \brief Create object for bin data of one feature, used for sparse feature
  * \param num_data Total number of data
  * \param num_bin Number of bin
  * \return The bin data object
  */
Guolin Ke's avatar
Guolin Ke committed
419
  static Bin* CreateSparseBin(data_size_t num_data, int num_bin);
Guolin Ke's avatar
Guolin Ke committed
420
421
};

Guolin Ke's avatar
Guolin Ke committed
422
inline uint32_t BinMapper::ValueToBin(double value) const {
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
  if (bin_type_ == BinType::NumericalBin) {
    // binary search to find bin
    int l = 0;
    int r = num_bin_ - 1;
    while (l < r) {
      int m = (r + l - 1) / 2;
      if (value <= bin_upper_bound_[m]) {
        r = m;
      } else {
        l = m + 1;
      }
    }
    return l;
  } else {
    int int_value = static_cast<int>(value);
    if (categorical_2_bin_.count(int_value)) {
      return categorical_2_bin_.at(int_value);
Guolin Ke's avatar
Guolin Ke committed
440
    } else {
441
      return num_bin_ - 1;
Guolin Ke's avatar
Guolin Ke committed
442
443
444
445
    }
  }
}

Guolin Ke's avatar
Guolin Ke committed
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#define AddGradientPtrToHistogram(data, bin0, bin1, bin2, bin3, bin4, bin5, bin6, bin7, \
gptr) { \
data[bin0].sum_gradients += *(gptr + 0);\
data[bin1].sum_gradients += *(gptr + 1);\
data[bin2].sum_gradients += *(gptr + 2);\
data[bin3].sum_gradients += *(gptr + 3);\
data[bin4].sum_gradients += *(gptr + 4);\
data[bin5].sum_gradients += *(gptr + 5);\
data[bin6].sum_gradients += *(gptr + 6);\
data[bin7].sum_gradients += *(gptr + 7);\
}

#define AddGradientToHistogram(data, bin0, bin1, bin2, bin3, bin4, bin5, bin6, bin7, \
g0, g1, g2, g3, g4, g5, g6, g7) { \
data[bin0].sum_gradients += (g0);\
data[bin1].sum_gradients += (g1);\
data[bin2].sum_gradients += (g2);\
data[bin3].sum_gradients += (g3);\
data[bin4].sum_gradients += (g4);\
data[bin5].sum_gradients += (g5);\
data[bin6].sum_gradients += (g6);\
data[bin7].sum_gradients += (g7);\
}

#define AddHessianPtrToHistogram(data, bin0, bin1, bin2, bin3, bin4, bin5, bin6, bin7, \
hptr) { \
data[bin0].sum_hessians += *(hptr + 0);\
data[bin1].sum_hessians += *(hptr + 1);\
data[bin2].sum_hessians += *(hptr + 2);\
data[bin3].sum_hessians += *(hptr + 3);\
data[bin4].sum_hessians += *(hptr + 4);\
data[bin5].sum_hessians += *(hptr + 5);\
data[bin6].sum_hessians += *(hptr + 6);\
data[bin7].sum_hessians += *(hptr + 7);\
}

#define AddHessianToHistogram(data, bin0, bin1, bin2, bin3, bin4, bin5, bin6, bin7, \
h0, h1, h2, h3, h4, h5, h6, h7) { \
data[bin0].sum_hessians += (h0);\
data[bin1].sum_hessians += (h1);\
data[bin2].sum_hessians += (h2);\
data[bin3].sum_hessians += (h3);\
data[bin4].sum_hessians += (h4);\
data[bin5].sum_hessians += (h5);\
data[bin6].sum_hessians += (h6);\
data[bin7].sum_hessians += (h7);\
}

#define AddCountToHistogram(data, bin0, bin1, bin2, bin3, bin4, bin5, bin6, bin7) { \
++data[bin0].cnt;\
++data[bin1].cnt;\
++data[bin2].cnt;\
++data[bin3].cnt;\
++data[bin4].cnt;\
++data[bin5].cnt;\
++data[bin6].cnt;\
++data[bin7].cnt;\
}

struct TmpGradCntPair {
public:
  float sum_gradients = 0.0f;
  data_size_t cnt = 0;
};

#define KNumSumupGroup (32768)
#define KNumSumupGroupMask (32767)


Guolin Ke's avatar
Guolin Ke committed
515
516
}  // namespace LightGBM

Guolin Ke's avatar
Guolin Ke committed
517
#endif   // LightGBM_BIN_H_