File: test_sycl_hist_updater.cc

package info (click to toggle)
xgboost 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,796 kB
  • sloc: cpp: 67,502; python: 35,503; java: 4,676; ansic: 1,426; sh: 1,320; xml: 1,197; makefile: 204; javascript: 19
file content (645 lines) | stat: -rw-r--r-- 24,861 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/**
 * Copyright 2020-2024 by XGBoost contributors
 */
#include <gtest/gtest.h>

#include <oneapi/dpl/random>

#include "../../../plugin/sycl/tree/hist_updater.h"
#include "../../../plugin/sycl/device_manager.h"

#include "../../../src/tree/common_row_partitioner.h"

#include "../helpers.h"

namespace xgboost::sycl::tree {

// Use this class to test the protected methods of HistUpdater
template <typename GradientSumT>
class TestHistUpdater : public HistUpdater<GradientSumT> {
 public:
  TestHistUpdater(const Context* ctx,
                  ::sycl::queue* qu,
                  const xgboost::tree::TrainParam& param,
                  FeatureInteractionConstraintHost int_constraints_,
                  DMatrix const* fmat) : HistUpdater<GradientSumT>(ctx, qu, param,
                                                                   int_constraints_, fmat) {}

  void TestInitSampling(const HostDeviceVector<GradientPair>& gpair,
                        USMVector<size_t, MemoryType::on_device>* row_indices) {
    HistUpdater<GradientSumT>::InitSampling(gpair, row_indices);
  }

  auto* TestInitData(const common::GHistIndexMatrix& gmat,
                     const HostDeviceVector<GradientPair>& gpair,
                     const DMatrix& fmat,
                     const RegTree& tree) {
    HistUpdater<GradientSumT>::InitData(gmat, gpair, fmat, tree);
    return &(HistUpdater<GradientSumT>::row_set_collection_);
  }

  const auto* TestBuildHistogramsLossGuide(ExpandEntry entry,
                                    const common::GHistIndexMatrix &gmat,
                                    RegTree *p_tree,
                                    const HostDeviceVector<GradientPair>& gpair) {
    HistUpdater<GradientSumT>::BuildHistogramsLossGuide(entry, gmat, p_tree, gpair);
    return &(HistUpdater<GradientSumT>::hist_);
  }

  auto TestInitNewNode(int nid,
                       const common::GHistIndexMatrix& gmat,
                       const HostDeviceVector<GradientPair>& gpair,
                       const RegTree& tree) {
    HistUpdater<GradientSumT>::InitNewNode(nid, gmat, gpair, tree);
    return HistUpdater<GradientSumT>::snode_host_[nid];
  }

  auto TestEvaluateSplits(const std::vector<ExpandEntry>& nodes_set,
                          const common::GHistIndexMatrix& gmat,
                          const RegTree& tree) {
    HistUpdater<GradientSumT>::EvaluateSplits(nodes_set, gmat, tree);
    return HistUpdater<GradientSumT>::snode_host_;
  }

  void TestApplySplit(const std::vector<ExpandEntry> nodes,
                      const common::GHistIndexMatrix& gmat,
                      RegTree* p_tree) {
    HistUpdater<GradientSumT>::ApplySplit(nodes, gmat, p_tree);
  }

  auto TestExpandWithLossGuide(const common::GHistIndexMatrix& gmat,
                               DMatrix *p_fmat,
                               RegTree* p_tree,
                               const HostDeviceVector<GradientPair>& gpair) {
    HistUpdater<GradientSumT>::ExpandWithLossGuide(gmat, p_tree, gpair);
  }

