train_share_states.h 14.2 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
5
6
#ifndef LIGHTGBM_INCLUDE_LIGHTGBM_TRAIN_SHARE_STATES_H_
#define LIGHTGBM_INCLUDE_LIGHTGBM_TRAIN_SHARE_STATES_H_
7
8

#include <LightGBM/bin.h>
Nikita Titov's avatar
Nikita Titov committed
9
#include <LightGBM/feature_group.h>
10
11
12
#include <LightGBM/meta.h>
#include <LightGBM/utils/threading.h>

Nikita Titov's avatar
Nikita Titov committed
13
#include <algorithm>
14
#include <cstdint>
15
16
17
18
19
20
21
22
#include <memory>
#include <vector>

namespace LightGBM {

class MultiValBinWrapper {
 public:
  MultiValBinWrapper(MultiValBin* bin, data_size_t num_data,
23
    const std::vector<int>& feature_groups_contained, const int num_grad_quant_bins);
24
25
26
27
28
29
30
31
32
33
34
35
36
37

  bool IsSparse() {
    if (multi_val_bin_ != nullptr) {
      return multi_val_bin_->IsSparse();
    }
    return false;
  }

  void InitTrain(const std::vector<int>& group_feature_start,
    const std::vector<std::unique_ptr<FeatureGroup>>& feature_groups,
    const std::vector<int8_t>& is_feature_used,
    const data_size_t* bagging_use_indices,
    data_size_t bagging_indices_cnt);

38
  template <bool USE_QUANT_GRAD, int HIST_BITS, int INNER_HIST_BITS>
39
40
  void HistMove(const std::vector<hist_t, Common::AlignmentAllocator<hist_t, kAlignedSize>>& hist_buf);

41
  template <bool USE_QUANT_GRAD, int HIST_BITS, int INNER_HIST_BITS>
42
43
44
45
46
47
  void HistMerge(std::vector<hist_t, Common::AlignmentAllocator<hist_t, kAlignedSize>>* hist_buf);

