Treelite
c_api.cc
Go to the documentation of this file.
1 
8 #include <treelite/annotator.h>
9 #include <treelite/c_api.h>
10 #include <treelite/c_api_error.h>
11 #include <treelite/compiler.h>
13 #include <treelite/data.h>
14 #include <treelite/filesystem.h>
15 #include <treelite/frontend.h>
16 #include <treelite/tree.h>
17 #include <treelite/math.h>
18 #include <treelite/gtil.h>
19 #include <treelite/logging.h>
20 #include <memory>
21 #include <algorithm>
22 #include <fstream>
23 #include <cstdio>
24 
25 using namespace treelite;
26 
28  ModelHandle model, DMatrixHandle dmat, int nthread, int verbose, AnnotationHandle* out) {
29  API_BEGIN();
30  std::unique_ptr<BranchAnnotator> annotator{new BranchAnnotator()};
31  const Model* model_ = static_cast<Model*>(model);
32  const auto* dmat_ = static_cast<const DMatrix*>(dmat);
33  TREELITE_CHECK(dmat_) << "Found a dangling reference to DMatrix";
34  annotator->Annotate(*model_, dmat_, nthread, verbose);
35  *out = static_cast<AnnotationHandle>(annotator.release());
36  API_END();
37 }
38 
40  const char* path) {
41  API_BEGIN();
42  const BranchAnnotator* annotator = static_cast<BranchAnnotator*>(handle);
43  std::ofstream fo(path);
44  annotator->Save(fo);
45  API_END();
46 }
47 
49  API_BEGIN();
50  delete static_cast<BranchAnnotator*>(handle);
51  API_END();
52 }
53 
54 int TreeliteCompilerCreateV2(const char* name, const char* params_json_str, CompilerHandle* out) {
55  API_BEGIN();
56  std::unique_ptr<Compiler> compiler{Compiler::Create(name, params_json_str)};
57  *out = static_cast<CompilerHandle>(compiler.release());
58  API_END();
59 }
60 
62  ModelHandle model,
63  const char* dirpath) {
64  API_BEGIN();
65  const Model* model_ = static_cast<Model*>(model);
66  Compiler* compiler_ = static_cast<Compiler*>(compiler);
67  TREELITE_CHECK(model_);
68  TREELITE_CHECK(compiler_);
69  compiler::CompilerParam param = compiler_->QueryParam();
70 
71  // create directory named dirpath
72  const std::string& dirpath_(dirpath);
73  filesystem::CreateDirectoryIfNotExist(dirpath);
74 
75  /* compile model */
76  auto compiled_model = compiler_->Compile(*model_);
77  if (param.verbose > 0) {
78  TREELITE_LOG(INFO) << "Code generation finished. Writing code to files...";
79  }
80 
81  for (const auto& it : compiled_model.files) {
82  if (param.verbose > 0) {
83  TREELITE_LOG(INFO) << "Writing file " << it.first << "...";
84  }
85  const std::string filename_full = dirpath_ + "/" + it.first;
86  if (it.second.is_binary) {
87  filesystem::WriteToFile(filename_full, it.second.content_binary);
88  } else {
89  filesystem::WriteToFile(filename_full, it.second.content);
90  }
91  }
92 
93  API_END();
94 }
95 
97  API_BEGIN();
98  delete static_cast<Compiler*>(handle);
99  API_END();
100 }
101 
102 int TreeliteLoadLightGBMModel(const char* filename, ModelHandle* out) {
103  API_BEGIN();
104  std::unique_ptr<Model> model = frontend::LoadLightGBMModel(filename);
105  *out = static_cast<ModelHandle>(model.release());
106  API_END();
107 }
108 
109 int TreeliteLoadXGBoostModel(const char* filename, ModelHandle* out) {
110  API_BEGIN();
111  std::unique_ptr<Model> model = frontend::LoadXGBoostModel(filename);
112  *out = static_cast<ModelHandle>(model.release());
113  API_END();
114 }
115 
116 int TreeliteLoadXGBoostJSON(const char* filename, ModelHandle* out) {
117  API_BEGIN();
118  std::unique_ptr<Model> model = frontend::LoadXGBoostJSONModel(filename);
119  *out = static_cast<ModelHandle>(model.release());
120  API_END();
121 }
122 
123 int TreeliteLoadXGBoostJSONString(const char* json_str, size_t length, ModelHandle* out) {
124  API_BEGIN();
125  std::unique_ptr<Model> model = frontend::LoadXGBoostJSONModelString(json_str, length);
126  *out = static_cast<ModelHandle>(model.release());
127  API_END();
128 }
129 
130 int TreeliteLoadXGBoostModelFromMemoryBuffer(const void* buf, size_t len, ModelHandle* out) {
131  API_BEGIN();
132  std::unique_ptr<Model> model = frontend::LoadXGBoostModel(buf, len);
133  *out = static_cast<ModelHandle>(model.release());
134  API_END();
135 }
136 
138  int n_estimators, int n_features, const int64_t* node_count, const int64_t** children_left,
139  const int64_t** children_right, const int64_t** feature, const double** threshold,
140  const double** value, const int64_t** n_node_samples, const double** impurity,
141  ModelHandle* out) {
142  API_BEGIN();
143  std::unique_ptr<Model> model = frontend::LoadSKLearnRandomForestRegressor(
144  n_estimators, n_features, node_count, children_left, children_right, feature, threshold,
145  value, n_node_samples, impurity);
146  *out = static_cast<ModelHandle>(model.release());
147  API_END();
148 }
149 
151  int n_estimators, int n_features, int n_classes, const int64_t* node_count,
152  const int64_t** children_left, const int64_t** children_right, const int64_t** feature,
153  const double** threshold, const double** value, const int64_t** n_node_samples,
154  const double** impurity, ModelHandle* out) {
155  API_BEGIN();
156  std::unique_ptr<Model> model = frontend::LoadSKLearnRandomForestClassifier(
157  n_estimators, n_features, n_classes, node_count, children_left, children_right, feature,
158  threshold, value, n_node_samples, impurity);
159  *out = static_cast<ModelHandle>(model.release());
160  API_END();
161 }
162 
164  int n_estimators, int n_features, const int64_t* node_count, const int64_t** children_left,
165  const int64_t** children_right, const int64_t** feature, const double** threshold,
166  const double** value, const int64_t** n_node_samples, const double** impurity,
167  ModelHandle* out) {
168  API_BEGIN();
169  std::unique_ptr<Model> model = frontend::LoadSKLearnGradientBoostingRegressor(
170  n_estimators, n_features, node_count, children_left, children_right, feature, threshold,
171  value, n_node_samples, impurity);
172  *out = static_cast<ModelHandle>(model.release());
173  API_END();
174 }
175 
177  int n_estimators, int n_features, int n_classes, const int64_t* node_count,
178  const int64_t** children_left, const int64_t** children_right, const int64_t** feature,
179  const double** threshold, const double** value, const int64_t** n_node_samples,
180  const double** impurity, ModelHandle* out) {
181  API_BEGIN();
182  std::unique_ptr<Model> model = frontend::LoadSKLearnGradientBoostingClassifier(
183  n_estimators, n_features, n_classes, node_count, children_left, children_right, feature,
184  threshold, value, n_node_samples, impurity);
185  *out = static_cast<ModelHandle>(model.release());
186  API_END();
187 }
188 
189 int TreeliteSerializeModel(const char* filename, ModelHandle handle) {
190  API_BEGIN();
191  FILE* fp = std::fopen(filename, "wb");
192  TREELITE_CHECK(fp) << "Failed to open file '" << filename << "'";
193  auto* model_ = static_cast<Model*>(handle);
194  model_->SerializeToFile(fp);
195  std::fclose(fp);
196  API_END();
197 }
198 
199 int TreeliteDeserializeModel(const char* filename, ModelHandle* out) {
200  API_BEGIN();
201  FILE* fp = std::fopen(filename, "rb");
202  TREELITE_CHECK(fp) << "Failed to open file '" << filename << "'";
203  std::unique_ptr<Model> model = Model::DeserializeFromFile(fp);
204  std::fclose(fp);
205  *out = static_cast<ModelHandle>(model.release());
206  API_END();
207 }
208 
210  API_BEGIN();
211  delete static_cast<Model*>(handle);
212  API_END();
213 }
214 
215 int TreeliteGTILGetPredictOutputSize(ModelHandle handle, size_t num_row, size_t* out) {
216  API_BEGIN();
217  const auto* model_ = static_cast<const Model*>(handle);
218  *out = gtil::GetPredictOutputSize(model_, num_row);
219  API_END();
220 }
221 
222 int TreeliteGTILPredict(ModelHandle handle, const float* input, size_t num_row, float* output,
223  int pred_transform, size_t* out_result_size) {
224  API_BEGIN();
225  const auto* model_ = static_cast<const Model*>(handle);
226  *out_result_size =
227  gtil::Predict(model_, input, num_row, output, (pred_transform == 1));
228  API_END();
229 }
230 
231 int TreeliteQueryNumTree(ModelHandle handle, size_t* out) {
232  API_BEGIN();
233  const auto* model_ = static_cast<const Model*>(handle);
234  *out = model_->GetNumTree();
235  API_END();
236 }
237 
238 int TreeliteQueryNumFeature(ModelHandle handle, size_t* out) {
239  API_BEGIN();
240  const auto* model_ = static_cast<const Model*>(handle);
241  *out = static_cast<size_t>(model_->num_feature);
242  API_END();
243 }
244 
245 int TreeliteQueryNumClass(ModelHandle handle, size_t* out) {
246  API_BEGIN();
247  const auto* model_ = static_cast<const Model*>(handle);
248  *out = static_cast<size_t>(model_->task_param.num_class);
249  API_END();
250 }
251 
252 int TreeliteSetTreeLimit(ModelHandle handle, size_t limit) {
253  API_BEGIN();
254  TREELITE_CHECK_GT(limit, 0) << "limit should be greater than 0!";
255  auto* model_ = static_cast<Model*>(handle);
256  const size_t num_tree = model_->GetNumTree();
257  TREELITE_CHECK_GE(num_tree, limit) << "Model contains fewer trees(" << num_tree << ") than limit";
258  model_->SetTreeLimit(limit);
259  API_END();
260 }
261 
262 int TreeliteTreeBuilderCreateValue(const void* init_value, const char* type, ValueHandle* out) {
263  API_BEGIN();
264  std::unique_ptr<frontend::Value> value = std::make_unique<frontend::Value>();
265  *value = frontend::Value::Create(init_value, GetTypeInfoByName(type));
266  *out = static_cast<ValueHandle>(value.release());
267  API_END();
268 }
269 
271  API_BEGIN();
272  delete static_cast<frontend::Value*>(handle);
273  API_END();
274 }
275 
276 int TreeliteCreateTreeBuilder(const char* threshold_type, const char* leaf_output_type,
277  TreeBuilderHandle* out) {
278  API_BEGIN();
279  std::unique_ptr<frontend::TreeBuilder> builder{
280  new frontend::TreeBuilder(GetTypeInfoByName(threshold_type),
281  GetTypeInfoByName(leaf_output_type))
282  };
283  *out = static_cast<TreeBuilderHandle>(builder.release());
284  API_END();
285 }
286 
288  API_BEGIN();
289  delete static_cast<frontend::TreeBuilder*>(handle);
290  API_END();
291 }
292 
294  API_BEGIN();
295  auto* builder = static_cast<frontend::TreeBuilder*>(handle);
296  TREELITE_CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
297  builder->CreateNode(node_key);
298  API_END();
299 }
300 
302  API_BEGIN();
303  auto* builder = static_cast<frontend::TreeBuilder*>(handle);
304  TREELITE_CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
305  builder->DeleteNode(node_key);
306  API_END();
307 }
308 
310  API_BEGIN();
311  auto* builder = static_cast<frontend::TreeBuilder*>(handle);
312  TREELITE_CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
313  builder->SetRootNode(node_key);
314  API_END();
315 }
316 
318  TreeBuilderHandle handle, int node_key, unsigned feature_id, const char* opname,
319  ValueHandle threshold, int default_left, int left_child_key, int right_child_key) {
320  API_BEGIN();
321  auto* builder = static_cast<frontend::TreeBuilder*>(handle);
322  TREELITE_CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
323  builder->SetNumericalTestNode(node_key, feature_id, opname,
324  *static_cast<const frontend::Value*>(threshold),
325  (default_left != 0), left_child_key, right_child_key);
326  API_END();
327 }
328 
330  TreeBuilderHandle handle, int node_key, unsigned feature_id,
331  const unsigned int* left_categories, size_t left_categories_len, int default_left,
332  int left_child_key, int right_child_key) {
333  API_BEGIN();
334  auto* builder = static_cast<frontend::TreeBuilder*>(handle);
335  TREELITE_CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
336  std::vector<uint32_t> vec(left_categories_len);
337  for (size_t i = 0; i < left_categories_len; ++i) {
338  TREELITE_CHECK(left_categories[i] <= std::numeric_limits<uint32_t>::max());
339  vec[i] = static_cast<uint32_t>(left_categories[i]);
340  }
341  builder->SetCategoricalTestNode(node_key, feature_id, vec, (default_left != 0),
342  left_child_key, right_child_key);
343  API_END();
344 }
345 
346 int TreeliteTreeBuilderSetLeafNode(TreeBuilderHandle handle, int node_key, ValueHandle leaf_value) {
347  API_BEGIN();
348  auto* builder = static_cast<frontend::TreeBuilder*>(handle);
349  TREELITE_CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
350  builder->SetLeafNode(node_key, *static_cast<const frontend::Value*>(leaf_value));
351  API_END();
352 }
353 
355  const ValueHandle* leaf_vector, size_t leaf_vector_len) {
356  API_BEGIN();
357  auto* builder = static_cast<frontend::TreeBuilder*>(handle);
358  TREELITE_CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
359  std::vector<frontend::Value> vec(leaf_vector_len);
360  TREELITE_CHECK(leaf_vector) << "leaf_vector argument must not be null";
361  for (size_t i = 0; i < leaf_vector_len; ++i) {
362  TREELITE_CHECK(leaf_vector[i]) << "leaf_vector[" << i << "] contains an empty Value handle";
363  vec[i] = *static_cast<const frontend::Value*>(leaf_vector[i]);
364  }
365  builder->SetLeafVectorNode(node_key, vec);
366  API_END();
367 }
368 
370  int num_feature, int num_class, int average_tree_output, const char* threshold_type,
371  const char* leaf_output_type, ModelBuilderHandle* out) {
372  API_BEGIN();
373  std::unique_ptr<frontend::ModelBuilder> builder{new frontend::ModelBuilder(
374  num_feature, num_class, (average_tree_output != 0), GetTypeInfoByName(threshold_type),
375  GetTypeInfoByName(leaf_output_type))};
376  *out = static_cast<ModelBuilderHandle>(builder.release());
377  API_END();
378 }
379 
381  const char* value) {
382  API_BEGIN();
383  auto* builder = static_cast<frontend::ModelBuilder*>(handle);
384  TREELITE_CHECK(builder) << "Detected dangling reference to deleted ModelBuilder object";
385  builder->SetModelParam(name, value);
386  API_END();
387 }
388 
390  API_BEGIN();
391  delete static_cast<frontend::ModelBuilder*>(handle);
392  API_END();
393 }
394 
396  int index) {
397  API_BEGIN();
398  auto* model_builder = static_cast<frontend::ModelBuilder*>(handle);
399  TREELITE_CHECK(model_builder) << "Detected dangling reference to deleted ModelBuilder object";
400  auto* tree_builder = static_cast<frontend::TreeBuilder*>(tree_builder_handle);
401  TREELITE_CHECK(tree_builder) << "Detected dangling reference to deleted TreeBuilder object";
402  return model_builder->InsertTree(tree_builder, index);
403  API_END();
404 }
405 
407  API_BEGIN();
408  auto* model_builder = static_cast<frontend::ModelBuilder*>(handle);
409  TREELITE_CHECK(model_builder) << "Detected dangling reference to deleted ModelBuilder object";
410  auto* tree_builder = model_builder->GetTree(index);
411  TREELITE_CHECK(tree_builder) << "Detected dangling reference to deleted TreeBuilder object";
412  *out = static_cast<TreeBuilderHandle>(tree_builder);
413  API_END();
414 }
415 
417  API_BEGIN();
418  auto* builder = static_cast<frontend::ModelBuilder*>(handle);
419  TREELITE_CHECK(builder) << "Detected dangling reference to deleted ModelBuilder object";
420  builder->DeleteTree(index);
421  API_END();
422 }
423 
425  API_BEGIN();
426  auto* builder = static_cast<frontend::ModelBuilder*>(handle);
427  TREELITE_CHECK(builder) << "Detected dangling reference to deleted ModelBuilder object";
428  std::unique_ptr<Model> model = builder->CommitModel();
429  *out = static_cast<ModelHandle>(model.release());
430  API_END();
431 }
Some useful math utilities.
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
C API of Treelite, used for interfacing with other languages This header is excluded from the runtime...
Parameters for tree compiler.
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
branch annotator class
Definition: annotator.h:21
int TreeliteModelBuilderGetTree(ModelBuilderHandle handle, int index, TreeBuilderHandle *out)
Get a reference to a tree in the ensemble.
Definition: c_api.cc:406
void Save(std::ostream &fo) const
save branch annotation to a JSON file
Definition: annotator.cc:257
Collection of front-end methods to load or construct ensemble model.
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
#define API_BEGIN()
macro to guard beginning and end section of all functions
Definition: c_api_error.h:14
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
tree builder class
Definition: frontend.h:210
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
parameters for tree compiler
Input data structure of Treelite.
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
model structure for tree ensemble
logging facility for Treelite
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
interface of compiler
Definition: compiler.h:53
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
Interface of compiler that compiles a tree ensemble model.
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
model builder class
Definition: frontend.h:299
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_handle, 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
Cross-platform wrapper for common filesystem functions.
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
Error handling for C API.
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
General Tree Inference Library (GTIL), providing a reference implementation for predicting with decis...
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
virtual compiler::CompiledModel Compile(const Model &model)=0
convert tree ensemble model
virtual compiler::CompilerParam QueryParam() const =0
Query the parameters used to intiailize the compiler.
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
TypeInfo GetTypeInfoByName(const std::string &str)
conversion table from string to TypeInfo, defined in tables.cc
Definition: typeinfo.cc:16
int TreeliteTreeBuilderDeleteValue(ValueHandle handle)
Delete a Value object from memory.
Definition: c_api.cc:270
Branch annotation tools.
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
Definition: c_api.cc:301
thin wrapper for tree ensemble model
Definition: tree.h:626
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
int verbose
if >0, produce extra messages
void * CompilerHandle
handle to compiler class
Definition: c_api.h:31
static Compiler * Create(const std::string &name, const char *param_json_str)
create a compiler from given name
Definition: compiler.cc:16
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
#define API_END()
every function starts with API_BEGIN(); and finishes with API_END() or API_END_HANDLE_ERROR ...
Definition: c_api_error.h:17
int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key)
Create an empty node within a tree.
Definition: c_api.cc:293