  auto TestExpandWithDepthWise(const common::GHistIndexMatrix& gmat,
                               DMatrix *p_fmat,
                               RegTree* p_tree,
                               const HostDeviceVector<GradientPair>& gpair) {
    HistUpdater<GradientSumT>::ExpandWithDepthWise(gmat, p_tree, gpair);
  }
};

void GenerateRandomGPairs(::sycl::queue* qu, GradientPair* gpair_ptr, size_t num_rows, bool has_neg_hess) {
  qu->submit([&](::sycl::handler& cgh) {
    cgh.parallel_for<>(::sycl::range<1>(::sycl::range<1>(num_rows)),
                                        [=](::sycl::item<1> pid) {
      uint64_t i = pid.get_linear_id();

      constexpr uint32_t seed = 777;
      oneapi::dpl::minstd_rand engine(seed, i);
      GradientPair::ValueT smallest_hess_val = has_neg_hess ? -1. : 0.;
      oneapi::dpl::uniform_real_distribution<GradientPair::ValueT> distr(smallest_hess_val, 1.);
      gpair_ptr[i] = {distr(engine), distr(engine)};
    });
  });
  qu->wait();
}

template <typename GradientSumT>
void TestHistUpdaterSampling(const xgboost::tree::TrainParam& param) {
  const size_t num_rows = 1u << 12;
  const size_t num_columns = 1;

  Context ctx;
  ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});

  DeviceManager device_manager;
  auto qu = device_manager.GetQueue(ctx.Device());

  auto p_fmat = RandomDataGenerator{num_rows, num_columns, 0.0}.GenerateDMatrix();

  FeatureInteractionConstraintHost int_constraints;

  TestHistUpdater<GradientSumT> updater(&ctx, qu, param, int_constraints, p_fmat.get());

  USMVector<size_t, MemoryType::on_device> row_indices_0(qu, num_rows);
  USMVector<size_t, MemoryType::on_device> row_indices_1(qu, num_rows);
  HostDeviceVector<GradientPair> gpair(num_rows, {0, 0}, ctx.Device());
  GenerateRandomGPairs(qu, gpair.DevicePointer(), num_rows, true);

  updater.TestInitSampling(gpair, &row_indices_0);
  
  size_t n_samples = row_indices_0.Size();
  // Half of gpairs have neg hess
  ASSERT_LT(n_samples, num_rows * 0.5 * param.subsample * 1.2);
  ASSERT_GT(n_samples, num_rows * 0.5 * param.subsample / 1.2);

  // Check if two lanunches generate different realisations:
  updater.TestInitSampling(gpair, &row_indices_1);
  if (row_indices_1.Size() == n_samples) {
    std::vector<size_t> row_indices_0_host(n_samples);
    std::vector<size_t> row_indices_1_host(n_samples);
    qu->memcpy(row_indices_0_host.data(), row_indices_0.Data(), n_samples * sizeof(size_t)).wait();
    qu->memcpy(row_indices_1_host.data(), row_indices_1.Data(), n_samples * sizeof(size_t)).wait();

    // The order in row_indices_0 and row_indices_1 can be different
    std::set<size_t> rows;
    for (auto row : row_indices_0_host) {
      rows.insert(row);
    }

    size_t num_diffs = 0;
    for (auto row : row_indices_1_host) {
      if (rows.count(row) == 0) num_diffs++;
    }

    ASSERT_NE(num_diffs, 0);
  }
}