  void ResizeHistBuf(std::vector<hist_t, Common::AlignmentAllocator<hist_t, kAlignedSize>>* hist_buf,
    MultiValBin* sub_multi_val_bin,
    hist_t* origin_hist_data);

48
  template <bool USE_INDICES, bool ORDERED, bool USE_QUANT_GRAD, int HIST_BITS>
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  void ConstructHistograms(const data_size_t* data_indices,
      data_size_t num_data,
      const score_t* gradients,
      const score_t* hessians,
      std::vector<hist_t, Common::AlignmentAllocator<hist_t, kAlignedSize>>* hist_buf,
      hist_t* origin_hist_data) {
    const auto cur_multi_val_bin = (is_use_subcol_ || is_use_subrow_)
          ? multi_val_bin_subset_.get()
          : multi_val_bin_.get();
    if (cur_multi_val_bin != nullptr) {
      global_timer.Start("Dataset::sparse_bin_histogram");
      n_data_block_ = 1;
      data_block_size_ = num_data;
      Threading::BlockInfo<data_size_t>(num_threads_, num_data, min_block_size_,
63
                                        &n_data_block_, &data_block_size_);
64
      ResizeHistBuf(hist_buf, cur_multi_val_bin, origin_hist_data);
65
      const int inner_hist_bits = (data_block_size_ * num_grad_quant_bins_ < 256 && HIST_BITS == 16) ? 8 : HIST_BITS;
66
67
68
69
70
71
      OMP_INIT_EX();
      #pragma omp parallel for schedule(static) num_threads(num_threads_)
      for (int block_id = 0; block_id < n_data_block_; ++block_id) {
        OMP_LOOP_EX_BEGIN();
        data_size_t start = block_id * data_block_size_;
        data_size_t end = std::min<data_size_t>(start + data_block_size_, num_data);
72
73
74
75
76
77
78
79
80
        if (inner_hist_bits == 8) {
          ConstructHistogramsForBlock<USE_INDICES, ORDERED, USE_QUANT_GRAD, 8>(
            cur_multi_val_bin, start, end, data_indices, gradients, hessians,
            block_id, hist_buf);
        } else {
          ConstructHistogramsForBlock<USE_INDICES, ORDERED, USE_QUANT_GRAD, HIST_BITS>(
            cur_multi_val_bin, start, end, data_indices, gradients, hessians,
            block_id, hist_buf);
        }
81
82
83
84
85
86
        OMP_LOOP_EX_END();
      }
      OMP_THROW_EX();
      global_timer.Stop("Dataset::sparse_bin_histogram");

      global_timer.Start("Dataset::sparse_bin_histogram_merge");
87
88
89
90
91
      if (inner_hist_bits == 8) {
        HistMerge<USE_QUANT_GRAD, HIST_BITS, 8>(hist_buf);
      } else {
        HistMerge<USE_QUANT_GRAD, HIST_BITS, HIST_BITS>(hist_buf);
      }
92
93
      global_timer.Stop("Dataset::sparse_bin_histogram_merge");
      global_timer.Start("Dataset::sparse_bin_histogram_move");
94
95
96
97
98
      if (inner_hist_bits == 8) {
        HistMove<USE_QUANT_GRAD, HIST_BITS, 8>(*hist_buf);
      } else {
        HistMove<USE_QUANT_GRAD, HIST_BITS, HIST_BITS>(*hist_buf);
      }
99
100
101
102
      global_timer.Stop("Dataset::sparse_bin_histogram_move");
    }
  }

103
  template <bool USE_INDICES, bool ORDERED, bool USE_QUANT_GRAD, int HIST_BITS>
104
105
106
107
  void ConstructHistogramsForBlock(const MultiValBin* sub_multi_val_bin,
    data_size_t start, data_size_t end, const data_size_t* data_indices,
    const score_t* gradients, const score_t* hessians, int block_id,
    std::vector<hist_t, Common::AlignmentAllocator<hist_t, kAlignedSize>>* hist_buf) {
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
    if (USE_QUANT_GRAD) {
      if (HIST_BITS == 8) {
        int8_t* hist_buf_ptr = reinterpret_cast<int8_t*>(hist_buf->data());
        int8_t* data_ptr = hist_buf_ptr +
          static_cast<size_t>(num_bin_aligned_) * block_id * 2;
        std::memset(reinterpret_cast<void*>(data_ptr), 0, num_bin_ * kInt8HistBufferEntrySize);
        if (USE_INDICES) {
          if (ORDERED) {
            sub_multi_val_bin->ConstructHistogramOrderedInt8(data_indices, start, end,
                                                              gradients, hessians,
                                                              reinterpret_cast<hist_t*>(data_ptr));
          } else {
            sub_multi_val_bin->ConstructHistogramInt8(data_indices, start, end, gradients,
                                                       hessians,
                                                       reinterpret_cast<hist_t*>(data_ptr));
          }
        } else {
          sub_multi_val_bin->ConstructHistogramInt8(start, end, gradients, hessians,
                                                     reinterpret_cast<hist_t*>(data_ptr));
        }
      } else if (HIST_BITS == 16) {
        int16_t* data_ptr = reinterpret_cast<int16_t*>(origin_hist_data_);
        int16_t* hist_buf_ptr = reinterpret_cast<int16_t*>(hist_buf->data());
        if (block_id == 0) {
          if (is_use_subcol_) {
            data_ptr = hist_buf_ptr + hist_buf->size() - 2 * static_cast<size_t>(num_bin_aligned_);
          }
        } else {
          data_ptr = hist_buf_ptr +
            static_cast<size_t>(num_bin_aligned_) * (block_id - 1) * 2;
        }
        std::memset(reinterpret_cast<void*>(data_ptr), 0, num_bin_ * kInt16HistBufferEntrySize);
        if (USE_INDICES) {
          if (ORDERED) {
            sub_multi_val_bin->ConstructHistogramOrderedInt16(data_indices, start, end,
                                                              gradients, hessians,
                                                              reinterpret_cast<hist_t*>(data_ptr));
          } else {
            sub_multi_val_bin->ConstructHistogramInt16(data_indices, start, end, gradients,
                                                       hessians,
                                                       reinterpret_cast<hist_t*>(data_ptr));
          }
        } else {
          sub_multi_val_bin->ConstructHistogramInt16(start, end, gradients, hessians,
                                                     reinterpret_cast<hist_t*>(data_ptr));
        }
      } else {
        int32_t* data_ptr = reinterpret_cast<int32_t*>(origin_hist_data_);
        int32_t* hist_buf_ptr = reinterpret_cast<int32_t*>(hist_buf->data());
        if (block_id == 0) {
          if (is_use_subcol_) {
            data_ptr = hist_buf_ptr + hist_buf->size() - 2 * static_cast<size_t>(num_bin_aligned_);
          }
        } else {
          data_ptr = hist_buf_ptr +
            static_cast<size_t>(num_bin_aligned_) * (block_id - 1) * 2;
        }
        std::memset(reinterpret_cast<void*>(data_ptr), 0, num_bin_ * kInt32HistBufferEntrySize);
        if (USE_INDICES) {
          if (ORDERED) {
            sub_multi_val_bin->ConstructHistogramOrderedInt32(data_indices, start, end,
                                                              gradients, hessians,
                                                              reinterpret_cast<hist_t*>(data_ptr));
          } else {
            sub_multi_val_bin->ConstructHistogramInt32(data_indices, start, end, gradients,
                                                       hessians,
                                                       reinterpret_cast<hist_t*>(data_ptr));
          }
        } else {
          sub_multi_val_bin->ConstructHistogramInt32(start, end, gradients, hessians,
                                                     reinterpret_cast<hist_t*>(data_ptr));
        }
180
181
      }
    } else {
182
183
184
185
186
      hist_t* data_ptr = origin_hist_data_;
      if (block_id == 0) {
        if (is_use_subcol_) {
          data_ptr = hist_buf->data() + hist_buf->size() - 2 * static_cast<size_t>(num_bin_aligned_);
        }
187
      } else {
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        data_ptr = hist_buf->data() +
          static_cast<size_t>(num_bin_aligned_) * (block_id - 1) * 2;
      }
      std::memset(reinterpret_cast<void*>(data_ptr), 0, num_bin_ * kHistBufferEntrySize);
      if (USE_INDICES) {
        if (ORDERED) {
          sub_multi_val_bin->ConstructHistogramOrdered(data_indices, start, end,
                                                  gradients, hessians, data_ptr);
        } else {
          sub_multi_val_bin->ConstructHistogram(data_indices, start, end, gradients,
                                            hessians, data_ptr);
        }
      } else {
        sub_multi_val_bin->ConstructHistogram(start, end, gradients, hessians,
                                          data_ptr);
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
      }
    }
  }

