Skip to content

Commit

Permalink
faster roundint
Browse files Browse the repository at this point in the history
  • Loading branch information
guolinke committed Jan 30, 2020
1 parent 1e030fc commit ce8725d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 4 additions & 0 deletions include/LightGBM/utils/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,10 @@ inline bool CheckAllowedJSON(const std::string& s) {
return true;
}

inline int RoundInt(double x) {
return static_cast<int>(x + 0.5f);
}

template <typename T, std::size_t N = 32>
class AlignmentAllocator {
public:
Expand Down
16 changes: 8 additions & 8 deletions src/treelearner/feature_histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class FeatureHistogram {
for (int t = 0; t < used_bin; ++t) {
const auto grad = GET_GRAD(data_, t);
const auto hess = GET_HESS(data_, t);
data_size_t cnt = static_cast<data_size_t>(std::round(hess * cnt_factor));
data_size_t cnt = static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
// if data not enough, or sum hessian too small
if (cnt < meta_->config->min_data_in_leaf
|| hess < meta_->config->min_sum_hessian_in_leaf) continue;
Expand Down Expand Up @@ -171,7 +171,7 @@ class FeatureHistogram {
}
} else {
for (int i = 0; i < used_bin; ++i) {
if (std::round(GET_HESS(data_, i) * cnt_factor) >= meta_->config->cat_smooth) {
if (Common::RoundInt(GET_HESS(data_, i) * cnt_factor) >= meta_->config->cat_smooth) {
sorted_idx.push_back(i);
}
}
Expand Down Expand Up @@ -207,7 +207,7 @@ class FeatureHistogram {
start_pos += dir;
const auto grad = GET_GRAD(data_, t);
const auto hess = GET_HESS(data_, t);
data_size_t cnt = static_cast<data_size_t>(std::round(hess * cnt_factor));
data_size_t cnt = static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));

sum_left_gradient += grad;
sum_left_hessian += hess;
Expand Down Expand Up @@ -329,7 +329,7 @@ class FeatureHistogram {
if (skip_default_bin && (t + offset) == static_cast<int>(meta_->default_bin)) { continue; }
const auto grad = GET_GRAD(data_, t);
const auto hess = GET_HESS(data_, t);
data_size_t cnt = static_cast<data_size_t>(std::round(hess * cnt_factor));
data_size_t cnt = static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
sum_right_gradient += grad;
sum_right_hessian += hess;
right_count += cnt;
Expand Down Expand Up @@ -390,7 +390,7 @@ class FeatureHistogram {
const double cnt_factor = num_data / sum_hessian;
const auto grad = GET_GRAD(data_, threshold);
const auto hess = GET_HESS(data_, threshold);
data_size_t cnt = static_cast<data_size_t>(std::round(hess * cnt_factor));
data_size_t cnt = static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));

double l2 = meta_->config->lambda_l2;
data_size_t left_count = cnt;
Expand Down Expand Up @@ -541,7 +541,7 @@ class FeatureHistogram {

const auto grad = GET_GRAD(data_, t);
const auto hess = GET_HESS(data_, t);
data_size_t cnt = static_cast<data_size_t>(std::round(hess * cnt_factor));
data_size_t cnt = static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
sum_right_gradient += grad;
sum_right_hessian += hess;
right_count += cnt;
Expand Down Expand Up @@ -591,7 +591,7 @@ class FeatureHistogram {
for (int i = 0; i < meta_->num_bin - offset; ++i) {
const auto grad = GET_GRAD(data_, i);
const auto hess = GET_HESS(data_, i);
data_size_t cnt = static_cast<data_size_t>(std::round(hess * cnt_factor));
data_size_t cnt = static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
sum_left_gradient -= grad;
sum_left_hessian -= hess;
left_count -= cnt;
Expand All @@ -605,7 +605,7 @@ class FeatureHistogram {
if (t >= 0) {
sum_left_gradient += GET_GRAD(data_, t);
sum_left_hessian += GET_HESS(data_, t);
left_count += static_cast<data_size_t>(std::round(GET_HESS(data_, t) * cnt_factor));
left_count += static_cast<data_size_t>(Common::RoundInt(GET_HESS(data_, t) * cnt_factor));
}
// if data not enough, or sum hessian too small
if (left_count < meta_->config->min_data_in_leaf
Expand Down

0 comments on commit ce8725d

Please sign in to comment.