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);
80 TREELITE_DLL int TreeliteCompilerCreate(const char* name,
81  CompilerHandle* out);
89 TREELITE_DLL int TreeliteCompilerSetParam(CompilerHandle handle,
90  const char* name,
91  const char* value);
111 TREELITE_DLL int TreeliteCompilerGenerateCode(CompilerHandle compiler,
112  ModelHandle model,
113  int verbose,
114  const char* dirpath);
120 TREELITE_DLL int TreeliteCompilerFree(CompilerHandle handle);
135 TREELITE_DLL int TreeliteLoadLightGBMModel(const char* filename, ModelHandle* out);
143 TREELITE_DLL int TreeliteLoadXGBoostModel(const char* filename, ModelHandle* out);
151 TREELITE_DLL int TreeliteLoadXGBoostJSON(const char* filename,
152  ModelHandle* out);
161 TREELITE_DLL int TreeliteLoadXGBoostJSONString(const char* json_str,
162  size_t length,
163  ModelHandle* out);
171 TREELITE_DLL int TreeliteLoadXGBoostModelFromMemoryBuffer(const void* buf,
172  size_t len,
173  ModelHandle* out);
174 
201  int n_estimators, int n_features, const int64_t* node_count, const int64_t** children_left,
202  const int64_t** children_right, const int64_t** feature, const double** threshold,
203  const double** value, const int64_t** n_node_samples, const double** impurity,
204  ModelHandle* out);
205 
233  int n_estimators, int n_features, int n_classes, const int64_t* node_count,
234  const int64_t** children_left, const int64_t** children_right, const int64_t** feature,
235  const double** threshold, const double** value, const int64_t** n_node_samples,
236  const double** impurity, ModelHandle* out);
237 
263  int n_estimators, int n_features, const int64_t* node_count, const int64_t** children_left,
264  const int64_t** children_right, const int64_t** feature, const double** threshold,
265  const double** value, const int64_t** n_node_samples, const double** impurity,
266  ModelHandle* out);
267 
294  int n_estimators, int n_features, int n_classes, const int64_t* node_count,
295  const int64_t** children_left, const int64_t** children_right, const int64_t** feature,
296  const double** threshold, const double** value, const int64_t** n_node_samples,
297  const double** impurity, ModelHandle* out);
298 
305 TREELITE_DLL int TreeliteQueryNumTree(ModelHandle handle, size_t* out);
312 TREELITE_DLL int TreeliteQueryNumFeature(ModelHandle handle, size_t* out);
320 TREELITE_DLL int TreeliteQueryNumClass(ModelHandle handle, size_t* out);
321 
328 TREELITE_DLL int TreeliteSetTreeLimit(ModelHandle handle, size_t limit);
329 
337 TREELITE_DLL int TreeliteSerializeModel(const char* filename, ModelHandle handle);
338 
346 TREELITE_DLL int TreeliteDeserializeModel(const char* filename, ModelHandle* out);
347 
353 TREELITE_DLL int TreeliteFreeModel(ModelHandle handle);
364 TREELITE_DLL int TreeliteGTILGetPredictOutputSize(ModelHandle handle, size_t num_row, size_t* out);
365 
366 TREELITE_DLL int TreeliteGTILPredict(ModelHandle handle, const float* input, size_t num_row,
367  float* output, int pred_transform, size_t* out_result_size);
368 
384 TREELITE_DLL int TreeliteTreeBuilderCreateValue(const void* init_value, const char* type,
385  ValueHandle* out);
391 TREELITE_DLL int TreeliteTreeBuilderDeleteValue(ValueHandle handle);
401 TREELITE_DLL int TreeliteCreateTreeBuilder(const char* threshold_type, const char* leaf_output_type,
402  TreeBuilderHandle* out);
408 TREELITE_DLL int TreeliteDeleteTreeBuilder(TreeBuilderHandle handle);
415 TREELITE_DLL int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key);
422 TREELITE_DLL int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key);
429 TREELITE_DLL int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle, int node_key);
446  TreeBuilderHandle handle, int node_key, unsigned feature_id, const char* opname,
447  ValueHandle threshold, int default_left, int left_child_key, int right_child_key);
465  TreeBuilderHandle handle, int node_key, unsigned feature_id,
466  const unsigned int* left_categories, size_t left_categories_len, int default_left,
467  int left_child_key, int right_child_key);
476 TREELITE_DLL int TreeliteTreeBuilderSetLeafNode(
477  TreeBuilderHandle handle, int node_key, ValueHandle leaf_value);
489 TREELITE_DLL int TreeliteTreeBuilderSetLeafVectorNode(
490  TreeBuilderHandle handle, int node_key, const ValueHandle* leaf_vector, size_t leaf_vector_len);
506 TREELITE_DLL int TreeliteCreateModelBuilder(
507  int num_feature, int num_class, int average_tree_output, const char* threshold_type,
508  const char* leaf_output_type, ModelBuilderHandle* out);
517  const char* name,
518  const char* value);
524 TREELITE_DLL int TreeliteDeleteModelBuilder(ModelBuilderHandle handle);
538 TREELITE_DLL int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle,
539  TreeBuilderHandle tree_builder,
540  int index);
548 TREELITE_DLL int TreeliteModelBuilderGetTree(ModelBuilderHandle handle,
549  int index,
550  TreeBuilderHandle *out);
557 TREELITE_DLL int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle,
558  int index);
567  ModelHandle* out);
570 #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:288
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
Definition: c_api.cc:423
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:180
int TreeliteModelBuilderGetTree(ModelBuilderHandle handle, int index, TreeBuilderHandle *out)
Get a reference to a tree in the ensemble.
Definition: c_api.cc:449
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:152
int TreeliteFreeModel(ModelHandle handle)
delete model from memory
Definition: c_api.cc:252
int TreeliteAnnotationSave(AnnotationHandle handle, const char *path)
save branch annotation to a JSON file
Definition: c_api.cc:51
int TreeliteQueryNumTree(ModelHandle handle, size_t *out)
Query the number of trees in the model.
Definition: c_api.cc:274
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:206
int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle, int index)
Remove a tree from the ensemble.
Definition: c_api.cc:459
int TreeliteCompilerSetParam(CompilerHandle handle, const char *name, const char *value)
set a parameter for a compiler
Definition: c_api.cc:74
int TreeliteDeserializeModel(const char *filename, ModelHandle *out)
Deserialize (load) a model object from disk.
Definition: c_api.cc:242
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:159
int TreeliteDeleteModelBuilder(ModelBuilderHandle handle)
Delete a model builder from memory.
Definition: c_api.cc:432
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:372
int TreeliteAnnotationFree(AnnotationHandle handle)
delete branch annotation from memory
Definition: c_api.cc:60
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:360
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:193
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:39
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:305
int TreeliteSetTreeLimit(ModelHandle handle, size_t limit)
keep first N trees of model, limit must smaller than number of trees.
Definition: c_api.cc:295
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:438
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:352
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:330
int TreeliteCreateTreeBuilder(const char *threshold_type, const char *leaf_output_type, TreeBuilderHandle *out)
Create a new tree builder.
Definition: c_api.cc:319
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:467
int TreeliteTreeBuilderSetLeafNode(TreeBuilderHandle handle, int node_key, ValueHandle leaf_value)
Turn an empty node into a leaf node.
Definition: c_api.cc:389
int TreeliteCompilerGenerateCode(CompilerHandle compiler, ModelHandle model, int verbose, 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:95
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:412
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:219
int TreeliteLoadXGBoostModelFromMemoryBuffer(const void *buf, size_t len, ModelHandle *out)
load an XGBoost model from a memory buffer.
Definition: c_api.cc:173
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:145
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:313
int TreeliteCompilerCreate(const char *name, CompilerHandle *out)
create a compiler with a given name
Definition: c_api.cc:66
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
Definition: c_api.cc:344
int TreeliteQueryNumFeature(ModelHandle handle, size_t *out)
Query the number of features used in the model.
Definition: c_api.cc:281
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:232
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:397
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:166
int TreeliteCompilerFree(CompilerHandle handle)
delete compiler from memory
Definition: c_api.cc:139
int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key)
Create an empty node within a tree.
Definition: c_api.cc:336