  void CopyMultiValBinSubset(const std::vector<int>& group_feature_start,
    const std::vector<std::unique_ptr<FeatureGroup>>& feature_groups,
    const std::vector<int8_t>& is_feature_used,
    const data_size_t* bagging_use_indices,
    data_size_t bagging_indices_cnt);

  void SetUseSubrow(bool is_use_subrow) {
    is_use_subrow_ = is_use_subrow;
  }

  void SetSubrowCopied(bool is_subrow_copied) {
    is_subrow_copied_ = is_subrow_copied;
  }

221

222
  #ifdef USE_CUDA
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  const void* GetRowWiseData(
    uint8_t* bit_type,
    size_t* total_size,
    bool* is_sparse,
    const void** out_data_ptr,
    uint8_t* data_ptr_bit_type) const {
    if (multi_val_bin_ == nullptr) {
      *bit_type = 0;
      *total_size = 0;
      *is_sparse = false;
      return nullptr;
    } else {
      return multi_val_bin_->GetRowWiseData(bit_type, total_size, is_sparse, out_data_ptr, data_ptr_bit_type);
    }
  }
238
  #endif  // USE_CUDA
239

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
 private:
  bool is_use_subcol_ = false;
  bool is_use_subrow_ = false;
  bool is_subrow_copied_ = false;
  std::unique_ptr<MultiValBin> multi_val_bin_;
  std::unique_ptr<MultiValBin> multi_val_bin_subset_;
  std::vector<uint32_t> hist_move_src_;
  std::vector<uint32_t> hist_move_dest_;
  std::vector<uint32_t> hist_move_size_;
  const std::vector<int> feature_groups_contained_;

  int num_threads_;
  int num_bin_;
  int num_bin_aligned_;
  int n_data_block_;
  int data_block_size_;
  int min_block_size_;
  int num_data_;
258
  int num_grad_quant_bins_;
259
260
261
262

  hist_t* origin_hist_data_;

  const size_t kHistBufferEntrySize = 2 * sizeof(hist_t);
263
264
265
  const size_t kInt32HistBufferEntrySize = 2 * sizeof(int32_t);
  const size_t kInt16HistBufferEntrySize = 2 * sizeof(int16_t);
  const size_t kInt8HistBufferEntrySize = 2 * sizeof(int8_t);
266
267
268
269
270
271
272
273
274
275
276
277
278
};

struct TrainingShareStates {
  int num_threads = 0;
  bool is_col_wise = true;
  bool is_constant_hessian = true;
  const data_size_t* bagging_use_indices;
  data_size_t bagging_indices_cnt;

