bin.h 10.3 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef LIGHTGBM_BIN_H_
#define LIGHTGBM_BIN_H_

#include <LightGBM/meta.h>

#include <vector>
#include <functional>

namespace LightGBM {


/*! \brief Store data for one histogram bin */
struct HistogramBinEntry {
public:
  /*! \brief Sum of gradients on this bin */
  score_t sum_gradients = 0.0;
  /*! \brief Sum of hessians on this bin */
  score_t sum_hessians = 0.0;
  /*! \brief Number of data on this bin */
  data_size_t cnt = 0;

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
23
  * \brief Sum up (reducers) functions for histogram bin
Guolin Ke's avatar
Guolin Ke committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  */
  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
45
46
/*! \brief This class used to convert feature values into bin,
*          and store some meta information for bin*/
Guolin Ke's avatar
Guolin Ke committed
47
48
49
50
51
52
53
54
55
class BinMapper {
public:
  BinMapper();
  BinMapper(const BinMapper& other);
  explicit BinMapper(const void* memory);
  ~BinMapper();

  /*! \brief Get number of bins */
  inline int num_bin() const { return num_bin_; }
Qiwei Ye's avatar
Qiwei Ye committed
56
  /*! \brief True if bin is trival (contains only one bin) */
Guolin Ke's avatar
Guolin Ke committed
57
  inline bool is_trival() const { return is_trival_; }
Qiwei Ye's avatar
Qiwei Ye committed
58
  /*! \brief Sparsity of this bin ( num_zero_bins / num_data ) */
59
  inline float sparse_rate() const { return sparse_rate_; }
Guolin Ke's avatar
Guolin Ke committed
60
61
62
63
64
65
  /*!
  * \brief Save binary data to file
  * \param file File want to write
  */
  void SaveBinaryToFile(FILE* file) const;
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
66
  * \brief Mapping bin into feature value
Guolin Ke's avatar
Guolin Ke committed
67
  * \param bin
Qiwei Ye's avatar
Qiwei Ye committed
68
  * \return Feature value of this bin
Guolin Ke's avatar
Guolin Ke committed
69
  */
70
  inline float BinToValue(unsigned int bin) const {
Guolin Ke's avatar
Guolin Ke committed
71
72
73
74
75
76
77
    return bin_upper_bound_[bin];
  }
  /*!
  * \brief Get sizes in byte of this object
  */
  size_t SizesInByte() const;
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
78
  * \brief Mapping feature value into bin 
Guolin Ke's avatar
Guolin Ke committed
79
80
81
  * \param value
  * \return bin for this feature value
  */
82
  inline unsigned int ValueToBin(float value) const;
Guolin Ke's avatar
Guolin Ke committed
83
84
85
86
87
88

  /*!
  * \brief Construct feature value to bin mapper according feature values
  * \param values (Sampled) values of this feature
  * \param max_bin The maximal number of bin
  */
89
  void FindBin(std::vector<float>* values, int max_bin);
Guolin Ke's avatar
Guolin Ke committed
90
91
92
93
94
95
96
97
98

  /*!
  * \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
99
  * \brief Seirilizing this object to buffer
Guolin Ke's avatar
Guolin Ke committed
100
101
102
103
104
  * \param buffer The destination
  */
  void CopyTo(char* buffer);

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
105
  * \brief Deserilizing this object from buffer
Guolin Ke's avatar
Guolin Ke committed
106
107
108
109
110
111
112
113
  * \param buffer The source
  */
  void CopyFrom(const char* buffer);

private:
  /*! \brief Number of bins */
  int num_bin_;
  /*! \brief Store upper bound for each bin */
114
  float* bin_upper_bound_;
Guolin Ke's avatar
Guolin Ke committed
115
116
117
  /*! \brief True if this feature is trival */
  bool is_trival_;
  /*! \brief Sparse rate of this bins( num_bin0/num_data ) */
118
  float sparse_rate_;
Guolin Ke's avatar
Guolin Ke committed
119
120
121
};

/*!
Qiwei Ye's avatar
Qiwei Ye committed
122
123
124
125
126
127
* \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
128
129
130
131
132
133
134
*/
class OrderedBin {
public:
  /*! \brief virtual destructor */
  virtual ~OrderedBin() {}

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
135
136
137
138
  * \brief Initialization logic.
  * \param used_indices If used_indices==nullptr means using all data, otherwise, used_indices[i] != 0 means i-th data is used
           (this logic was build for bagging logic)
  * \param num_leaves Number of leaves on this iteration
Guolin Ke's avatar
Guolin Ke committed
139
  */
Qiwei Ye's avatar
Qiwei Ye committed
140
  virtual void Init(const char* used_indices, data_size_t num_leaves) = 0;
Guolin Ke's avatar
Guolin Ke committed
141
142
143

