Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[RF] Fix handling of min_samples_leaf and min_samples_split param…
…eters
  • Loading branch information
chyunsu3 committed Oct 29, 2026
commit 35dbc31d9b8dc5f70d6bf559d1af60998e1f90d4
32 changes: 27 additions & 5 deletions cpp/src/decisiontree/batched-levelalgo/builder.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ struct Builder {
std::int64_t* column_samples;
/** temporary row IDs for row-wise out-of-place partitioning */
std::int64_t* partition_row_ids;
/** temporary bootstrap counts partitioned alongside row IDs */
std::uint32_t* partition_bootstrap_count;
/** rmm device workspace buffer */
rmm::device_uvector<char> d_buff;
/** pinned host buffer to store the trained nodes */
Expand All @@ -222,6 +224,7 @@ struct Builder {
const DataT* data,
const LabelT* labels,
const double* sample_weight,
std::uint32_t* bootstrap_count,
std::int64_t n_rows,
std::int64_t n_cols,
rmm::device_uvector<std::int64_t>* row_ids,
Expand All @@ -236,6 +239,7 @@ struct Builder {
dataset{data,
labels,
sample_weight,
bootstrap_count,
n_rows,
n_cols,
row_major ? n_cols : std::int64_t{1},
Expand Down Expand Up @@ -317,6 +321,10 @@ struct Builder {
ML::checked_mul<std::size_t>(sizeof(std::int64_t), max_batch, dataset.n_sampled_cols);
auto partition_row_ids_bytes =
ML::checked_mul<std::size_t>(sizeof(std::int64_t), dataset.n_sampled_rows);
auto partition_bootstrap_count_bytes =
dataset.bootstrap_count == nullptr
? std::size_t{0}
: ML::checked_mul<std::size_t>(sizeof(std::uint32_t), dataset.n_sampled_rows);

d_wsize += calculateAlignedBytes(sizeof(std::int64_t)); // n_nodes
d_wsize += calculateAlignedBytes(histograms_bytes); // histograms
Expand All @@ -326,6 +334,8 @@ struct Builder {
d_wsize += calculateAlignedBytes(workload_info_bytes); // workload_info
d_wsize += calculateAlignedBytes(column_samples_bytes); // column_samples
d_wsize += calculateAlignedBytes(partition_row_ids_bytes); // partition row IDs
d_wsize +=
calculateAlignedBytes(partition_bootstrap_count_bytes); // partition bootstrap counts
d_wsize += packedHistogramWorkspaceSize(max_len_histograms);

// all nodes in the tree
Expand Down Expand Up @@ -358,6 +368,10 @@ struct Builder {
ML::checked_mul<std::size_t>(sizeof(std::int64_t), max_batch, dataset.n_sampled_cols);
auto partition_row_ids_bytes =
ML::checked_mul<std::size_t>(sizeof(std::int64_t), dataset.n_sampled_rows);
auto partition_bootstrap_count_bytes =
dataset.bootstrap_count == nullptr
? std::size_t{0}
: ML::checked_mul<std::size_t>(sizeof(std::uint32_t), dataset.n_sampled_rows);
// device
n_nodes = reinterpret_cast<std::int64_t*>(d_wspace);
d_wspace += calculateAlignedBytes(sizeof(std::int64_t));
Expand All @@ -375,6 +389,9 @@ struct Builder {
d_wspace += calculateAlignedBytes(column_samples_bytes);
partition_row_ids = reinterpret_cast<std::int64_t*>(d_wspace);
d_wspace += calculateAlignedBytes(partition_row_ids_bytes);
partition_bootstrap_count =
dataset.bootstrap_count == nullptr ? nullptr : reinterpret_cast<std::uint32_t*>(d_wspace);
d_wspace += calculateAlignedBytes(partition_bootstrap_count_bytes);
packed_histograms = reinterpret_cast<void*>(d_wspace);
d_wspace += packedHistogramWorkspaceSize(max_len_histograms);

Expand All @@ -400,12 +417,16 @@ struct Builder {
// Total training weight across all ranks for this tree.
auto root_weight = static_cast<double>(global_sampled_rows);
if constexpr (ObjectiveT::weighted) {
auto weights = dataset.sample_weight;
auto local_weight = thrust::transform_reduce(
auto row_positions = thrust::make_counting_iterator<std::int64_t>(0);
auto dataset_copy = dataset;
auto local_weight = thrust::transform_reduce(
rmm::exec_policy(builder_stream),
dataset.row_ids,
dataset.row_ids + dataset.n_sampled_rows,
[weights] __device__(std::int64_t row) -> double { return weights[row]; },
row_positions,
row_positions + dataset.n_sampled_rows,
[dataset_copy] __device__(std::int64_t row_position) -> double {
auto row = dataset_copy.row_ids[row_position];
return dataset_copy.weight(row_position, row);
},
0.0,
cuda::std::plus{});
root_weight = globalSum(local_weight);
Expand Down Expand Up @@ -529,6 +550,7 @@ struct Builder {
n_partition_blocks,
work_items.size(),
partition_row_ids,
partition_bootstrap_count,
builder_stream);
RAFT_CUDA_TRY(cudaPeekAtLastError());
raft::common::nvtx::pop_range();
Expand Down
9 changes: 9 additions & 0 deletions cpp/src/decisiontree/batched-levelalgo/dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <raft/util/cuda_utils.cuh>

#include <cstddef>
#include <cstdint>

namespace ML {
Expand All @@ -20,6 +21,8 @@ struct Dataset {
const LabelT* labels;
/** optional input sample weights */
const double* sample_weight;
/** optional bootstrap multiplicities, aligned with row_ids */
std::uint32_t* bootstrap_count;
/** total rows in dataset */
std::int64_t n_rows;
/** total cols in dataset */
Expand All @@ -41,6 +44,12 @@ struct Dataset {
{
return data[row * row_stride + col * col_stride];
}

HDI double weight(std::size_t row_position, std::int64_t row) const
{
if (bootstrap_count != nullptr) { return static_cast<double>(bootstrap_count[row_position]); }
return sample_weight == nullptr ? 1.0 : sample_weight[row];
}
};

} // namespace DT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void launchNodeSplitKernel(const Dataset<DataT, LabelT>& dataset,
size_t n_blocks_dimx,
size_t n_work_items,
std::int64_t* partition_row_ids,
std::uint32_t* partition_bootstrap_count,
cudaStream_t builder_stream);

template <typename DatasetT, typename NodeT, typename ObjectiveT>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ struct NodeSplitPartitionWriter {
const Split<DataT>* splits;
const WorkloadInfo* workload_info;
std::int64_t* partition_row_ids;
std::uint32_t* partition_bootstrap_count;

__host__ __device__ void operator()(std::ptrdiff_t index, NodeSplitPartitionState state) const
{
Expand All @@ -117,6 +118,9 @@ struct NodeSplitPartitionWriter {
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;
if (partition_bootstrap_count != nullptr) {
partition_bootstrap_count[out_idx] = dataset.bootstrap_count[range_start + range_pos];
}
}
};

Expand All @@ -127,7 +131,8 @@ CUML_KERNEL void nodeSplitCopyBackKernel(const Dataset<DataT, LabelT> dataset,
const NodeWorkItem* work_items,
const Split<DataT>* splits,
const WorkloadInfo* workload_info,
const std::int64_t* partition_row_ids)
const std::int64_t* partition_row_ids,
const std::uint32_t* partition_bootstrap_count)
{
const auto workload_info_cta = workload_info[blockIdx.x];
const auto nid = workload_info_cta.nodeid;
Expand All @@ -141,6 +146,9 @@ CUML_KERNEL void nodeSplitCopyBackKernel(const Dataset<DataT, LabelT> dataset,
if (range_pos < range_len) {
const auto idx = range_start + range_pos;
dataset.row_ids[idx] = partition_row_ids[idx];
if (partition_bootstrap_count != nullptr) {
dataset.bootstrap_count[idx] = partition_bootstrap_count[idx];
}
}
}

Expand All @@ -152,6 +160,7 @@ void launchNodeSplitKernel(const Dataset<DataT, LabelT>& dataset,
size_t n_blocks_dimx,
size_t n_work_items,
std::int64_t* partition_row_ids,
std::uint32_t* partition_bootstrap_count,
cudaStream_t builder_stream)
{
if (n_blocks_dimx == 0) return;
Expand Down Expand Up @@ -200,7 +209,7 @@ void launchNodeSplitKernel(const Dataset<DataT, LabelT>& dataset,
auto partition_states = thrust::make_transform_iterator(slots_begin, partition_state);
auto partition_writer =
thrust::make_tabulate_output_iterator(NodeSplitPartitionWriter<DataT, LabelT, TPB>{
dataset, work_items, splits, workload_info, partition_row_ids});
dataset, work_items, splits, workload_info, partition_row_ids, partition_bootstrap_count});
thrust::inclusive_scan_by_key(exec_policy,
node_keys,
node_keys + n_slots,
Expand All @@ -211,7 +220,7 @@ void launchNodeSplitKernel(const Dataset<DataT, LabelT>& dataset,

// The original row_ids buffer remains the source during the scan, so copy back after it finishes.
nodeSplitCopyBackKernel<DataT, LabelT, TPB><<<n_blocks_dimx, TPB, 0, builder_stream>>>(
dataset, work_items, splits, workload_info, partition_row_ids);
dataset, work_items, splits, workload_info, partition_row_ids, partition_bootstrap_count);
}

template <typename DatasetT, typename NodeT, typename ObjectiveT>
Expand All @@ -236,7 +245,7 @@ CUML_KERNEL void buildLeafHistogramsKernel(ObjectiveT objective,
for (auto i = range.begin + tid; i < range.begin + range.count; i += blockDim.x) {
auto row = dataset.row_ids[i];
auto label = dataset.labels[row];
objective.IncrementHistogram(histogram, 1, 0, label, dataset, row);
objective.IncrementHistogram(histogram, 1, 0, label, dataset, row, i);
}
__syncthreads();
for (int i = tid; i < dataset.num_outputs; i += blockDim.x) {
Expand Down Expand Up @@ -374,7 +383,7 @@ CUML_KERNEL void buildHistogramsKernel(typename ObjectiveT::BinT* histograms,
return quantiles_for_split[bin] < value;
});
auto bin = bin_it == bin_end ? n_bins - 1 : *bin_it;
objective.IncrementHistogram(histogram, n_bins, bin, label, dataset, row);
objective.IncrementHistogram(histogram, n_bins, bin, label, dataset, row, i);
}

if (!use_global_memory_histogram) {
Expand Down
55 changes: 31 additions & 24 deletions cpp/src/decisiontree/batched-levelalgo/kernels/node-split.cu
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,40 @@ namespace ML {
namespace DT {

// Explicit instantiations are split across separate .cu files to increase compilation parallelism.
template void launchNodeSplitKernel<float, int, TPB_DEFAULT>(const Dataset<float, int>& dataset,
const NodeWorkItem* work_items,
Split<float>* splits,
const WorkloadInfo* workload_info,
size_t n_blocks_dimx,
size_t n_work_items,
std::int64_t* partition_row_ids,
cudaStream_t builder_stream);
template void launchNodeSplitKernel<float, int, TPB_DEFAULT>(
const Dataset<float, int>& dataset,
const NodeWorkItem* work_items,
Split<float>* splits,
const WorkloadInfo* workload_info,
size_t n_blocks_dimx,
size_t n_work_items,
std::int64_t* partition_row_ids,
std::uint32_t* partition_bootstrap_count,
cudaStream_t builder_stream);

// Explicit instantiations are split across separate .cu files to increase compilation parallelism.
template void launchNodeSplitKernel<double, int, TPB_DEFAULT>(const Dataset<double, int>& dataset,
const NodeWorkItem* work_items,
Split<double>* splits,
const WorkloadInfo* workload_info,
size_t n_blocks_dimx,
size_t n_work_items,
std::int64_t* partition_row_ids,
cudaStream_t builder_stream);
template void launchNodeSplitKernel<double, int, TPB_DEFAULT>(
const Dataset<double, int>& dataset,
const NodeWorkItem* work_items,
Split<double>* splits,
const WorkloadInfo* workload_info,
size_t n_blocks_dimx,
size_t n_work_items,
std::int64_t* partition_row_ids,
std::uint32_t* partition_bootstrap_count,
cudaStream_t builder_stream);

// Explicit instantiations are split across separate .cu files to increase compilation parallelism.
template void launchNodeSplitKernel<float, float, TPB_DEFAULT>(const Dataset<float, float>& dataset,
const NodeWorkItem* work_items,
Split<float>* splits,
const WorkloadInfo* workload_info,
size_t n_blocks_dimx,
size_t n_work_items,
std::int64_t* partition_row_ids,
cudaStream_t builder_stream);
template void launchNodeSplitKernel<float, float, TPB_DEFAULT>(
const Dataset<float, float>& dataset,
const NodeWorkItem* work_items,
Split<float>* splits,
const WorkloadInfo* workload_info,
size_t n_blocks_dimx,
size_t n_work_items,
std::int64_t* partition_row_ids,
std::uint32_t* partition_bootstrap_count,
cudaStream_t builder_stream);

// Explicit instantiations are split across separate .cu files to increase compilation parallelism.
template void launchNodeSplitKernel<double, double, TPB_DEFAULT>(
Expand All @@ -47,6 +53,7 @@ template void launchNodeSplitKernel<double, double, TPB_DEFAULT>(
size_t n_blocks_dimx,
size_t n_work_items,
std::int64_t* partition_row_ids,
std::uint32_t* partition_bootstrap_count,
cudaStream_t builder_stream);

} // namespace DT
Expand Down
15 changes: 7 additions & 8 deletions cpp/src/decisiontree/batched-levelalgo/objectives.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <cuml/tree/algo_helper.h>

#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
Expand Down Expand Up @@ -165,12 +166,11 @@ class ClassificationObjectiveFunction {
int bin,
LabelT label,
const DatasetT& dataset,
std::int64_t row) const
std::int64_t row,
std::size_t row_position) const
{
double weight = 1.0;
if constexpr (weighted) {
weight = dataset.sample_weight == nullptr ? 1.0 : double(dataset.sample_weight[row]);
}
if constexpr (weighted) { weight = dataset.weight(row_position, row); }
BinT::IncrementHistogram(histogram, n_bins, bin, label, weight);
}

Expand Down Expand Up @@ -395,12 +395,11 @@ class RegressionObjectiveFunction {
int bin,
LabelT label,
const DatasetT& dataset,
std::int64_t row) const
std::int64_t row,
std::size_t row_position) const
{
double weight = 1.0;
if constexpr (weighted) {
weight = dataset.sample_weight == nullptr ? 1.0 : double(dataset.sample_weight[row]);
}
if constexpr (weighted) { weight = dataset.weight(row_position, row); }
BinT::IncrementHistogram(histogram, n_bins, bin, label, weight);
}

Expand Down
13 changes: 9 additions & 4 deletions cpp/src/decisiontree/decisiontree.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ class DecisionTree {
uint64_t seed,
const Quantiles<DataT>& quantiles,
std::int64_t treeid,
const double* sample_weight = nullptr,
bool row_major = false)
const double* sample_weight = nullptr,
bool row_major = false,
std::uint32_t* bootstrap_count = nullptr)
{
if (params.split_criterion ==
CRITERION::CRITERION_END) { // Set default to GINI (classification) or MSE (regression)
Expand All @@ -253,7 +254,7 @@ class DecisionTree {
// Dispatch objective family. The objective object switches on the criterion at runtime.
if (not std::is_same<DataT, LabelT>::value and (params.split_criterion == CRITERION::GINI ||
params.split_criterion == CRITERION::ENTROPY)) {
if (sample_weight != nullptr) {
if (sample_weight != nullptr || bootstrap_count != nullptr) {
return Builder<ClassificationObjectiveFunction<DataT, LabelT, true>>(handle,
s,
treeid,
Expand All @@ -262,6 +263,7 @@ class DecisionTree {
data,
labels,
sample_weight,
bootstrap_count,
nrows,
ncols,
row_ids,
Expand All @@ -278,6 +280,7 @@ class DecisionTree {
data,
labels,
sample_weight,
bootstrap_count,
nrows,
ncols,
row_ids,
Expand All @@ -290,7 +293,7 @@ class DecisionTree {
params.split_criterion == CRITERION::POISSON ||
params.split_criterion == CRITERION::GAMMA ||
params.split_criterion == CRITERION::INVERSE_GAUSSIAN)) {
if (sample_weight != nullptr) {
if (sample_weight != nullptr || bootstrap_count != nullptr) {
return Builder<RegressionObjectiveFunction<DataT, LabelT, true>>(handle,
s,
treeid,
Expand All @@ -299,6 +302,7 @@ class DecisionTree {
data,
labels,
sample_weight,
bootstrap_count,
nrows,
ncols,
row_ids,
Expand All @@ -315,6 +319,7 @@ class DecisionTree {
data,
labels,
sample_weight,
bootstrap_count,
nrows,
ncols,
row_ids,
Expand Down
Loading