Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -248,6 +248,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 @@ -263,6 +265,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 @@ -277,6 +280,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 @@ -358,6 +362,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(histograms_bytes); // histograms
d_wsize += calculateAlignedBytes(mutex_bytes); // mutex
Expand All @@ -366,6 +374,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 @@ -399,6 +409,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
histograms = reinterpret_cast<BinT*>(d_wspace);
d_wspace += calculateAlignedBytes(histograms_bytes);
Expand All @@ -414,6 +428,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 Down Expand Up @@ -441,12 +458,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 @@ -590,6 +611,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 @@ -106,6 +106,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 @@ -99,6 +99,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 @@ -119,6 +120,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 @@ -129,7 +133,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 @@ -143,6 +148,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 @@ -154,6 +162,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 @@ -202,7 +211,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 @@ -213,7 +222,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 <bool kSingleTile, typename DatasetT, typename NodeT, typename ObjectiveT>
Expand Down Expand Up @@ -243,7 +252,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];
objective.template IncrementLeafHistogram<kSingleTile>(
histogram, output_begin, output_count, dataset, row);
histogram, output_begin, output_count, dataset, row, i);
}
__syncthreads();
for (int i = tid; i < output_count; i += blockDim.x) {
Expand Down Expand Up @@ -403,7 +412,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 @@ -10,34 +10,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 @@ -48,6 +54,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
32 changes: 18 additions & 14 deletions cpp/src/decisiontree/batched-levelalgo/objectives.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <cuml/tree/algo_helper.h>

#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
Expand Down Expand Up @@ -167,12 +168,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 All @@ -181,13 +181,14 @@ class ClassificationObjectiveFunction {
int output_begin,
int output_count,
const DatasetT& dataset,
std::int64_t row) const
std::int64_t row,
std::size_t row_position) const
{
auto label = dataset.labels[row];
if constexpr (single_tile) {
IncrementHistogram(histogram, 1, 0, label, dataset, row);
IncrementHistogram(histogram, 1, 0, label, dataset, row, row_position);
} else if (label >= output_begin && label - output_begin < output_count) {
IncrementHistogram(histogram, 1, 0, label - output_begin, dataset, row);
IncrementHistogram(histogram, 1, 0, label - output_begin, dataset, row, row_position);
}
}

Expand Down Expand Up @@ -414,22 +415,25 @@ 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);
}

template <bool single_tile, typename DatasetT>
DI void IncrementLeafHistogram(
BinT* histogram, int output_begin, int, const DatasetT& dataset, std::int64_t row) const
DI void IncrementLeafHistogram(BinT* histogram,
int output_begin,
int,
const DatasetT& dataset,
std::int64_t row,
std::size_t row_position) const
{
// Every row contributes its label to the leaf's regression statistics.
auto label = dataset.labels[row + std::int64_t(output_begin) * dataset.n_rows];
IncrementHistogram(histogram, 1, 0, label, dataset, row);
IncrementHistogram(histogram, 1, 0, label, dataset, row, row_position);
}

DI Split<DataT> Gain(BinT const* shist,
Expand Down
Loading
Loading