Skip to content

Commit 98ac153

Browse files
authored
Avoid warning from NVCC. (dmlc#10757)
1 parent 5cc7c73 commit 98ac153

File tree

2 files changed

+75
-76
lines changed

2 files changed

+75
-76
lines changed

tests/cpp/tree/hist/test_evaluate_splits.cc

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2021-2023 by XGBoost Contributors
2+
* Copyright 2021-2024, XGBoost Contributors
33
*/
44
#include "../test_evaluate_splits.h"
55

@@ -10,20 +10,90 @@
1010
#include <xgboost/logging.h> // for CHECK_EQ
1111
#include <xgboost/tree_model.h> // for RegTree, RTreeNodeStat
1212

13-
#include <memory> // for make_shared, shared_ptr, addressof
13+
#include <memory> // for make_shared, shared_ptr, addressof
14+
#include <numeric> // for iota
15+
#include <tuple> // for make_tuple
1416

1517
#include "../../../../src/common/hist_util.h" // for HistCollection, HistogramCuts
1618
#include "../../../../src/common/random.h" // for ColumnSampler
1719
#include "../../../../src/common/row_set.h" // for RowSetCollection
1820
#include "../../../../src/data/gradient_index.h" // for GHistIndexMatrix
19-
#include "../../../../src/tree/hist/evaluate_splits.h" // for HistEvaluator
21+
#include "../../../../src/tree/hist/evaluate_splits.h" // for HistEvaluator, TreeEvaluator
2022
#include "../../../../src/tree/hist/expand_entry.h" // for CPUExpandEntry
2123
#include "../../../../src/tree/hist/hist_cache.h" // for BoundedHistCollection
2224
#include "../../../../src/tree/hist/param.h" // for HistMakerTrainParam
2325
#include "../../../../src/tree/param.h" // for GradStats, TrainParam
2426
#include "../../helpers.h" // for RandomDataGenerator, AllThreadsFo...
2527

2628
namespace xgboost::tree {
29+
void TestPartitionBasedSplit::SetUp() {
30+
param_.UpdateAllowUnknown(Args{{"min_child_weight", "0"}, {"reg_lambda", "0"}});
31+
sorted_idx_.resize(n_bins_);
32+
std::iota(sorted_idx_.begin(), sorted_idx_.end(), 0);
33+
34+
info_.num_col_ = 1;
35+
36+
cuts_.cut_ptrs_.Resize(2);
37+
cuts_.SetCategorical(true, n_bins_);
38+
auto &h_cuts = cuts_.cut_ptrs_.HostVector();
39+
h_cuts[0] = 0;
40+
h_cuts[1] = n_bins_;
41+
auto &h_vals = cuts_.cut_values_.HostVector();
42+
h_vals.resize(n_bins_);
43+
std::iota(h_vals.begin(), h_vals.end(), 0.0);
44+
45+
cuts_.min_vals_.Resize(1);
46+
47+
Context ctx;
48+
HistMakerTrainParam hist_param;
49+
hist_.Reset(cuts_.TotalBins(), hist_param.MaxCachedHistNodes(ctx.Device()));
50+
hist_.AllocateHistograms({0});
51+
auto node_hist = hist_[0];
52+
53+
SimpleLCG lcg;
54+
SimpleRealUniformDistribution<double> grad_dist{-4.0, 4.0};
55+
SimpleRealUniformDistribution<double> hess_dist{0.0, 4.0};
56+
57+
for (auto &e : node_hist) {
58+
e = GradientPairPrecise{grad_dist(&lcg), hess_dist(&lcg)};
59+
total_gpair_ += e;
60+
}
61+
62+
auto enumerate = [this, n_feat = info_.num_col_](common::GHistRow hist,
63+
GradientPairPrecise parent_sum) {
64+
int32_t best_thresh = -1;
65+
float best_score{-std::numeric_limits<float>::infinity()};
66+
TreeEvaluator evaluator{param_, static_cast<bst_feature_t>(n_feat), DeviceOrd::CPU()};
67+
auto tree_evaluator = evaluator.GetEvaluator<TrainParam>();
68+
GradientPairPrecise left_sum;
69+
auto parent_gain = tree_evaluator.CalcGain(0, param_, GradStats{total_gpair_});
70+
for (size_t i = 0; i < hist.size() - 1; ++i) {
71+
left_sum += hist[i];
72+
auto right_sum = parent_sum - left_sum;
73+
auto gain =
74+
tree_evaluator.CalcSplitGain(param_, 0, 0, GradStats{left_sum}, GradStats{right_sum}) -
75+
parent_gain;
76+
if (gain > best_score) {
77+
best_score = gain;
78+
best_thresh = i;
79+
}
80+
}
81+
return std::make_tuple(best_thresh, best_score);
82+
};
83+
84+
// enumerate all possible partitions to find the optimal split
85+
do {
86+
std::vector<GradientPairPrecise> sorted_hist(node_hist.size());
87+
for (size_t i = 0; i < sorted_hist.size(); ++i) {
88+
sorted_hist[i] = node_hist[sorted_idx_[i]];
89+
}
90+
auto [thresh, score] = enumerate({sorted_hist}, total_gpair_);
91+
if (score > best_score_) {
92+
best_score_ = score;
93+
}
94+
} while (std::next_permutation(sorted_idx_.begin(), sorted_idx_.end()));
95+
}
96+
2797
void TestEvaluateSplits(bool force_read_by_column) {
2898
Context ctx;
2999
ctx.nthread = 4;

tests/cpp/tree/test_evaluate_splits.h

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,15 @@
1212
#include <cstddef> // for size_t
1313
#include <cstdint> // for int32_t, uint64_t, uint32_t
1414
#include <limits> // for numeric_limits
15-
#include <numeric> // for iota
16-
#include <tuple> // for make_tuple, tie, tuple
1715
#include <vector> // for vector
1816

1917
#include "../../../src/common/hist_util.h" // for HistogramCuts, HistCollection, GHistRow
2018
#include "../../../src/tree/hist/hist_cache.h" // for HistogramCollection
21-
#include "../../../src/tree/hist/param.h" // for HistMakerTrainParam
2219
#include "../../../src/tree/param.h" // for TrainParam, GradStats
23-
#include "../../../src/tree/split_evaluator.h" // for TreeEvaluator
24-
#include "../helpers.h" // for SimpleLCG, SimpleRealUniformDistribution
2520

2621
namespace xgboost::tree {
2722
/**
28-
* \brief Enumerate all possible partitions for categorical split.
23+
* @brief Enumerate all possible partitions for categorical split.
2924
*/
3025
class TestPartitionBasedSplit : public ::testing::Test {
3126
protected:
@@ -38,73 +33,7 @@ class TestPartitionBasedSplit : public ::testing::Test {
3833
BoundedHistCollection hist_;
3934
GradientPairPrecise total_gpair_;
4035

41-
void SetUp() override {
42-
param_.UpdateAllowUnknown(Args{{"min_child_weight", "0"}, {"reg_lambda", "0"}});
43-
sorted_idx_.resize(n_bins_);
44-
std::iota(sorted_idx_.begin(), sorted_idx_.end(), 0);
45-
46-
info_.num_col_ = 1;
47-
48-
cuts_.cut_ptrs_.Resize(2);
49-
cuts_.SetCategorical(true, n_bins_);
50-
auto &h_cuts = cuts_.cut_ptrs_.HostVector();
51-
h_cuts[0] = 0;
52-
h_cuts[1] = n_bins_;
53-
auto &h_vals = cuts_.cut_values_.HostVector();
54-
h_vals.resize(n_bins_);
55-
std::iota(h_vals.begin(), h_vals.end(), 0.0);
56-
57-
cuts_.min_vals_.Resize(1);
58-
59-
Context ctx;
60-
HistMakerTrainParam hist_param;
61-
hist_.Reset(cuts_.TotalBins(), hist_param.MaxCachedHistNodes(ctx.Device()));
62-
hist_.AllocateHistograms({0});
63-
auto node_hist = hist_[0];
64-
65-
SimpleLCG lcg;
66-
SimpleRealUniformDistribution<double> grad_dist{-4.0, 4.0};
67-
SimpleRealUniformDistribution<double> hess_dist{0.0, 4.0};
68-
69-
for (auto &e : node_hist) {
70-
e = GradientPairPrecise{grad_dist(&lcg), hess_dist(&lcg)};
71-
total_gpair_ += e;
72-
}
73-
74-
auto enumerate = [this, n_feat = info_.num_col_](common::GHistRow hist,
75-
GradientPairPrecise parent_sum) {
76-
int32_t best_thresh = -1;
77-
float best_score{-std::numeric_limits<float>::infinity()};
78-
TreeEvaluator evaluator{param_, static_cast<bst_feature_t>(n_feat), DeviceOrd::CPU()};
79-
auto tree_evaluator = evaluator.GetEvaluator<TrainParam>();
80-
GradientPairPrecise left_sum;
81-
auto parent_gain = tree_evaluator.CalcGain(0, param_, GradStats{total_gpair_});
82-
for (size_t i = 0; i < hist.size() - 1; ++i) {
83-
left_sum += hist[i];
84-
auto right_sum = parent_sum - left_sum;
85-
auto gain =
86-
tree_evaluator.CalcSplitGain(param_, 0, 0, GradStats{left_sum}, GradStats{right_sum}) -
87-
parent_gain;
88-
if (gain > best_score) {
89-
best_score = gain;
90-
best_thresh = i;
91-
}
92-
}
93-
return std::make_tuple(best_thresh, best_score);
94-
};
95-
96-
// enumerate all possible partitions to find the optimal split
97-
do {
98-
std::vector<GradientPairPrecise> sorted_hist(node_hist.size());
99-
for (size_t i = 0; i < sorted_hist.size(); ++i) {
100-
sorted_hist[i] = node_hist[sorted_idx_[i]];
101-
}
102-
auto [thresh, score] = enumerate({sorted_hist}, total_gpair_);
103-
if (score > best_score_) {
104-
best_score_ = score;
105-
}
106-
} while (std::next_permutation(sorted_idx_.begin(), sorted_idx_.end()));
107-
}
36+
void SetUp() override;
10837
};
10938

11039
inline auto MakeCutsForTest(std::vector<float> values, std::vector<uint32_t> ptrs,

0 commit comments

Comments
 (0)