data_parallel_tree_learner.cpp 10 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
#include "parallel_tree_learner.h"

#include <cstring>

#include <tuple>
#include <vector>

namespace LightGBM {

DataParallelTreeLearner::DataParallelTreeLearner(const TreeConfig& tree_config)
Guolin Ke's avatar
Guolin Ke committed
11
  :SerialTreeLearner(tree_config) {
Guolin Ke's avatar
Guolin Ke committed
12
13
14
}

DataParallelTreeLearner::~DataParallelTreeLearner() {
Guolin Ke's avatar
Guolin Ke committed
15

Guolin Ke's avatar
Guolin Ke committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
}

void DataParallelTreeLearner::Init(const Dataset* train_data) {
  // initialize SerialTreeLearner
  SerialTreeLearner::Init(train_data);
  // Get local rank and global machine size
  rank_ = Network::rank();
  num_machines_ = Network::num_machines();
  // allocate buffer for communication
  size_t buffer_size = 0;
  for (int i = 0; i < num_features_; ++i) {
    buffer_size += train_data_->FeatureAt(i)->num_bin() * sizeof(HistogramBinEntry);
  }

Guolin Ke's avatar
Guolin Ke committed
30
31
  input_buffer_.resize(buffer_size);
  output_buffer_.resize(buffer_size);
Guolin Ke's avatar
Guolin Ke committed
32

Guolin Ke's avatar
Guolin Ke committed
33
  is_feature_aggregated_.resize(num_features_);
Guolin Ke's avatar
Guolin Ke committed
34

Guolin Ke's avatar
Guolin Ke committed
35
36
  block_start_.resize(num_machines_);
  block_len_.resize(num_machines_);
Guolin Ke's avatar
Guolin Ke committed
37

Guolin Ke's avatar
Guolin Ke committed
38
39
  buffer_write_start_pos_.resize(num_features_);
  buffer_read_start_pos_.resize(num_features_);
Guolin Ke's avatar
Guolin Ke committed
40
  global_data_count_in_leaf_.resize(tree_config_.num_leaves);
Guolin Ke's avatar
Guolin Ke committed
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
}



void DataParallelTreeLearner::BeforeTrain() {
  SerialTreeLearner::BeforeTrain();
  // generate feature partition for current tree
  std::vector<std::vector<int>> feature_distribution(num_machines_, std::vector<int>());
  std::vector<int> num_bins_distributed(num_machines_, 0);
  for (int i = 0; i < train_data_->num_features(); ++i) {
    if (is_feature_used_[i]) {
      int cur_min_machine = static_cast<int>(ArrayArgs<int>::ArgMin(num_bins_distributed));
      feature_distribution[cur_min_machine].push_back(i);
      num_bins_distributed[cur_min_machine] += train_data_->FeatureAt(i)->num_bin();
    }
    is_feature_aggregated_[i] = false;
  }
  // get local used feature
  for (auto fid : feature_distribution[rank_]) {
    is_feature_aggregated_[fid] = true;
  }

  // get block start and block len for reduce scatter
  reduce_scatter_size_ = 0;
  for (int i = 0; i < num_machines_; ++i) {
    block_len_[i] = 0;
    for (auto fid : feature_distribution[i]) {
      block_len_[i] += train_data_->FeatureAt(fid)->num_bin() * sizeof(HistogramBinEntry);
    }
    reduce_scatter_size_ += block_len_[i];
  }

  block_start_[0] = 0;
  for (int i = 1; i < num_machines_; ++i) {
    block_start_[i] = block_start_[i - 1] + block_len_[i - 1];
  }

  // get buffer_write_start_pos_
  int bin_size = 0;
  for (int i = 0; i < num_machines_; ++i) {
    for (auto fid : feature_distribution[i]) {
      buffer_write_start_pos_[fid] = bin_size;
      bin_size += train_data_->FeatureAt(fid)->num_bin() * sizeof(HistogramBinEntry);
    }
  }

  // get buffer_read_start_pos_
  bin_size = 0;
  for (auto fid : feature_distribution[rank_]) {
    buffer_read_start_pos_[fid] = bin_size;
    bin_size += train_data_->FeatureAt(fid)->num_bin() * sizeof(HistogramBinEntry);
  }

  // sync global data sumup info
Guolin Ke's avatar
Guolin Ke committed
95
  std::tuple<data_size_t, double, double> data(smaller_leaf_splits_->num_data_in_leaf(),
Guolin Ke's avatar
Guolin Ke committed
96
97
             smaller_leaf_splits_->sum_gradients(), smaller_leaf_splits_->sum_hessians());
  int size = sizeof(data);
Guolin Ke's avatar
Guolin Ke committed
98
  std::memcpy(input_buffer_.data(), &data, size);
Guolin Ke's avatar
Guolin Ke committed
99
  // global sumup reduce
Guolin Ke's avatar
Guolin Ke committed
100
  Network::Allreduce(input_buffer_.data(), size, size, output_buffer_.data(), [](const char *src, char *dst, int len) {
Guolin Ke's avatar
Guolin Ke committed
101
    int used_size = 0;
Guolin Ke's avatar
Guolin Ke committed
102
103
104
    int type_size = sizeof(std::tuple<data_size_t, double, double>);
    const std::tuple<data_size_t, double, double> *p1;
    std::tuple<data_size_t, double, double> *p2;
Guolin Ke's avatar
Guolin Ke committed
105
    while (used_size < len) {
Guolin Ke's avatar
Guolin Ke committed
106
107
      p1 = reinterpret_cast<const std::tuple<data_size_t, double, double> *>(src);
      p2 = reinterpret_cast<std::tuple<data_size_t, double, double> *>(dst);
Guolin Ke's avatar
Guolin Ke committed
108
109
110
111
112
113
114
115
116
      std::get<0>(*p2) = std::get<0>(*p2) + std::get<0>(*p1);
      std::get<1>(*p2) = std::get<1>(*p2) + std::get<1>(*p1);
      std::get<2>(*p2) = std::get<2>(*p2) + std::get<2>(*p1);
      src += type_size;
      dst += type_size;
      used_size += type_size;
    }
  });
  // copy back
Guolin Ke's avatar
Guolin Ke committed
117
  std::memcpy(&data, output_buffer_.data(), size);
Guolin Ke's avatar
Guolin Ke committed
118
119
120
121
122
123
124
125
126
127
  // set global sumup info
  smaller_leaf_splits_->Init(std::get<1>(data), std::get<2>(data));
  // init global data count in leaf
  global_data_count_in_leaf_[0] = std::get<0>(data);
}

void DataParallelTreeLearner::FindBestThresholds() {
  // construct local histograms
  #pragma omp parallel for schedule(guided)
  for (int feature_index = 0; feature_index < num_features_; ++feature_index) {
Guolin Ke's avatar
Guolin Ke committed
128
    if ((!is_feature_used_.empty() && is_feature_used_[feature_index] == false)) continue;
Guolin Ke's avatar
Guolin Ke committed
129
130
131
132
133
134
    // construct histograms for smaller leaf
    if (ordered_bins_[feature_index] == nullptr) {
      smaller_leaf_histogram_array_[feature_index].Construct(smaller_leaf_splits_->data_indices(),
                                                             smaller_leaf_splits_->num_data_in_leaf(),
                                                             smaller_leaf_splits_->sum_gradients(),
                                                             smaller_leaf_splits_->sum_hessians(),
135
136
                                                             ptr_to_ordered_gradients_smaller_leaf_,
                                                             ptr_to_ordered_hessians_smaller_leaf_);
Guolin Ke's avatar
Guolin Ke committed
137
    } else {
Guolin Ke's avatar
Guolin Ke committed
138
      smaller_leaf_histogram_array_[feature_index].Construct(ordered_bins_[feature_index].get(),
Guolin Ke's avatar
Guolin Ke committed
139
140
141
142
143
144
145
146
                                                             smaller_leaf_splits_->LeafIndex(),
                                                             smaller_leaf_splits_->num_data_in_leaf(),
                                                             smaller_leaf_splits_->sum_gradients(),
                                                             smaller_leaf_splits_->sum_hessians(),
                                                             gradients_,
                                                             hessians_);
    }
    // copy to buffer
Guolin Ke's avatar
Guolin Ke committed
147
    std::memcpy(input_buffer_.data() + buffer_write_start_pos_[feature_index],
Guolin Ke's avatar
Guolin Ke committed
148
149
150
151
152
                smaller_leaf_histogram_array_[feature_index].HistogramData(),
                smaller_leaf_histogram_array_[feature_index].SizeOfHistgram());
  }

  // Reduce scatter for histogram
Guolin Ke's avatar
Guolin Ke committed
153
154
  Network::ReduceScatter(input_buffer_.data(), reduce_scatter_size_, block_start_.data(),
                         block_len_.data(), output_buffer_.data(), &HistogramBinEntry::SumReducer);
Guolin Ke's avatar
Guolin Ke committed
155
156
157
158
159
160
161
162
163
164
165
  #pragma omp parallel for schedule(guided)
  for (int feature_index = 0; feature_index < num_features_; ++feature_index) {
    if (!is_feature_aggregated_[feature_index]) continue;
    // copy global sumup info
    smaller_leaf_histogram_array_[feature_index].SetSumup(
        GetGlobalDataCountInLeaf(smaller_leaf_splits_->LeafIndex()),
                                smaller_leaf_splits_->sum_gradients(), 
                                smaller_leaf_splits_->sum_hessians());

    // restore global histograms from buffer
    smaller_leaf_histogram_array_[feature_index].FromMemory(
Guolin Ke's avatar
Guolin Ke committed
166
        output_buffer_.data() + buffer_read_start_pos_[feature_index]);
Guolin Ke's avatar
Guolin Ke committed
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

    // find best threshold for smaller child
    smaller_leaf_histogram_array_[feature_index].FindBestThreshold(
        &smaller_leaf_splits_->BestSplitPerFeature()[feature_index]);

    // only root leaf
    if (larger_leaf_splits_ == nullptr || larger_leaf_splits_->LeafIndex() < 0) continue;

    // construct histgroms for large leaf, we init larger leaf as the parent, so we can just subtract the smaller leaf's histograms
    larger_leaf_histogram_array_[feature_index].Subtract(
        smaller_leaf_histogram_array_[feature_index]);
    // set sumup info for histogram
    larger_leaf_histogram_array_[feature_index].SetSumup(
        GetGlobalDataCountInLeaf(larger_leaf_splits_->LeafIndex()),
                                                         larger_leaf_splits_->sum_gradients(), larger_leaf_splits_->sum_hessians());
    // find best threshold for larger child
    larger_leaf_histogram_array_[feature_index].FindBestThreshold(
        &larger_leaf_splits_->BestSplitPerFeature()[feature_index]);
  }

}

void DataParallelTreeLearner::FindBestSplitsForLeaves() {
  int smaller_best_feature = -1, larger_best_feature = -1;
  SplitInfo smaller_best, larger_best;
Guolin Ke's avatar
Guolin Ke committed
192
  std::vector<double> gains;
Guolin Ke's avatar
Guolin Ke committed
193
194
195
196
  // find local best split for smaller leaf
  for (size_t i = 0; i < smaller_leaf_splits_->BestSplitPerFeature().size(); ++i) {
    gains.push_back(smaller_leaf_splits_->BestSplitPerFeature()[i].gain);
  }
Guolin Ke's avatar
Guolin Ke committed
197
  smaller_best_feature = static_cast<int>(ArrayArgs<double>::ArgMax(gains));
Guolin Ke's avatar
Guolin Ke committed
198
199
200
201
202
203
204
  smaller_best = smaller_leaf_splits_->BestSplitPerFeature()[smaller_best_feature];
  // find local best split for larger leaf
  if (larger_leaf_splits_->LeafIndex() >= 0) {
    gains.clear();
    for (size_t i = 0; i < larger_leaf_splits_->BestSplitPerFeature().size(); ++i) {
      gains.push_back(larger_leaf_splits_->BestSplitPerFeature()[i].gain);
    }
Guolin Ke's avatar
Guolin Ke committed
205
    larger_best_feature = static_cast<int>(ArrayArgs<double>::ArgMax(gains));
Guolin Ke's avatar
Guolin Ke committed
206
207
208
209
    larger_best = larger_leaf_splits_->BestSplitPerFeature()[larger_best_feature];
  }

  // sync global best info
Guolin Ke's avatar
Guolin Ke committed
210
211
  std::memcpy(input_buffer_.data(), &smaller_best, sizeof(SplitInfo));
  std::memcpy(input_buffer_.data() + sizeof(SplitInfo), &larger_best, sizeof(SplitInfo));
Guolin Ke's avatar
Guolin Ke committed
212

Guolin Ke's avatar
Guolin Ke committed
213
214
  Network::Allreduce(input_buffer_.data(), sizeof(SplitInfo) * 2, sizeof(SplitInfo),
                     output_buffer_.data(), &SplitInfo::MaxReducer);
Guolin Ke's avatar
Guolin Ke committed
215

Guolin Ke's avatar
Guolin Ke committed
216
217
  std::memcpy(&smaller_best, output_buffer_.data(), sizeof(SplitInfo));
  std::memcpy(&larger_best, output_buffer_.data() + sizeof(SplitInfo), sizeof(SplitInfo));
Guolin Ke's avatar
Guolin Ke committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

  // set best split
  best_split_per_leaf_[smaller_leaf_splits_->LeafIndex()] = smaller_best;
  if (larger_leaf_splits_->LeafIndex() >= 0) {
    best_split_per_leaf_[larger_leaf_splits_->LeafIndex()] = larger_best;
  }
}

void DataParallelTreeLearner::Split(Tree* tree, int best_Leaf, int* left_leaf, int* right_leaf) {
  SerialTreeLearner::Split(tree, best_Leaf, left_leaf, right_leaf);
  const SplitInfo& best_split_info = best_split_per_leaf_[best_Leaf];
  // need update global number of data in leaf
  global_data_count_in_leaf_[*left_leaf] = best_split_info.left_count;
  global_data_count_in_leaf_[*right_leaf] = best_split_info.right_count;
}


}  // namespace LightGBM