|
8 | 8 | #include "../../../plugin/sycl/tree/hist_updater.h"
|
9 | 9 | #include "../../../plugin/sycl/device_manager.h"
|
10 | 10 |
|
| 11 | +#include "../../../src/tree/common_row_partitioner.h" |
| 12 | + |
11 | 13 | #include "../helpers.h"
|
12 | 14 |
|
13 | 15 | namespace xgboost::sycl::tree {
|
@@ -59,6 +61,12 @@ class TestHistUpdater : public HistUpdater<GradientSumT> {
|
59 | 61 | HistUpdater<GradientSumT>::EvaluateSplits(nodes_set, gmat, tree);
|
60 | 62 | return HistUpdater<GradientSumT>::snode_host_;
|
61 | 63 | }
|
| 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 | + } |
62 | 70 | };
|
63 | 71 |
|
64 | 72 | 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) {
|
385 | 393 | ASSERT_NEAR(best_loss_chg_des[0], best_loss_chg, 1e-6);
|
386 | 394 | }
|
387 | 395 |
|
| 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 | + |
388 | 482 | TEST(SyclHistUpdater, Sampling) {
|
389 | 483 | xgboost::tree::TrainParam param;
|
390 | 484 | param.UpdateAllowUnknown(Args{{"subsample", "0.7"}});
|
@@ -432,4 +526,24 @@ TEST(SyclHistUpdater, EvaluateSplits) {
|
432 | 526 | TestHistUpdaterEvaluateSplits<double>(param);
|
433 | 527 | }
|
434 | 528 |
|
| 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 | + |
435 | 549 | } // namespace xgboost::sycl::tree
|
0 commit comments