template <typename GradientSumT>
void TestHistUpdaterInitData(const xgboost::tree::TrainParam& param, bool has_neg_hess) {
  const size_t num_rows = 1u << 8;
  const size_t num_columns = 1;
  const size_t n_bins = 32;

  Context ctx;
  ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});

  DeviceManager device_manager;
  auto qu = device_manager.GetQueue(ctx.Device());

  auto p_fmat = RandomDataGenerator{num_rows, num_columns, 0.0}.GenerateDMatrix();

  FeatureInteractionConstraintHost int_constraints;

  TestHistUpdater<GradientSumT> updater(&ctx, qu, param, int_constraints, p_fmat.get());

  HostDeviceVector<GradientPair> gpair(num_rows, {0, 0}, ctx.Device());
  GenerateRandomGPairs(qu, gpair.DevicePointer(), num_rows, has_neg_hess);

  common::GHistIndexMatrix gmat;
  gmat.Init(qu, &ctx, p_fmat.get(), n_bins);
  RegTree tree;

  auto* row_set_collection = updater.TestInitData(gmat, gpair, *p_fmat, tree);
  auto& row_indices = row_set_collection->Data();

  std::vector<size_t> row_indices_host(row_indices.Size());
  qu->memcpy(row_indices_host.data(), row_indices.DataConst(), row_indices.Size()*sizeof(size_t)).wait();

  if (!has_neg_hess) {
    for (size_t i = 0; i < num_rows; ++i) {
      ASSERT_EQ(row_indices_host[i], i);
    }
  } else {
    std::set<size_t> rows;
    for (size_t i = 0; i < num_rows; ++i) {
      if (gpair.HostVector()[i].GetHess() >= 0.0f) {
        rows.insert(i);
      }
    }
    ASSERT_EQ(rows.size(), row_indices_host.size());
    for (size_t row_idx : row_indices_host) {
      ASSERT_EQ(rows.count(row_idx), 1);
    }
  }
}

template <typename GradientSumT>
void TestHistUpdaterBuildHistogramsLossGuide(const xgboost::tree::TrainParam& param, float sparsity) {
  const size_t num_rows = 1u << 8;
  const size_t num_columns = 1;
  const size_t n_bins = 32;

  Context ctx;
  ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});

  DeviceManager device_manager;
  auto qu = device_manager.GetQueue(ctx.Device());

  auto p_fmat = RandomDataGenerator{num_rows, num_columns, sparsity}.GenerateDMatrix();

  FeatureInteractionConstraintHost int_constraints;

  TestHistUpdater<GradientSumT> updater(&ctx, qu, param, int_constraints, p_fmat.get());
  updater.SetHistSynchronizer(new BatchHistSynchronizer<GradientSumT>());
  updater.SetHistRowsAdder(new BatchHistRowsAdder<GradientSumT>());

  HostDeviceVector<GradientPair> gpair(num_rows, {0, 0}, ctx.Device());
  GenerateRandomGPairs(qu, gpair.DevicePointer(), num_rows, false);

  common::GHistIndexMatrix gmat;
  gmat.Init(qu, &ctx, p_fmat.get(), n_bins);

  RegTree tree;
  tree.ExpandNode(0, 0, 0, false, 0, 0, 0, 0, 0, 0, 0);
  tree.ExpandNode(tree[0].LeftChild(), 0, 0, false, 0, 0, 0, 0, 0, 0, 0);
  tree.ExpandNode(tree[0].RightChild(), 0, 0, false, 0, 0, 0, 0, 0, 0, 0);

  ExpandEntry node0(0, tree.GetDepth(0));
  ExpandEntry node1(1, tree.GetDepth(1));
  ExpandEntry node2(2, tree.GetDepth(2));

  auto* row_set_collection = updater.TestInitData(gmat, gpair, *p_fmat, tree);
  row_set_collection->AddSplit(0, 1, 2, 42, num_rows - 42);

  updater.TestBuildHistogramsLossGuide(node0, gmat, &tree, gpair);
  const auto* hist = updater.TestBuildHistogramsLossGuide(node1, gmat, &tree, gpair);

  ASSERT_EQ((*hist)[0].Size(), n_bins);
  ASSERT_EQ((*hist)[1].Size(), n_bins);
  ASSERT_EQ((*hist)[2].Size(), n_bins);

  std::vector<xgboost::detail::GradientPairInternal<GradientSumT>> hist0_host(n_bins);
  std::vector<xgboost::detail::GradientPairInternal<GradientSumT>> hist1_host(n_bins);
  std::vector<xgboost::detail::GradientPairInternal<GradientSumT>> hist2_host(n_bins);
  qu->memcpy(hist0_host.data(), (*hist)[0].DataConst(), sizeof(xgboost::detail::GradientPairInternal<GradientSumT>) * n_bins);
  qu->memcpy(hist1_host.data(), (*hist)[1].DataConst(), sizeof(xgboost::detail::GradientPairInternal<GradientSumT>) * n_bins);
  qu->memcpy(hist2_host.data(), (*hist)[2].DataConst(), sizeof(xgboost::detail::GradientPairInternal<GradientSumT>) * n_bins);
  qu->wait();

  for (size_t idx_bin = 0; idx_bin < n_bins; ++idx_bin) {
    EXPECT_NEAR(hist0_host[idx_bin].GetGrad(), hist1_host[idx_bin].GetGrad() + hist2_host[idx_bin].GetGrad(), 1e-6);
    EXPECT_NEAR(hist0_host[idx_bin].GetHess(), hist1_host[idx_bin].GetHess() + hist2_host[idx_bin].GetHess(), 1e-6);
  }
}

