"vscode:/vscode.git/clone" did not exist on "c324e891be58789f0423cd5c986e7396a524b3ce"
ordered_sparse_bin.hpp 4.3 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef LIGHTGBM_IO_ORDERED_SPARSE_BIN_HPP_
#define LIGHTGBM_IO_ORDERED_SPARSE_BIN_HPP_

#include <LightGBM/bin.h>

#include <cstring>
#include <cstdint>

#include <vector>
#include <mutex>
#include <algorithm>

13
14
#include "sparse_bin.hpp"

Guolin Ke's avatar
Guolin Ke committed
15
16
17
namespace LightGBM {

/*!
Qiwei Ye's avatar
Qiwei Ye committed
18
19
20
21
22
23
* \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
24
25
*/
template <typename VAL_T>
26
class OrderedSparseBin: public OrderedBin {
Guolin Ke's avatar
Guolin Ke committed
27
28
29
30
31
public:
  /*! \brief Pair to store one bin entry */
  struct SparsePair {
    data_size_t ridx;  // data(row) index
    VAL_T bin;  // bin for this data
32
    SparsePair() : ridx(0), bin(0) {}
Guolin Ke's avatar
Guolin Ke committed
33
34
  };

35
36
  OrderedSparseBin(const SparseBin<VAL_T>* bin_data)
    :bin_data_(bin_data) {
Guolin Ke's avatar
Guolin Ke committed
37
    data_size_t cur_pos = 0;
38
    data_size_t i_delta = -1;
39
    int non_zero_cnt = 0;
40
    while (bin_data_->NextNonzero(&i_delta, &cur_pos)) {
41
      ++non_zero_cnt;
Guolin Ke's avatar
Guolin Ke committed
42
    }
43
    ordered_pair_.resize(non_zero_cnt);
Guolin Ke's avatar
Guolin Ke committed
44
45
46
47
48
49
50
51
52
53
54
55
  }

  ~OrderedSparseBin() {
  }

  void Init(const char* used_idices, int num_leaves) override {
    // initialize the leaf information
    leaf_start_ = std::vector<data_size_t>(num_leaves, 0);
    leaf_cnt_ = std::vector<data_size_t>(num_leaves, 0);
    if (used_idices == nullptr) {
      // if using all data, copy all non-zero pair
      data_size_t j = 0;
56
57
58
59
60
61
      data_size_t cur_pos = 0;
      data_size_t i_delta = -1;
      while (bin_data_->NextNonzero(&i_delta, &cur_pos)) {
        ordered_pair_[j].ridx = cur_pos;
        ordered_pair_[j].bin = bin_data_->vals_[i_delta];
        ++j;
Guolin Ke's avatar
Guolin Ke committed
62
      }
63
      leaf_cnt_[0] = static_cast<data_size_t>(j);
Guolin Ke's avatar
Guolin Ke committed
64
65
66
67
    } else {
      // if using part of data(bagging)
      data_size_t j = 0;
      data_size_t cur_pos = 0;
68
69
70
      data_size_t i_delta = -1;
      while (bin_data_->NextNonzero(&i_delta, &cur_pos)) {
        if (used_idices[cur_pos]) {
Guolin Ke's avatar
Guolin Ke committed
71
          ordered_pair_[j].ridx = cur_pos;
72
          ordered_pair_[j].bin = bin_data_->vals_[i_delta];
Guolin Ke's avatar
Guolin Ke committed
73
74
75
76
77
78
79
80
          ++j;
        }
      }
      leaf_cnt_[0] = j;
    }
  }

  void ConstructHistogram(int leaf, const score_t* gradient, const score_t* hessian,
81
    HistogramBinEntry* out) const override {
Guolin Ke's avatar
Guolin Ke committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    // get current leaf boundary
    const data_size_t start = leaf_start_[leaf];
    const data_size_t end = start + leaf_cnt_[leaf];
    // use data on current leaf to construct histogram
    for (data_size_t i = start; i < end; ++i) {
      const VAL_T bin = ordered_pair_[i].bin;
      const data_size_t idx = ordered_pair_[i].ridx;
      out[bin].sum_gradients += gradient[idx];
      out[bin].sum_hessians += hessian[idx];
      ++out[bin].cnt;
    }
  }

  void Split(int leaf, int right_leaf, const char* left_indices) override {
    // get current leaf boundary
    const data_size_t l_start = leaf_start_[leaf];
    const data_size_t l_end = l_start + leaf_cnt_[leaf];
    // new left leaf end after split
    data_size_t new_left_end = l_start;

    for (data_size_t i = l_start; i < l_end; ++i) {
Guolin Ke's avatar
Guolin Ke committed
103
      if (left_indices[ordered_pair_[i].ridx]) {
Guolin Ke's avatar
Guolin Ke committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
        std::swap(ordered_pair_[new_left_end], ordered_pair_[i]);
        ++new_left_end;
      }
    }

    leaf_start_[right_leaf] = new_left_end;
    leaf_cnt_[leaf] = new_left_end - l_start;
    leaf_cnt_[right_leaf] = l_end - new_left_end;
  }

  /*! \brief Disable copy */
  OrderedSparseBin<VAL_T>& operator=(const OrderedSparseBin<VAL_T>&) = delete;
  /*! \brief Disable copy */
  OrderedSparseBin<VAL_T>(const OrderedSparseBin<VAL_T>&) = delete;

private:
120
  const SparseBin<VAL_T>* bin_data_;
Guolin Ke's avatar
Guolin Ke committed
121
122
123
124
125
126
127
  /*! \brief Store non-zero pair , group by leaf */
  std::vector<SparsePair> ordered_pair_;
  /*! \brief leaf_start_[i] means data in i-th leaf start from */
  std::vector<data_size_t> leaf_start_;
  /*! \brief leaf_cnt_[i] means number of data in i-th leaf */
  std::vector<data_size_t> leaf_cnt_;
};
128
129
130
131
132
133

template <typename VAL_T>
OrderedBin* SparseBin<VAL_T>::CreateOrderedBin() const {
  return new OrderedSparseBin<VAL_T>(this);
}

Guolin Ke's avatar
Guolin Ke committed
134
}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
135
#endif   // LightGBM_IO_ORDERED_SPARSE_BIN_HPP_