Skip to content

Commit 2896011

Browse files
authored
Refactor split application and add related tests (#61)
* reuse FindSplitConditons from cpu branch * linting * add tests * fix test --------- Co-authored-by: Dmitry Razdoburdin <>
1 parent 916d4a4 commit 2896011

File tree

4 files changed

+125
-42
lines changed

4 files changed

+125
-42
lines changed

plugin/sycl/tree/hist_updater.cc

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <limits>
1414
#include <vector>
1515

16+
#include "../../src/tree/common_row_partitioner.h"
17+
1618
#include "../common/hist_util.h"
1719
#include "../../src/collective/allreduce.h"
1820

@@ -188,7 +190,7 @@ void HistUpdater<GradientSumT>::EvaluateAndApplySplits(
188190
std::vector<ExpandEntry> nodes_for_apply_split;
189191
AddSplitsToTree(gmat, p_tree, num_leaves, depth,
190192
&nodes_for_apply_split, temp_qexpand_depth);
191-
ApplySplit(nodes_for_apply_split, gmat, hist_, p_tree);
193+
ApplySplit(nodes_for_apply_split, gmat, p_tree);
192194
}
193195

194196
// Split nodes to 2 sets depending on amount of rows in each node
@@ -304,7 +306,7 @@ void HistUpdater<GradientSumT>::ExpandWithLossGuide(
304306
right_leaf_weight, e.best.loss_chg, e.stats.GetHess(),
305307
e.best.left_sum.GetHess(), e.best.right_sum.GetHess());
306308

307-
this->ApplySplit({candidate}, gmat, hist_, p_tree);
309+
this->ApplySplit({candidate}, gmat, p_tree);
308310

309311
const int cleft = (*p_tree)[nid].LeftChild();
310312
const int cright = (*p_tree)[nid].RightChild();
@@ -786,34 +788,6 @@ void HistUpdater<GradientSumT>::EnumerateSplit(
786788
best.SplitIndex() == total_split_index) p_best->Update(best);
787789
}
788790

