Treelite
c_api.h
Go to the documentation of this file.
1 
9 /* Note: Make sure to use slash-asterisk form of comments in this file
10  (like this one). Do not use double-slash (//). */
11 
12 #ifndef TREELITE_C_API_H_
13 #define TREELITE_C_API_H_
14 
15 #include "c_api_common.h"
16 
23 typedef void* ModelHandle;
25 typedef void* TreeBuilderHandle;
27 typedef void* ModelBuilderHandle;
29 typedef void* AnnotationHandle;
31 typedef void* CompilerHandle;
33 typedef void* ValueHandle;
51 TREELITE_DLL int TreeliteAnnotateBranch(
52  ModelHandle model, DMatrixHandle dmat, int nthread, int verbose, AnnotationHandle* out);
59 TREELITE_DLL int TreeliteAnnotationSave(AnnotationHandle handle,
60  const char* path);
66 TREELITE_DLL int TreeliteAnnotationFree(AnnotationHandle handle);
81 TREELITE_DLL int TreeliteCompilerCreateV2(const char* name, const char* params_json_str,
82  CompilerHandle* out);
101 TREELITE_DLL int TreeliteCompilerGenerateCodeV2(CompilerHandle compiler,
102  ModelHandle model,
103  const char* dirpath);
109 TREELITE_DLL int TreeliteCompilerFree(CompilerHandle handle);
124 TREELITE_DLL int TreeliteLoadLightGBMModel(const char* filename, ModelHandle* out);
132 TREELITE_DLL int TreeliteLoadXGBoostModel(const char* filename, ModelHandle* out);
140 TREELITE_DLL int TreeliteLoadXGBoostJSON(const char* filename,
141  ModelHandle* out);
150 TREELITE_DLL int TreeliteLoadXGBoostJSONString(const char* json_str,
151  size_t length,
152  ModelHandle* out);
160 TREELITE_DLL int TreeliteLoadXGBoostModelFromMemoryBuffer(const void* buf,
161  size_t len,
162  ModelHandle* out);
163 
190  int n_estimators, int n_features, const int64_t* node_count, const int64_t** children_left,
191  const int64_t** children_right, const int64_t** feature, const double** threshold,
192  const double** value, const int64_t** n_node_samples, const double** impurity,
193  ModelHandle* out);
194 
222  int n_estimators, int n_features, int n_classes, const int64_t* node_count,
223  const int64_t** children_left, const int64_t** children_right, const int64_t** feature,
224  const double** threshold, const double** value, const int64_t** n_node_samples,
225  const double** impurity, ModelHandle* out);
226 
252  int n_estimators, int n_features, const int64_t* node_count, const int64_t** children_left,
253  const int64_t** children_right, const int64_t** feature, const double** threshold,
254  const double** value, const int64_t** n_node_samples, const double** impurity,
255  ModelHandle* out);
256 
283  int n_estimators, int n_features, int n_classes, const int64_t* node_count,
284  const int64_t** children_left, const int64_t** children_right, const int64_t** feature,
285  const double** threshold, const double** value, const int64_t** n_node_samples,
286  const double** impurity, ModelHandle* out);
287 
294 TREELITE_DLL int TreeliteQueryNumTree(ModelHandle handle, size_t* out);
301 TREELITE_DLL int TreeliteQueryNumFeature(ModelHandle handle, size_t* out);
309 TREELITE_DLL int TreeliteQueryNumClass(ModelHandle handle, size_t* out);
310 
317 TREELITE_DLL int TreeliteSetTreeLimit(ModelHandle handle, size_t limit);
318 
326 TREELITE_DLL int TreeliteSerializeModel(const char* filename, ModelHandle handle);
327 
335 TREELITE_DLL int TreeliteDeserializeModel(const char* filename, ModelHandle* out);
336 
344 TREELITE_DLL int TreeliteDumpAsJSON(ModelHandle handle, int pretty_print,
345  const char** out_json_str);
346 
352 TREELITE_DLL int TreeliteFreeModel(ModelHandle handle);
363 TREELITE_DLL int TreeliteGTILGetPredictOutputSize(ModelHandle handle, size_t num_row, size_t* out);
364 
365 TREELITE_DLL int TreeliteGTILPredict(ModelHandle handle, const float* input, size_t num_row,
366  float* output, int pred_transform, size_t* out_result_size);
367 
383 TREELITE_DLL int TreeliteTreeBuilderCreateValue(const void* init_value, const char* type,
384  ValueHandle* out);
390 TREELITE_DLL int TreeliteTreeBuilderDeleteValue(ValueHandle handle);
400 TREELITE_DLL int TreeliteCreateTreeBuilder(const char* threshold_type, const char* leaf_output_type,
401  TreeBuilderHandle* out);
407 TREELITE_DLL int TreeliteDeleteTreeBuilder(TreeBuilderHandle handle);
414 TREELITE_DLL int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key);
421 TREELITE_DLL int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key);
428 TREELITE_DLL int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle, int node_key);
445  TreeBuilderHandle handle, int node_key, unsigned feature_id, const char* opname,
446  ValueHandle threshold, int default_left, int left_child_key, int right_child_key);
464  TreeBuilderHandle handle, int node_key, unsigned feature_id,
465  const unsigned int* left_categories, size_t left_categories_len, int default_left,
466  int left_child_key, int right_child_key);
475 TREELITE_DLL int TreeliteTreeBuilderSetLeafNode(
476  TreeBuilderHandle handle, int node_key, ValueHandle leaf_value);
488 TREELITE_DLL int TreeliteTreeBuilderSetLeafVectorNode(
489  TreeBuilderHandle handle, int node_key, const ValueHandle* leaf_vector, size_t leaf_vector_len);
505 TREELITE_DLL int TreeliteCreateModelBuilder(
506  int num_feature, int num_class, int average_tree_output, const char* threshold_type,
507  const char* leaf_output_type, ModelBuilderHandle* out);
516  const char* name,
517  const char* value);
523 TREELITE_DLL int TreeliteDeleteModelBuilder(ModelBuilderHandle handle);
537 TREELITE_DLL int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle,
538  TreeBuilderHandle tree_builder,
539  int index);
547 TREELITE_DLL int TreeliteModelBuilderGetTree(ModelBuilderHandle handle,
548  int index,
549  TreeBuilderHandle *out);
556 TREELITE_DLL int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle,
557  int index);
566  ModelHandle* out);
569 #endif /* TREELITE_C_API_H_ */
int TreeliteQueryNumClass(ModelHandle handle, size_t *out)
Query the number of classes of the model. (1 if the model is binary classifier or regressor) ...
Definition: c_api.cc:268
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
Definition: c_api.cc:403
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...
Definition: c_api.cc:151
int TreeliteModelBuilderGetTree(ModelBuilderHandle handle, int index, TreeBuilderHandle *out)
Get a reference to a tree in the ensemble.
Definition: c_api.cc:429
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...
Definition: c_api.cc:123
int TreeliteDumpAsJSON(ModelHandle handle, int pretty_print, const char **out_json_str)
Dump a model object as a JSON string.
Definition: c_api.cc:223
int TreeliteFreeModel(ModelHandle handle)
delete model from memory
Definition: c_api.cc:232
int TreeliteAnnotationSave(AnnotationHandle handle, const char *path)
save branch annotation to a JSON file
Definition: c_api.cc:53
int TreeliteQueryNumTree(ModelHandle handle, size_t *out)
Query the number of trees in the model.
Definition: c_api.cc:254
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.
Definition: c_api.cc:177
int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle, int index)
Remove a tree from the ensemble.
Definition: c_api.cc:439
int TreeliteDeserializeModel(const char *filename, ModelHandle *out)
Deserialize (load) a model object from disk.
Definition: c_api.cc:213
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...
Definition: c_api.cc:130
int TreeliteDeleteModelBuilder(ModelBuilderHandle handle)
Delete a model builder from memory.
Definition: c_api.cc:412
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 ...
Definition: c_api.cc:352
int TreeliteAnnotationFree(AnnotationHandle handle)
delete branch annotation from memory
Definition: c_api.cc:62
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 ...
Definition: c_api.cc:340
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...
Definition: c_api.cc:164
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. ...
Definition: c_api.cc:41
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...
Definition: c_api.cc:285
int TreeliteSetTreeLimit(ModelHandle handle, size_t limit)
keep first N trees of model, limit must smaller than number of trees.
Definition: c_api.cc:275
void * ValueHandle
handle to a polymorphic value type, used in the model builder API
Definition: c_api.h:33
int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle, TreeBuilderHandle tree_builder, int index)
Insert a tree at specified location.
Definition: c_api.cc:418
void * DMatrixHandle
handle to a data matrix
Definition: c_api_common.h:30
int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle, int node_key)
Set a node as the root of a tree.
Definition: c_api.cc:332
int TreeliteCompilerCreateV2(const char *name, const char *params_json_str, CompilerHandle *out)
Create a compiler with a given name.
Definition: c_api.cc:68
void * TreeBuilderHandle
handle to tree builder class
Definition: c_api.h:25
int TreeliteDeleteTreeBuilder(TreeBuilderHandle handle)
Delete a tree builder from memory.
Definition: c_api.cc:310
int TreeliteCreateTreeBuilder(const char *threshold_type, const char *leaf_output_type, TreeBuilderHandle *out)
Create a new tree builder.
Definition: c_api.cc:299
void * AnnotationHandle
handle to branch annotation data
Definition: c_api.h:29
int TreeliteModelBuilderCommitModel(ModelBuilderHandle handle, ModelHandle *out)
finalize the model and produce the in-memory representation
Definition: c_api.cc:447
int TreeliteTreeBuilderSetLeafNode(TreeBuilderHandle handle, int node_key, ValueHandle leaf_value)
Turn an empty node into a leaf node.
Definition: c_api.cc:369
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).
Definition: c_api.cc:75
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.
Definition: c_api.cc:392
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.
Definition: c_api.cc:190
int TreeliteLoadXGBoostModelFromMemoryBuffer(const void *buf, size_t len, ModelHandle *out)
load an XGBoost model from a memory buffer.
Definition: c_api.cc:144
void * ModelHandle
handle to a decision tree ensemble model
Definition: c_api.h:23
int TreeliteLoadLightGBMModel(const char *filename, ModelHandle *out)
load a model file generated by LightGBM (Microsoft/LightGBM). The model file must contain a decision ...
Definition: c_api.cc:116
C API of Treelite, used for interfacing with other languages This header is used by both the runtime ...
int TreeliteTreeBuilderDeleteValue(ValueHandle handle)
Delete a Value object from memory.
Definition: c_api.cc:293
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
Definition: c_api.cc:324
int TreeliteQueryNumFeature(ModelHandle handle, size_t *out)
Query the number of features used in the model.
Definition: c_api.cc:261
void * ModelBuilderHandle
handle to ensemble builder class
Definition: c_api.h:27
int TreeliteSerializeModel(const char *filename, ModelHandle handle)
Serialize (persist) a model object to disk.
Definition: c_api.cc:203
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...
Definition: c_api.cc:377
void * CompilerHandle
handle to compiler class
Definition: c_api.h:31
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...
Definition: c_api.cc:137
int TreeliteCompilerFree(CompilerHandle handle)
delete compiler from memory
Definition: c_api.cc:110
int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key)
Create an empty node within a tree.
Definition: c_api.cc:316