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* DMatrixHandle;
25 typedef void* ModelHandle;
27 typedef void* TreeBuilderHandle;
29 typedef void* ModelBuilderHandle;
31 typedef void* AnnotationHandle;
33 typedef void* CompilerHandle;
50 TREELITE_DLL int TreeliteDMatrixCreateFromFile(const char* path,
51  const char* format,
52  int nthread,
53  int verbose,
54  DMatrixHandle* out);
65 TREELITE_DLL int TreeliteDMatrixCreateFromCSR(const float* data,
66  const unsigned* col_ind,
67  const size_t* row_ptr,
68  size_t num_row,
69  size_t num_col,
70  DMatrixHandle* out);
80 TREELITE_DLL int TreeliteDMatrixCreateFromMat(const float* data,
81  size_t num_row,
82  size_t num_col,
83  float missing_value,
84  DMatrixHandle* out);
93 TREELITE_DLL int TreeliteDMatrixGetDimension(DMatrixHandle handle,
94  size_t* out_num_row,
95  size_t* out_num_col,
96  size_t* out_nelem);
97 
105 TREELITE_DLL int TreeliteDMatrixGetPreview(DMatrixHandle handle,
106  const char** out_preview);
107 
117 TREELITE_DLL int TreeliteDMatrixGetArrays(DMatrixHandle handle,
118  const float** out_data,
119  const uint32_t** out_col_ind,
120  const size_t** out_row_ptr);
121 
127 TREELITE_DLL int TreeliteDMatrixFree(DMatrixHandle handle);
145 TREELITE_DLL int TreeliteAnnotateBranch(ModelHandle model,
146  DMatrixHandle dmat,
147  int nthread,
148  int verbose,
149  AnnotationHandle* out);
156 TREELITE_DLL int TreeliteAnnotationSave(AnnotationHandle handle,
157  const char* path);
163 TREELITE_DLL int TreeliteAnnotationFree(AnnotationHandle handle);
177 TREELITE_DLL int TreeliteCompilerCreate(const char* name,
178  CompilerHandle* out);
186 TREELITE_DLL int TreeliteCompilerSetParam(CompilerHandle handle,
187  const char* name,
188  const char* value);
208 TREELITE_DLL int TreeliteCompilerGenerateCode(CompilerHandle compiler,
209  ModelHandle model,
210  int verbose,
211  const char* dirpath);
217 TREELITE_DLL int TreeliteCompilerFree(CompilerHandle handle);
232 TREELITE_DLL int TreeliteLoadLightGBMModel(const char* filename,
233  ModelHandle* out);
241 TREELITE_DLL int TreeliteLoadXGBoostModel(const char* filename,
242  ModelHandle* out);
250 TREELITE_DLL int TreeliteLoadXGBoostModelFromMemoryBuffer(const void* buf,
251  size_t len,
252  ModelHandle* out);
261 TREELITE_DLL int TreeliteLoadProtobufModel(const char* filename,
262  ModelHandle* out);
271 TREELITE_DLL int TreeliteExportProtobufModel(const char* filename,
272  ModelHandle model);
279 TREELITE_DLL int TreeliteQueryNumTree(ModelHandle handle, size_t* out);
286 TREELITE_DLL int TreeliteQueryNumFeature(ModelHandle handle, size_t* out);
293 TREELITE_DLL int TreeliteQueryNumOutputGroups(ModelHandle handle, size_t* out);
299 TREELITE_DLL int TreeliteFreeModel(ModelHandle handle);
312 TREELITE_DLL int TreeliteCreateTreeBuilder(TreeBuilderHandle* out);
318 TREELITE_DLL int TreeliteDeleteTreeBuilder(TreeBuilderHandle handle);
325 TREELITE_DLL int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle,
326  int node_key);
333 TREELITE_DLL int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle,
334  int node_key);
341 TREELITE_DLL int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle,
342  int node_key);
359  TreeBuilderHandle handle,
360  int node_key, unsigned feature_id,
361  const char* opname,
362  double threshold, int default_left,
363  int left_child_key,
364  int right_child_key);
382  TreeBuilderHandle handle,
383  int node_key, unsigned feature_id,
384  const unsigned int* left_categories,
385  size_t left_categories_len,
386  int default_left,
387  int left_child_key,
388  int right_child_key);
397 TREELITE_DLL int TreeliteTreeBuilderSetLeafNode(TreeBuilderHandle handle,
398  int node_key,
399  double leaf_value);
412  int node_key,
413  const double* leaf_vector,
414  size_t leaf_vector_len);
430 TREELITE_DLL int TreeliteCreateModelBuilder(int num_feature,
431  int num_output_group,
432  int random_forest_flag,
433  ModelBuilderHandle* out);
442  const char* name,
443  const char* value);
449 TREELITE_DLL int TreeliteDeleteModelBuilder(ModelBuilderHandle handle);
463 TREELITE_DLL int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle,
464  TreeBuilderHandle tree_builder,
465  int index);
473 TREELITE_DLL int TreeliteModelBuilderGetTree(ModelBuilderHandle handle,
474  int index,
475  TreeBuilderHandle *out);
482 TREELITE_DLL int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle,
483  int index);
492  ModelHandle* out);
495 #endif /* TREELITE_C_API_H_ */
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
Definition: c_api.cc:489
int TreeliteModelBuilderGetTree(ModelBuilderHandle handle, int index, TreeBuilderHandle *out)
Get a reference to a tree in the ensemble.
Definition: c_api.cc:517
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:314
int TreeliteFreeModel(ModelHandle handle)
delete model from memory
Definition: c_api.cc:346
int TreeliteAnnotationSave(AnnotationHandle handle, const char *path)
save branch annotation to a JSON file
Definition: c_api.cc:217
int TreeliteQueryNumTree(ModelHandle handle, size_t *out)
Query the number of trees in the model.
Definition: c_api.cc:352
int TreeliteDMatrixCreateFromCSR(const float *data, const unsigned *col_ind, const size_t *row_ptr, size_t num_row, size_t num_col, DMatrixHandle *out)
create DMatrix from a (in-memory) CSR matrix
Definition: c_api.cc:59
int TreeliteDMatrixFree(DMatrixHandle handle)
delete DMatrix from memory
Definition: c_api.cc:197
int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle, int index)
Remove a tree from the ensemble.
Definition: c_api.cc:528
int TreeliteCompilerSetParam(CompilerHandle handle, const char *name, const char *value)
set a parameter for a compiler
Definition: c_api.cc:239
int TreeliteDeleteModelBuilder(ModelBuilderHandle handle)
Delete a model builder from memory.
Definition: c_api.cc:499
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:430
int TreeliteCreateTreeBuilder(TreeBuilderHandle *out)
Create a new tree builder.
Definition: c_api.cc:373
int TreeliteAnnotationFree(AnnotationHandle handle)
delete branch annotation from memory
Definition: c_api.cc:226
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:203
int TreeliteQueryNumOutputGroups(ModelHandle handle, size_t *out)
Query the number of output groups of the model.
Definition: c_api.cc:366
int TreeliteDMatrixCreateFromMat(const float *data, size_t num_row, size_t num_col, float missing_value, DMatrixHandle *out)
create DMatrix from a (in-memory) dense matrix
Definition: c_api.cc:97
int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle, TreeBuilderHandle tree_builder, int index)
Insert a tree at specified location.
Definition: c_api.cc:505
int TreeliteDMatrixGetArrays(DMatrixHandle handle, const float **out_data, const uint32_t **out_col_ind, const size_t **out_row_ptr)
extract three arrays (data, col_ind, row_ptr) that define a DMatrix.
Definition: c_api.cc:185
int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle, int node_key)
Set a node as the root of a tree.
Definition: c_api.cc:402
void * TreeBuilderHandle
handle to tree builder class
Definition: c_api.h:27
int TreeliteDeleteTreeBuilder(TreeBuilderHandle handle)
Delete a tree builder from memory.
Definition: c_api.cc:380
void * AnnotationHandle
handle to branch annotation data
Definition: c_api.h:31
int TreeliteModelBuilderCommitModel(ModelBuilderHandle handle, ModelHandle *out)
finalize the model and produce the in-memory representation
Definition: c_api.cc:536
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:260
int TreeliteCreateModelBuilder(int num_feature, int num_output_group, int random_forest_flag, ModelBuilderHandle *out)
Create a new model builder.
Definition: c_api.cc:478
int TreeliteTreeBuilderSetLeafVectorNode(TreeBuilderHandle handle, int node_key, const double *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:463
int TreeliteLoadXGBoostModelFromMemoryBuffer(const void *buf, size_t len, ModelHandle *out)
load an XGBoost model from a memory buffer.
Definition: c_api.cc:322
int TreeliteDMatrixGetPreview(DMatrixHandle handle, const char **out_preview)
produce a human-readable preview of a DMatrix Will print first and last 25 non-zero entries...
Definition: c_api.cc:156
void * ModelHandle
handle to a decision tree ensemble model
Definition: c_api.h:25
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:306
void * DMatrixHandle
handle to a data matrix
Definition: c_api.h:23
int TreeliteLoadProtobufModel(const char *filename, ModelHandle *out)
load a model in Protocol Buffers format. Protocol Buffers (google/protobuf) is a language- and platfo...
Definition: c_api.cc:330
int TreeliteTreeBuilderSetNumericalTestNode(TreeBuilderHandle handle, int node_key, unsigned feature_id, const char *opname, double 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:410
int TreeliteCompilerCreate(const char *name, CompilerHandle *out)
create a compiler with a given name
Definition: c_api.cc:232
int TreeliteExportProtobufModel(const char *filename, ModelHandle model)
export a model in Protocol Buffers format. Protocol Buffers (google/protobuf) is a language- and plat...
Definition: c_api.cc:338
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
Definition: c_api.cc:394
int TreeliteQueryNumFeature(ModelHandle handle, size_t *out)
Query the number of features used in the model.
Definition: c_api.cc:359
int TreeliteDMatrixGetDimension(DMatrixHandle handle, size_t *out_num_row, size_t *out_num_col, size_t *out_nelem)
get dimensions of a DMatrix
Definition: c_api.cc:144
void * ModelBuilderHandle
handle to ensemble builder class
Definition: c_api.h:29
void * CompilerHandle
handle to compiler class
Definition: c_api.h:33
int TreeliteTreeBuilderSetLeafNode(TreeBuilderHandle handle, int node_key, double leaf_value)
Turn an empty node into a leaf node.
Definition: c_api.cc:453
int TreeliteDMatrixCreateFromFile(const char *path, const char *format, int nthread, int verbose, DMatrixHandle *out)
create DMatrix from a file
Definition: c_api.cc:48
int TreeliteCompilerFree(CompilerHandle handle)
delete compiler from memory
Definition: c_api.cc:300
int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key)
Create an empty node within a tree.
Definition: c_api.cc:386