35 virtual char const*
what() const noexcept {
return msg_; }
44 template <
typename decision_forest_t>
47 using node_type =
typename decision_forest_t::node_type;
50 template <
typename iter_t>
54 std::optional<int> tl_node_id = std::nullopt,
55 std::size_t depth = std::size_t{1},
56 bool default_to_distant_child =
false,
57 typename node_type::metadata_storage_type feature =
typename node_type::metadata_storage_type{},
58 typename node_type::offset_type offset =
typename node_type::offset_type{})
62 auto set_storage = &node_value;
63 auto max_node_categories =
64 (vec_begin != vec_end) ? *std::max_element(vec_begin, vec_end) + 1 : 1;
65 if (max_num_categories_ > bin_width) {
67 node_value = categorical_storage_.size();
69 categorical_storage_.push_back(max_node_categories);
70 categorical_storage_.resize(categorical_storage_.size() + bins_required);
71 set_storage = &(categorical_storage_[node_value + 1]);
73 auto set = bitset{set_storage, max_node_categories};
74 std::for_each(vec_begin, vec_end, [&set](
auto&& cat_index) { set.set(cat_index); });
77 node_value, tl_node_id, depth,
false, default_to_distant_child,
true, feature, offset,
false);
81 template <
typename iter_t>
84 std::optional<int> tl_node_id = std::nullopt,
85 std::size_t depth = std::size_t{1})
88 std::copy(vec_begin, vec_end, std::back_inserter(vector_output_));
96 typename node_type::metadata_storage_type{},
97 typename node_type::offset_type{},
102 template <
typename value_t>
105 std::optional<int> tl_node_id = std::nullopt,
106 std::size_t depth = std::size_t{1},
107 bool is_leaf_node =
true,
108 bool default_to_distant_child =
false,
109 bool is_categorical_node =
false,
110 typename node_type::metadata_storage_type feature =
typename node_type::metadata_storage_type{},
111 typename node_type::offset_type offset =
typename node_type::offset_type{},
112 bool is_inclusive =
false)
114 if (depth == std::size_t{}) {
116 if (cur_node_index_ % alignment_ !=
index_type{}) {
117 auto padding = (alignment_ - cur_node_index_ % alignment_);
118 for (
auto i =
index_type{}; i < padding; ++i) {
119 add_node(
typename node_type::threshold_type{}, std::nullopt);
123 root_node_indexes_.push_back(cur_node_index_);
126 if (is_inclusive) { val = std::nextafter(val, std::numeric_limits<value_t>::infinity()); }
128 val, is_leaf_node, default_to_distant_child, is_categorical_node, feature, offset);
130 node_id_mapping_.push_back(
static_cast<index_type>(tl_node_id.value_or(0)));
144 bias_.resize(val.size());
145 std::transform(val.begin(), val.end(), bias_.begin(), [](
double e) {
146 return static_cast<typename node_type::threshold_type>(e);
155 if (output_size_ !=
index_type{1} && output_size_ != val) {
164 max_num_categories_{max_num_categories},
170 postproc_constant_{},
172 root_node_indexes_{},
188 #pragma GCC diagnostic push
189 #pragma GCC diagnostic ignored "-Wnarrowing"
190 return decision_forest_t{
204 max_num_categories_ != 0,
205 vector_output_.empty()
207 : std::make_optional<raft_proto::buffer<typename node_type::threshold_type>>(
212 categorical_storage_.empty()
214 : std::make_optional<raft_proto::buffer<typename node_type::index_type>>(
222 static_cast<typename node_type::threshold_type
>(average_factor_),
223 static_cast<typename node_type::threshold_type
>(postproc_constant_)};
224 #pragma GCC diagnostic pop
234 double average_factor_;
235 double postproc_constant_;
237 std::vector<node_type> nodes_;
238 std::vector<index_type> root_node_indexes_;
239 std::vector<typename node_type::threshold_type> vector_output_;
240 std::vector<typename node_type::threshold_type> bias_;
241 std::vector<typename node_type::index_type> categorical_storage_;
242 std::vector<index_type> node_id_mapping_;
row_op
Definition: postproc_ops.hpp:10
element_op
Definition: postproc_ops.hpp:17
uint32_t index_type
Definition: index_type.hpp:9
void transform(const raft::handle_t &handle, const KMeansParams ¶ms, const float *centroids, const float *X, int n_samples, int n_features, float *X_new)
Transform X to a cluster-distance space.
Definition: dbscan.hpp:18
HOST DEVICE constexpr auto ceildiv(T dividend, U divisor)
Definition: ceildiv.hpp:10
int cuda_stream
Definition: cuda_stream.hpp:14
device_type
Definition: device_type.hpp:7
const_agnostic_same_t< T, U > copy(buffer< T > &dst, buffer< U > const &src, typename buffer< T >::index_type dst_offset, typename buffer< U >::index_type src_offset, typename buffer< T >::index_type size, cuda_stream stream)
Definition: buffer.hpp:316
Definition: decision_forest_builder.hpp:45
void set_average_factor(double val)
Definition: decision_forest_builder.hpp:139
void set_bias(std::vector< double > val)
Definition: decision_forest_builder.hpp:142
void set_postproc_constant(double val)
Definition: decision_forest_builder.hpp:151
decision_forest_builder(index_type max_num_categories=index_type{}, index_type align_bytes=index_type{})
Definition: decision_forest_builder.hpp:161
void set_row_postproc(row_op val)
Definition: decision_forest_builder.hpp:137
void add_node(value_t val, std::optional< int > tl_node_id=std::nullopt, std::size_t depth=std::size_t{1}, bool is_leaf_node=true, bool default_to_distant_child=false, bool is_categorical_node=false, typename node_type::metadata_storage_type feature=typename node_type::metadata_storage_type{}, typename node_type::offset_type offset=typename node_type::offset_type{}, bool is_inclusive=false)
Definition: decision_forest_builder.hpp:103
void add_categorical_node(iter_t vec_begin, iter_t vec_end, std::optional< int > tl_node_id=std::nullopt, std::size_t depth=std::size_t{1}, bool default_to_distant_child=false, typename node_type::metadata_storage_type feature=typename node_type::metadata_storage_type{}, typename node_type::offset_type offset=typename node_type::offset_type{})
Definition: decision_forest_builder.hpp:51
void set_output_size(index_type val)
Definition: decision_forest_builder.hpp:153
auto get_decision_forest(index_type num_feature, index_type num_class, raft_proto::device_type mem_type=raft_proto::device_type::cpu, int device=0, raft_proto::cuda_stream stream=raft_proto::cuda_stream{})
Definition: decision_forest_builder.hpp:179
void set_element_postproc(element_op val)
Definition: decision_forest_builder.hpp:135
void add_leaf_vector_node(iter_t vec_begin, iter_t vec_end, std::optional< int > tl_node_id=std::nullopt, std::size_t depth=std::size_t{1})
Definition: decision_forest_builder.hpp:82
typename decision_forest_t::node_type node_type
Definition: decision_forest_builder.hpp:47
Definition: decision_forest_builder.hpp:32
model_builder_error(char const *msg)
Definition: decision_forest_builder.hpp:34
virtual char const * what() const noexcept
Definition: decision_forest_builder.hpp:35
model_builder_error()
Definition: decision_forest_builder.hpp:33
Definition: exceptions.hpp:24
A container which may or may not own its own data on host or device.
Definition: buffer.hpp:30