17 #include "./pred_transform.h" 21 using PredTransformFuncType = std::size_t (*) (
const treelite::Model&,
const float*,
float*);
25 int left_child,
int right_child,
int default_child) {
26 if (std::isnan(fvalue)) {
30 case treelite::Operator::kEQ:
31 return (fvalue == threshold) ? left_child : right_child;
32 case treelite::Operator::kLT:
33 return (fvalue < threshold) ? left_child : right_child;
34 case treelite::Operator::kLE:
35 return (fvalue <= threshold) ? left_child : right_child;
36 case treelite::Operator::kGT:
37 return (fvalue > threshold) ? left_child : right_child;
38 case treelite::Operator::kGE:
39 return (fvalue >= threshold) ? left_child : right_child;
41 TREELITE_CHECK(
false) <<
"Unrecognized comparison operator " <<
static_cast<int>(op);
46 inline int NextNodeCategorical(
float fvalue,
const std::vector<uint32_t>& matching_categories,
47 bool categories_list_right_child,
int left_child,
int right_child,
49 if (std::isnan(fvalue)) {
52 const auto category_value =
static_cast<uint32_t
>(fvalue);
53 const bool is_matching_category = (
54 std::find(matching_categories.begin(), matching_categories.end(), category_value)
55 != matching_categories.end());
56 if (categories_list_right_child) {
57 return is_matching_category ? right_child : left_child;
59 return is_matching_category ? left_child : right_child;
63 template <
typename ThresholdType,
typename LeafOutputType,
typename DMatrixType,
66 const DMatrixType* input,
float* output,
bool pred_transform,
67 OutputFunc output_func) {
69 const size_t num_row = input->GetNumRow();
70 const size_t num_col = input->GetNumCol();
71 std::vector<ThresholdType> row(num_col);
73 std::vector<float> sum(task_param.
num_class);
76 std::size_t output_offset = 0;
77 for (
size_t row_id = 0; row_id < num_row; ++row_id) {
78 input->FillRow(row_id, row.data());
79 std::fill(sum.begin(), sum.end(), 0.0f);
80 const std::size_t num_tree = model.
trees.size();
81 for (std::size_t tree_id = 0; tree_id < num_tree; ++tree_id) {
82 const TreeType& tree = model.
trees[tree_id];
84 while (!tree.IsLeaf(node_id)) {
86 if (split_type == treelite::SplitFeatureType::kNumerical) {
87 node_id = NextNode(row[tree.SplitIndex(node_id)], tree.Threshold(node_id),
88 tree.ComparisonOp(node_id), tree.LeftChild(node_id),
89 tree.RightChild(node_id), tree.DefaultChild(node_id));
90 }
else if (split_type == treelite::SplitFeatureType::kCategorical) {
91 node_id = NextNodeCategorical(row[tree.SplitIndex(node_id)],
92 tree.MatchingCategories(node_id),
93 tree.CategoriesListRightChild(node_id),
94 tree.LeftChild(node_id), tree.RightChild(node_id),
95 tree.DefaultChild(node_id));
97 TREELITE_CHECK(
false) <<
"Unrecognized split type: " <<
static_cast<int>(split_type);
100 output_func(tree, tree_id, node_id, sum.data());
103 float average_factor;
104 if (model.
task_type == treelite::TaskType::kMultiClfGrovePerClass) {
107 TREELITE_CHECK_GT(task_param.
num_class, 1);
108 TREELITE_CHECK_EQ(num_tree % task_param.
num_class, 0)
109 <<
"Expected the number of trees to be divisible by the number of classes";
110 int num_boosting_round = num_tree /
static_cast<int>(task_param.
num_class);
111 average_factor =
static_cast<float>(num_boosting_round);
113 TREELITE_CHECK(model.
task_type == treelite::TaskType::kBinaryClfRegr
114 || model.
task_type == treelite::TaskType::kMultiClfProbDistLeaf);
117 average_factor =
static_cast<float>(num_tree);
119 for (
unsigned int i = 0; i < task_param.
num_class; ++i) {
120 sum[i] /= average_factor;
123 for (
unsigned int i = 0; i < task_param.
num_class; ++i) {
124 sum[i] += model.
param.global_bias;
126 if (pred_transform) {
127 PredTransformFuncType pred_transform_func
128 = treelite::gtil::LookupPredTransform(model.
param.pred_transform);
129 output_offset += pred_transform_func(model, sum.data(), &output[output_offset]);
131 for (
unsigned int i = 0; i < task_param.
num_class; ++i) {
132 output[output_offset + i] = sum[i];
136 input->ClearRow(row_id, row.data());
138 return output_offset;
141 template <
typename ThresholdType,
typename LeafOutputType,
typename DMatrixType>
143 const DMatrixType* input,
float* output,
144 bool pred_transform) {
150 auto output_logic = [task_param](
151 const TreeType& tree, int,
int node_id,
float* sum) {
152 auto leaf_vector = tree.LeafVector(node_id);
154 sum[i] += leaf_vector[i];
157 return PredictImplInner(model, input, output, pred_transform, output_logic);
160 auto output_logic = [task_param](
161 const TreeType& tree,
int tree_id,
int node_id,
float* sum) {
162 sum[tree_id % task_param.
num_class] += tree.LeafValue(node_id);
164 return PredictImplInner(model, input, output, pred_transform, output_logic);
167 auto output_logic = [task_param](
168 const TreeType& tree,
int tree_id,
int node_id,
float* sum) {
169 sum[0] += tree.LeafValue(node_id);
171 return PredictImplInner(model, input, output, pred_transform, output_logic);
180 std::size_t Predict(
const Model* model,
const DMatrix* input,
float* output,
bool pred_transform) {
182 const auto* d1 =
dynamic_cast<const DenseDMatrixImpl<float>*
>(input);
183 const auto* d2 =
dynamic_cast<const CSRDMatrixImpl<float>*
>(input);
185 return model->Dispatch([d1, output, pred_transform](
const auto& model) {
186 return PredictImpl(model, d1, output, pred_transform);
189 return model->Dispatch([d2, output, pred_transform](
const auto& model) {
190 return PredictImpl(model, d2, output, pred_transform);
193 TREELITE_LOG(FATAL) <<
"DMatrix with float64 data is not supported";
198 std::size_t Predict(
const Model* model,
const float* input, std::size_t num_row,
float* output,
199 bool pred_transform) {
200 std::unique_ptr<DenseDMatrixImpl<float>> dmat =
201 std::make_unique<DenseDMatrixImpl<float>>(
202 std::vector<float>(input, input + num_row * model->num_feature),
203 std::numeric_limits<float>::quiet_NaN(),
206 return Predict(model, dmat.get(), output, pred_transform);
209 std::size_t GetPredictOutputSize(
const Model* model, std::size_t num_row) {
210 return model->task_param.num_class * num_row;
213 std::size_t GetPredictOutputSize(
const Model* model,
const DMatrix* input) {
214 return GetPredictOutputSize(model, input->GetNumRow());
ModelParam param
extra parameters
SplitFeatureType
feature split type
bool grove_per_class
Whether we designate a subset of the trees to compute the prediction for each class.
Group of parameters that are dependent on the choice of the task type.
bool average_tree_output
whether to average tree outputs
Input data structure of Treelite.
model structure for tree ensemble
unsigned int leaf_vector_size
Dimension of the output from each leaf node.
in-memory representation of a decision tree
logging facility for Treelite
unsigned int num_class
The number of classes in the target label.
TaskType task_type
Task type.
std::vector< Tree< ThresholdType, LeafOutputType > > trees
member trees
General Tree Inference Library (GTIL), providing a reference implementation for predicting with decis...
TaskParam task_param
Group of parameters that are specific to the particular task type.
thin wrapper for tree ensemble model
Operator
comparison operators