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/compiler.h>
11 #include <treelite/data.h>
12 #include <treelite/frontend.h>
13 #include <dmlc/json.h>
14 #include <dmlc/thread_local.h>
15 #include <memory>
16 #include <unordered_map>
17 #include <algorithm>
18 #include "./c_api_error.h"
19 #include "../compiler/param.h"
20 #include "../common/filesystem.h"
21 #include "../common/math.h"
22 
23 using namespace treelite;
24 
25 namespace {
26 
27 struct CompilerHandleImpl {
28  std::string name;
29  std::vector<std::pair<std::string, std::string>> cfg;
30  std::unique_ptr<Compiler> compiler;
31  CompilerHandleImpl(const std::string& name)
32  : name(name), cfg(), compiler(nullptr) {}
33  ~CompilerHandleImpl() = default;
34 };
35 
37 struct TreeliteAPIThreadLocalEntry {
39  std::string ret_str;
40 };
41 
42 // define threadlocal store for returning information
43 using TreeliteAPIThreadLocalStore
44  = dmlc::ThreadLocalStore<TreeliteAPIThreadLocalEntry>;
45 
46 } // namespace anonymous
47 
48 int TreeliteDMatrixCreateFromFile(const char* path,
49  const char* format,
50  int nthread,
51  int verbose,
52  DMatrixHandle* out) {
53  API_BEGIN();
54  *out = static_cast<DMatrixHandle>(DMatrix::Create(path, format,
55  nthread, verbose));
56  API_END();
57 }
58 
59 int TreeliteDMatrixCreateFromCSR(const float* data,
60  const unsigned* col_ind,
61  const size_t* row_ptr,
62  size_t num_row,
63  size_t num_col,
64  DMatrixHandle* out) {
65  API_BEGIN();
66  DMatrix* dmat = new DMatrix();
67  dmat->Clear();
68  auto& data_ = dmat->data;
69  auto& col_ind_ = dmat->col_ind;
70  auto& row_ptr_ = dmat->row_ptr;
71  data_.reserve(row_ptr[num_row]);
72  col_ind_.reserve(row_ptr[num_row]);
73  row_ptr_.reserve(num_row + 1);
74  for (size_t i = 0; i < num_row; ++i) {
75  const size_t jbegin = row_ptr[i];
76  const size_t jend = row_ptr[i + 1];
77  for (size_t j = jbegin; j < jend; ++j) {
78  if (!common::math::CheckNAN(data[j])) { // skip NaN
79  data_.push_back(data[j]);
80  CHECK_LT(col_ind[j], std::numeric_limits<uint32_t>::max())
81  << "feature index too big to fit into uint32_t";
82  col_ind_.push_back(static_cast<uint32_t>(col_ind[j]));
83  }
84  }
85  row_ptr_.push_back(data_.size());
86  }
87  data_.shrink_to_fit();
88  col_ind_.shrink_to_fit();
89  dmat->num_row = num_row;
90  dmat->num_col = num_col;
91  dmat->nelem = data_.size(); // some nonzeros may have been deleted as NAN
92 
93  *out = static_cast<DMatrixHandle>(dmat);
94  API_END();
95 }
96 
97 int TreeliteDMatrixCreateFromMat(const float* data,
98  size_t num_row,
99  size_t num_col,
100  float missing_value,
101  DMatrixHandle* out) {
102  const bool nan_missing = common::math::CheckNAN(missing_value);
103  API_BEGIN();
104  CHECK_LT(num_col, std::numeric_limits<uint32_t>::max())
105  << "num_col argument is too big";
106  DMatrix* dmat = new DMatrix();
107  dmat->Clear();
108  auto& data_ = dmat->data;
109  auto& col_ind_ = dmat->col_ind;
110  auto& row_ptr_ = dmat->row_ptr;
111  // make an educated guess for initial sizes,
112  // so as to present initial wave of allocation
113  const size_t guess_size
114  = std::min(std::min(num_row * num_col, num_row * 1000),
115  static_cast<size_t>(64 * 1024 * 1024));
116  data_.reserve(guess_size);
117  col_ind_.reserve(guess_size);
118  row_ptr_.reserve(num_row + 1);
119  const float* row = &data[0]; // points to beginning of each row
120  for (size_t i = 0; i < num_row; ++i, row += num_col) {
121  for (size_t j = 0; j < num_col; ++j) {
122  if (common::math::CheckNAN(row[j])) {
123  CHECK(nan_missing)
124  << "The missing_value argument must be set to NaN if there is any "
125  << "NaN in the matrix.";
126  } else if (nan_missing || row[j] != missing_value) {
127  // row[j] is a valid entry
128  data_.push_back(row[j]);
129  col_ind_.push_back(static_cast<uint32_t>(j));
130  }
131  }
132  row_ptr_.push_back(data_.size());
133  }
134  data_.shrink_to_fit();
135  col_ind_.shrink_to_fit();
136  dmat->num_row = num_row;
137  dmat->num_col = num_col;
138  dmat->nelem = data_.size(); // some nonzeros may have been deleted as NaN
139 
140  *out = static_cast<DMatrixHandle>(dmat);
141  API_END();
142 }
143 
145  size_t* out_num_row,
146  size_t* out_num_col,
147  size_t* out_nelem) {
148  API_BEGIN();
149  const DMatrix* dmat = static_cast<DMatrix*>(handle);
150  *out_num_row = dmat->num_row;
151  *out_num_col = dmat->num_col;
152  *out_nelem = dmat->nelem;
153  API_END();
154 }
155 
157  const char** out_preview) {
158  API_BEGIN();
159  const DMatrix* dmat = static_cast<DMatrix*>(handle);
160  std::string& ret_str = TreeliteAPIThreadLocalStore::Get()->ret_str;
161  std::ostringstream oss;
162  const size_t iend = (dmat->nelem <= 50) ? dmat->nelem : 25;
163  for (size_t i = 0; i < iend; ++i) {
164  const size_t row_ind =
165  std::upper_bound(&dmat->row_ptr[0], &dmat->row_ptr[dmat->num_row + 1], i)
166  - &dmat->row_ptr[0] - 1;
167  oss << " (" << row_ind << ", " << dmat->col_ind[i] << ")\t"
168  << dmat->data[i] << "\n";
169  }
170  if (dmat->nelem > 50) {
171  oss << " :\t:\n";
172  for (size_t i = dmat->nelem - 25; i < dmat->nelem; ++i) {
173  const size_t row_ind =
174  std::upper_bound(&dmat->row_ptr[0], &dmat->row_ptr[dmat->num_row + 1], i)
175  - &dmat->row_ptr[0] - 1;
176  oss << " (" << row_ind << ", " << dmat->col_ind[i] << ")\t"
177  << dmat->data[i] << "\n";
178  }
179  }
180  ret_str = oss.str();
181  *out_preview = ret_str.c_str();
182  API_END();
183 }
184 
186  const float** out_data,
187  const uint32_t** out_col_ind,
188  const size_t** out_row_ptr) {
189  API_BEGIN();
190  const DMatrix* dmat_ = static_cast<DMatrix*>(handle);
191  *out_data = &dmat_->data[0];
192  *out_col_ind = &dmat_->col_ind[0];
193  *out_row_ptr = &dmat_->row_ptr[0];
194  API_END();
195 }
196 
198  API_BEGIN();
199  delete static_cast<DMatrix*>(handle);
200  API_END();
201 }
202 
204  DMatrixHandle dmat,
205  int nthread,
206  int verbose,
207  AnnotationHandle* out) {
208  API_BEGIN();
209  BranchAnnotator* annotator = new BranchAnnotator();
210  const Model* model_ = static_cast<Model*>(model);
211  const DMatrix* dmat_ = static_cast<DMatrix*>(dmat);
212  annotator->Annotate(*model_, dmat_, nthread, verbose);
213  *out = static_cast<AnnotationHandle>(annotator);
214  API_END();
215 }
216 
217 int TreeliteAnnotationLoad(const char* path,
218  AnnotationHandle* out) {
219  API_BEGIN();
220  BranchAnnotator* annotator = new BranchAnnotator();
221  std::unique_ptr<dmlc::Stream> fi(dmlc::Stream::Create(path, "r"));
222  annotator->Load(fi.get());
223  *out = static_cast<AnnotationHandle>(annotator);
224  API_END();
225 }
226 
228  const char* path) {
229  API_BEGIN();
230  const BranchAnnotator* annotator = static_cast<BranchAnnotator*>(handle);
231  std::unique_ptr<dmlc::Stream> fo(dmlc::Stream::Create(path, "w"));
232  annotator->Save(fo.get());
233  API_END();
234 }
235 
237  API_BEGIN();
238  delete static_cast<BranchAnnotator*>(handle);
239  API_END();
240 }
241 
242 int TreeliteCompilerCreate(const char* name,
243  CompilerHandle* out) {
244  API_BEGIN();
245  *out = static_cast<CompilerHandle>(new CompilerHandleImpl(name));
246  API_END();
247 }
248 
250  const char* name,
251  const char* value) {
252  API_BEGIN();
253  CompilerHandleImpl* impl = static_cast<CompilerHandleImpl*>(handle);
254  auto& cfg_ = impl->cfg;
255  std::string name_(name);
256  std::string value_(value);
257  // check for duplicate parameters
258  auto it = std::find_if(cfg_.begin(), cfg_.end(),
259  [&name_](const std::pair<std::string, std::string>& x) {
260  return x.first == name_;
261  });
262  if (it == cfg_.end()) {
263  cfg_.emplace_back(name_, value_);
264  } else {
265  it->second = value;
266  }
267  API_END();
268 }
269 
271  ModelHandle model,
272  int verbose,
273  const char* dirpath) {
274  API_BEGIN();
275  if (verbose > 0) { // verbose enabled
276  int ret = TreeliteCompilerSetParam(compiler, "verbose",
277  std::to_string(verbose).c_str());
278  if (ret < 0) { // SetParam failed
279  return ret;
280  }
281  }
282  const Model* model_ = static_cast<Model*>(model);
283  CompilerHandleImpl* impl = static_cast<CompilerHandleImpl*>(compiler);
284 
285  // create directory named dirpath
286  const std::string& dirpath_(dirpath);
287  common::filesystem::CreateDirectoryIfNotExist(dirpath);
288 
290  cparam.Init(impl->cfg, dmlc::parameter::kAllMatch);
291 
292  /* compile model */
293  // TODO: produce recipe.json
294  impl->compiler.reset(Compiler::Create(impl->name, cparam));
295  auto compiled_model = impl->compiler->Compile(*model_);
296  if (verbose > 0) {
297  LOG(INFO) << "Code generation finished. Writing code to files...";
298  }
299 
300  if (!compiled_model.file_prefix.empty()) {
301  const std::vector<std::string> tokens
302  = common::Split(compiled_model.file_prefix, '/');
303  std::string accum = dirpath_ + "/" + tokens[0];
304  for (size_t i = 0; i < tokens.size(); ++i) {
305  common::filesystem::CreateDirectoryIfNotExist(accum.c_str());
306  if (i < tokens.size() - 1) {
307  accum += "/";
308  accum += tokens[i + 1];
309  }
310  }
311  }
312 
313  for (const auto& it : compiled_model.files) {
314  LOG(INFO) << "Writing file " << it.first << "...";
315  const std::string filename_full = dirpath_ + "/" + it.first;
316  common::WriteToFile(filename_full, it.second);
317  }
318 
319  API_END();
320 }
321 
323  API_BEGIN();
324  delete static_cast<CompilerHandleImpl*>(handle);
325  API_END();
326 }
327 
328 int TreeliteLoadLightGBMModel(const char* filename,
329  ModelHandle* out) {
330  API_BEGIN();
331  Model* model = new Model(std::move(frontend::LoadLightGBMModel(filename)));
332  *out = static_cast<ModelHandle>(model);
333  API_END();
334 }
335 
336 int TreeliteLoadXGBoostModel(const char* filename,
337  ModelHandle* out) {
338  API_BEGIN();
339  Model* model = new Model(std::move(frontend::LoadXGBoostModel(filename)));
340  *out = static_cast<ModelHandle>(model);
341  API_END();
342 }
343 
344 int TreeliteLoadXGBoostModelFromMemoryBuffer(const void* buf, size_t len,
345  ModelHandle* out) {
346  API_BEGIN();
347  Model* model = new Model(std::move(frontend::LoadXGBoostModel(buf, len)));
348  *out = static_cast<ModelHandle>(model);
349  API_END();
350 }
351 
352 int TreeliteLoadProtobufModel(const char* filename,
353  ModelHandle* out) {
354  API_BEGIN();
355  Model* model = new Model(std::move(frontend::LoadProtobufModel(filename)));
356  *out = static_cast<ModelHandle>(model);
357  API_END();
358 }
359 
360 int TreeliteExportXGBoostModel(const char* filename,
361  ModelHandle model,
362  const char* name_obj) {
363  API_BEGIN();
364  Model* model_ = static_cast<Model*>(model);
365  frontend::ExportXGBoostModel(filename, *model_, name_obj);
366  API_END();
367 }
368 
370  API_BEGIN();
371  delete static_cast<Model*>(handle);
372  API_END();
373 }
374 
376  API_BEGIN();
377  auto builder = new frontend::TreeBuilder();
378  *out = static_cast<TreeBuilderHandle>(builder);
379  API_END();
380 }
381 
383  API_BEGIN();
384  delete static_cast<frontend::TreeBuilder*>(handle);
385  API_END();
386 }
387 
389  API_BEGIN();
390  auto builder = static_cast<frontend::TreeBuilder*>(handle);
391  return (builder->CreateNode(node_key)) ? 0 : -1;
392  API_END();
393 }
394 
396  API_BEGIN();
397  auto builder = static_cast<frontend::TreeBuilder*>(handle);
398  return (builder->DeleteNode(node_key)) ? 0 : -1;
399  API_END();
400 }
401 
403  API_BEGIN();
404  auto builder = static_cast<frontend::TreeBuilder*>(handle);
405  return (builder->SetRootNode(node_key)) ? 0 : -1;
406  API_END();
407 }
408 
410  int node_key, unsigned feature_id,
411  const char* opname,
412  float threshold, int default_left,
413  int left_child_key,
414  int right_child_key) {
415  API_BEGIN();
416  auto builder = static_cast<frontend::TreeBuilder*>(handle);
417  CHECK_GT(optable.count(opname), 0)
418  << "No operator `" << opname << "\" exists";
419  return (builder->SetNumericalTestNode(node_key, feature_id,
420  optable.at(opname),
421  static_cast<tl_float>(threshold),
422  (default_left != 0),
423  left_child_key, right_child_key)) \
424  ? 0 : -1;
425  API_END();
426 }
427 
429  TreeBuilderHandle handle,
430  int node_key, unsigned feature_id,
431  const unsigned int* left_categories,
432  size_t left_categories_len,
433  int default_left,
434  int left_child_key,
435  int right_child_key) {
436  API_BEGIN();
437  auto builder = static_cast<frontend::TreeBuilder*>(handle);
438  std::vector<uint32_t> vec(left_categories_len);
439  for (size_t i = 0; i < left_categories_len; ++i) {
440  CHECK(left_categories[i] <= std::numeric_limits<uint32_t>::max());
441  vec[i] = static_cast<uint32_t>(left_categories[i]);
442  }
443  return (builder->SetCategoricalTestNode(node_key, feature_id, vec,
444  (default_left != 0),
445  left_child_key, right_child_key)) \
446  ? 0 : -1;
447  API_END();
448 }
449 
451  float leaf_value) {
452  API_BEGIN();
453  auto builder = static_cast<frontend::TreeBuilder*>(handle);
454  return (builder->SetLeafNode(node_key, static_cast<tl_float>(leaf_value))) \
455  ? 0 : -1;
456  API_END();
457 }
458 
460  int node_key,
461  const float* leaf_vector,
462  size_t leaf_vector_len) {
463  API_BEGIN();
464  auto builder = static_cast<frontend::TreeBuilder*>(handle);
465  std::vector<tl_float> vec(leaf_vector_len);
466  for (size_t i = 0; i < leaf_vector_len; ++i) {
467  vec[i] = static_cast<tl_float>(leaf_vector[i]);
468  }
469  return (builder->SetLeafVectorNode(node_key, vec)) ? 0 : -1;
470  API_END();
471 }
472 
473 int TreeliteCreateModelBuilder(int num_feature,
474  int num_output_group,
475  int random_forest_flag,
476  ModelBuilderHandle* out) {
477  API_BEGIN();
478  auto builder = new frontend::ModelBuilder(num_feature, num_output_group,
479  (random_forest_flag != 0));
480  *out = static_cast<ModelBuilderHandle>(builder);
481  API_END();
482 }
483 
485  const char* name,
486  const char* value) {
487  API_BEGIN();
488  auto builder = static_cast<frontend::ModelBuilder*>(handle);
489  builder->SetModelParam(name, value);
490  API_END();
491 }
492 
494  API_BEGIN();
495  delete static_cast<frontend::ModelBuilder*>(handle);
496  API_END();
497 }
498 
500  TreeBuilderHandle tree_builder_handle,
501  int index) {
502  API_BEGIN();
503  auto model_builder = static_cast<frontend::ModelBuilder*>(handle);
504  auto tree_builder = static_cast<frontend::TreeBuilder*>(tree_builder_handle);
505  return model_builder->InsertTree(tree_builder, index);
506  API_END();
507 }
508 
510  TreeBuilderHandle *out) {
511  API_BEGIN();
512  auto model_builder = static_cast<frontend::ModelBuilder*>(handle);
513  auto tree_builder = &model_builder->GetTree(index);
514  *out = static_cast<TreeBuilderHandle>(tree_builder);
515  API_END();
516 }
517 
519  API_BEGIN();
520  auto builder = static_cast<frontend::ModelBuilder*>(handle);
521  return (builder->DeleteTree(index)) ? 0 : -1;
522  API_END();
523 }
524 
526  ModelHandle* out) {
527  API_BEGIN();
528  auto builder = static_cast<frontend::ModelBuilder*>(handle);
529  Model* model = new Model();
530  const bool result = builder->CommitModel(model);
531  if (result) {
532  *out = static_cast<ModelHandle>(model);
533  return 0;
534  } else {
535  return -1;
536  }
537  API_END();
538 }
C API of treelite, used for interfacing with other languages This header is excluded from the runtime...
int TreeliteTreeBuilderSetNumericalTestNode(TreeBuilderHandle handle, int node_key, unsigned feature_id, const char *opname, float 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:409
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
Definition: c_api.cc:484
branch annotator class
Definition: annotator.h:16
int TreeliteModelBuilderGetTree(ModelBuilderHandle handle, int index, TreeBuilderHandle *out)
Get a reference to a tree in the ensemble.
Definition: c_api.cc:509
std::vector< float > data
feature values
Definition: data.h:17
Collection of front-end methods to load or construct ensemble model.
thin wrapper for tree ensemble model
Definition: tree.h:351
float tl_float
float type to be used internally
Definition: base.h:17
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:336
#define API_BEGIN()
macro to guard beginning and end section of all functions
Definition: c_api_error.h:15
int TreeliteFreeModel(ModelHandle handle)
delete model from memory
Definition: c_api.cc:369
int TreeliteAnnotationSave(AnnotationHandle handle, const char *path)
save branch annotation to a JSON file
Definition: c_api.cc:227
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
tree builder class
Definition: frontend.h:71
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:518
parameters for tree compiler
Definition: param.h:16
Input data structure of treelite.
void Annotate(const Model &model, const DMatrix *dmat, int nthread, int verbose)
annotate branches in a given model using frequency patterns in the training data. The annotation can ...
Definition: annotator.cc:95
int TreeliteCompilerSetParam(CompilerHandle handle, const char *name, const char *value)
set a parameter for a compiler
Definition: c_api.cc:249
int TreeliteDeleteModelBuilder(ModelBuilderHandle handle)
Delete a model builder from memory.
Definition: c_api.cc:493
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:428
void SetModelParam(const char *name, const char *value)
Set a model parameter.
Definition: builder.cc:263
int TreeliteCreateTreeBuilder(TreeBuilderHandle *out)
Create a new tree builder.
Definition: c_api.cc:375
int TreeliteAnnotationFree(AnnotationHandle handle)
delete branch annotation from memory
Definition: c_api.cc:236
std::vector< uint32_t > col_ind
feature indices
Definition: data.h:19
static DMatrix * Create(const char *filename, const char *format, int nthread, int verbose)
construct a new DMatrix from a file
Definition: data.cc:17
int TreeliteTreeBuilderSetLeafVectorNode(TreeBuilderHandle handle, int node_key, const float *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:459
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
Interface of compiler that compiles a tree ensemble model.
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
model builder class
Definition: frontend.h:161
int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle, TreeBuilderHandle tree_builder_handle, int index)
Insert a tree at specified location.
Definition: c_api.cc:499
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
size_t num_row
number of rows
Definition: data.h:23
a simple data matrix in CSR (Compressed Sparse Row) storage
Definition: data.h:15
void Load(dmlc::Stream *fi)
load branch annotation from a JSON file
Definition: annotator.cc:138
void Save(dmlc::Stream *fo) const
save branch annotation to a JSON file
Definition: annotator.cc:145
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
Error handling for C API.
int TreeliteDeleteTreeBuilder(TreeBuilderHandle handle)
Delete a tree builder from memory.
Definition: c_api.cc:382
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:525
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:270
void Clear()
clear all data fields
Definition: data.h:32
int TreeliteCreateModelBuilder(int num_feature, int num_output_group, int random_forest_flag, ModelBuilderHandle *out)
Create a new model builder.
Definition: c_api.cc:473
int TreeliteAnnotationLoad(const char *path, AnnotationHandle *out)
load branch annotation from a JSON file
Definition: c_api.cc:217
const std::unordered_map< std::string, Operator > optable
conversion table from string to operator, defined in optable.cc
Definition: optable.cc:12
int TreeliteLoadXGBoostModelFromMemoryBuffer(const void *buf, size_t len, ModelHandle *out)
load an XGBoost model from a memory buffer.
Definition: c_api.cc:344
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:328
static Compiler * Create(const std::string &name, const compiler::CompilerParam &param)
create a compiler from given name
Definition: compiler.cc:15
void * DMatrixHandle
handle to a data matrix
Definition: c_api.h:23
size_t num_col
number of columns
Definition: data.h:25
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:352
int TreeliteCompilerCreate(const char *name, CompilerHandle *out)
create a compiler with a given name
Definition: c_api.cc:242
int TreeliteTreeBuilderSetLeafNode(TreeBuilderHandle handle, int node_key, float leaf_value)
Turn an empty node into a leaf node.
Definition: c_api.cc:450
Branch annotation tools.
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
Definition: c_api.cc:395
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
size_t nelem
number of nonzero entries
Definition: data.h:27
std::vector< size_t > row_ptr
pointer to row headers; length of [num_row] + 1
Definition: data.h:21
void * CompilerHandle
handle to compiler class
Definition: c_api.h:33
int TreeliteExportXGBoostModel(const char *filename, ModelHandle model, const char *name_obj)
(EXPERIMENTAL FEATURE) export a model in XGBoost format. The exported model can be read by XGBoost (d...
Definition: c_api.cc:360
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:322
TreeBuilder & GetTree(int index)
Get a reference to a tree in the ensemble.
Definition: builder.cc:321
#define API_END()
every function starts with API_BEGIN(); and finishes with API_END() or API_END_HANDLE_ERROR ...
Definition: c_api_error.h:18
int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key)
Create an empty node within a tree.
Definition: c_api.cc:388