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  explicit 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 } // anonymous namespace
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 
218  const char* path) {
219  API_BEGIN();
220  const BranchAnnotator* annotator = static_cast<BranchAnnotator*>(handle);
221  std::unique_ptr<dmlc::Stream> fo(dmlc::Stream::Create(path, "w"));
222  annotator->Save(fo.get());
223  API_END();
224 }
225 
227  API_BEGIN();
228  delete static_cast<BranchAnnotator*>(handle);
229  API_END();
230 }
231 
232 int TreeliteCompilerCreate(const char* name,
233  CompilerHandle* out) {
234  API_BEGIN();
235  *out = static_cast<CompilerHandle>(new CompilerHandleImpl(name));
236  API_END();
237 }
238 
240  const char* name,
241  const char* value) {
242  API_BEGIN();
243  CompilerHandleImpl* impl = static_cast<CompilerHandleImpl*>(handle);
244  auto& cfg_ = impl->cfg;
245  std::string name_(name);
246  std::string value_(value);
247  // check for duplicate parameters
248  auto it = std::find_if(cfg_.begin(), cfg_.end(),
249  [&name_](const std::pair<std::string, std::string>& x) {
250  return x.first == name_;
251  });
252  if (it == cfg_.end()) {
253  cfg_.emplace_back(name_, value_);
254  } else {
255  it->second = value;
256  }
257  API_END();
258 }
259 
261  ModelHandle model,
262  int verbose,
263  const char* dirpath) {
264  API_BEGIN();
265  if (verbose > 0) { // verbose enabled
266  int ret = TreeliteCompilerSetParam(compiler, "verbose",
267  std::to_string(verbose).c_str());
268  if (ret < 0) { // SetParam failed
269  return ret;
270  }
271  }
272  const Model* model_ = static_cast<Model*>(model);
273  CompilerHandleImpl* impl = static_cast<CompilerHandleImpl*>(compiler);
274 
275  // create directory named dirpath
276  const std::string& dirpath_(dirpath);
277  common::filesystem::CreateDirectoryIfNotExist(dirpath);
278 
280  cparam.Init(impl->cfg, dmlc::parameter::kAllMatch);
281 
282  /* compile model */
283  impl->compiler.reset(Compiler::Create(impl->name, cparam));
284  auto compiled_model = impl->compiler->Compile(*model_);
285  if (verbose > 0) {
286  LOG(INFO) << "Code generation finished. Writing code to files...";
287  }
288 
289  for (const auto& it : compiled_model.files) {
290  if (verbose > 0) {
291  LOG(INFO) << "Writing file " << it.first << "...";
292  }
293  const std::string filename_full = dirpath_ + "/" + it.first;
294  common::WriteToFile(filename_full, it.second);
295  }
296 
297  API_END();
298 }
299 
301  API_BEGIN();
302  delete static_cast<CompilerHandleImpl*>(handle);
303  API_END();
304 }
305 
306 int TreeliteLoadLightGBMModel(const char* filename,
307  ModelHandle* out) {
308  API_BEGIN();
309  Model* model = new Model(std::move(frontend::LoadLightGBMModel(filename)));
310  *out = static_cast<ModelHandle>(model);
311  API_END();
312 }
313 
314 int TreeliteLoadXGBoostModel(const char* filename,
315  ModelHandle* out) {
316  API_BEGIN();
317  Model* model = new Model(std::move(frontend::LoadXGBoostModel(filename)));
318  *out = static_cast<ModelHandle>(model);
319  API_END();
320 }
321 
322 int TreeliteLoadXGBoostModelFromMemoryBuffer(const void* buf, size_t len,
323  ModelHandle* out) {
324  API_BEGIN();
325  Model* model = new Model(std::move(frontend::LoadXGBoostModel(buf, len)));
326  *out = static_cast<ModelHandle>(model);
327  API_END();
328 }
329 
330 int TreeliteLoadProtobufModel(const char* filename,
331  ModelHandle* out) {
332  API_BEGIN();
333  Model* model = new Model(std::move(frontend::LoadProtobufModel(filename)));
334  *out = static_cast<ModelHandle>(model);
335  API_END();
336 }
337 
338 int TreeliteExportProtobufModel(const char* filename,
339  ModelHandle model) {
340  API_BEGIN();
341  Model* model_ = static_cast<Model*>(model);
342  frontend::ExportProtobufModel(filename, *model_);
343  API_END();
344 }
345 
347  API_BEGIN();
348  delete static_cast<Model*>(handle);
349  API_END();
350 }
351 
352 int TreeliteQueryNumTree(ModelHandle handle, size_t* out) {
353  API_BEGIN();
354  const Model* model_ = static_cast<Model*>(handle);
355  *out = model_->trees.size();
356  API_END();
357 }
358 
359 int TreeliteQueryNumFeature(ModelHandle handle, size_t* out) {
360  API_BEGIN();
361  const Model* model_ = static_cast<Model*>(handle);
362  *out = static_cast<size_t>(model_->num_feature);
363  API_END();
364 }
365 
366 int TreeliteQueryNumOutputGroups(ModelHandle handle, size_t* out) {
367  API_BEGIN();
368  const Model* model_ = static_cast<Model*>(handle);
369  *out = static_cast<size_t>(model_->num_output_group);
370  API_END();
371 }
372 
373 int TreeliteSetTreeLimit(ModelHandle handle, size_t limit) {
374  API_BEGIN();
375  CHECK_GT(limit, 0) << "limit should be greater than 0!";
376  auto* model_ = static_cast<Model*>(handle);
377  CHECK_GE(model_->trees.size(), limit)
378  << "Model contains less trees(" << model_->trees.size() << ") than limit";
379  model_->trees.resize(limit);
380  API_END();
381 }
382 
384  API_BEGIN();
385  auto builder = new frontend::TreeBuilder();
386  *out = static_cast<TreeBuilderHandle>(builder);
387  API_END();
388 }
389 
391  API_BEGIN();
392  delete static_cast<frontend::TreeBuilder*>(handle);
393  API_END();
394 }
395 
397  API_BEGIN();
398  auto builder = static_cast<frontend::TreeBuilder*>(handle);
399  CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
400  return (builder->CreateNode(node_key)) ? 0 : -1;
401  API_END();
402 }
403 
405  API_BEGIN();
406  auto builder = static_cast<frontend::TreeBuilder*>(handle);
407  CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
408  return (builder->DeleteNode(node_key)) ? 0 : -1;
409  API_END();
410 }
411 
413  API_BEGIN();
414  auto builder = static_cast<frontend::TreeBuilder*>(handle);
415  CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
416  return (builder->SetRootNode(node_key)) ? 0 : -1;
417  API_END();
418 }
419 
421  int node_key, unsigned feature_id,
422  const char* opname,
423  double threshold, int default_left,
424  int left_child_key,
425  int right_child_key) {
426  API_BEGIN();
427  auto builder = static_cast<frontend::TreeBuilder*>(handle);
428  CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
429  CHECK_GT(optable.count(opname), 0)
430  << "No operator `" << opname << "\" exists";
431  return (builder->SetNumericalTestNode(node_key, feature_id,
432  optable.at(opname),
433  static_cast<tl_float>(threshold),
434  (default_left != 0),
435  left_child_key, right_child_key)) \
436  ? 0 : -1;
437  API_END();
438 }
439 
441  TreeBuilderHandle handle,
442  int node_key, unsigned feature_id,
443  const unsigned int* left_categories,
444  size_t left_categories_len,
445  int default_left,
446  int left_child_key,
447  int right_child_key) {
448  API_BEGIN();
449  auto builder = static_cast<frontend::TreeBuilder*>(handle);
450  CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
451  std::vector<uint32_t> vec(left_categories_len);
452  for (size_t i = 0; i < left_categories_len; ++i) {
453  CHECK(left_categories[i] <= std::numeric_limits<uint32_t>::max());
454  vec[i] = static_cast<uint32_t>(left_categories[i]);
455  }
456  return (builder->SetCategoricalTestNode(node_key, feature_id, vec,
457  (default_left != 0),
458  left_child_key, right_child_key)) \
459  ? 0 : -1;
460  API_END();
461 }
462 
464  double leaf_value) {
465  API_BEGIN();
466  auto builder = static_cast<frontend::TreeBuilder*>(handle);
467  CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
468  return (builder->SetLeafNode(node_key, static_cast<tl_float>(leaf_value))) \
469  ? 0 : -1;
470  API_END();
471 }
472 
474  int node_key,
475  const double* leaf_vector,
476  size_t leaf_vector_len) {
477  API_BEGIN();
478  auto builder = static_cast<frontend::TreeBuilder*>(handle);
479  CHECK(builder) << "Detected dangling reference to deleted TreeBuilder object";
480  std::vector<tl_float> vec(leaf_vector_len);
481  for (size_t i = 0; i < leaf_vector_len; ++i) {
482  vec[i] = static_cast<tl_float>(leaf_vector[i]);
483  }
484  return (builder->SetLeafVectorNode(node_key, vec)) ? 0 : -1;
485  API_END();
486 }
487 
488 int TreeliteCreateModelBuilder(int num_feature,
489  int num_output_group,
490  int random_forest_flag,
491  ModelBuilderHandle* out) {
492  API_BEGIN();
493  auto builder = new frontend::ModelBuilder(num_feature, num_output_group,
494  (random_forest_flag != 0));
495  *out = static_cast<ModelBuilderHandle>(builder);
496  API_END();
497 }
498 
500  const char* name,
501  const char* value) {
502  API_BEGIN();
503  auto builder = static_cast<frontend::ModelBuilder*>(handle);
504  CHECK(builder) << "Detected dangling reference to deleted ModelBuilder object";
505  builder->SetModelParam(name, value);
506  API_END();
507 }
508 
510  API_BEGIN();
511  delete static_cast<frontend::ModelBuilder*>(handle);
512  API_END();
513 }
514 
516  TreeBuilderHandle tree_builder_handle,
517  int index) {
518  API_BEGIN();
519  auto model_builder = static_cast<frontend::ModelBuilder*>(handle);
520  CHECK(model_builder) << "Detected dangling reference to deleted ModelBuilder object";
521  auto tree_builder = static_cast<frontend::TreeBuilder*>(tree_builder_handle);
522  CHECK(tree_builder) << "Detected dangling reference to deleted TreeBuilder object";
523  return model_builder->InsertTree(tree_builder, index);
524  API_END();
525 }
526 
528  TreeBuilderHandle *out) {
529  API_BEGIN();
530  auto model_builder = static_cast<frontend::ModelBuilder*>(handle);
531  CHECK(model_builder) << "Detected dangling reference to deleted ModelBuilder object";
532  auto tree_builder = &model_builder->GetTree(index);
533  CHECK(tree_builder) << "Detected dangling reference to deleted TreeBuilder object";
534  *out = static_cast<TreeBuilderHandle>(tree_builder);
535  API_END();
536 }
537 
539  API_BEGIN();
540  auto builder = static_cast<frontend::ModelBuilder*>(handle);
541  CHECK(builder) << "Detected dangling reference to deleted ModelBuilder object";
542  return (builder->DeleteTree(index)) ? 0 : -1;
543  API_END();
544 }
545 
547  ModelHandle* out) {
548  API_BEGIN();
549  auto builder = static_cast<frontend::ModelBuilder*>(handle);
550  CHECK(builder) << "Detected dangling reference to deleted ModelBuilder object";
551  Model* model = new Model();
552  const bool result = builder->CommitModel(model);
553  if (result) {
554  *out = static_cast<ModelHandle>(model);
555  return 0;
556  } else {
557  return -1;
558  }
559  API_END();
560 }
C API of treelite, used for interfacing with other languages This header is excluded from the runtime...
int num_output_group
number of output groups – for multi-class classification Set to 1 for everything else ...
Definition: tree.h:438
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
Definition: c_api.cc:499
branch annotator class
Definition: annotator.h:17
int TreeliteModelBuilderGetTree(ModelBuilderHandle handle, int index, TreeBuilderHandle *out)
Get a reference to a tree in the ensemble.
Definition: c_api.cc:527
std::vector< float > data
feature values
Definition: data.h:18
Collection of front-end methods to load or construct ensemble model.
thin wrapper for tree ensemble model
Definition: tree.h:428
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
tree builder class
Definition: frontend.h:70
std::vector< Tree > trees
member trees
Definition: tree.h:430
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:538
parameters for tree compiler
Definition: param.h:18
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:239
int TreeliteDeleteModelBuilder(ModelBuilderHandle handle)
Delete a model builder from memory.
Definition: c_api.cc:509
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:440
void SetModelParam(const char *name, const char *value)
Set a model parameter.
Definition: builder.cc:261
int TreeliteCreateTreeBuilder(TreeBuilderHandle *out)
Create a new tree builder.
Definition: c_api.cc:383
int TreeliteAnnotationFree(AnnotationHandle handle)
delete branch annotation from memory
Definition: c_api.cc:226
std::vector< uint32_t > col_ind
feature indices
Definition: data.h:20
void Save(dmlc::Stream *fo) const
save branch annotation to a JSON file
Definition: annotator.cc:145
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 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
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
int TreeliteSetTreeLimit(ModelHandle handle, size_t limit)
keep first N trees of model, limit must smaller than number of trees.
Definition: c_api.cc:373
model builder class
Definition: frontend.h:160
int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle, TreeBuilderHandle tree_builder_handle, int index)
Insert a tree at specified location.
Definition: c_api.cc:515
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:24
a simple data matrix in CSR (Compressed Sparse Row) storage
Definition: data.h:16
int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle, int node_key)
Set a node as the root of a tree.
Definition: c_api.cc:412
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:390
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:546
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
void Clear()
clear all data fields
Definition: data.h:33
int TreeliteCreateModelBuilder(int num_feature, int num_output_group, int random_forest_flag, ModelBuilderHandle *out)
Create a new model builder.
Definition: c_api.cc:488
const std::unordered_map< std::string, Operator > optable
conversion table from string to operator, defined in optable.cc
Definition: optable.cc:12
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:473
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
static Compiler * Create(const std::string &name, const compiler::CompilerParam &param)
create a compiler from given name
Definition: compiler.cc:15
double tl_float
float type to be used internally
Definition: base.h:17
void * DMatrixHandle
handle to a data matrix
Definition: c_api.h:23
size_t num_col
number of columns
Definition: data.h:26
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:420
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
Branch annotation tools.
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
Definition: c_api.cc:404
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
size_t nelem
number of nonzero entries
Definition: data.h:28
std::vector< size_t > row_ptr
pointer to row headers; length of [num_row] + 1
Definition: data.h:22
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:463
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
TreeBuilder & GetTree(int index)
Get a reference to a tree in the ensemble.
Definition: builder.cc:319
int num_feature
number of features used for the model. It is assumed that all feature indices are between 0 and [num_...
Definition: tree.h:435
int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key)
Create an empty node within a tree.
Definition: c_api.cc:396