Skip to content

Commit

Permalink
Add tests for partitioning (#35)
Browse files Browse the repository at this point in the history
* add tests for partitioning in sparce and dence cases

* refactor tests

* separate dence test into 3

* print debug info, when failed

* modify test to avoid false-positive results

---------

Co-authored-by: Dmitry Razdoburdin <>
  • Loading branch information
razdoburdin authored Mar 1, 2024
1 parent 9bc99f0 commit 6578b3e
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions tests/cpp/plugin/test_sycl_partition_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,116 @@
#include <string>
#include <utility>
#include <vector>
#include <numeric>

#include "../../../plugin/sycl/common/partition_builder.h"
#include "../../../plugin/sycl/device_manager.h"
#include "../helpers.h"

namespace xgboost::sycl::common {

void TestPartitioning(float sparsity, int max_bins) {
const size_t num_rows = 16;
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, sparsity}.GenerateDMatrix();
sycl::DeviceMatrix dmat;
dmat.Init(qu, p_fmat.get());

common::GHistIndexMatrix gmat;
gmat.Init(qu, &ctx, dmat, max_bins);

RowSetCollection row_set_collection;
auto& row_indices = row_set_collection.Data();
row_indices.Resize(&qu, num_rows);
size_t* p_row_indices = row_indices.Data();

qu.submit([&](::sycl::handler& cgh) {
cgh.parallel_for<>(::sycl::range<1>(num_rows),
[p_row_indices](::sycl::item<1> pid) {
const size_t idx = pid.get_id(0);
p_row_indices[idx] = idx;
});
}).wait_and_throw();
row_set_collection.Init();

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

const size_t n_nodes = row_set_collection.Size();
PartitionBuilder partition_builder;
partition_builder.Init(&qu, n_nodes, [&](size_t nid) {
return row_set_collection[nid].Size();
});

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

::sycl::event event;
std::vector<int32_t> split_conditions = {2};
partition_builder.Partition(gmat, nodes, row_set_collection,
split_conditions, &tree, &event);
qu.wait_and_throw();

size_t* data_result = const_cast<size_t*>(row_set_collection[0].begin);
partition_builder.MergeToArray(0, data_result, &event);
qu.wait_and_throw();

bst_float split_pt = gmat.cut.Values()[split_conditions[0]];

std::vector<uint8_t> ridx_left(num_rows, 0);
std::vector<uint8_t> ridx_right(num_rows, 0);
for (auto &batch : gmat.p_fmat->GetBatches<SparsePage>()) {
const auto& data_vec = batch.data.HostVector();
const auto& offset_vec = batch.offset.HostVector();

size_t begin = offset_vec[0];
for (size_t idx = 0; idx < offset_vec.size() - 1; ++idx) {
size_t end = offset_vec[idx + 1];
if (begin < end) {
const auto& entry = data_vec[begin];
if (entry.fvalue < split_pt) {
ridx_left[idx] = 1;
} else {
ridx_right[idx] = 1;
}
} else {
// missing value
if (tree[0].DefaultLeft()) {
ridx_left[idx] = 1;
} else {
ridx_right[idx] = 1;
}
}
begin = end;
}
}
auto n_left = std::accumulate(ridx_left.begin(), ridx_left.end(), 0);
auto n_right = std::accumulate(ridx_right.begin(), ridx_right.end(), 0);

std::vector<size_t> row_indices_host(num_rows);
qu.memcpy(row_indices_host.data(), row_indices.Data(), num_rows * sizeof(size_t));
qu.wait_and_throw();

ASSERT_EQ(n_left, partition_builder.GetNLeftElems(0));
for (size_t i = 0; i < n_left; ++i) {
auto idx = row_indices_host[i];
ASSERT_EQ(ridx_left[idx], 1);
}

ASSERT_EQ(n_right, partition_builder.GetNRightElems(0));
for (size_t i = 0; i < n_right; ++i) {
auto idx = row_indices_host[num_rows - 1 - i];
ASSERT_EQ(ridx_right[idx], 1);
}
}

TEST(SyclPartitionBuilder, BasicTest) {
constexpr size_t kNodes = 5;
// Number of rows for each node
Expand Down Expand Up @@ -88,4 +191,20 @@ TEST(SyclPartitionBuilder, BasicTest) {
}
}

TEST(SyclPartitionBuilder, PartitioningSparce) {
TestPartitioning(0.3, 256);
}

TEST(SyclPartitionBuilder, PartitioningDence8Bits) {
TestPartitioning(0.0, 256);
}

TEST(SyclPartitionBuilder, PartitioningDence16Bits) {
TestPartitioning(0.0, 256 + 1);
}

TEST(SyclPartitionBuilder, PartitioningDence32Bits) {
TestPartitioning(0.0, (1u << 16) + 1);
}

} // namespace xgboost::sycl::common

0 comments on commit 6578b3e

Please sign in to comment.