31 struct TreeliteAPIThreadLocalEntry {
45 const Model* model_ =
static_cast<Model*
>(model);
46 const auto* dmat_ =
static_cast<const DMatrix*
>(dmat);
47 TREELITE_CHECK(dmat_) <<
"Found a dangling reference to DMatrix";
48 annotator->Annotate(*model_, dmat_, nthread, verbose);
57 std::ofstream fo(path);
77 const char* dirpath) {
79 const Model* model_ =
static_cast<Model*
>(model);
81 TREELITE_CHECK(model_);
82 TREELITE_CHECK(compiler_);
86 const std::string& dirpath_(dirpath);
87 filesystem::CreateDirectoryIfNotExist(dirpath);
90 auto compiled_model = compiler_->
Compile(*model_);
92 TREELITE_LOG(INFO) <<
"Code generation finished. Writing code to files...";
95 for (
const auto& it : compiled_model.files) {
97 TREELITE_LOG(INFO) <<
"Writing file " << it.first <<
"...";
99 const std::string filename_full = dirpath_ +
"/" + it.first;
100 if (it.second.is_binary) {
101 filesystem::WriteToFile(filename_full, it.second.content_binary);
103 filesystem::WriteToFile(filename_full, it.second.content);
112 delete static_cast<Compiler*
>(handle);
118 std::unique_ptr<Model> model = frontend::LoadLightGBMModel(filename);
125 std::unique_ptr<Model> model = frontend::LoadXGBoostModel(filename);
132 std::unique_ptr<Model> model = frontend::LoadXGBoostJSONModel(filename);
139 std::unique_ptr<Model> model = frontend::LoadXGBoostJSONModelString(json_str, length);
146 std::unique_ptr<Model> model = frontend::LoadXGBoostModel(buf, len);
152 int n_estimators,
int n_features,
const int64_t* node_count,
const int64_t** children_left,
153 const int64_t** children_right,
const int64_t** feature,
const double** threshold,
154 const double** value,
const int64_t** n_node_samples,
const double** impurity,
157 std::unique_ptr<Model> model = frontend::LoadSKLearnRandomForestRegressor(
158 n_estimators, n_features, node_count, children_left, children_right, feature, threshold,
159 value, n_node_samples, impurity);
165 int n_estimators,
int n_features,
int n_classes,
const int64_t* node_count,
166 const int64_t** children_left,
const int64_t** children_right,
const int64_t** feature,
167 const double** threshold,
const double** value,
const int64_t** n_node_samples,
170 std::unique_ptr<Model> model = frontend::LoadSKLearnRandomForestClassifier(
171 n_estimators, n_features, n_classes, node_count, children_left, children_right, feature,
172 threshold, value, n_node_samples, impurity);
178 int n_estimators,
int n_features,
const int64_t* node_count,
const int64_t** children_left,
179 const int64_t** children_right,
const int64_t** feature,
const double** threshold,
180 const double** value,
const int64_t** n_node_samples,
const double** impurity,
183 std::unique_ptr<Model> model = frontend::LoadSKLearnGradientBoostingRegressor(
184 n_estimators, n_features, node_count, children_left, children_right, feature, threshold,
185 value, n_node_samples, impurity);
191 int n_estimators,
int n_features,
int n_classes,
const int64_t* node_count,
192 const int64_t** children_left,
const int64_t** children_right,
const int64_t** feature,
193 const double** threshold,
const double** value,
const int64_t** n_node_samples,
196 std::unique_ptr<Model> model = frontend::LoadSKLearnGradientBoostingClassifier(
197 n_estimators, n_features, n_classes, node_count, children_left, children_right, feature,
198 threshold, value, n_node_samples, impurity);
205 FILE* fp = std::fopen(filename,
"wb");
206 TREELITE_CHECK(fp) <<
"Failed to open file '" << filename <<
"'";
207 auto* model_ =
static_cast<Model*
>(handle);
208 model_->SerializeToFile(fp);
215 FILE* fp = std::fopen(filename,
"rb");
216 TREELITE_CHECK(fp) <<
"Failed to open file '" << filename <<
"'";
217 std::unique_ptr<Model> model = Model::DeserializeFromFile(fp);
225 auto* model_ =
static_cast<Model*
>(handle);
226 std::string& ret_str = TreeliteAPIThreadLocalStore::Get()->ret_str;
227 ret_str = model_->DumpAsJSON(pretty_print != 0);
228 *out_json_str = ret_str.c_str();
234 delete static_cast<Model*
>(handle);
238 int TreeliteGTILGetPredictOutputSize(
ModelHandle handle,
size_t num_row,
size_t* out) {
240 const auto* model_ =
static_cast<const Model*
>(handle);
241 *out = gtil::GetPredictOutputSize(model_, num_row);
245 int TreeliteGTILPredict(
ModelHandle handle,
const float* input,
size_t num_row,
float* output,
246 int pred_transform,
size_t* out_result_size) {
248 const auto* model_ =
static_cast<const Model*
>(handle);
250 gtil::Predict(model_, input, num_row, output, (pred_transform == 1));
256 const auto* model_ =
static_cast<const Model*
>(handle);
257 *out = model_->GetNumTree();
263 const auto* model_ =
static_cast<const Model*
>(handle);
264 *out =
static_cast<size_t>(model_->num_feature);
270 const auto* model_ =
static_cast<const Model*
>(handle);
271 *out =
static_cast<size_t>(model_->task_param.num_class);
277 TREELITE_CHECK_GT(limit, 0) <<
"limit should be greater than 0!";
278 auto* model_ =
static_cast<Model*
>(handle);
279 const size_t num_tree = model_->GetNumTree();
280 TREELITE_CHECK_GE(num_tree, limit) <<
"Model contains fewer trees(" << num_tree <<
") than limit";
281 model_->SetTreeLimit(limit);
287 std::unique_ptr<frontend::Value> value = std::make_unique<frontend::Value>();
302 std::unique_ptr<frontend::TreeBuilder> builder{
319 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
320 builder->CreateNode(node_key);
327 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
328 builder->DeleteNode(node_key);
335 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
336 builder->SetRootNode(node_key);
342 ValueHandle threshold,
int default_left,
int left_child_key,
int right_child_key) {
345 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
346 builder->SetNumericalTestNode(node_key, feature_id, opname,
347 *static_cast<const frontend::Value*>(threshold),
348 (default_left != 0), left_child_key, right_child_key);
354 const unsigned int* left_categories,
size_t left_categories_len,
int default_left,
355 int left_child_key,
int right_child_key) {
358 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
359 std::vector<uint32_t> vec(left_categories_len);
360 for (
size_t i = 0; i < left_categories_len; ++i) {
361 TREELITE_CHECK(left_categories[i] <= std::numeric_limits<uint32_t>::max());
362 vec[i] =
static_cast<uint32_t
>(left_categories[i]);
364 builder->SetCategoricalTestNode(node_key, feature_id, vec, (default_left != 0),
365 left_child_key, right_child_key);
372 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
373 builder->SetLeafNode(node_key, *static_cast<const frontend::Value*>(leaf_value));
378 const ValueHandle* leaf_vector,
size_t leaf_vector_len) {
381 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
382 std::vector<frontend::Value> vec(leaf_vector_len);
383 TREELITE_CHECK(leaf_vector) <<
"leaf_vector argument must not be null";
384 for (
size_t i = 0; i < leaf_vector_len; ++i) {
385 TREELITE_CHECK(leaf_vector[i]) <<
"leaf_vector[" << i <<
"] contains an empty Value handle";
388 builder->SetLeafVectorNode(node_key, vec);
393 int num_feature,
int num_class,
int average_tree_output,
const char* threshold_type,
397 num_feature, num_class, (average_tree_output != 0),
GetTypeInfoByName(threshold_type),
407 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted ModelBuilder object";
408 builder->SetModelParam(name, value);
422 TREELITE_CHECK(model_builder) <<
"Detected dangling reference to deleted ModelBuilder object";
424 TREELITE_CHECK(tree_builder) <<
"Detected dangling reference to deleted TreeBuilder object";
425 return model_builder->InsertTree(tree_builder, index);
432 TREELITE_CHECK(model_builder) <<
"Detected dangling reference to deleted ModelBuilder object";
433 auto* tree_builder = model_builder->GetTree(index);
434 TREELITE_CHECK(tree_builder) <<
"Detected dangling reference to deleted TreeBuilder object";
442 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted ModelBuilder object";
443 builder->DeleteTree(index);
450 TREELITE_CHECK(builder) <<
"Detected dangling reference to deleted ModelBuilder object";
451 std::unique_ptr<Model> model = builder->CommitModel();
Some useful math utilities.
int TreeliteQueryNumClass(ModelHandle handle, size_t *out)
Query the number of classes of the model. (1 if the model is binary classifier or regressor) ...
C API of Treelite, used for interfacing with other languages This header is excluded from the runtime...
Parameters for tree compiler.
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
int TreeliteLoadSKLearnRandomForestRegressor(int n_estimators, int n_features, const int64_t *node_count, const int64_t **children_left, const int64_t **children_right, const int64_t **feature, const double **threshold, const double **value, const int64_t **n_node_samples, const double **impurity, ModelHandle *out)
Load a scikit-learn random forest regressor model from a collection of arrays. Refer to https://sciki...
int TreeliteModelBuilderGetTree(ModelBuilderHandle handle, int index, TreeBuilderHandle *out)
Get a reference to a tree in the ensemble.
void Save(std::ostream &fo) const
save branch annotation to a JSON file
Collection of front-end methods to load or construct ensemble model.
int TreeliteLoadXGBoostModel(const char *filename, ModelHandle *out)
load a model file generated by XGBoost (dmlc/xgboost). The model file must contain a decision tree en...
int TreeliteDumpAsJSON(ModelHandle handle, int pretty_print, const char **out_json_str)
Dump a model object as a JSON string.
#define API_BEGIN()
macro to guard beginning and end section of all functions
int TreeliteFreeModel(ModelHandle handle)
delete model from memory
int TreeliteAnnotationSave(AnnotationHandle handle, const char *path)
save branch annotation to a JSON file
int TreeliteQueryNumTree(ModelHandle handle, size_t *out)
Query the number of trees in the model.
int TreeliteLoadSKLearnGradientBoostingRegressor(int n_estimators, int n_features, const int64_t *node_count, const int64_t **children_left, const int64_t **children_right, const int64_t **feature, const double **threshold, const double **value, const int64_t **n_node_samples, const double **impurity, ModelHandle *out)
Load a scikit-learn gradient boosting regressor model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail.
int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle, int index)
Remove a tree from the ensemble.
parameters for tree compiler
Input data structure of Treelite.
int TreeliteDeserializeModel(const char *filename, ModelHandle *out)
Deserialize (load) a model object from disk.
int TreeliteLoadXGBoostJSON(const char *filename, ModelHandle *out)
load a json model file generated by XGBoost (dmlc/xgboost). The model file must contain a decision tr...
int TreeliteDeleteModelBuilder(ModelBuilderHandle handle)
Delete a model builder from memory.
int TreeliteTreeBuilderSetCategoricalTestNode(TreeBuilderHandle handle, int node_key, unsigned feature_id, const unsigned int *left_categories, size_t left_categories_len, int default_left, int left_child_key, int right_child_key)
Turn an empty node into a test node with categorical split. A list defines all categories that would ...
model structure for tree ensemble
logging facility for Treelite
int TreeliteAnnotationFree(AnnotationHandle handle)
delete branch annotation from memory
int TreeliteTreeBuilderSetNumericalTestNode(TreeBuilderHandle handle, int node_key, unsigned feature_id, const char *opname, ValueHandle threshold, int default_left, int left_child_key, int right_child_key)
Turn an empty node into a test node with numerical split. The test is in the form [feature value] OP ...
int TreeliteLoadSKLearnRandomForestClassifier(int n_estimators, int n_features, int n_classes, const int64_t *node_count, const int64_t **children_left, const int64_t **children_right, const int64_t **feature, const double **threshold, const double **value, const int64_t **n_node_samples, const double **impurity, ModelHandle *out)
Load a scikit-learn random forest classifier model from a collection of arrays. Refer to https://scik...
int TreeliteAnnotateBranch(ModelHandle model, DMatrixHandle dmat, int nthread, int verbose, AnnotationHandle *out)
annotate branches in a given model using frequency patterns in the training data. ...
int TreeliteTreeBuilderCreateValue(const void *init_value, const char *type, ValueHandle *out)
Create a new Value object. Some model builder API functions accept this Value type to accommodate val...
Interface of compiler that compiles a tree ensemble model.
int TreeliteSetTreeLimit(ModelHandle handle, size_t limit)
keep first N trees of model, limit must smaller than number of trees.
void * ValueHandle
handle to a polymorphic value type, used in the model builder API
int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle, TreeBuilderHandle tree_builder_handle, int index)
Insert a tree at specified location.
void * DMatrixHandle
handle to a data matrix
Cross-platform wrapper for common filesystem functions.
int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle, int node_key)
Set a node as the root of a tree.
int TreeliteCompilerCreateV2(const char *name, const char *params_json_str, CompilerHandle *out)
Create a compiler with a given name.
void * TreeBuilderHandle
handle to tree builder class
Error handling for C API.
int TreeliteDeleteTreeBuilder(TreeBuilderHandle handle)
Delete a tree builder from memory.
int TreeliteCreateTreeBuilder(const char *threshold_type, const char *leaf_output_type, TreeBuilderHandle *out)
Create a new tree builder.
void * AnnotationHandle
handle to branch annotation data
int TreeliteModelBuilderCommitModel(ModelBuilderHandle handle, ModelHandle *out)
finalize the model and produce the in-memory representation
General Tree Inference Library (GTIL), providing a reference implementation for predicting with decis...
int TreeliteTreeBuilderSetLeafNode(TreeBuilderHandle handle, int node_key, ValueHandle leaf_value)
Turn an empty node into a leaf node.
int TreeliteCompilerGenerateCodeV2(CompilerHandle compiler, ModelHandle model, const char *dirpath)
Generate prediction code from a tree ensemble model. The code will be C99 compliant. One header file (.h) will be generated, along with one or more source files (.c).
int TreeliteCreateModelBuilder(int num_feature, int num_class, int average_tree_output, const char *threshold_type, const char *leaf_output_type, ModelBuilderHandle *out)
Create a new model builder.
int TreeliteLoadSKLearnGradientBoostingClassifier(int n_estimators, int n_features, int n_classes, const int64_t *node_count, const int64_t **children_left, const int64_t **children_right, const int64_t **feature, const double **threshold, const double **value, const int64_t **n_node_samples, const double **impurity, ModelHandle *out)
Load a scikit-learn gradient boosting classifier model from a collection of arrays. Refer to https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html to learn the mearning of the arrays in detail.
virtual compiler::CompiledModel Compile(const Model &model)=0
convert tree ensemble model
virtual compiler::CompilerParam QueryParam() const =0
Query the parameters used to intiailize the compiler.
int TreeliteLoadXGBoostModelFromMemoryBuffer(const void *buf, size_t len, ModelHandle *out)
load an XGBoost model from a memory buffer.
void * ModelHandle
handle to a decision tree ensemble model
int TreeliteLoadLightGBMModel(const char *filename, ModelHandle *out)
load a model file generated by LightGBM (Microsoft/LightGBM). The model file must contain a decision ...
TypeInfo GetTypeInfoByName(const std::string &str)
conversion table from string to TypeInfo, defined in tables.cc
int TreeliteTreeBuilderDeleteValue(ValueHandle handle)
Delete a Value object from memory.
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
thin wrapper for tree ensemble model
int TreeliteQueryNumFeature(ModelHandle handle, size_t *out)
Query the number of features used in the model.
void * ModelBuilderHandle
handle to ensemble builder class
int TreeliteSerializeModel(const char *filename, ModelHandle handle)
Serialize (persist) a model object to disk.
int TreeliteTreeBuilderSetLeafVectorNode(TreeBuilderHandle handle, int node_key, const ValueHandle *leaf_vector, size_t leaf_vector_len)
Turn an empty node into a leaf vector node The leaf vector (collection of multiple leaf weights per l...
int verbose
if >0, produce extra messages
void * CompilerHandle
handle to compiler class
static Compiler * Create(const std::string &name, const char *param_json_str)
create a compiler from given name
int TreeliteLoadXGBoostJSONString(const char *json_str, size_t length, ModelHandle *out)
load a model stored as JSON stringby XGBoost (dmlc/xgboost). The model json must contain a decision t...
int TreeliteCompilerFree(CompilerHandle handle)
delete compiler from memory
#define API_END()
every function starts with API_BEGIN(); and finishes with API_END() or API_END_HANDLE_ERROR ...
int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key)
Create an empty node within a tree.