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

#include <cstring>

#include <vector>

namespace LightGBM {

9
10

template <typename TREELEARNER_T>
Guolin Ke's avatar
Guolin Ke committed
11
12
FeatureParallelTreeLearner<TREELEARNER_T>::FeatureParallelTreeLearner(const Config* config)
  :TREELEARNER_T(config) {
Guolin Ke's avatar
Guolin Ke committed
13
14
}

15
16
template <typename TREELEARNER_T>
FeatureParallelTreeLearner<TREELEARNER_T>::~FeatureParallelTreeLearner() {
Guolin Ke's avatar
Guolin Ke committed
17
}
18
19
20
21

template <typename TREELEARNER_T>
void FeatureParallelTreeLearner<TREELEARNER_T>::Init(const Dataset* train_data, bool is_constant_hessian) {
  TREELEARNER_T::Init(train_data, is_constant_hessian);
Guolin Ke's avatar
Guolin Ke committed
22
23
  rank_ = Network::rank();
  num_machines_ = Network::num_machines();
Guolin Ke's avatar
Guolin Ke committed
24
25
  input_buffer_.resize((sizeof(SplitInfo) + sizeof(uint32_t) * this->config_->max_cat_threshold) * 2);
  output_buffer_.resize((sizeof(SplitInfo) + sizeof(uint32_t) * this->config_->max_cat_threshold) * 2);
Guolin Ke's avatar
Guolin Ke committed
26
27
28
}


29
30
31
template <typename TREELEARNER_T>
void FeatureParallelTreeLearner<TREELEARNER_T>::BeforeTrain() {
  TREELEARNER_T::BeforeTrain();
Guolin Ke's avatar
Guolin Ke committed
32
33
34
  // get feature partition
  std::vector<std::vector<int>> feature_distribution(num_machines_, std::vector<int>());
  std::vector<int> num_bins_distributed(num_machines_, 0);
35
36
  for (int i = 0; i < this->train_data_->num_total_features(); ++i) {
    int inner_feature_index = this->train_data_->InnerFeatureIndex(i);
Guolin Ke's avatar
Guolin Ke committed
37
    if (inner_feature_index == -1) { continue; }
38
    if (this->is_feature_used_[inner_feature_index]) {
Guolin Ke's avatar
Guolin Ke committed
39
      int cur_min_machine = static_cast<int>(ArrayArgs<int>::ArgMin(num_bins_distributed));
Guolin Ke's avatar
Guolin Ke committed
40
      feature_distribution[cur_min_machine].push_back(inner_feature_index);
41
42
      num_bins_distributed[cur_min_machine] += this->train_data_->FeatureNumBin(inner_feature_index);
      this->is_feature_used_[inner_feature_index] = false;
Guolin Ke's avatar
Guolin Ke committed
43
44
45
46
    }
  }
  // get local used features
  for (auto fid : feature_distribution[rank_]) {
47
    this->is_feature_used_[fid] = true;
Guolin Ke's avatar
Guolin Ke committed
48
49
50
  }
}

51
template <typename TREELEARNER_T>
Guolin Ke's avatar
Guolin Ke committed
52
53
54
void FeatureParallelTreeLearner<TREELEARNER_T>::FindBestSplitsFromHistograms(const std::vector<int8_t>& is_feature_used, bool use_subtract) {
  TREELEARNER_T::FindBestSplitsFromHistograms(is_feature_used, use_subtract);
  SplitInfo smaller_best_split, larger_best_split;
Guolin Ke's avatar
Guolin Ke committed
55
  // get best split at smaller leaf
Guolin Ke's avatar
Guolin Ke committed
56
  smaller_best_split = this->best_split_per_leaf_[this->smaller_leaf_splits_->LeafIndex()];
Guolin Ke's avatar
Guolin Ke committed
57
  // find local best split for larger leaf
58
  if (this->larger_leaf_splits_->LeafIndex() >= 0) {
Guolin Ke's avatar
Guolin Ke committed
59
    larger_best_split = this->best_split_per_leaf_[this->larger_leaf_splits_->LeafIndex()];
Guolin Ke's avatar
Guolin Ke committed
60
61
  }
  // sync global best info
Guolin Ke's avatar
Guolin Ke committed
62
  SyncUpGlobalBestSplit(input_buffer_.data(), input_buffer_.data(), &smaller_best_split, &larger_best_split, this->config_->max_cat_threshold);
Guolin Ke's avatar
Guolin Ke committed
63
  // update best split
Guolin Ke's avatar
Guolin Ke committed
64
  this->best_split_per_leaf_[this->smaller_leaf_splits_->LeafIndex()] = smaller_best_split;
65
  if (this->larger_leaf_splits_->LeafIndex() >= 0) {
Guolin Ke's avatar
Guolin Ke committed
66
    this->best_split_per_leaf_[this->larger_leaf_splits_->LeafIndex()] = larger_best_split;
Guolin Ke's avatar
Guolin Ke committed
67
68
69
  }
}

70
71
72
// instantiate template classes, otherwise linker cannot find the code
template class FeatureParallelTreeLearner<GPUTreeLearner>;
template class FeatureParallelTreeLearner<SerialTreeLearner>;
Guolin Ke's avatar
Guolin Ke committed
73
}  // namespace LightGBM