template <typename GradientSumT>
void TestHistUpdaterInitNewNode(const xgboost::tree::TrainParam& param, float sparsity) {
  const size_t num_rows = 1u << 8;
  const size_t num_columns = 1;
  const size_t n_bins = 32;

  Context ctx;
  ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});

  DeviceManager device_manager;
  auto qu = device_manager.GetQueue(ctx.Device());

  auto p_fmat = RandomDataGenerator{num_rows, num_columns, sparsity}.GenerateDMatrix();

  FeatureInteractionConstraintHost int_constraints;

  TestHistUpdater<GradientSumT> updater(&ctx, qu, param, int_constraints, p_fmat.get());
  updater.SetHistSynchronizer(new BatchHistSynchronizer<GradientSumT>());
  updater.SetHistRowsAdder(new BatchHistRowsAdder<GradientSumT>());

  HostDeviceVector<GradientPair> gpair(num_rows, {0, 0}, ctx.Device());
  auto* gpair_ptr = gpair.DevicePointer();
  GenerateRandomGPairs(qu, gpair_ptr, num_rows, false);

  common::GHistIndexMatrix gmat;
  gmat.Init(qu, &ctx, p_fmat.get(), n_bins);

  RegTree tree;
  tree.ExpandNode(0, 0, 0, false, 0, 0, 0, 0, 0, 0, 0);
  ExpandEntry node(ExpandEntry::kRootNid, tree.GetDepth(ExpandEntry::kRootNid));

  auto* row_set_collection = updater.TestInitData(gmat, gpair, *p_fmat, tree);
  auto& row_idxs = row_set_collection->Data();
  const size_t* row_idxs_ptr = row_idxs.DataConst();
  updater.TestBuildHistogramsLossGuide(node, gmat, &tree, gpair);
  const auto snode = updater.TestInitNewNode(ExpandEntry::kRootNid, gmat, gpair, tree);

  GradStats<GradientSumT> grad_stat;
  {
    ::sycl::buffer<GradStats<GradientSumT>> buff(&grad_stat, 1);
    qu->submit([&](::sycl::handler& cgh) {
      auto buff_acc  = buff.template get_access<::sycl::access::mode::read_write>(cgh);
      cgh.single_task<>([=]() {
        for (size_t i = 0; i < num_rows; ++i) {
          size_t row_idx = row_idxs_ptr[i];
          buff_acc[0] += GradStats<GradientSumT>(gpair_ptr[row_idx].GetGrad(),
                                                 gpair_ptr[row_idx].GetHess());
        }
      });
    }).wait_and_throw();
  }

  EXPECT_NEAR(snode.stats.GetGrad(), grad_stat.GetGrad(), 1e-6 * grad_stat.GetGrad());
  EXPECT_NEAR(snode.stats.GetHess(), grad_stat.GetHess(), 1e-6 * grad_stat.GetHess());
}

