|
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,88 @@ 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 = 16; |
| 399 | + const size_t num_columns = 1; |
| 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 | + { |
| 435 | + TestHistUpdater<GradientSumT> updater4verification(&ctx, qu, param, int_constraints, p_fmat.get()); |
| 436 | + auto* row_set_collection4verification = updater4verification.TestInitData(gmat, gpair, *p_fmat, tree); |
| 437 | + |
| 438 | + size_t n_nodes = nodes.size(); |
| 439 | + std::vector<int32_t> split_conditions(n_nodes); |
| 440 | + xgboost::tree::CommonRowPartitioner::FindSplitConditions(nodes, tree, gmat, &split_conditions); |
| 441 | + |
| 442 | + common::PartitionBuilder partition_builder; |
| 443 | + partition_builder.Init(&qu, n_nodes, [&](size_t node_in_set) { |
| 444 | + const int32_t nid = nodes[node_in_set].nid; |
| 445 | + return (*row_set_collection4verification)[nid].Size(); |
| 446 | + }); |
| 447 | + |
| 448 | + ::sycl::event event; |
| 449 | + partition_builder.Partition(gmat, nodes, (*row_set_collection4verification), |
| 450 | + split_conditions, &tree, &event); |
| 451 | + qu.wait_and_throw(); |
| 452 | + |
| 453 | + for (size_t node_in_set = 0; node_in_set < n_nodes; node_in_set++) { |
| 454 | + const int32_t nid = nodes[node_in_set].nid; |
| 455 | + size_t* data_result = const_cast<size_t*>((*row_set_collection4verification)[nid].begin); |
| 456 | + partition_builder.MergeToArray(node_in_set, data_result, &event); |
| 457 | + } |
| 458 | + qu.wait_and_throw(); |
| 459 | + |
| 460 | + for (size_t i = 0; i < n_nodes; ++i) { |
| 461 | + const int32_t nid = nodes[i].nid; |
| 462 | + const size_t n_left = partition_builder.GetNLeftElems(i); |
| 463 | + const size_t n_right = partition_builder.GetNRightElems(i); |
| 464 | + |
| 465 | + row_set_collection4verification->AddSplit(nid, tree[nid].LeftChild(), |
| 466 | + tree[nid].RightChild(), n_left, n_right); |
| 467 | + } |
| 468 | + |
| 469 | + qu.memcpy(row_indices_desired_host.data(), row_set_collection4verification->Data().Data(), sizeof(size_t)*num_rows).wait(); |
| 470 | + } |
| 471 | + |
| 472 | + for (size_t row = 0; row < num_rows; ++row) { |
| 473 | + ASSERT_EQ(row_indices_desired_host[row], row_indices_host[row]); |
| 474 | + } |
| 475 | + |
| 476 | +} |
| 477 | + |
388 | 478 | TEST(SyclHistUpdater, Sampling) {
|
389 | 479 | xgboost::tree::TrainParam param;
|
390 | 480 | param.UpdateAllowUnknown(Args{{"subsample", "0.7"}});
|
@@ -432,12 +522,24 @@ TEST(SyclHistUpdater, EvaluateSplits) {
|
432 | 522 | TestHistUpdaterEvaluateSplits<double>(param);
|
433 | 523 | }
|
434 | 524 |
|
435 |
| -TEST(SyclHistUpdater, ApplySplit) { |
| 525 | +TEST(SyclHistUpdater, ApplySplitSparce) { |
| 526 | + xgboost::tree::TrainParam param; |
| 527 | + param.UpdateAllowUnknown(Args{{"max_depth", "3"}}); |
| 528 | + |
| 529 | + TestHistUpdaterApplySplit<float>(param, 0.3, 256); |
| 530 | + TestHistUpdaterApplySplit<double>(param, 0.3, 256); |
| 531 | +} |
| 532 | + |
| 533 | +TEST(SyclHistUpdater, ApplySplitDence) { |
436 | 534 | xgboost::tree::TrainParam param;
|
437 | 535 | param.UpdateAllowUnknown(Args{{"max_depth", "3"}});
|
438 | 536 |
|
439 |
| - // TestHistUpdaterApplySplit<float>(param); |
440 |
| - // TestHistUpdaterApplySplit<double>(param); |
| 537 | + TestHistUpdaterApplySplit<float>(param, 0.0, 256); |
| 538 | + TestHistUpdaterApplySplit<float>(param, 0.0, 256+1); |
| 539 | + TestHistUpdaterApplySplit<float>(param, 0.0, (1u << 16) + 1); |
| 540 | + TestHistUpdaterApplySplit<double>(param, 0.0, 256); |
| 541 | + TestHistUpdaterApplySplit<double>(param, 0.0, 256+1); |
| 542 | + TestHistUpdaterApplySplit<double>(param, 0.0, (1u << 16) + 1); |
441 | 543 | }
|
442 | 544 |
|
443 | 545 | } // namespace xgboost::sycl::tree
|
0 commit comments