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