template <typename GradientSumT>
void TestHistUpdaterEvaluateSplits(const xgboost::tree::TrainParam& param) {
  const size_t num_rows = 1u << 8;
  const size_t num_columns = 2;
  const size_t n_bins = 32;

  Context ctx;
  ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});

  DeviceManager device_manager;
  auto qu = device_manager.GetQueue(ctx.Device());

  auto p_fmat = RandomDataGenerator{num_rows, num_columns, 0.0f}.GenerateDMatrix();

  FeatureInteractionConstraintHost int_constraints;

  TestHistUpdater<GradientSumT> updater(&ctx, qu, param, int_constraints, p_fmat.get());
  updater.SetHistSynchronizer(new BatchHistSynchronizer<GradientSumT>());
  updater.SetHistRowsAdder(new BatchHistRowsAdder<GradientSumT>());

  HostDeviceVector<GradientPair> gpair(num_rows, {0, 0}, ctx.Device());
  auto* gpair_ptr = gpair.DevicePointer();
  GenerateRandomGPairs(qu, gpair_ptr, num_rows, false);

  common::GHistIndexMatrix gmat;
  gmat.Init(qu, &ctx, p_fmat.get(), n_bins);

  RegTree tree;
  tree.ExpandNode(0, 0, 0, false, 0, 0, 0, 0, 0, 0, 0);
  ExpandEntry node(ExpandEntry::kRootNid, tree.GetDepth(ExpandEntry::kRootNid));

  auto* row_set_collection = updater.TestInitData(gmat, gpair, *p_fmat, tree);
  auto& row_idxs = row_set_collection->Data();
  const size_t* row_idxs_ptr = row_idxs.DataConst();
  const auto* hist = updater.TestBuildHistogramsLossGuide(node, gmat, &tree, gpair);
  const auto snode_init = updater.TestInitNewNode(ExpandEntry::kRootNid, gmat, gpair, tree);

  const auto snode_updated = updater.TestEvaluateSplits({node}, gmat, tree);
  auto best_loss_chg = snode_updated[0].best.loss_chg;
  auto stats = snode_init.stats;
  auto root_gain = snode_init.root_gain;

  // Check all splits manually. Save the best one and compare with the ans
  TreeEvaluator<GradientSumT> tree_evaluator(qu, param, num_columns);
  auto evaluator = tree_evaluator.GetEvaluator();
  const uint32_t* cut_ptr = gmat.cut.cut_ptrs_.ConstDevicePointer();
  const size_t size = gmat.cut.cut_ptrs_.Size();
  int n_better_splits = 0;
  const auto* hist_ptr = (*hist)[0].DataConst();
  std::vector<bst_float> best_loss_chg_des(1, -1);
  {
    ::sycl::buffer<bst_float> best_loss_chg_buff(best_loss_chg_des.data(), 1);
    qu->submit([&](::sycl::handler& cgh) {
      auto best_loss_chg_acc = best_loss_chg_buff.template get_access<::sycl::access::mode::read_write>(cgh);
      cgh.single_task<>([=]() {
        for (size_t i = 1; i < size; ++i) {
          GradStats<GradientSumT> left(0, 0);
          GradStats<GradientSumT> right = stats - left;
          for (size_t j = cut_ptr[i-1]; j < cut_ptr[i]; ++j) {
            auto loss_change = evaluator.CalcSplitGain(0, i - 1, left, right) - root_gain;
            if (loss_change > best_loss_chg_acc[0]) {
              best_loss_chg_acc[0] = loss_change;
            }
            left.Add(hist_ptr[j].GetGrad(), hist_ptr[j].GetHess());
            right = stats - left;
          }
        }
      });
    }).wait();
  }

  ASSERT_NEAR(best_loss_chg_des[0], best_loss_chg, 1e-4);
}