789-
template <typename GradientSumT>
790-
void HistUpdater<GradientSumT>::FindSplitConditions(
791-
const std::vector<ExpandEntry>& nodes,
792-
const RegTree& tree,
793-
const common::GHistIndexMatrix& gmat,
794-
std::vector<int32_t>* split_conditions) {
795-
const size_t n_nodes = nodes.size();
796-
split_conditions->resize(n_nodes);
797-
798-
for (size_t i = 0; i < nodes.size(); ++i) {
799-
const int32_t nid = nodes[i].nid;
800-
const bst_uint fid = tree[nid].SplitIndex();
801-
const bst_float split_pt = tree[nid].SplitCond();
802-
const uint32_t lower_bound = gmat.cut.Ptrs()[fid];
803-
const uint32_t upper_bound = gmat.cut.Ptrs()[fid + 1];
804-
int32_t split_cond = -1;
805-
// convert floating-point split_pt into corresponding bin_id
806-
// split_cond = -1 indicates that split_pt is less than all known cut points
807-
CHECK_LT(upper_bound,
808-
static_cast<uint32_t>(std::numeric_limits<int32_t>::max()));
809-
for (uint32_t i = lower_bound; i < upper_bound; ++i) {
810-
if (split_pt == gmat.cut.Values()[i]) {
811-
split_cond = static_cast<int32_t>(i);
812-
}
813-
}
814-
(*split_conditions)[i] = split_cond;
815-
}
816-
}
817791
template <typename GradientSumT>
818792
void HistUpdater<GradientSumT>::AddSplitsToRowSet(
819793
const std::vector<ExpandEntry>& nodes,
@@ -833,13 +807,13 @@ template <typename GradientSumT>
833807
void HistUpdater<GradientSumT>::ApplySplit(
834808
const std::vector<ExpandEntry> nodes,
835809
const common::GHistIndexMatrix& gmat,
836-
const common::HistCollection<GradientSumT, MemoryType::on_device>& hist,
837810
RegTree* p_tree) {
811+
using CommonRowPartitioner = xgboost::tree::CommonRowPartitioner;
838812
builder_monitor_.Start("ApplySplit");
839813

840814
const size_t n_nodes = nodes.size();
841-
std::vector<int32_t> split_conditions;
842-
FindSplitConditions(nodes, *p_tree, gmat, &split_conditions);
815+
std::vector<int32_t> split_conditions(n_nodes);
816+
CommonRowPartitioner::FindSplitConditions(nodes, *p_tree, gmat, &split_conditions);
843817

844818
partition_builder_.Init(&qu_, n_nodes, [&](size_t node_in_set) {
845819
const int32_t nid = nodes[node_in_set].nid;

plugin/sycl/tree/hist_updater.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,10 @@ class HistUpdater {
119119

120120
void ApplySplit(std::vector<ExpandEntry> nodes,
121121
const common::GHistIndexMatrix& gmat,
122-
const common::HistCollection<GradientSumT, MemoryType::on_device>& hist,
123122
RegTree* p_tree);
124123

125124
void AddSplitsToRowSet(const std::vector<ExpandEntry>& nodes, RegTree* p_tree);
126125

127-
128-
void FindSplitConditions(const std::vector<ExpandEntry>& nodes, const RegTree& tree,
129-
const common::GHistIndexMatrix& gmat,
130-
std::vector<int32_t>* split_conditions);
131-
132126
void InitData(const common::GHistIndexMatrix& gmat,
133127
const USMVector<GradientPair, MemoryType::on_device> &gpair,
134128
const DMatrix& fmat,

src/tree/common_row_partitioner.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ class CommonRowPartitioner {
110110
}
111111
}
112112

113-
template <typename ExpandEntry>
114-
void FindSplitConditions(const std::vector<ExpandEntry>& nodes, const RegTree& tree,
115-
const GHistIndexMatrix& gmat, std::vector<int32_t>* split_conditions) {
113+
/* Making GHistIndexMatrix_t a templete parameter allows reuse this function for sycl-plugin */
114+
template <typename ExpandEntry, typename GHistIndexMatrix_t>
115+
static void FindSplitConditions(const std::vector<ExpandEntry>& nodes, const RegTree& tree,
116+
const GHistIndexMatrix_t& gmat, std::vector<int32_t>* split_conditions) {
116117
auto const& ptrs = gmat.cut.Ptrs();
117118
auto const& vals = gmat.cut.Values();
118119

tests/cpp/plugin/test_sycl_hist_updater.cc

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "../../../plugin/sycl/tree/hist_updater.h"
99
#include "../../../plugin/sycl/device_manager.h"
1010

11+
#include "../../../src/tree/common_row_partitioner.h"
12+
1113
#include "../helpers.h"
1214

1315
namespace xgboost::sycl::tree {
@@ -59,6 +61,12 @@ class TestHistUpdater : public HistUpdater<GradientSumT> {
5961
HistUpdater<GradientSumT>::EvaluateSplits(nodes_set, gmat, tree);
6062
return HistUpdater<GradientSumT>::snode_host_;
6163
}
64+
65+
auto TestApplySplit(const std::vector<ExpandEntry> nodes,
66+
const common::GHistIndexMatrix& gmat,
67+
RegTree* p_tree) {
68+
HistUpdater<GradientSumT>::ApplySplit(nodes, gmat, p_tree);
69+
}
6270
};
6371

6472
void GenerateRandomGPairs(::sycl::queue* qu, GradientPair* gpair_ptr, size_t num_rows, bool has_neg_hess) {
@@ -385,6 +393,92 @@ void TestHistUpdaterEvaluateSplits(const xgboost::tree::TrainParam& param) {
385393
ASSERT_NEAR(best_loss_chg_des[0], best_loss_chg, 1e-6);
386394
}
387395

396+
template <typename GradientSumT>
397+
void TestHistUpdaterApplySplit(const xgboost::tree::TrainParam& param, float sparsity, int max_bins) {
398+
const size_t num_rows = 1024;
399+
const size_t num_columns = 2;
400+
401+
Context ctx;
402+
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
403+
404+
DeviceManager device_manager;
405+
auto qu = device_manager.GetQueue(ctx.Device());
406+
407+
auto p_fmat = RandomDataGenerator{num_rows, num_columns, sparsity}.GenerateDMatrix();
408+
sycl::DeviceMatrix dmat;
409+
dmat.Init(qu, p_fmat.get());
410+
411+
common::GHistIndexMatrix gmat;
412+
gmat.Init(qu, &ctx, dmat, max_bins);
413+
414+
RegTree tree;
415+
tree.ExpandNode(0, 0, 0, false, 0, 0, 0, 0, 0, 0, 0);
416+
417+
std::vector<tree::ExpandEntry> nodes;
418+
nodes.emplace_back(tree::ExpandEntry(0, tree.GetDepth(0)));
419+
420+
FeatureInteractionConstraintHost int_constraints;
421+
TestHistUpdater<GradientSumT> updater(&ctx, qu, param, int_constraints, p_fmat.get());
422+
USMVector<GradientPair, MemoryType::on_device> gpair(&qu, num_rows);
423+
GenerateRandomGPairs(&qu, gpair.Data(), num_rows, false);
424+
425+
auto* row_set_collection = updater.TestInitData(gmat, gpair, *p_fmat, tree);
426+
updater.TestApplySplit(nodes, gmat, &tree);
427+
428+
// Copy indexes to host
429+
std::vector<size_t> row_indices_host(num_rows);
430+
qu.memcpy(row_indices_host.data(), row_set_collection->Data().Data(), sizeof(size_t)*num_rows).wait();
431+
432+
// Reference Implementation
433+
std::vector<size_t> row_indices_desired_host(num_rows);
434+
size_t n_left, n_right;
435+
{
436+
TestHistUpdater<GradientSumT> updater4verification(&ctx, qu, param, int_constraints, p_fmat.get());
437+
auto* row_set_collection4verification = updater4verification.TestInitData(gmat, gpair, *p_fmat, tree);
438+
439+
size_t n_nodes = nodes.size();
440+
std::vector<int32_t> split_conditions(n_nodes);
441+
xgboost::tree::CommonRowPartitioner::FindSplitConditions(nodes, tree, gmat, &split_conditions);
442+
443+
common::PartitionBuilder partition_builder;
444+
partition_builder.Init(&qu, n_nodes, [&](size_t node_in_set) {
445+
const int32_t nid = nodes[node_in_set].nid;
446+
return (*row_set_collection4verification)[nid].Size();
447+
});
448+
449+
::sycl::event event;
450+
partition_builder.Partition(gmat, nodes, (*row_set_collection4verification),
451+
split_conditions, &tree, &event);
452+
qu.wait_and_throw();
453+
454+
for (size_t node_in_set = 0; node_in_set < n_nodes; node_in_set++) {
455+
const int32_t nid = nodes[node_in_set].nid;
456+
size_t* data_result = const_cast<size_t*>((*row_set_collection4verification)[nid].begin);
457+
partition_builder.MergeToArray(node_in_set, data_result, &event);
458+
}
459+
qu.wait_and_throw();
460+
461+
const int32_t nid = nodes[0].nid;
462+
n_left = partition_builder.GetNLeftElems(0);
463+
n_right = partition_builder.GetNRightElems(0);
464+
465+
row_set_collection4verification->AddSplit(nid, tree[nid].LeftChild(),
466+
tree[nid].RightChild(), n_left, n_right);
467+
468+
qu.memcpy(row_indices_desired_host.data(), row_set_collection4verification->Data().Data(), sizeof(size_t)*num_rows).wait();
469+
}
470+
471+
std::sort(row_indices_desired_host.begin(), row_indices_desired_host.begin() + n_left);
472+
std::sort(row_indices_host.begin(), row_indices_host.begin() + n_left);
473+
std::sort(row_indices_desired_host.begin() + n_left, row_indices_desired_host.end());
474+
std::sort(row_indices_host.begin() + n_left, row_indices_host.end());
475+
476+
for (size_t row = 0; row < num_rows; ++row) {
477+
ASSERT_EQ(row_indices_desired_host[row], row_indices_host[row]);
478+
}
479+
480+
}
481+
388482
TEST(SyclHistUpdater, Sampling) {
389483
xgboost::tree::TrainParam param;
390484
param.UpdateAllowUnknown(Args{{"subsample", "0.7"}});
@@ -432,4 +526,24 @@ TEST(SyclHistUpdater, EvaluateSplits) {
432526
TestHistUpdaterEvaluateSplits<double>(param);
433527
}
434528

529+
TEST(SyclHistUpdater, ApplySplitSparce) {
530+
xgboost::tree::TrainParam param;
531+
param.UpdateAllowUnknown(Args{{"max_depth", "3"}});
532+
533+
TestHistUpdaterApplySplit<float>(param, 0.3, 256);
534+
TestHistUpdaterApplySplit<double>(param, 0.3, 256);
535+
}
536+
537+
TEST(SyclHistUpdater, ApplySplitDence) {
538+
xgboost::tree::TrainParam param;
539+
param.UpdateAllowUnknown(Args{{"max_depth", "3"}});
540+
541+
TestHistUpdaterApplySplit<float>(param, 0.0, 256);
542+
TestHistUpdaterApplySplit<float>(param, 0.0, 256+1);
543+
TestHistUpdaterApplySplit<float>(param, 0.0, (1u << 16) + 1);
544+
TestHistUpdaterApplySplit<double>(param, 0.0, 256);
545+
TestHistUpdaterApplySplit<double>(param, 0.0, 256+1);
546+
TestHistUpdaterApplySplit<double>(param, 0.0, (1u << 16) + 1);
547+
}
548+
435549
} // namespace xgboost::sycl::tree

0 commit comments

Comments
 (0)