From 346091595290fdb8fb77aa5b3d6e72d16e532020 Mon Sep 17 00:00:00 2001 From: Rory Mitchell Date: Wed, 8 Jul 2026 10:18:46 +0200 Subject: [PATCH 1/6] Add RF split global and local counts --- .../batched-levelalgo/builder.cuh | 32 +++---- .../kernels/builder_kernels.cuh | 8 +- .../kernels/builder_kernels_impl.cuh | 31 ++++--- .../batched-levelalgo/objectives.cuh | 51 +++++++---- .../decisiontree/batched-levelalgo/split.cuh | 88 +++++++++++++------ cpp/tests/sg/rf_test.cu | 28 ++---- 6 files changed, 133 insertions(+), 105 deletions(-) diff --git a/cpp/src/decisiontree/batched-levelalgo/builder.cuh b/cpp/src/decisiontree/batched-levelalgo/builder.cuh index 7127f96449..ed438afe0a 100644 --- a/cpp/src/decisiontree/batched-levelalgo/builder.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/builder.cuh @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -107,8 +108,9 @@ class NodeQueue { parent_range.count); tree->leaf_counter++; // left - tree->sparsetree.emplace_back(NodeT::CreateLeafNode(split.nLeft)); - node_instances_.emplace_back(InstanceRange{parent_range.begin, std::size_t(split.nLeft)}); + tree->sparsetree.emplace_back(NodeT::CreateLeafNode(split.global_nLeft)); + node_instances_.emplace_back( + InstanceRange{parent_range.begin, std::size_t(split.local_nLeft)}); // Do not add a work item if this child is definitely a leaf if (this->IsExpandable(tree->sparsetree.back(), item.depth + 1)) { @@ -117,9 +119,11 @@ class NodeQueue { } // right - tree->sparsetree.emplace_back(NodeT::CreateLeafNode(parent_range.count - split.nLeft)); + tree->sparsetree.emplace_back( + NodeT::CreateLeafNode(tree->sparsetree.at(item.idx).InstanceCount() - split.global_nLeft)); node_instances_.emplace_back( - InstanceRange{parent_range.begin + split.nLeft, parent_range.count - split.nLeft}); + InstanceRange{parent_range.begin + std::size_t(split.local_nLeft), + parent_range.count - std::size_t(split.local_nLeft)}); // Do not add a work item if this child is definitely a leaf if (this->IsExpandable(tree->sparsetree.back(), item.depth + 1)) { @@ -401,21 +405,10 @@ struct Builder { "n_sampled_cols must be in [1, n_cols]"); const std::size_t max_sampling_rounds = std::size_t((dataset.N + original_n_sampled_cols - 1) / original_n_sampled_cols); - struct HostSplit { - DataT quesval; - IdxT colid; - DataT best_metric_val; - int nLeft; - IdxT split_start; - IdxT split_end; - }; - static_assert(sizeof(HostSplit) == sizeof(SplitT)); - static_assert(alignof(HostSplit) == alignof(SplitT)); - // The final split chosen for each original work item. Nodes that need // additional feature samples are compacted in active_items, so successful // splits must be copied back to their original batch position. - std::vector final_splits(work_items.size()); + std::vector final_splits(work_items.size()); // Current retry batch. It starts as the full batch and shrinks to only // nodes whose sampled features did not produce a valid split. std::vector active_items(work_items); @@ -437,12 +430,7 @@ struct Builder { std::vector retry_to_original; for (std::size_t i = 0; i < active_items.size(); ++i) { const auto original_idx = active_to_original[i]; - final_splits[original_idx] = HostSplit{h_splits[i].quesval, - h_splits[i].colid, - h_splits[i].best_metric_val, - h_splits[i].nLeft, - h_splits[i].split_start, - h_splits[i].split_end}; + final_splits[original_idx] = h_splits[i]; if (SplitPartitionNotValid( h_splits[i], params.min_samples_leaf, active_items[i].instances.count)) { retry_items.push_back(active_items[i]); diff --git a/cpp/src/decisiontree/batched-levelalgo/kernels/builder_kernels.cuh b/cpp/src/decisiontree/batched-levelalgo/kernels/builder_kernels.cuh index b13ba2d9d0..882f7fbdcb 100644 --- a/cpp/src/decisiontree/batched-levelalgo/kernels/builder_kernels.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/kernels/builder_kernels.cuh @@ -18,6 +18,8 @@ #include #include +#include + namespace ML { namespace DT { @@ -51,8 +53,10 @@ struct WorkloadInfo { template HDI bool SplitPartitionNotValid(const SplitT& split, IdxT min_samples_leaf, std::size_t num_rows) { - return split.colid == IdxT(-1) || split.nLeft < min_samples_leaf || - (IdxT(num_rows) - split.nLeft) < min_samples_leaf; + const auto local_count = static_cast(num_rows); + const auto min_leaf = static_cast(min_samples_leaf); + return split.colid == IdxT(-1) || split.local_nLeft > local_count || + split.local_nLeft < min_leaf || (local_count - split.local_nLeft) < min_leaf; } template diff --git a/cpp/src/decisiontree/batched-levelalgo/kernels/builder_kernels_impl.cuh b/cpp/src/decisiontree/batched-levelalgo/kernels/builder_kernels_impl.cuh index faf0fe241f..b8d3105953 100644 --- a/cpp/src/decisiontree/batched-levelalgo/kernels/builder_kernels_impl.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/kernels/builder_kernels_impl.cuh @@ -22,6 +22,7 @@ #include #include +#include #include namespace ML { @@ -29,17 +30,15 @@ namespace DT { static constexpr int TPB_DEFAULT = 128; -template struct NodeSplitPartitionState { - IdxT left_count; + std::int64_t left_count; bool valid_row; bool goes_left; }; -template struct NodeSplitPartitionScanOp { - __host__ __device__ NodeSplitPartitionState operator()( - const NodeSplitPartitionState& lhs, const NodeSplitPartitionState& rhs) const + __host__ __device__ NodeSplitPartitionState operator()(const NodeSplitPartitionState& lhs, + const NodeSplitPartitionState& rhs) const { return {lhs.left_count + rhs.left_count, rhs.valid_row, rhs.goes_left}; } @@ -57,8 +56,7 @@ struct NodeSplitPartitionWriter { const WorkloadInfo* workload_info; IdxT* partition_row_ids; - __host__ __device__ void operator()(std::ptrdiff_t index, - NodeSplitPartitionState state) const + __host__ __device__ void operator()(std::ptrdiff_t index, NodeSplitPartitionState state) const { if (!state.valid_row) { return; } @@ -71,11 +69,12 @@ struct NodeSplitPartitionWriter { const auto range_start = work_item.instances.begin; const auto range_pos = std::size_t(workload_info_cta.offset_blockid) * TPB + slot % TPB; - const auto row = dataset.row_ids[range_start + range_pos]; - const auto rank = - state.goes_left ? state.left_count - IdxT(1) : IdxT(range_pos) - state.left_count; - const auto out_idx = range_start + (state.goes_left ? rank : split.nLeft + rank); - partition_row_ids[out_idx] = row; + const auto row = dataset.row_ids[range_start + range_pos]; + const auto rank = state.goes_left ? std::size_t(state.left_count - std::int64_t{1}) + : range_pos - std::size_t(state.left_count); + const auto local_left_count = std::size_t(split.local_nLeft); + const auto out_idx = range_start + (state.goes_left ? rank : local_left_count + rank); + partition_row_ids[out_idx] = row; } }; @@ -135,18 +134,18 @@ void launchNodeSplitKernel(const IdxT min_samples_leaf, const auto work_item = work_items[nid]; const auto split = splits[nid]; if (SplitNotValid(split, min_impurity_decrease, min_samples_leaf, work_item.instances.count)) { - return NodeSplitPartitionState{IdxT(0), false, false}; + return NodeSplitPartitionState{std::int64_t{0}, false, false}; } const auto range_pos = std::size_t(workload_info_cta.offset_blockid) * TPB + slot % TPB; if (range_pos >= work_item.instances.count) { - return NodeSplitPartitionState{IdxT(0), false, false}; + return NodeSplitPartitionState{std::int64_t{0}, false, false}; } const auto row = dataset.row_ids[work_item.instances.begin + range_pos]; const auto col_idx = std::size_t(split.colid) * dataset.M + row; const auto goes_left = dataset.data[col_idx] <= split.quesval; - return NodeSplitPartitionState{goes_left ? IdxT(1) : IdxT(0), true, goes_left}; + return NodeSplitPartitionState{goes_left ? std::int64_t{1} : std::int64_t{0}, true, goes_left}; }; // The scan input is a stream of per-slot partition states keyed by node id. @@ -163,7 +162,7 @@ void launchNodeSplitKernel(const IdxT min_samples_leaf, partition_states, partition_writer, thrust::equal_to{}, - NodeSplitPartitionScanOp{}); + NodeSplitPartitionScanOp{}); // The original row_ids buffer remains the source during the scan, so copy back after it finishes. nodeSplitCopyBackKernel diff --git a/cpp/src/decisiontree/batched-levelalgo/objectives.cuh b/cpp/src/decisiontree/batched-levelalgo/objectives.cuh index 6d3284e3cb..79c93da2a6 100644 --- a/cpp/src/decisiontree/batched-levelalgo/objectives.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/objectives.cuh @@ -11,6 +11,7 @@ #include +#include #include #include @@ -39,7 +40,8 @@ class ClassificationObjectiveFunction { return weight; } - HDI DataT GiniGain(BinT const* hist, IdxT i, IdxT n_bins, IdxT, IdxT, IdxT) const + HDI DataT + GiniGain(BinT const* hist, IdxT i, IdxT n_bins, std::int64_t, std::int64_t, std::int64_t) const { constexpr DataT One = DataT(1.0); auto total_weight = WeightAt(hist, n_bins - 1, n_bins); @@ -74,7 +76,8 @@ class ClassificationObjectiveFunction { return gain; } - HDI DataT EntropyGain(BinT const* hist, IdxT i, IdxT n_bins, IdxT, IdxT, IdxT) const + HDI DataT + EntropyGain(BinT const* hist, IdxT i, IdxT n_bins, std::int64_t, std::int64_t, std::int64_t) const { auto total_weight = WeightAt(hist, n_bins - 1, n_bins); auto left_weight = WeightAt(hist, i, n_bins); @@ -114,10 +117,15 @@ class ClassificationObjectiveFunction { } public: - HDI DataT - GainPerSplit(BinT const* hist, IdxT i, IdxT n_bins, IdxT len, IdxT nLeft, IdxT nRight) const + HDI DataT GainPerSplit(BinT const* hist, + IdxT i, + IdxT n_bins, + std::int64_t len, + std::int64_t nLeft, + std::int64_t nRight) const { - if (nLeft < min_samples_leaf || nRight < min_samples_leaf) + if (nLeft < static_cast(min_samples_leaf) || + nRight < static_cast(min_samples_leaf)) return -std::numeric_limits::max(); switch (criterion) { @@ -146,14 +154,15 @@ class ClassificationObjectiveFunction { } DI Split Gain( - BinT const* shist, DataT const* squantiles, IdxT col, IdxT len, IdxT n_bins) const + BinT const* shist, DataT const* squantiles, IdxT col, std::int64_t len, IdxT n_bins) const { Split sp; for (IdxT i = threadIdx.x; i < n_bins; i += blockDim.x) { auto nLeft = detail::CountLeft(shist, i, n_bins, nclasses); auto nRight = len - nLeft; auto gain = -std::numeric_limits::max(); - if (nLeft >= min_samples_leaf && nRight >= min_samples_leaf) { + if (nLeft >= static_cast(min_samples_leaf) && + nRight >= static_cast(min_samples_leaf)) { gain = GainPerSplit(shist, i, n_bins, len, nLeft, nRight); } sp.update({squantiles[i], col, gain, nLeft, i}); @@ -194,7 +203,8 @@ class RegressionObjectiveFunction { CRITERION criterion; static constexpr auto eps_ = 10 * std::numeric_limits::epsilon(); - HDI DataT MSEGain(BinT const* hist, IdxT i, IdxT n_bins, IdxT, IdxT, IdxT) const + HDI DataT + MSEGain(BinT const* hist, IdxT i, IdxT n_bins, std::int64_t, std::int64_t, std::int64_t) const { auto parent_weight = hist[n_bins - 1].Weight(); auto left_weight = hist[i].Weight(); @@ -216,7 +226,8 @@ class RegressionObjectiveFunction { return gain; } - HDI DataT PoissonGain(BinT const* hist, IdxT i, IdxT n_bins, IdxT, IdxT, IdxT) const + HDI DataT + PoissonGain(BinT const* hist, IdxT i, IdxT n_bins, std::int64_t, std::int64_t, std::int64_t) const { auto parent_weight = hist[n_bins - 1].Weight(); auto left_weight = hist[i].Weight(); @@ -243,7 +254,8 @@ class RegressionObjectiveFunction { return gain; } - HDI DataT GammaGain(BinT const* hist, IdxT i, IdxT n_bins, IdxT, IdxT, IdxT) const + HDI DataT + GammaGain(BinT const* hist, IdxT i, IdxT n_bins, std::int64_t, std::int64_t, std::int64_t) const { auto parent_weight = hist[n_bins - 1].Weight(); auto left_weight = hist[i].Weight(); @@ -270,7 +282,8 @@ class RegressionObjectiveFunction { return gain; } - HDI DataT InverseGaussianGain(BinT const* hist, IdxT i, IdxT n_bins, IdxT, IdxT, IdxT) const + HDI DataT InverseGaussianGain( + BinT const* hist, IdxT i, IdxT n_bins, std::int64_t, std::int64_t, std::int64_t) const { auto parent_weight = hist[n_bins - 1].Weight(); auto left_weight = hist[i].Weight(); @@ -297,10 +310,15 @@ class RegressionObjectiveFunction { } public: - HDI DataT - GainPerSplit(BinT const* hist, IdxT i, IdxT n_bins, IdxT len, IdxT nLeft, IdxT nRight) const + HDI DataT GainPerSplit(BinT const* hist, + IdxT i, + IdxT n_bins, + std::int64_t len, + std::int64_t nLeft, + std::int64_t nRight) const { - if (nLeft < min_samples_leaf || nRight < min_samples_leaf) + if (nLeft < static_cast(min_samples_leaf) || + nRight < static_cast(min_samples_leaf)) return -std::numeric_limits::max(); switch (criterion) { @@ -332,14 +350,15 @@ class RegressionObjectiveFunction { } DI Split Gain( - BinT const* shist, DataT const* squantiles, IdxT col, IdxT len, IdxT n_bins) const + BinT const* shist, DataT const* squantiles, IdxT col, std::int64_t len, IdxT n_bins) const { Split sp; for (IdxT i = threadIdx.x; i < n_bins; i += blockDim.x) { auto nLeft = detail::CountLeft(shist, i, n_bins, IdxT{1}); auto nRight = len - nLeft; auto gain = -std::numeric_limits::max(); - if (nLeft >= min_samples_leaf && nRight >= min_samples_leaf) { + if (nLeft >= static_cast(min_samples_leaf) && + nRight >= static_cast(min_samples_leaf)) { gain = GainPerSplit(shist, i, n_bins, len, nLeft, nRight); } sp.update({squantiles[i], col, gain, nLeft, i}); diff --git a/cpp/src/decisiontree/batched-levelalgo/split.cuh b/cpp/src/decisiontree/batched-levelalgo/split.cuh index bea1a2b15e..a82a32e426 100644 --- a/cpp/src/decisiontree/batched-levelalgo/split.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/split.cuh @@ -5,21 +5,25 @@ #pragma once +#include "bins.cuh" + #include #include +#include + namespace ML { namespace DT { namespace detail { template -DI IdxT CountLeft(BinT const* hist, IdxT i, IdxT n_bins, IdxT n_outputs) +DI std::int64_t CountLeft(BinT const* hist, IdxT i, IdxT n_bins, IdxT n_outputs) { auto nLeft = hist[i].Count(); for (IdxT j = 1; j < n_outputs; ++j) { nLeft += hist[n_bins * j + i].Count(); } - return static_cast(nLeft); + return static_cast(nLeft); } } // namespace detail @@ -32,7 +36,6 @@ DI IdxT CountLeft(BinT const* hist, IdxT i, IdxT n_bins, IdxT n_outputs) template struct Split { typedef Split SplitT; - /** start with this as the initial gain */ static constexpr DataT Min = -std::numeric_limits::max(); @@ -42,15 +45,37 @@ struct Split { IdxT colid; /** best info gain on this node */ DataT best_metric_val; - /** number of samples in the left child */ - int nLeft; + /** global number of samples in the left child */ + std::int64_t global_nLeft; + /** rank-local number of samples in the left child */ + std::int64_t local_nLeft; /** first quantile index in an inclusive range of training-equivalent splits */ IdxT split_start; /** last quantile index in an inclusive range of training-equivalent splits */ IdxT split_end; - DI Split(DataT quesval, IdxT colid, DataT best_metric_val, IdxT nLeft, IdxT bin = -1) - : quesval(quesval), colid(colid), best_metric_val(best_metric_val), nLeft(nLeft) + DI Split(DataT quesval, IdxT colid, DataT best_metric_val, std::int64_t nLeft, IdxT bin = -1) + : quesval(quesval), + colid(colid), + best_metric_val(best_metric_val), + global_nLeft(nLeft), + local_nLeft(nLeft) + { + split_start = bin; + split_end = bin; + } + + DI Split(DataT quesval, + IdxT colid, + DataT best_metric_val, + std::int64_t global_nLeft, + std::int64_t local_nLeft, + IdxT bin) + : quesval(quesval), + colid(colid), + best_metric_val(best_metric_val), + global_nLeft(global_nLeft), + local_nLeft(local_nLeft) { split_start = bin; split_end = bin; @@ -60,7 +85,8 @@ struct Split { { quesval = best_metric_val = Min; colid = -1; - nLeft = 0; + global_nLeft = 0; + local_nLeft = 0; split_start = -1; split_end = -1; } @@ -77,7 +103,8 @@ struct Split { quesval = other.quesval; colid = other.colid; best_metric_val = other.best_metric_val; - nLeft = other.nLeft; + global_nLeft = other.global_nLeft; + local_nLeft = other.local_nLeft; split_start = other.split_start; split_end = other.split_end; return *this; @@ -90,7 +117,8 @@ struct Split { DI bool can_merge_equivalent_split_range(const SplitT& other) const { - return nLeft == other.nLeft && has_valid_split_range() && other.has_valid_split_range(); + return global_nLeft == other.global_nLeft && local_nLeft == other.local_nLeft && + has_valid_split_range() && other.has_valid_split_range(); } // Extend the candidate's inclusive range of training-equivalent split @@ -158,14 +186,15 @@ struct Split { auto lane = raft::laneId(); #pragma unroll for (int i = raft::WarpSize / 2; i >= 1; i /= 2) { - auto id = lane + i; - auto qu = raft::shfl(quesval, id); - auto co = raft::shfl(colid, id); - auto be = raft::shfl(best_metric_val, id); - auto nl = raft::shfl(nLeft, id); - auto bs = raft::shfl(split_start, id); - auto bn = raft::shfl(split_end, id); - SplitT other(qu, co, be, nl, bs); + auto id = lane + i; + auto qu = raft::shfl(quesval, id); + auto co = raft::shfl(colid, id); + auto be = raft::shfl(best_metric_val, id); + auto gnl = raft::shfl(global_nLeft, id); + auto lnl = raft::shfl(local_nLeft, id); + auto bs = raft::shfl(split_start, id); + auto bn = raft::shfl(split_end, id); + SplitT other(qu, co, be, gnl, lnl, bs); other.split_start = bs; other.split_end = bn; update(other); @@ -207,7 +236,8 @@ struct Split { split_reg.quesval = split->quesval; split_reg.colid = split->colid; split_reg.best_metric_val = split->best_metric_val; - split_reg.nLeft = split->nLeft; + split_reg.global_nLeft = split->global_nLeft; + split_reg.local_nLeft = split->local_nLeft; split_reg.split_start = split->split_start; split_reg.split_end = split->split_end; bool update_result = split_reg.update(*this); @@ -215,7 +245,8 @@ struct Split { split->quesval = split_reg.quesval; split->colid = split_reg.colid; split->best_metric_val = split_reg.best_metric_val; - split->nLeft = split_reg.nLeft; + split->global_nLeft = split_reg.global_nLeft; + split->local_nLeft = split_reg.local_nLeft; split->split_start = split_reg.split_start; split->split_end = split_reg.split_end; } @@ -244,13 +275,16 @@ template void printSplits(Split* splits, IdxT len, cudaStream_t s) { auto op = [] __device__(Split * ptr, IdxT idx) { - printf("quesval = %e, colid = %d, best_metric_val = %e, nLeft = %d, split_range = [%d, %d]\n", - ptr->quesval, - ptr->colid, - ptr->best_metric_val, - ptr->nLeft, - ptr->split_start, - ptr->split_end); + printf( + "quesval = %e, colid = %d, best_metric_val = %e, global_nLeft = %lld, " + "local_nLeft = %lld, split_range = [%d, %d]\n", + ptr->quesval, + ptr->colid, + ptr->best_metric_val, + static_cast(ptr->global_nLeft), + static_cast(ptr->local_nLeft), + ptr->split_start, + ptr->split_end); }; raft::linalg::writeOnlyUnaryOp, decltype(op), IdxT, TPB>(splits, len, op, s); RAFT_CUDA_TRY(cudaDeviceSynchronize()); diff --git a/cpp/tests/sg/rf_test.cu b/cpp/tests/sg/rf_test.cu index 92a2cc3cb0..90db1ca393 100644 --- a/cpp/tests/sg/rf_test.cu +++ b/cpp/tests/sg/rf_test.cu @@ -1320,21 +1320,13 @@ TEST(RFEquivalentSplitRangeTest, ClassificationChoosesUpperMiddleBin) n_bins); RAFT_CUDA_TRY(cudaGetLastError()); - struct HostSplit { - DataT quesval; - IdxT colid; - DataT best_metric_val; - int nLeft; - IdxT split_start; - IdxT split_end; - }; - static_assert(sizeof(HostSplit) == sizeof(DT::Split)); - HostSplit h_split; + DT::Split h_split; RAFT_CUDA_TRY(cudaMemcpyAsync( &h_split, split.data().get(), sizeof(h_split), cudaMemcpyDeviceToHost, handle.get_stream())); handle.sync_stream(); - EXPECT_EQ(h_split.nLeft, 4); + EXPECT_EQ(h_split.global_nLeft, 4); + EXPECT_EQ(h_split.local_nLeft, 4); EXPECT_EQ(h_split.quesval, DataT{3}); EXPECT_EQ(h_split.split_start, 3); EXPECT_EQ(h_split.split_end, 3); @@ -1376,21 +1368,13 @@ TEST(RFEquivalentSplitRangeTest, RegressionChoosesUpperMiddleBin) n_bins); RAFT_CUDA_TRY(cudaGetLastError()); - struct HostSplit { - DataT quesval; - IdxT colid; - DataT best_metric_val; - int nLeft; - IdxT split_start; - IdxT split_end; - }; - static_assert(sizeof(HostSplit) == sizeof(DT::Split)); - HostSplit h_split; + DT::Split h_split; RAFT_CUDA_TRY(cudaMemcpyAsync( &h_split, split.data().get(), sizeof(h_split), cudaMemcpyDeviceToHost, handle.get_stream())); handle.sync_stream(); - EXPECT_EQ(h_split.nLeft, 4); + EXPECT_EQ(h_split.global_nLeft, 4); + EXPECT_EQ(h_split.local_nLeft, 4); EXPECT_EQ(h_split.quesval, DataT{3}); EXPECT_EQ(h_split.split_start, 3); EXPECT_EQ(h_split.split_end, 3); From 3a2f2132bd228a601080486aa252fc3a7b2a6921 Mon Sep 17 00:00:00 2001 From: Rory Mitchell Date: Wed, 8 Jul 2026 12:14:06 +0200 Subject: [PATCH 2/6] Make RF split host constructible --- .../decisiontree/batched-levelalgo/split.cuh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cpp/src/decisiontree/batched-levelalgo/split.cuh b/cpp/src/decisiontree/batched-levelalgo/split.cuh index a82a32e426..b4c7b96a08 100644 --- a/cpp/src/decisiontree/batched-levelalgo/split.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/split.cuh @@ -54,7 +54,7 @@ struct Split { /** last quantile index in an inclusive range of training-equivalent splits */ IdxT split_end; - DI Split(DataT quesval, IdxT colid, DataT best_metric_val, std::int64_t nLeft, IdxT bin = -1) + HDI Split(DataT quesval, IdxT colid, DataT best_metric_val, std::int64_t nLeft, IdxT bin = -1) : quesval(quesval), colid(colid), best_metric_val(best_metric_val), @@ -65,12 +65,12 @@ struct Split { split_end = bin; } - DI Split(DataT quesval, - IdxT colid, - DataT best_metric_val, - std::int64_t global_nLeft, - std::int64_t local_nLeft, - IdxT bin) + HDI Split(DataT quesval, + IdxT colid, + DataT best_metric_val, + std::int64_t global_nLeft, + std::int64_t local_nLeft, + IdxT bin) : quesval(quesval), colid(colid), best_metric_val(best_metric_val), @@ -81,7 +81,7 @@ struct Split { split_end = bin; } - DI Split() + HDI Split() { quesval = best_metric_val = Min; colid = -1; @@ -98,7 +98,7 @@ struct Split { * * @return the reference to the copied object (typically useful for chaining) */ - DI SplitT& operator=(const SplitT& other) + HDI SplitT& operator=(const SplitT& other) { quesval = other.quesval; colid = other.colid; From 51a99493462996fb850f829ba19b933e4112a557 Mon Sep 17 00:00:00 2001 From: Rory Mitchell Date: Wed, 8 Jul 2026 13:05:00 +0200 Subject: [PATCH 3/6] Make RF split counts explicit --- cpp/src/decisiontree/batched-levelalgo/objectives.cuh | 4 ++-- cpp/src/decisiontree/batched-levelalgo/split.cuh | 11 ----------- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/cpp/src/decisiontree/batched-levelalgo/objectives.cuh b/cpp/src/decisiontree/batched-levelalgo/objectives.cuh index 79c93da2a6..5edb72423f 100644 --- a/cpp/src/decisiontree/batched-levelalgo/objectives.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/objectives.cuh @@ -165,7 +165,7 @@ class ClassificationObjectiveFunction { nRight >= static_cast(min_samples_leaf)) { gain = GainPerSplit(shist, i, n_bins, len, nLeft, nRight); } - sp.update({squantiles[i], col, gain, nLeft, i}); + sp.update({squantiles[i], col, gain, nLeft, nLeft, i}); } return sp; } @@ -361,7 +361,7 @@ class RegressionObjectiveFunction { nRight >= static_cast(min_samples_leaf)) { gain = GainPerSplit(shist, i, n_bins, len, nLeft, nRight); } - sp.update({squantiles[i], col, gain, nLeft, i}); + sp.update({squantiles[i], col, gain, nLeft, nLeft, i}); } return sp; } diff --git a/cpp/src/decisiontree/batched-levelalgo/split.cuh b/cpp/src/decisiontree/batched-levelalgo/split.cuh index b4c7b96a08..1b6de0677d 100644 --- a/cpp/src/decisiontree/batched-levelalgo/split.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/split.cuh @@ -54,17 +54,6 @@ struct Split { /** last quantile index in an inclusive range of training-equivalent splits */ IdxT split_end; - HDI Split(DataT quesval, IdxT colid, DataT best_metric_val, std::int64_t nLeft, IdxT bin = -1) - : quesval(quesval), - colid(colid), - best_metric_val(best_metric_val), - global_nLeft(nLeft), - local_nLeft(nLeft) - { - split_start = bin; - split_end = bin; - } - HDI Split(DataT quesval, IdxT colid, DataT best_metric_val, From 98fc016dc3042477037426a6459e1d7b4a503eb9 Mon Sep 17 00:00:00 2001 From: Rory Mitchell Date: Wed, 8 Jul 2026 22:36:47 +0200 Subject: [PATCH 4/6] Address RF split count review comments --- .../batched-levelalgo/builder.cuh | 24 +++++++++------ .../decisiontree/batched-levelalgo/split.cuh | 29 ++++--------------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/cpp/src/decisiontree/batched-levelalgo/builder.cuh b/cpp/src/decisiontree/batched-levelalgo/builder.cuh index ed438afe0a..b283228709 100644 --- a/cpp/src/decisiontree/batched-levelalgo/builder.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/builder.cuh @@ -100,17 +100,26 @@ class NodeQueue { if (params.max_leaves != -1 && tree->leaf_counter >= params.max_leaves) break; + using NodeCountT = decltype(std::declval().InstanceCount()); + auto const parent_node_count = ML::narrow_cast(parent_range.count); + auto const global_left_count = ML::narrow_cast(split.global_nLeft); + auto const local_left_count = ML::narrow_cast(split.local_nLeft); + auto const global_right_count = + ML::checked_sub(parent_node_count, global_left_count); + auto const local_right_count = + ML::checked_sub(parent_range.count, local_left_count); + auto const right_begin = ML::checked_add(parent_range.begin, local_left_count); + // parent tree->sparsetree.at(item.idx) = NodeT::CreateSplitNode(split.colid, split.quesval, split.best_metric_val, int64_t(tree->sparsetree.size()), - parent_range.count); + parent_node_count); tree->leaf_counter++; // left - tree->sparsetree.emplace_back(NodeT::CreateLeafNode(split.global_nLeft)); - node_instances_.emplace_back( - InstanceRange{parent_range.begin, std::size_t(split.local_nLeft)}); + tree->sparsetree.emplace_back(NodeT::CreateLeafNode(global_left_count)); + node_instances_.emplace_back(InstanceRange{parent_range.begin, local_left_count}); // Do not add a work item if this child is definitely a leaf if (this->IsExpandable(tree->sparsetree.back(), item.depth + 1)) { @@ -119,11 +128,8 @@ class NodeQueue { } // right - tree->sparsetree.emplace_back( - NodeT::CreateLeafNode(tree->sparsetree.at(item.idx).InstanceCount() - split.global_nLeft)); - node_instances_.emplace_back( - InstanceRange{parent_range.begin + std::size_t(split.local_nLeft), - parent_range.count - std::size_t(split.local_nLeft)}); + tree->sparsetree.emplace_back(NodeT::CreateLeafNode(global_right_count)); + node_instances_.emplace_back(InstanceRange{right_begin, local_right_count}); // Do not add a work item if this child is definitely a leaf if (this->IsExpandable(tree->sparsetree.back(), item.depth + 1)) { diff --git a/cpp/src/decisiontree/batched-levelalgo/split.cuh b/cpp/src/decisiontree/batched-levelalgo/split.cuh index 1b6de0677d..2f61d4de5c 100644 --- a/cpp/src/decisiontree/batched-levelalgo/split.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/split.cuh @@ -80,24 +80,7 @@ struct Split { split_end = -1; } - /** - * @brief Assignment operator overload - * - * @param[in] other source object from where to copy - * - * @return the reference to the copied object (typically useful for chaining) - */ - HDI SplitT& operator=(const SplitT& other) - { - quesval = other.quesval; - colid = other.colid; - best_metric_val = other.best_metric_val; - global_nLeft = other.global_nLeft; - local_nLeft = other.local_nLeft; - split_start = other.split_start; - split_end = other.split_end; - return *this; - } + HDI SplitT& operator=(const SplitT& other) = default; DI bool has_valid_split_range() const { @@ -265,15 +248,15 @@ void printSplits(Split* splits, IdxT len, cudaStream_t s) { auto op = [] __device__(Split * ptr, IdxT idx) { printf( - "quesval = %e, colid = %d, best_metric_val = %e, global_nLeft = %lld, " - "local_nLeft = %lld, split_range = [%d, %d]\n", + "quesval = %e, colid = %lld, best_metric_val = %e, global_nLeft = %lld, " + "local_nLeft = %lld, split_range = [%lld, %lld]\n", ptr->quesval, - ptr->colid, + static_cast(ptr->colid), ptr->best_metric_val, static_cast(ptr->global_nLeft), static_cast(ptr->local_nLeft), - ptr->split_start, - ptr->split_end); + static_cast(ptr->split_start), + static_cast(ptr->split_end)); }; raft::linalg::writeOnlyUnaryOp, decltype(op), IdxT, TPB>(splits, len, op, s); RAFT_CUDA_TRY(cudaDeviceSynchronize()); From da3a3520b2ac214a252b836ba1eea8aec6807137 Mon Sep 17 00:00:00 2001 From: Rory Mitchell Date: Wed, 8 Jul 2026 22:50:00 +0200 Subject: [PATCH 5/6] Inline RF split count checks --- .../batched-levelalgo/builder.cuh | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/cpp/src/decisiontree/batched-levelalgo/builder.cuh b/cpp/src/decisiontree/batched-levelalgo/builder.cuh index b283228709..6e68c1e460 100644 --- a/cpp/src/decisiontree/batched-levelalgo/builder.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/builder.cuh @@ -100,25 +100,20 @@ class NodeQueue { if (params.max_leaves != -1 && tree->leaf_counter >= params.max_leaves) break; - using NodeCountT = decltype(std::declval().InstanceCount()); - auto const parent_node_count = ML::narrow_cast(parent_range.count); - auto const global_left_count = ML::narrow_cast(split.global_nLeft); - auto const local_left_count = ML::narrow_cast(split.local_nLeft); - auto const global_right_count = - ML::checked_sub(parent_node_count, global_left_count); - auto const local_right_count = - ML::checked_sub(parent_range.count, local_left_count); - auto const right_begin = ML::checked_add(parent_range.begin, local_left_count); + using NodeCountT = decltype(std::declval().InstanceCount()); + auto const local_left_count = ML::narrow_cast(split.local_nLeft); // parent - tree->sparsetree.at(item.idx) = NodeT::CreateSplitNode(split.colid, - split.quesval, - split.best_metric_val, - int64_t(tree->sparsetree.size()), - parent_node_count); + tree->sparsetree.at(item.idx) = + NodeT::CreateSplitNode(split.colid, + split.quesval, + split.best_metric_val, + int64_t(tree->sparsetree.size()), + ML::narrow_cast(parent_range.count)); tree->leaf_counter++; // left - tree->sparsetree.emplace_back(NodeT::CreateLeafNode(global_left_count)); + tree->sparsetree.emplace_back( + NodeT::CreateLeafNode(ML::narrow_cast(split.global_nLeft))); node_instances_.emplace_back(InstanceRange{parent_range.begin, local_left_count}); // Do not add a work item if this child is definitely a leaf @@ -128,8 +123,12 @@ class NodeQueue { } // right - tree->sparsetree.emplace_back(NodeT::CreateLeafNode(global_right_count)); - node_instances_.emplace_back(InstanceRange{right_begin, local_right_count}); + tree->sparsetree.emplace_back(NodeT::CreateLeafNode( + ML::checked_sub(tree->sparsetree.at(item.idx).InstanceCount(), + ML::narrow_cast(split.global_nLeft)))); + node_instances_.emplace_back( + InstanceRange{ML::checked_add(parent_range.begin, local_left_count), + ML::checked_sub(parent_range.count, local_left_count)}); // Do not add a work item if this child is definitely a leaf if (this->IsExpandable(tree->sparsetree.back(), item.depth + 1)) { From 3b2fdecae103a24d55e0ad2d5102a5fb508fab51 Mon Sep 17 00:00:00 2001 From: Rory Mitchell Date: Fri, 10 Jul 2026 16:12:43 +0200 Subject: [PATCH 6/6] Fix RF split assignment for nvcc --- cpp/src/decisiontree/batched-levelalgo/split.cuh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cpp/src/decisiontree/batched-levelalgo/split.cuh b/cpp/src/decisiontree/batched-levelalgo/split.cuh index 2f61d4de5c..75db31e1b2 100644 --- a/cpp/src/decisiontree/batched-levelalgo/split.cuh +++ b/cpp/src/decisiontree/batched-levelalgo/split.cuh @@ -80,7 +80,17 @@ struct Split { split_end = -1; } - HDI SplitT& operator=(const SplitT& other) = default; + HDI SplitT& operator=(const SplitT& other) + { + quesval = other.quesval; + colid = other.colid; + best_metric_val = other.best_metric_val; + global_nLeft = other.global_nLeft; + local_nLeft = other.local_nLeft; + split_start = other.split_start; + split_end = other.split_end; + return *this; + } DI bool has_valid_split_range() const {