template <typename GradientSumT>
void TestHistUpdaterApplySplit(const xgboost::tree::TrainParam& param, float sparsity, int max_bins) {
  const size_t num_rows = 1024;
  const size_t num_columns = 2;

  Context ctx;
  ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});

  DeviceManager device_manager;
  auto qu = device_manager.GetQueue(ctx.Device());

  auto p_fmat = RandomDataGenerator{num_rows, num_columns, sparsity}.GenerateDMatrix();
  common::GHistIndexMatrix gmat;
  gmat.Init(qu, &ctx, p_fmat.get(), max_bins);

  RegTree tree;
  tree.ExpandNode(0, 0, 0, false, 0, 0, 0, 0, 0, 0, 0);

  std::vector<tree::ExpandEntry> nodes;
  nodes.emplace_back(tree::ExpandEntry(0, tree.GetDepth(0)));

  FeatureInteractionConstraintHost int_constraints;
  TestHistUpdater<GradientSumT> updater(&ctx, qu, param, int_constraints, p_fmat.get());
  HostDeviceVector<GradientPair> gpair(num_rows, {0, 0}, ctx.Device());
  auto* gpair_ptr = gpair.DevicePointer();
  GenerateRandomGPairs(qu, gpair_ptr, num_rows, false);

  auto* row_set_collection = updater.TestInitData(gmat, gpair, *p_fmat, tree);
  updater.TestApplySplit(nodes, gmat, &tree);

  // Copy indexes to host
  std::vector<size_t> row_indices_host(num_rows);
  qu->memcpy(row_indices_host.data(), row_set_collection->Data().Data(), sizeof(size_t)*num_rows).wait();

  // Reference Implementation
  std::vector<size_t> row_indices_desired_host(num_rows);
  size_t n_left, n_right;
  {
    TestHistUpdater<GradientSumT> updater4verification(&ctx, qu, param, int_constraints, p_fmat.get());
    auto* row_set_collection4verification = updater4verification.TestInitData(gmat, gpair, *p_fmat, tree);

    size_t n_nodes = nodes.size();
    std::vector<int32_t> split_conditions(n_nodes);
    xgboost::tree::CommonRowPartitioner::FindSplitConditions(nodes, tree, gmat, &split_conditions);

    common::PartitionBuilder partition_builder;
    partition_builder.Init(qu, n_nodes, [&](size_t node_in_set) {
      const int32_t nid = nodes[node_in_set].nid;
      return (*row_set_collection4verification)[nid].Size();
    });

    ::sycl::event event;
    partition_builder.Partition(gmat, nodes, (*row_set_collection4verification),
                                split_conditions, &tree, &event);
    qu->wait_and_throw();

    for (size_t node_in_set = 0; node_in_set < n_nodes; node_in_set++) {
      const int32_t nid = nodes[node_in_set].nid;
      size_t* data_result = const_cast<size_t*>((*row_set_collection4verification)[nid].begin);
      partition_builder.MergeToArray(node_in_set, data_result, &event);
    }
    qu->wait_and_throw();

    const int32_t nid = nodes[0].nid;
    n_left = partition_builder.GetNLeftElems(0);
    n_right = partition_builder.GetNRightElems(0);

    row_set_collection4verification->AddSplit(nid, tree[nid].LeftChild(),
        tree[nid].RightChild(), n_left, n_right);

    qu->memcpy(row_indices_desired_host.data(), row_set_collection4verification->Data().Data(), sizeof(size_t)*num_rows).wait();
  }

  std::sort(row_indices_desired_host.begin(), row_indices_desired_host.begin() + n_left);
  std::sort(row_indices_host.begin(), row_indices_host.begin() + n_left);
  std::sort(row_indices_desired_host.begin() + n_left, row_indices_desired_host.end());
  std::sort(row_indices_host.begin() + n_left, row_indices_host.end());

  for (size_t row = 0; row < num_rows; ++row) {
    ASSERT_EQ(row_indices_desired_host[row], row_indices_host[row]);
  }
}

