1#ifndef LIGHTGBM_IO_ORDERED_SPARSE_BIN_HPP_
2#define LIGHTGBM_IO_ORDERED_SPARSE_BIN_HPP_
4#include <LightGBM/bin.h>
13#include "sparse_bin.hpp"
25template <
typename VAL_T>
36 :bin_data_(bin_data) {
40 while (bin_data_->NextNonzero(&i_delta, &cur_pos)) {
43 ordered_pair_.resize(non_zero_cnt);
44 leaf_cnt_.push_back(non_zero_cnt);
50 void Init(
const char* used_idices,
int num_leaves)
override {
52 leaf_start_ = std::vector<data_size_t>(num_leaves, 0);
53 leaf_cnt_ = std::vector<data_size_t>(num_leaves, 0);
54 if (used_idices ==
nullptr) {
59 while (bin_data_->NextNonzero(&i_delta, &cur_pos)) {
60 ordered_pair_[j].ridx = cur_pos;
61 ordered_pair_[j].bin = bin_data_->vals_[i_delta];
70 while (bin_data_->NextNonzero(&i_delta, &cur_pos)) {
71 if (used_idices[cur_pos]) {
72 ordered_pair_[j].ridx = cur_pos;
73 ordered_pair_[j].bin = bin_data_->vals_[i_delta];
86 const int rest = (end - start) % 4;
89 for (; i < end - rest; i += 4) {
90 const VAL_T bin0 = ordered_pair_[i].bin;
91 const VAL_T bin1 = ordered_pair_[i + 1].bin;
92 const VAL_T bin2 = ordered_pair_[i + 2].bin;
93 const VAL_T bin3 = ordered_pair_[i + 3].bin;
95 const auto g0 = gradient[ordered_pair_[i].ridx];
96 const auto h0 = hessian[ordered_pair_[i].ridx];
97 const auto g1 = gradient[ordered_pair_[i + 1].ridx];
98 const auto h1 = hessian[ordered_pair_[i + 1].ridx];
99 const auto g2 = gradient[ordered_pair_[i + 2].ridx];
100 const auto h2 = hessian[ordered_pair_[i + 2].ridx];
101 const auto g3 = gradient[ordered_pair_[i + 3].ridx];
102 const auto h3 = hessian[ordered_pair_[i + 3].ridx];
104 out[bin0].sum_gradients += g0;
105 out[bin1].sum_gradients += g1;
106 out[bin2].sum_gradients += g2;
107 out[bin3].sum_gradients += g3;
109 out[bin0].sum_hessians += h0;
110 out[bin1].sum_hessians += h1;
111 out[bin2].sum_hessians += h2;
112 out[bin3].sum_hessians += h3;
120 for (; i < end; ++i) {
121 const VAL_T bin0 = ordered_pair_[i].bin;
123 const auto g0 = gradient[ordered_pair_[i].ridx];
124 const auto h0 = hessian[ordered_pair_[i].ridx];
126 out[bin0].sum_gradients += g0;
127 out[bin0].sum_hessians += h0;
137 const int rest = (end - start) % 4;
140 for (; i < end - rest; i += 4) {
141 const VAL_T bin0 = ordered_pair_[i].bin;
142 const VAL_T bin1 = ordered_pair_[i + 1].bin;
143 const VAL_T bin2 = ordered_pair_[i + 2].bin;
144 const VAL_T bin3 = ordered_pair_[i + 3].bin;
146 const auto g0 = gradient[ordered_pair_[i].ridx];
147 const auto g1 = gradient[ordered_pair_[i + 1].ridx];
148 const auto g2 = gradient[ordered_pair_[i + 2].ridx];
149 const auto g3 = gradient[ordered_pair_[i + 3].ridx];
151 out[bin0].sum_gradients += g0;
152 out[bin1].sum_gradients += g1;
153 out[bin2].sum_gradients += g2;
154 out[bin3].sum_gradients += g3;
161 for (; i < end; ++i) {
162 const VAL_T bin0 = ordered_pair_[i].bin;
163 const auto g0 = gradient[ordered_pair_[i].ridx];
164 out[bin0].sum_gradients += g0;
169 void Split(
int leaf,
int right_leaf,
const char* is_in_leaf,
char mark)
override {
172 const data_size_t l_end = l_start + leaf_cnt_[leaf];
177 if (is_in_leaf[ordered_pair_[i].ridx] == mark) {
178 std::swap(ordered_pair_[new_left_end], ordered_pair_[i]);
183 leaf_start_[right_leaf] = new_left_end;
184 leaf_cnt_[leaf] = new_left_end - l_start;
185 leaf_cnt_[right_leaf] = l_end - new_left_end;
187 data_size_t NonZeroCount(
int leaf)
const override {
198 std::vector<SparsePair> ordered_pair_;
200 std::vector<data_size_t> leaf_start_;
202 std::vector<data_size_t> leaf_cnt_;
205template <
typename VAL_T>
Interface for ordered bin data. efficient for construct histogram, especially for sparse bin There ar...
Definition bin.h:219
Interface for ordered bin data. efficient for construct histogram, especially for sparse bin There ar...
Definition ordered_sparse_bin.hpp:26
void Split(int leaf, int right_leaf, const char *is_in_leaf, char mark) override
Split current bin, and perform re-order by leaf.
Definition ordered_sparse_bin.hpp:169
void ConstructHistogram(int leaf, const score_t *gradient, const score_t *hessian, HistogramBinEntry *out) const override
Construct histogram by using this bin Note: Unlike Bin, OrderedBin doesn't use ordered gradients and ...
Definition ordered_sparse_bin.hpp:81
void ConstructHistogram(int leaf, const score_t *gradient, HistogramBinEntry *out) const override
Construct histogram by using this bin Note: Unlike Bin, OrderedBin doesn't use ordered gradients and ...
Definition ordered_sparse_bin.hpp:132
OrderedSparseBin< VAL_T > & operator=(const OrderedSparseBin< VAL_T > &)=delete
Disable copy.
Definition sparse_bin.hpp:69
OrderedBin * CreateOrderedBin() const override
Create the ordered bin for this bin.
Definition ordered_sparse_bin.hpp:206
desc and descl2 fields must be written in reStructuredText format
Definition application.h:10
float score_t
Type of score, and gradients.
Definition meta.h:26
int32_t data_size_t
Type of data size, it is better to use signed type.
Definition meta.h:14
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:24418
Store data for one histogram bin.
Definition bin.h:29
Pair to store one bin entry.
Definition ordered_sparse_bin.hpp:29