12 #include <dmlc/logging.h> 16 #include "./pred_transform.h" 20 using PredTransformFuncType = std::size_t (*) (
const treelite::Model&,
const float*,
float*);
24 int left_child,
int right_child,
int default_child) {
25 if (std::isnan(fvalue)) {
29 case treelite::Operator::kEQ:
30 return (fvalue == threshold) ? left_child : right_child;
31 case treelite::Operator::kLT:
32 return (fvalue < threshold) ? left_child : right_child;
33 case treelite::Operator::kLE:
34 return (fvalue <= threshold) ? left_child : right_child;
35 case treelite::Operator::kGT:
36 return (fvalue > threshold) ? left_child : right_child;
37 case treelite::Operator::kGE:
38 return (fvalue >= threshold) ? left_child : right_child;
40 CHECK(
false) <<
"Unrecognized comparison operator " <<
static_cast<int>(op);
45 inline int NextNodeCategorical(
float fvalue,
const std::vector<uint32_t>& matching_categories,
46 bool categories_list_right_child,
int left_child,
int right_child,
48 if (std::isnan(fvalue)) {
51 const auto category_value =
static_cast<uint32_t
>(fvalue);
52 const bool is_matching_category = (
53 std::find(matching_categories.begin(), matching_categories.end(), category_value)
54 != matching_categories.end());
55 if (categories_list_right_child) {
56 return is_matching_category ? right_child : left_child;
58 return is_matching_category ? left_child : right_child;
62 template <
typename ThresholdType,
typename LeafOutputType,
typename DMatrixType,
65 const DMatrixType* input,
float* output,
bool pred_transform,
66 OutputFunc output_func) {
68 const size_t num_row = input->GetNumRow();
69 const size_t num_col = input->GetNumCol();
70 std::vector<ThresholdType> row(num_col);
72 std::vector<float> sum(task_param.
num_class);
75 std::size_t output_offset = 0;
76 for (
size_t row_id = 0; row_id < num_row; ++row_id) {
77 input->FillRow(row_id, row.data());
78 std::fill(sum.begin(), sum.end(), 0.0f);
79 const std::size_t num_tree = model.
trees.size();
80 for (std::size_t tree_id = 0; tree_id < num_tree; ++tree_id) {
81 const TreeType& tree = model.
trees[tree_id];
83 while (!tree.IsLeaf(node_id)) {
85 if (split_type == treelite::SplitFeatureType::kNumerical) {
86 node_id = NextNode(row[tree.SplitIndex(node_id)], tree.Threshold(node_id),
87 tree.ComparisonOp(node_id), tree.LeftChild(node_id),
88 tree.RightChild(node_id), tree.DefaultChild(node_id));
89 }
else if (split_type == treelite::SplitFeatureType::kCategorical) {
90 node_id = NextNodeCategorical(row[tree.SplitIndex(node_id)],
91 tree.MatchingCategories(node_id),
92 tree.CategoriesListRightChild(node_id),
93 tree.LeftChild(node_id), tree.RightChild(node_id),
94 tree.DefaultChild(node_id));
96 CHECK(
false) <<
"Unrecognized split type: " <<
static_cast<int>(split_type);
99 output_func(tree, tree_id, node_id, sum.data());
102 float average_factor;
103 if (model.
task_type == treelite::TaskType::kMultiClfGrovePerClass) {
107 CHECK_EQ(num_tree % task_param.
num_class, 0)
108 <<
"Expected the number of trees to be divisible by the number of classes";
109 int num_boosting_round = num_tree /
static_cast<int>(task_param.
num_class);
110 average_factor =
static_cast<float>(num_boosting_round);
112 CHECK(model.
task_type == treelite::TaskType::kBinaryClfRegr
113 || model.
task_type == treelite::TaskType::kMultiClfProbDistLeaf);
116 average_factor =
static_cast<float>(num_tree);
118 for (
unsigned int i = 0; i < task_param.
num_class; ++i) {
119 sum[i] /= average_factor;
122 for (
unsigned int i = 0; i < task_param.
num_class; ++i) {
123 sum[i] += model.
param.global_bias;
125 if (pred_transform) {
126 PredTransformFuncType pred_transform_func
127 = treelite::gtil::LookupPredTransform(model.
param.pred_transform);
128 output_offset += pred_transform_func(model, sum.data(), &output[output_offset]);
130 for (
unsigned int i = 0; i < task_param.
num_class; ++i) {
131 output[output_offset + i] = sum[i];
135 input->ClearRow(row_id, row.data());
137 return output_offset;
140 template <
typename ThresholdType,
typename LeafOutputType,
typename DMatrixType>
142 const DMatrixType* input,
float* output,
143 bool pred_transform) {
149 auto output_logic = [task_param](
150 const TreeType& tree, int,
int node_id,
float* sum) {
151 auto leaf_vector = tree.LeafVector(node_id);
153 sum[i] += leaf_vector[i];
156 return PredictImplInner(model, input, output, pred_transform, output_logic);
159 auto output_logic = [task_param](
160 const TreeType& tree,
int tree_id,
int node_id,
float* sum) {
161 sum[tree_id % task_param.
num_class] += tree.LeafValue(node_id);
163 return PredictImplInner(model, input, output, pred_transform, output_logic);
166 auto output_logic = [task_param](
167 const TreeType& tree,
int tree_id,
int node_id,
float* sum) {
168 sum[0] += tree.LeafValue(node_id);
170 return PredictImplInner(model, input, output, pred_transform, output_logic);
179 std::size_t Predict(
const Model* model,
const DMatrix* input,
float* output,
bool pred_transform) {
181 const auto* d1 =
dynamic_cast<const DenseDMatrixImpl<float>*
>(input);
182 const auto* d2 =
dynamic_cast<const CSRDMatrixImpl<float>*
>(input);
184 return model->Dispatch([d1, output, pred_transform](
const auto& model) {
185 return PredictImpl(model, d1, output, pred_transform);
188 return model->Dispatch([d2, output, pred_transform](
const auto& model) {
189 return PredictImpl(model, d2, output, pred_transform);
192 LOG(FATAL) <<
"DMatrix with float64 data is not supported";
197 std::size_t Predict(
const Model* model,
const float* input, std::size_t num_row,
float* output,
198 bool pred_transform) {
199 std::unique_ptr<DenseDMatrixImpl<float>> dmat =
200 std::make_unique<DenseDMatrixImpl<float>>(
201 std::vector<float>(input, input + num_row * model->num_feature),
202 std::numeric_limits<float>::quiet_NaN(),
205 return Predict(model, dmat.get(), output, pred_transform);
208 std::size_t GetPredictOutputSize(
const Model* model, std::size_t num_row) {
209 return model->task_param.num_class * num_row;
212 std::size_t GetPredictOutputSize(
const Model* model,
const DMatrix* input) {
213 return GetPredictOutputSize(model, input->GetNumRow());
ModelParam param
extra parameters
SplitFeatureType
feature split type
bool average_tree_output
whether to average tree outputs
Input data structure of Treelite.
model structure for tree ensemble
in-memory representation of a decision tree
TaskType task_type
Task type.
Group of parameters that are dependent on the choice of the task type.
std::vector< Tree< ThresholdType, LeafOutputType > > trees
member trees
General Tree Inference Library (GTIL), providing a reference implementation for predicting with decis...
TaskParameter task_param
Group of parameters that are specific to the particular task type.
unsigned int num_class
The number of classes in the target label.
bool grove_per_class
Whether we designate a subset of the trees to compute the prediction for each class.
thin wrapper for tree ensemble model
unsigned int leaf_vector_size
Dimension of the output from each leaf node.
Operator
comparison operators