ordered_sparse_bin.hpp 5.12 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.
 */
Guolin Ke's avatar
Guolin Ke committed
5
6
7
8
9
#ifndef LIGHTGBM_IO_ORDERED_SPARSE_BIN_HPP_
#define LIGHTGBM_IO_ORDERED_SPARSE_BIN_HPP_

#include <LightGBM/bin.h>

10
#include <algorithm>
Guolin Ke's avatar
Guolin Ke committed
11
#include <cstdint>
12
#include <cstring>
Guolin Ke's avatar
Guolin Ke committed
13
#include <mutex>
14
15
#include <utility>
#include <vector>
Guolin Ke's avatar
Guolin Ke committed
16

17
18
#include "sparse_bin.hpp"

Guolin Ke's avatar
Guolin Ke committed
19
20
21
namespace LightGBM {

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

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

  ~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;
61
62
63
64
65
66
      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
67
      }
68
      leaf_cnt_[0] = static_cast<data_size_t>(j);
Guolin Ke's avatar
Guolin Ke committed
69
70
71
72
    } else {
      // if using part of data(bagging)
      data_size_t j = 0;
      data_size_t cur_pos = 0;
73
74
75
      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
76
          ordered_pair_[j].ridx = cur_pos;
77
          ordered_pair_[j].bin = bin_data_->vals_[i_delta];
Guolin Ke's avatar
Guolin Ke committed
78
79
80
81
82
83
84
          ++j;
        }
      }
      leaf_cnt_[0] = j;
    }
  }

85
  void ConstructHistogram(int leaf, const score_t* gradient, const score_t* hessian,
86
                          HistogramBinEntry* out) const override {
Guolin Ke's avatar
Guolin Ke committed
87
88
89
    // get current leaf boundary
    const data_size_t start = leaf_start_[leaf];
    const data_size_t end = start + leaf_cnt_[leaf];
90
    for (data_size_t i = start; i < end; ++i) {
91
92
93
      const VAL_T bin = ordered_pair_[i].bin;
      const auto g = gradient[ordered_pair_[i].ridx];
      const auto h = hessian[ordered_pair_[i].ridx];
94

95
96
97
      out[bin].sum_gradients += g;
      out[bin].sum_hessians += h;
      ++out[bin].cnt;
Guolin Ke's avatar
Guolin Ke committed
98
    }
99
100
  }

101
  void ConstructHistogram(int leaf, const score_t* gradient,
102
103
104
105
                          HistogramBinEntry* out) const override {
    // get current leaf boundary
    const data_size_t start = leaf_start_[leaf];
    const data_size_t end = start + leaf_cnt_[leaf];
106
    for (data_size_t i = start; i < end; ++i) {
107
108
109
110
      const VAL_T bin = ordered_pair_[i].bin;
      const auto g = gradient[ordered_pair_[i].ridx];
      out[bin].sum_gradients += g;
      ++out[bin].cnt;
111
    }
Guolin Ke's avatar
Guolin Ke committed
112
113
  }

Guolin Ke's avatar
Guolin Ke committed
114
  void Split(int leaf, int right_leaf, const char* is_in_leaf, char mark) override {
Guolin Ke's avatar
Guolin Ke committed
115
116
117
118
119
120
121
    // 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
122
      if (is_in_leaf[ordered_pair_[i].ridx] == mark) {
Guolin Ke's avatar
Guolin Ke committed
123
124
125
126
127
128
129
130
131
        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;
  }
Guolin Ke's avatar
Guolin Ke committed
132
133
134
  data_size_t NonZeroCount(int leaf) const override {
    return static_cast<data_size_t>(leaf_cnt_[leaf]);
  }
Guolin Ke's avatar
Guolin Ke committed
135
136
137
138
139
  /*! \brief Disable copy */
  OrderedSparseBin<VAL_T>& operator=(const OrderedSparseBin<VAL_T>&) = delete;
  /*! \brief Disable copy */
  OrderedSparseBin<VAL_T>(const OrderedSparseBin<VAL_T>&) = delete;

Nikita Titov's avatar
Nikita Titov committed
140
 private:
141
  const SparseBin<VAL_T>* bin_data_;
Guolin Ke's avatar
Guolin Ke committed
142
143
144
145
146
147
148
  /*! \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_;
};
149
150
151
152
153
154

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

Guolin Ke's avatar
Guolin Ke committed
155
}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
156
#endif   // LightGBM_IO_ORDERED_SPARSE_BIN_HPP_