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 
342 TREELITE_DLL int TreeliteFreeModel(ModelHandle handle);
353 TREELITE_DLL int TreeliteGTILGetPredictOutputSize(ModelHandle handle, size_t num_row, size_t* out);
354 
355 TREELITE_DLL int TreeliteGTILPredict(ModelHandle handle, const float* input, size_t num_row,
356  float* output, int pred_transform, size_t* out_result_size);
357 
373 TREELITE_DLL int TreeliteTreeBuilderCreateValue(const void* init_value, const char* type,
374  ValueHandle* out);
380 TREELITE_DLL int TreeliteTreeBuilderDeleteValue(ValueHandle handle);
390 TREELITE_DLL int TreeliteCreateTreeBuilder(const char* threshold_type, const char* leaf_output_type,
391  TreeBuilderHandle* out);
397 TREELITE_DLL int TreeliteDeleteTreeBuilder(TreeBuilderHandle handle);
404 TREELITE_DLL int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key);
411 TREELITE_DLL int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key);
418 TREELITE_DLL int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle, int node_key);
435  TreeBuilderHandle handle, int node_key, unsigned feature_id, const char* opname,
436  ValueHandle threshold, int default_left, int left_child_key, int right_child_key);
454  TreeBuilderHandle handle, int node_key, unsigned feature_id,
455  const unsigned int* left_categories, size_t left_categories_len, int default_left,
456  int left_child_key, int right_child_key);
465 TREELITE_DLL int TreeliteTreeBuilderSetLeafNode(
466  TreeBuilderHandle handle, int node_key, ValueHandle leaf_value);
478 TREELITE_DLL int TreeliteTreeBuilderSetLeafVectorNode(
479  TreeBuilderHandle handle, int node_key, const ValueHandle* leaf_vector, size_t leaf_vector_len);
495 TREELITE_DLL int TreeliteCreateModelBuilder(
496  int num_feature, int num_class, int average_tree_output, const char* threshold_type,
497  const char* leaf_output_type, ModelBuilderHandle* out);
506  const char* name,
507  const char* value);
513 TREELITE_DLL int TreeliteDeleteModelBuilder(ModelBuilderHandle handle);
527 TREELITE_DLL int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle,
528  TreeBuilderHandle tree_builder,
529  int index);
537 TREELITE_DLL int TreeliteModelBuilderGetTree(ModelBuilderHandle handle,
538  int index,
539  TreeBuilderHandle *out);
546 TREELITE_DLL int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle,
547  int index);
556  ModelHandle* out);
559 #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:245
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
Definition: c_api.cc:380
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:137
int TreeliteModelBuilderGetTree(ModelBuilderHandle handle, int index, TreeBuilderHandle *out)
Get a reference to a tree in the ensemble.
Definition: c_api.cc:406
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:109
int TreeliteFreeModel(ModelHandle handle)
delete model from memory
Definition: c_api.cc:209
int TreeliteAnnotationSave(AnnotationHandle handle, const char *path)
save branch annotation to a JSON file
Definition: c_api.cc:39
int TreeliteQueryNumTree(ModelHandle handle, size_t *out)
Query the number of trees in the model.
Definition: c_api.cc:231
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:163
int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle, int index)
Remove a tree from the ensemble.
Definition: c_api.cc:416
int TreeliteDeserializeModel(const char *filename, ModelHandle *out)
Deserialize (load) a model object from disk.
Definition: c_api.cc:199
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:116
int TreeliteDeleteModelBuilder(ModelBuilderHandle handle)
Delete a model builder from memory.
Definition: c_api.cc:389
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:329
int TreeliteAnnotationFree(AnnotationHandle handle)
delete branch annotation from memory
Definition: c_api.cc:48
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:317
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:150
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:27
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:262
int TreeliteSetTreeLimit(ModelHandle handle, size_t limit)
keep first N trees of model, limit must smaller than number of trees.
Definition: c_api.cc:252
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:395
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:309
int TreeliteCompilerCreateV2(const char *name, const char *params_json_str, CompilerHandle *out)
Create a compiler with a given name.
Definition: c_api.cc:54
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:287
int TreeliteCreateTreeBuilder(const char *threshold_type, const char *leaf_output_type, TreeBuilderHandle *out)
Create a new tree builder.
Definition: c_api.cc:276
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:424
int TreeliteTreeBuilderSetLeafNode(TreeBuilderHandle handle, int node_key, ValueHandle leaf_value)
Turn an empty node into a leaf node.
Definition: c_api.cc:346
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:61
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:369
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:176
int TreeliteLoadXGBoostModelFromMemoryBuffer(const void *buf, size_t len, ModelHandle *out)
load an XGBoost model from a memory buffer.
Definition: c_api.cc:130
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:102
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:270
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
Definition: c_api.cc:301
int TreeliteQueryNumFeature(ModelHandle handle, size_t *out)
Query the number of features used in the model.
Definition: c_api.cc:238
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:189
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:354
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:123
int TreeliteCompilerFree(CompilerHandle handle)
delete compiler from memory
Definition: c_api.cc:96
int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key)
Create an empty node within a tree.
Definition: c_api.cc:293