template <typename GradientSumT>
void TestHistUpdaterExpandWithLossGuide(const xgboost::tree::TrainParam& param) {
  const size_t num_rows = 3;
  const size_t num_columns = 1;
  const size_t n_bins = 16;

  Context ctx;
  ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});

  DeviceManager device_manager;
  auto qu = device_manager.GetQueue(ctx.Device());

  std::vector<float> data = {7, 3, 15};
  auto p_fmat = GetDMatrixFromData(data, num_rows, num_columns);
  common::GHistIndexMatrix gmat;
  gmat.Init(qu, &ctx, p_fmat.get(), n_bins);

  HostDeviceVector<GradientPair> gpair({{1, 2}, {3, 1}, {1, 1}}, ctx.Device());

  RegTree tree;
  FeatureInteractionConstraintHost int_constraints;
  TestHistUpdater<GradientSumT> updater(&ctx, qu, param, int_constraints, p_fmat.get());
  updater.SetHistSynchronizer(new BatchHistSynchronizer<GradientSumT>());
  updater.SetHistRowsAdder(new BatchHistRowsAdder<GradientSumT>());
  auto* row_set_collection = updater.TestInitData(gmat, gpair, *p_fmat, tree);

  updater.TestExpandWithLossGuide(gmat, p_fmat.get(), &tree, gpair);

  const auto& nodes = tree.GetNodes();
  std::vector<float> ans(data.size());
  for (size_t data_idx = 0; data_idx < data.size(); ++data_idx) {
      size_t node_idx = 0;
      while (!nodes[node_idx].IsLeaf()) {
        node_idx = data[data_idx] < nodes[node_idx].SplitCond() ? nodes[node_idx].LeftChild() : nodes[node_idx].RightChild();
      }
      ans[data_idx] = nodes[node_idx].LeafValue();
  }

  ASSERT_NEAR(ans[0], -0.15, 1e-6);
  ASSERT_NEAR(ans[1], -0.45, 1e-6);
  ASSERT_NEAR(ans[2], -0.15, 1e-6);
}


template <typename GradientSumT>
void TestHistUpdaterExpandWithDepthWise(const xgboost::tree::TrainParam& param) {
  const size_t num_rows = 3;
  const size_t num_columns = 1;
  const size_t n_bins = 16;

  Context ctx;
  ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});

  DeviceManager device_manager;
  auto qu = device_manager.GetQueue(ctx.Device());

  std::vector<float> data = {7, 3, 15};
  auto p_fmat = GetDMatrixFromData(data, num_rows, num_columns);
  common::GHistIndexMatrix gmat;
  gmat.Init(qu, &ctx, p_fmat.get(), n_bins);

  HostDeviceVector<GradientPair> gpair({{1, 2}, {3, 1}, {1, 1}}, ctx.Device());

  RegTree tree;
  FeatureInteractionConstraintHost int_constraints;
  TestHistUpdater<GradientSumT> updater(&ctx, qu, param, int_constraints, p_fmat.get());
  updater.SetHistSynchronizer(new BatchHistSynchronizer<GradientSumT>());
  updater.SetHistRowsAdder(new BatchHistRowsAdder<GradientSumT>());
  auto* row_set_collection = updater.TestInitData(gmat, gpair, *p_fmat, tree);

  updater.TestExpandWithDepthWise(gmat, p_fmat.get(), &tree, gpair);

  const auto& nodes = tree.GetNodes();
  std::vector<float> ans(data.size());
  for (size_t data_idx = 0; data_idx < data.size(); ++data_idx) {
      size_t node_idx = 0;
      while (!nodes[node_idx].IsLeaf()) {
        node_idx = data[data_idx] < nodes[node_idx].SplitCond() ? nodes[node_idx].LeftChild() : nodes[node_idx].RightChild();
      }
      ans[data_idx] = nodes[node_idx].LeafValue();
  }

  ASSERT_NEAR(ans[0], -0.15, 1e-6);
  ASSERT_NEAR(ans[1], -0.45, 1e-6);
  ASSERT_NEAR(ans[2], -0.15, 1e-6);
}

