Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
47 changes: 20 additions & 27 deletions cpp/src/decisiontree/batched-levelalgo/builder.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <cub/cub.cuh>

#include <algorithm>
#include <cstdint>
#include <deque>
#include <memory>
#include <utility>
Expand Down Expand Up @@ -99,16 +100,21 @@ class NodeQueue {

if (params.max_leaves != -1 && tree->leaf_counter >= params.max_leaves) break;

using NodeCountT = decltype(std::declval<NodeT>().InstanceCount());
auto const local_left_count = ML::narrow_cast<std::size_t>(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_range.count);
tree->sparsetree.at(item.idx) =
NodeT::CreateSplitNode(split.colid,
split.quesval,
split.best_metric_val,
int64_t(tree->sparsetree.size()),
ML::narrow_cast<NodeCountT>(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(ML::narrow_cast<NodeCountT>(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
if (this->IsExpandable(tree->sparsetree.back(), item.depth + 1)) {
Expand All @@ -117,9 +123,12 @@ class NodeQueue {
}

// right
tree->sparsetree.emplace_back(NodeT::CreateLeafNode(parent_range.count - split.nLeft));
tree->sparsetree.emplace_back(NodeT::CreateLeafNode(
ML::checked_sub<NodeCountT>(tree->sparsetree.at(item.idx).InstanceCount(),
ML::narrow_cast<NodeCountT>(split.global_nLeft))));
node_instances_.emplace_back(
InstanceRange{parent_range.begin + split.nLeft, parent_range.count - split.nLeft});
InstanceRange{ML::checked_add<std::size_t>(parent_range.begin, local_left_count),
ML::checked_sub<std::size_t>(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)) {
Expand Down Expand Up @@ -401,21 +410,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<HostSplit> final_splits(work_items.size());
std::vector<SplitT> 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<NodeWorkItem> active_items(work_items);
Expand All @@ -437,12 +435,7 @@ struct Builder {
std::vector<std::size_t> 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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <thrust/for_each.h>
#include <thrust/iterator/counting_iterator.h>

#include <cstdint>

namespace ML {
namespace DT {

Expand Down Expand Up @@ -51,8 +53,10 @@ struct WorkloadInfo {
template <typename SplitT, typename IdxT>
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<std::int64_t>(num_rows);
const auto min_leaf = static_cast<std::int64_t>(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 <typename SplitT, typename DataT, typename IdxT>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,23 @@
#include <thrust/iterator/transform_iterator.h>
#include <thrust/scan.h>

#include <cstdint>
#include <cstdio>

namespace ML {
namespace DT {

static constexpr int TPB_DEFAULT = 128;

template <typename IdxT>
struct NodeSplitPartitionState {
IdxT left_count;
std::int64_t left_count;
bool valid_row;
bool goes_left;
};

template <typename IdxT>
struct NodeSplitPartitionScanOp {
__host__ __device__ NodeSplitPartitionState<IdxT> operator()(
const NodeSplitPartitionState<IdxT>& lhs, const NodeSplitPartitionState<IdxT>& 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};
}
Expand All @@ -57,8 +56,7 @@ struct NodeSplitPartitionWriter {
const WorkloadInfo<IdxT>* workload_info;
IdxT* partition_row_ids;

__host__ __device__ void operator()(std::ptrdiff_t index,
NodeSplitPartitionState<IdxT> state) const
__host__ __device__ void operator()(std::ptrdiff_t index, NodeSplitPartitionState state) const
{
if (!state.valid_row) { return; }

Expand All @@ -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;
}
};

Expand Down Expand Up @@ -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>{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>{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<IdxT>{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.
Expand All @@ -163,7 +162,7 @@ void launchNodeSplitKernel(const IdxT min_samples_leaf,
partition_states,
partition_writer,
thrust::equal_to<IdxT>{},
NodeSplitPartitionScanOp<IdxT>{});
NodeSplitPartitionScanOp{});

// The original row_ids buffer remains the source during the scan, so copy back after it finishes.
nodeSplitCopyBackKernel<DataT, LabelT, IdxT, TPB>
Expand Down
55 changes: 37 additions & 18 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 <cstdint>
#include <limits>
#include <type_traits>

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<std::int64_t>(min_samples_leaf) ||
nRight < static_cast<std::int64_t>(min_samples_leaf))
return -std::numeric_limits<DataT>::max();

switch (criterion) {
Expand Down Expand Up @@ -146,17 +154,18 @@ class ClassificationObjectiveFunction {
}

DI Split<DataT, IdxT> 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<DataT, IdxT> 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<DataT>::max();
if (nLeft >= min_samples_leaf && nRight >= min_samples_leaf) {
if (nLeft >= static_cast<std::int64_t>(min_samples_leaf) &&
nRight >= static_cast<std::int64_t>(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;
}
Expand Down Expand Up @@ -194,7 +203,8 @@ class RegressionObjectiveFunction {
CRITERION criterion;
static constexpr auto eps_ = 10 * std::numeric_limits<DataT>::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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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<std::int64_t>(min_samples_leaf) ||
nRight < static_cast<std::int64_t>(min_samples_leaf))
return -std::numeric_limits<DataT>::max();

switch (criterion) {
Expand Down Expand Up @@ -332,17 +350,18 @@ class RegressionObjectiveFunction {
}

DI Split<DataT, IdxT> 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<DataT, IdxT> 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<DataT>::max();
if (nLeft >= min_samples_leaf && nRight >= min_samples_leaf) {
if (nLeft >= static_cast<std::int64_t>(min_samples_leaf) &&
nRight >= static_cast<std::int64_t>(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;
}
Expand Down
Loading
Loading