  TrainingShareStates() {
    multi_val_bin_wrapper_.reset(nullptr);
  }

Guolin Ke's avatar
Guolin Ke committed
279
  int num_hist_total_bin() { return num_hist_total_bin_; }
280

281
282
  const std::vector<uint32_t>& feature_hist_offsets() const { return feature_hist_offsets_; }

283
  #ifdef USE_CUDA
284
  const std::vector<uint32_t>& column_hist_offsets() const { return column_hist_offsets_; }
285
  #endif  // USE_CUDA
286
287
288
289
290
291
292

  bool IsSparseRowwise() {
    return (multi_val_bin_wrapper_ != nullptr && multi_val_bin_wrapper_->IsSparse());
  }

  void SetMultiValBin(MultiValBin* bin, data_size_t num_data,
    const std::vector<std::unique_ptr<FeatureGroup>>& feature_groups,
293
    bool dense_only, bool sparse_only, const int num_grad_quant_bins);
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

  void CalcBinOffsets(const std::vector<std::unique_ptr<FeatureGroup>>& feature_groups,
    std::vector<uint32_t>* offsets, bool is_col_wise);

  void InitTrain(const std::vector<int>& group_feature_start,
        const std::vector<std::unique_ptr<FeatureGroup>>& feature_groups,
        const std::vector<int8_t>& is_feature_used) {
    if (multi_val_bin_wrapper_ != nullptr) {
      multi_val_bin_wrapper_->InitTrain(group_feature_start,
        feature_groups,
        is_feature_used,
        bagging_use_indices,
        bagging_indices_cnt);
    }
  }

310
  template <bool USE_INDICES, bool ORDERED, bool USE_QUANT_GRAD, int HIST_BITS>
311
312
313
314
315
316
  void ConstructHistograms(const data_size_t* data_indices,
                          data_size_t num_data,
                          const score_t* gradients,
                          const score_t* hessians,
                          hist_t* hist_data) {
    if (multi_val_bin_wrapper_ != nullptr) {
317
      multi_val_bin_wrapper_->ConstructHistograms<USE_INDICES, ORDERED, USE_QUANT_GRAD, HIST_BITS>(
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
        data_indices, num_data, gradients, hessians, &hist_buf_, hist_data);
    }
  }

  void SetUseSubrow(bool is_use_subrow) {
    if (multi_val_bin_wrapper_ != nullptr) {
      multi_val_bin_wrapper_->SetUseSubrow(is_use_subrow);
    }
  }

  void SetSubrowCopied(bool is_subrow_copied) {
    if (multi_val_bin_wrapper_ != nullptr) {
      multi_val_bin_wrapper_->SetSubrowCopied(is_subrow_copied);
    }
  }

334

335
  #ifdef USE_CUDA
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  const void* GetRowWiseData(uint8_t* bit_type,
    size_t* total_size,
    bool* is_sparse,
    const void** out_data_ptr,
    uint8_t* data_ptr_bit_type) {
    if (multi_val_bin_wrapper_ != nullptr) {
      return multi_val_bin_wrapper_->GetRowWiseData(bit_type, total_size, is_sparse, out_data_ptr, data_ptr_bit_type);
    } else {
      *bit_type = 0;
      *total_size = 0;
      *is_sparse = false;
      return nullptr;
    }
  }
350
  #endif  // USE_CUDA
351

352
353
 private:
  std::vector<uint32_t> feature_hist_offsets_;
354
  #ifdef USE_CUDA
355
  std::vector<uint32_t> column_hist_offsets_;
356
  #endif  // USE_CUDA
Guolin Ke's avatar
Guolin Ke committed
357
  int num_hist_total_bin_ = 0;
358
359
360
361
362
363
364
365
  std::unique_ptr<MultiValBinWrapper> multi_val_bin_wrapper_;
  std::vector<hist_t, Common::AlignmentAllocator<hist_t, kAlignedSize>> hist_buf_;
  int num_total_bin_ = 0;
  double num_elements_per_row_ = 0.0f;
};

}  // namespace LightGBM

366
#endif   // LIGHTGBM_INCLUDE_LIGHTGBM_TRAIN_SHARE_STATES_H_