TEST(SyclHistUpdater, Sampling) {
  xgboost::tree::TrainParam param;
  param.UpdateAllowUnknown(Args{{"subsample", "0.7"}});

  TestHistUpdaterSampling<float>(param);
  TestHistUpdaterSampling<double>(param);
}

TEST(SyclHistUpdater, InitData) {
  xgboost::tree::TrainParam param;
  param.UpdateAllowUnknown(Args{{"subsample", "1"}});

  TestHistUpdaterInitData<float>(param, true);
  TestHistUpdaterInitData<float>(param, false);

  TestHistUpdaterInitData<double>(param, true);
  TestHistUpdaterInitData<double>(param, false);
}

TEST(SyclHistUpdater, BuildHistogramsLossGuide) {
  xgboost::tree::TrainParam param;
  param.UpdateAllowUnknown(Args{{"max_depth", "3"}});

  TestHistUpdaterBuildHistogramsLossGuide<float>(param, 0.0);
  TestHistUpdaterBuildHistogramsLossGuide<float>(param, 0.5);
  TestHistUpdaterBuildHistogramsLossGuide<double>(param, 0.0);
  TestHistUpdaterBuildHistogramsLossGuide<double>(param, 0.5);
}

TEST(SyclHistUpdater, InitNewNode) {
  xgboost::tree::TrainParam param;
  param.UpdateAllowUnknown(Args{{"max_depth", "3"}});

  TestHistUpdaterInitNewNode<float>(param, 0.0);
  TestHistUpdaterInitNewNode<float>(param, 0.5);
  TestHistUpdaterInitNewNode<double>(param, 0.0);
  TestHistUpdaterInitNewNode<double>(param, 0.5);
}

TEST(SyclHistUpdater, EvaluateSplits) {
  xgboost::tree::TrainParam param;
  param.UpdateAllowUnknown(Args{{"max_depth", "3"}});

  TestHistUpdaterEvaluateSplits<float>(param);
  TestHistUpdaterEvaluateSplits<double>(param);
}

TEST(SyclHistUpdater, ApplySplitSparce) {
  xgboost::tree::TrainParam param;
  param.UpdateAllowUnknown(Args{{"max_depth", "3"}});

  TestHistUpdaterApplySplit<float>(param, 0.3, 256);
  TestHistUpdaterApplySplit<double>(param, 0.3, 256);
}

TEST(SyclHistUpdater, ApplySplitDence) {
  xgboost::tree::TrainParam param;
  param.UpdateAllowUnknown(Args{{"max_depth", "3"}});

  TestHistUpdaterApplySplit<float>(param, 0.0, 256);
  TestHistUpdaterApplySplit<float>(param, 0.0, 256+1);
  TestHistUpdaterApplySplit<float>(param, 0.0, (1u << 16) + 1);
  TestHistUpdaterApplySplit<double>(param, 0.0, 256);
  TestHistUpdaterApplySplit<double>(param, 0.0, 256+1);
  TestHistUpdaterApplySplit<double>(param, 0.0, (1u << 16) + 1);
}

TEST(SyclHistUpdater, ExpandWithLossGuide) {
  xgboost::tree::TrainParam param;
  param.UpdateAllowUnknown(Args{{"max_depth", "2"},
                                {"grow_policy", "lossguide"}});

  TestHistUpdaterExpandWithLossGuide<float>(param);
  TestHistUpdaterExpandWithLossGuide<double>(param);
}

TEST(SyclHistUpdater, ExpandWithDepthWise) {
  xgboost::tree::TrainParam param;
  param.UpdateAllowUnknown(Args{{"max_depth", "2"}});

  TestHistUpdaterExpandWithDepthWise<float>(param);
  TestHistUpdaterExpandWithDepthWise<double>(param);
}

}  // namespace xgboost::sycl::tree