treelite
c_api.h
Go to the documentation of this file.
1 
8 /* Note: Make sure to use slash-asterisk form of comments in this file
9  (like this one). Do not use double-slash (//). */
10 
11 #ifndef TREELITE_C_API_H_
12 #define TREELITE_C_API_H_
13 
14 #ifdef __cplusplus
15 #define TREELITE_EXTERN_C extern "C"
16 #include <cstdint>
17 #include <cstdio>
18 #else
19 #define TREELITE_EXTERN_C
20 #include <stdint.h>
21 #include <stdio.h>
22 #endif
23 
24 /* Special symbols for DLL library on Windows */
25 #if defined(_MSC_VER) || defined(_WIN32)
26 #define TREELITE_DLL TREELITE_EXTERN_C __declspec(dllexport)
27 #define TREELITE_DLL_STRUCT TREELITE_EXTERN_C struct __declspec(dllexport)
28 #else
29 #define TREELITE_DLL TREELITE_EXTERN_C __attribute__((visibility("default")))
30 #define TREELITE_DLL_STRUCT TREELITE_EXTERN_C struct __attribute__((visibility("default")))
31 #endif
32 
38 typedef void* TreeliteModelHandle;
54  void* buf;
55  char* format;
56  size_t itemsize;
57  size_t nitem;
58 };
61 #ifndef __cplusplus
63 #endif
64 
78  char const* filename, char const* config_json, TreeliteModelHandle* out);
89  void const* buf, size_t len, char const* config_json, TreeliteModelHandle* out);
99  char const* filename, char const* config_json, TreeliteModelHandle* out);
110  char const* json_str, size_t length, char const* config_json, TreeliteModelHandle* out);
121  char const* filename, char const* config_json, TreeliteModelHandle* out);
132  char const* model_str, char const* config_json, TreeliteModelHandle* out);
167 TREELITE_DLL int TreeliteLoadSKLearnRandomForestRegressor(int n_estimators, int n_features,
168  int n_targets, int64_t const* node_count, int64_t const** children_left,
169  int64_t const** children_right, int64_t const** feature, double const** threshold,
170  double const** value, int64_t const** n_node_samples, double const** weighted_n_node_samples,
171  double const** impurity, TreeliteModelHandle* out);
198 TREELITE_DLL int TreeliteLoadSKLearnIsolationForest(int n_estimators, int n_features,
199  int64_t const* node_count, int64_t const** children_left, int64_t const** children_right,
200  int64_t const** feature, double const** threshold, double const** value,
201  int64_t const** n_node_samples, double const** weighted_n_node_samples, double const** impurity,
202  double ratio_c, TreeliteModelHandle* out);
232 TREELITE_DLL int TreeliteLoadSKLearnRandomForestClassifier(int n_estimators, int n_features,
233  int n_targets, int32_t const* n_classes, int64_t const* node_count,
234  int64_t const** children_left, int64_t const** children_right, int64_t const** feature,
235  double const** threshold, double const** value, int64_t const** n_node_samples,
236  double const** weighted_n_node_samples, double const** impurity, TreeliteModelHandle* out);
268  int64_t const* node_count, int64_t const** children_left, int64_t const** children_right,
269  int64_t const** feature, double const** threshold, double const** value,
270  int64_t const** n_node_samples, double const** weighted_n_node_samples, double const** impurity,
271  double const* base_scores, TreeliteModelHandle* out);
304  int n_classes, int64_t const* node_count, int64_t const** children_left,
305  int64_t const** children_right, int64_t const** feature, double const** threshold,
306  double const** value, int64_t const** n_node_samples, double const** weighted_n_node_samples,
307  double const** impurity, double const* base_scores, TreeliteModelHandle* out);
308 
339  int64_t const* node_count, void const** nodes, int expected_sizeof_node_struct,
340  uint32_t n_categorical_splits, uint32_t const** raw_left_cat_bitsets,
341  uint32_t const* known_cat_bitsets, uint32_t const* known_cat_bitsets_offset_map,
342  int32_t const* features_map, int64_t const** categories_map, double const* base_scores,
343  TreeliteModelHandle* out);
344 
378  int n_classes, int64_t const* node_count, void const** nodes, int expected_sizeof_node_struct,
379  uint32_t n_categorical_splits, uint32_t const** raw_left_cat_bitsets,
380  uint32_t const* known_cat_bitsets, uint32_t const* known_cat_bitsets_offset_map,
381  int32_t const* features_map, int64_t const** categories_map, double const* base_scores,
382  TreeliteModelHandle* out);
444  TreeliteModelBuilderHandle model_builder, int node_key);
466  int32_t split_index, double threshold, int default_left, char const* cmp, int left_child_key,
467  int right_child_key);
483  int32_t split_index, int default_left, uint32_t const* category_list, size_t category_list_len,
484  int category_list_right_child, int left_child_key, int right_child_key);
492  TreeliteModelBuilderHandle model_builder, double leaf_value);
501  TreeliteModelBuilderHandle model_builder, float const* leaf_vector, size_t leaf_vector_len);
510  TreeliteModelBuilderHandle model_builder, double const* leaf_vector, size_t leaf_vector_len);
525  TreeliteModelBuilderHandle model_builder, uint64_t data_count);
534  TreeliteModelBuilderHandle model_builder, double sum_hess);
541  TreeliteModelBuilderHandle model_builder, TreeliteModelHandle* out);
556  TreeliteModelHandle handle, int pretty_print, char const** out_json_str);
563 TREELITE_DLL int TreeliteGetInputType(TreeliteModelHandle model, char const** out_str);
570 TREELITE_DLL int TreeliteGetOutputType(TreeliteModelHandle model, char const** out_str);
593  TreeliteModelHandle const* objs, size_t len, TreeliteModelHandle* out);
630  TreeliteModelHandle handle, char const** out_bytes, size_t* out_bytes_len);
640  char const* bytes, size_t bytes_len, TreeliteModelHandle* out);
641 
650  TreeliteModelHandle handle, TreelitePyBufferFrame** out_frames, size_t* out_num_frames);
659  TreelitePyBufferFrame* frames, size_t num_frames, TreeliteModelHandle* out);
682 
689 
701  TreeliteGTILConfigHandle config, uint64_t const** out, uint64_t* out_ndim);
702 
715  char const* input_type, uint64_t num_row, void* output, TreeliteGTILConfigHandle config);
716 
735  char const* input_type, uint64_t const* col_ind, uint64_t const* row_ptr, uint64_t num_row,
736  void* output, TreeliteGTILConfigHandle config);
737 
756  TreeliteModelHandle model, char const* name, TreelitePyBufferFrame* out_frame);
767 TREELITE_DLL int TreeliteGetTreeField(TreeliteModelHandle model, uint64_t tree_id, char const* name,
768  TreelitePyBufferFrame* out_frame);
779  TreeliteModelHandle model, char const* name, TreelitePyBufferFrame frame);
791  TreeliteModelHandle model, uint64_t tree_id, char const* name, TreelitePyBufferFrame frame);
800 
808 TREELITE_DLL int TreeliteRegisterLogCallback(void (*callback)(char const*));
809 
816 TREELITE_DLL int TreeliteRegisterWarningCallback(void (*callback)(char const*));
817 
818 #ifdef __cplusplus
819 extern "C" {
820 extern char const* TREELITE_VERSION;
821 }
822 #else
823 extern char const* TREELITE_VERSION;
824 #endif
825 
826 #endif // TREELITE_C_API_H_
char const * TreeliteGetLastError(void)
Display last error; can be called by multiple threads Note. Each thread will get the last error occur...
int TreeliteRegisterLogCallback(void(*callback)(char const *))
Register callback function for LOG(INFO) messages – helpful messages that are not errors....
char const * TREELITE_VERSION
int TreeliteRegisterWarningCallback(void(*callback)(char const *))
Register callback function for LOG(WARNING) messages Note: This function can be called by multiple th...
#define TREELITE_DLL_STRUCT
Definition: c_api.h:30
#define TREELITE_DLL
Definition: c_api.h:29
int TreeliteGetHeaderField(TreeliteModelHandle model, char const *name, TreelitePyBufferFrame *out_frame)
Get a field in the header.
int TreeliteGetTreeField(TreeliteModelHandle model, uint64_t tree_id, char const *name, TreelitePyBufferFrame *out_frame)
Get a field in a tree.
int TreeliteSetTreeField(TreeliteModelHandle model, uint64_t tree_id, char const *name, TreelitePyBufferFrame frame)
Set a field in a tree.
int TreeliteSetHeaderField(TreeliteModelHandle model, char const *name, TreelitePyBufferFrame frame)
Set a field in the header.
size_t nitem
Definition: c_api.h:57
char * format
Definition: c_api.h:55
struct TreelitePyBufferFrame TreelitePyBufferFrame
Represent a frame in the Python buffer protocol (PEP 3118).
Definition: c_api.h:62
size_t itemsize
Definition: c_api.h:56
int TreeliteGTILGetOutputShape(TreeliteModelHandle model, uint64_t num_row, TreeliteGTILConfigHandle config, uint64_t const **out, uint64_t *out_ndim)
Given a data matrix, query the necessary shape of array to hold predictions for all data points.
int TreeliteGTILParseConfig(char const *config_json, TreeliteGTILConfigHandle *out)
Load a configuration for GTIL predictor from a JSON string.
int TreeliteGTILPredictSparse(TreeliteModelHandle model, void const *data, char const *input_type, uint64_t const *col_ind, uint64_t const *row_ptr, uint64_t num_row, void *output, TreeliteGTILConfigHandle config)
Predict with sparse data with CSR (compressed sparse row) layout.
int TreeliteGTILPredict(TreeliteModelHandle model, void const *input, char const *input_type, uint64_t num_row, void *output, TreeliteGTILConfigHandle config)
Predict with a 2D dense array.
int TreeliteGTILDeleteConfig(TreeliteGTILConfigHandle handle)
Delete a GTIL configuration from memory.
int TreeliteDeleteModelBuilder(TreeliteModelBuilderHandle model_builder)
Delete model builder object from memory.
int TreeliteModelBuilderEndTree(TreeliteModelBuilderHandle model_builder)
End the current tree.
int TreeliteModelBuilderGain(TreeliteModelBuilderHandle model_builder, double gain)
Specify the gain (loss reduction) that's resulted from the current split.
int TreeliteModelBuilderStartNode(TreeliteModelBuilderHandle model_builder, int node_key)
Start a new node.
int TreeliteGetModelBuilder(char const *json_str, TreeliteModelBuilderHandle *out)
Initialize a model builder object from a JSON string.
int TreeliteModelBuilderLeafVectorFloat64(TreeliteModelBuilderHandle model_builder, double const *leaf_vector, size_t leaf_vector_len)
Declare the current node as a leaf node with a vector output (float64)
int TreeliteModelBuilderLeafVectorFloat32(TreeliteModelBuilderHandle model_builder, float const *leaf_vector, size_t leaf_vector_len)
Declare the current node as a leaf node with a vector output (float32)
int TreeliteModelBuilderNumericalTest(TreeliteModelBuilderHandle model_builder, int32_t split_index, double threshold, int default_left, char const *cmp, int left_child_key, int right_child_key)
Declare the current node as a numerical test node, where the test is of form [feature value] [cmp] [t...
int TreeliteModelBuilderLeafScalar(TreeliteModelBuilderHandle model_builder, double leaf_value)
Declare the current node as a leaf node with a scalar output.
int TreeliteModelBuilderDataCount(TreeliteModelBuilderHandle model_builder, uint64_t data_count)
Specify the number of data points (samples) that are mapped to the current node.
int TreeliteModelBuilderStartTree(TreeliteModelBuilderHandle model_builder)
Start a new tree.
int TreeliteModelBuilderCommitModel(TreeliteModelBuilderHandle model_builder, TreeliteModelHandle *out)
Conclude model building and obtain the final model object.
int TreeliteModelBuilderSumHess(TreeliteModelBuilderHandle model_builder, double sum_hess)
Specify the weighted sample count or the sum of Hessians for the data points that are mapped to the c...
int TreeliteModelBuilderEndNode(TreeliteModelBuilderHandle model_builder)
End the current node.
int TreeliteModelBuilderCategoricalTest(TreeliteModelBuilderHandle model_builder, int32_t split_index, int default_left, uint32_t const *category_list, size_t category_list_len, int category_list_right_child, int left_child_key, int right_child_key)
Declare the current node as a categorical test node, where the test is of form [feature value] \in [c...
int TreeliteLoadLightGBMModel(char const *filename, char const *config_json, TreeliteModelHandle *out)
Load a model file generated by LightGBM (Microsoft/LightGBM). The model file must contain a decision ...
int TreeliteLoadXGBoostModel(char const *filename, char const *config_json, TreeliteModelHandle *out)
Load a model file generated by XGBoost (dmlc/xgboost), stored in the JSON format.
int TreeliteLoadLightGBMModelFromString(char const *model_str, char const *config_json, TreeliteModelHandle *out)
Load a LightGBM model from a string. The string should be created with the model_to_string() method i...
int TreeliteLoadXGBoostModelLegacyBinaryFromMemoryBuffer(void const *buf, size_t len, char const *config_json, TreeliteModelHandle *out)
Load an XGBoost model from a memory buffer using the legacy binary format.
int TreeliteLoadXGBoostModelLegacyBinary(char const *filename, char const *config_json, TreeliteModelHandle *out)
Load a model file generated by XGBoost (dmlc/xgboost), stored in the legacy binary format.
int TreeliteLoadXGBoostModelFromString(char const *json_str, size_t length, char const *config_json, TreeliteModelHandle *out)
Load an XGBoost model from a JSON string.
int TreeliteFreeModel(TreeliteModelHandle handle)
Delete model from memory.
int TreeliteDumpAsJSON(TreeliteModelHandle handle, int pretty_print, char const **out_json_str)
Dump a model object as a JSON string.
int TreeliteConcatenateModelObjects(TreeliteModelHandle const *objs, size_t len, TreeliteModelHandle *out)
Concatenate multiple model objects into a single model object by copying all member trees into the de...
int TreeliteGetOutputType(TreeliteModelHandle model, char const **out_str)
Query the output type of a Treelite model object.
int TreeliteQueryNumFeature(TreeliteModelHandle model, int *out)
Query the number of features used in the model.
int TreeliteGetInputType(TreeliteModelHandle model, char const **out_str)
Query the input type of a Treelite model object.
int TreeliteQueryNumTree(TreeliteModelHandle model, size_t *out)
Query the number of trees in the model.
void * TreeliteGTILConfigHandle
Handle to a configuration of GTIL predictor.
Definition: c_api.h:42
void * TreeliteModelBuilderHandle
Handle to a model builder object.
Definition: c_api.h:40
void * TreeliteModelHandle
Handle to a decision tree ensemble model.
Definition: c_api.h:38
int TreeliteSerializeModelToPyBuffer(TreeliteModelHandle handle, TreelitePyBufferFrame **out_frames, size_t *out_num_frames)
Serialize a model object using the Python buffer protocol (PEP 3118).
int TreeliteDeserializeModelFromPyBuffer(TreelitePyBufferFrame *frames, size_t num_frames, TreeliteModelHandle *out)
Deserialize a model object using the Python buffer protocol (PEP 3118).
int TreeliteSerializeModelToBytes(TreeliteModelHandle handle, char const **out_bytes, size_t *out_bytes_len)
Serialize (persist) a model object to a byte sequence.
int TreeliteDeserializeModelFromFile(char const *filename, TreeliteModelHandle *out)
Deserialize (load) a model object from disk.
int TreeliteSerializeModelToFile(TreeliteModelHandle handle, char const *filename)
Serialize (persist) a model object to disk.
int TreeliteDeserializeModelFromBytes(char const *bytes, size_t bytes_len, TreeliteModelHandle *out)
Deserialize (load) a model object from a byte sequence.
int TreeliteLoadSKLearnHistGradientBoostingRegressor(int n_iter, int n_features, int64_t const *node_count, void const **nodes, int expected_sizeof_node_struct, uint32_t n_categorical_splits, uint32_t const **raw_left_cat_bitsets, uint32_t const *known_cat_bitsets, uint32_t const *known_cat_bitsets_offset_map, int32_t const *features_map, int64_t const **categories_map, double const *base_scores, TreeliteModelHandle *out)
Load a scikit-learn HistGradientBoostingRegressor model from a collection of arrays....
int TreeliteLoadSKLearnRandomForestRegressor(int n_estimators, int n_features, int n_targets, int64_t const *node_count, int64_t const **children_left, int64_t const **children_right, int64_t const **feature, double const **threshold, double const **value, int64_t const **n_node_samples, double const **weighted_n_node_samples, double const **impurity, TreeliteModelHandle *out)
Load a scikit-learn RandomForestRegressor model from a collection of arrays. Refer to https://scikit-...
int TreeliteLoadSKLearnIsolationForest(int n_estimators, int n_features, int64_t const *node_count, int64_t const **children_left, int64_t const **children_right, int64_t const **feature, double const **threshold, double const **value, int64_t const **n_node_samples, double const **weighted_n_node_samples, double const **impurity, double ratio_c, TreeliteModelHandle *out)
Load a scikit-learn IsolationForest model from a collection of arrays. Refer to https://scikit-learn....
int TreeliteLoadSKLearnHistGradientBoostingClassifier(int n_iter, int n_features, int n_classes, int64_t const *node_count, void const **nodes, int expected_sizeof_node_struct, uint32_t n_categorical_splits, uint32_t const **raw_left_cat_bitsets, uint32_t const *known_cat_bitsets, uint32_t const *known_cat_bitsets_offset_map, int32_t const *features_map, int64_t const **categories_map, double const *base_scores, TreeliteModelHandle *out)
Load a scikit-learn HistGradientBoostingClassifier model from a collection of arrays....
int TreeliteLoadSKLearnRandomForestClassifier(int n_estimators, int n_features, int n_targets, int32_t const *n_classes, int64_t const *node_count, int64_t const **children_left, int64_t const **children_right, int64_t const **feature, double const **threshold, double const **value, int64_t const **n_node_samples, double const **weighted_n_node_samples, double const **impurity, TreeliteModelHandle *out)
Load a scikit-learn RandomForestClassifier model from a collection of arrays. Refer to https://scikit...
int TreeliteLoadSKLearnGradientBoostingRegressor(int n_iter, int n_features, int64_t const *node_count, int64_t const **children_left, int64_t const **children_right, int64_t const **feature, double const **threshold, double const **value, int64_t const **n_node_samples, double const **weighted_n_node_samples, double const **impurity, double const *base_scores, TreeliteModelHandle *out)
Load a scikit-learn GradientBoostingRegressor model from a collection of arrays. Refer to https://sci...
int TreeliteLoadSKLearnGradientBoostingClassifier(int n_iter, int n_features, int n_classes, int64_t const *node_count, int64_t const **children_left, int64_t const **children_right, int64_t const **feature, double const **threshold, double const **value, int64_t const **n_node_samples, double const **weighted_n_node_samples, double const **impurity, double const *base_scores, TreeliteModelHandle *out)
Load a scikit-learn GradientBoostingClassifier model from a collection of arrays. Refer to https://sc...