  /*!
  * \brief Construct histogram by using this bin
Qiwei Ye's avatar
Qiwei Ye committed
144
145
  *        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
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
  * \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
  * \param out Output Result
  */
  virtual void ConstructHistogram(int leaf, const score_t* gradients,
    const score_t* hessians, HistogramBinEntry* out) const = 0;

  /*!
  * \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
  * \param left_indices left_indices[i] != 0 means the i-th data will be on left leaf after split
  */
  virtual void Split(int leaf, int right_leaf, const char* left_indices) = 0;
};

/*! \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;
};

/*!
* \brief Interface for bin data. This class will store bin data for one feature.
Qiwei Ye's avatar
Qiwei Ye committed
176
177
178
*        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
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
*/
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;

  /*!
  * \brief Get bin interator of this bin
  * \param start_idx start index of this 
  * \return Iterator of this bin
  */
  virtual BinIterator* GetIterator(data_size_t start_idx) const = 0;

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

  /*!
  * \brief Load from memory
  * \param file File want to write
  */
  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;

  /*!
  * \brief Construct histogram of this feature,
Qiwei Ye's avatar
Qiwei Ye committed
222
223
224
225
226
  *        Note: We use ordered_gradients and ordered_hessians to improve cache hit chance
  *        The navie solution is use 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).
Guolin Ke's avatar
Guolin Ke committed
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
  * \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]
  * \param out Output Result
  */
  virtual void ConstructHistogram(
    data_size_t* data_indices, data_size_t num_data,
    const score_t* ordered_gradients, const score_t* ordered_hessians,
    HistogramBinEntry* out) const = 0;

  /*!
  * \brief Split data according to threshold, if bin <= threshold, will put into left(lte_indices), else put into right(gt_indices)
  * \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.
  * \return The number of less than or equal data.
  */
  virtual data_size_t Split(
    unsigned int threshold, data_size_t* data_indices, data_size_t num_data,
    data_size_t* lte_indices, data_size_t* gt_indices) const = 0;

  /*!
  * \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
258
  * \brief After pushed all feature data, call this could have better refactor for bin data
Guolin Ke's avatar
Guolin Ke committed
259
260
261
262
263
264
265
  */
  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
Hui Xue's avatar
Hui Xue committed
266
  * \param is_sparse True if this feature is sparse
Guolin Ke's avatar
Guolin Ke committed
267
268
269
  * \param sparse_rate Sparse rate of this bins( num_bin0/num_data )
  * \param is_enable_sparse True if enable sparse feature
  * \param is_sparse Will set to true if this bin is sparse
Guolin Ke's avatar
Guolin Ke committed
270
  * \param default_bin Default bin for zeros value
Guolin Ke's avatar
Guolin Ke committed
271
272
273
  * \return The bin data object
  */
  static Bin* CreateBin(data_size_t num_data, int num_bin,
274
    float sparse_rate, bool is_enable_sparse, bool* is_sparse, int default_bin);
Guolin Ke's avatar
Guolin Ke committed
275
276
277
278
279

  /*!
  * \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
Guolin Ke's avatar
Guolin Ke committed
280
  * \param default_bin Default bin for zeros value
Guolin Ke's avatar
Guolin Ke committed
281
282
  * \return The bin data object
  */
Guolin Ke's avatar
Guolin Ke committed
283
  static Bin* CreateDenseBin(data_size_t num_data, int num_bin, int default_bin);
Guolin Ke's avatar
Guolin Ke committed
284
285
286
287
288

  /*!
  * \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
Guolin Ke's avatar
Guolin Ke committed
289
  * \param default_bin Default bin for zeros value
Guolin Ke's avatar
Guolin Ke committed
290
291
292
  * \return The bin data object
  */
  static Bin* CreateSparseBin(data_size_t num_data,
Guolin Ke's avatar
Guolin Ke committed
293
    int num_bin, int default_bin);
Guolin Ke's avatar
Guolin Ke committed
294
295
};

296
inline unsigned int BinMapper::ValueToBin(float value) const {
Qiwei Ye's avatar
Qiwei Ye committed
297
  // binary search to find bin
Guolin Ke's avatar
Guolin Ke committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  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;
}

}  // namespace LightGBM

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