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  std::vector<std::string> object_list;
324  if (semantic_model.units.size() == 1) { // single file (translation unit)
325  const std::string filename = basename + ".c";
326  const std::string filename_full = dirpath_ + "/" + filename;
327  const std::string objname = basename + ".o";
328  if (verbose > 0) {
329  LOG(INFO) << "Writing " << filename_full << " ...";
330  }
331  auto lines = semantic_model.units[0].Compile(header_filename);
332  source_list.push_back({ {"name", basename},
333  {"length", std::to_string(lines.size())} });
334  object_list.push_back(objname);
335  common::WriteToFile(filename_full, lines);
336  } else { // multiple files (translation units)
337  for (size_t i = 0; i < semantic_model.units.size(); ++i) {
338  const std::string filename = basename + std::to_string(i) + ".c";
339  const std::string filename_full = dirpath_ + "/" + filename;
340  const std::string objname = basename + std::to_string(i) + ".o";
341  if (verbose > 0) {
342  LOG(INFO) << "Writing " << filename_full << " ...";
343  }
344  auto lines = semantic_model.units[i].Compile(header_filename);
345  source_list.push_back({ {"name", basename + std::to_string(i)},
346  {"length", std::to_string(lines.size())} });
347  object_list.push_back(objname);
348  common::WriteToFile(filename_full, lines);
349  }
350  }
351  /* write build recipe, to be used by Python binding */
352  {
353  const std::string recipe_name = dirpath_ + "/recipe.json";
354  if (verbose > 0) {
355  LOG(INFO) << "Writing " << recipe_name << " ...";
356  }
357  std::unique_ptr<dmlc::Stream> fo(
358  dmlc::Stream::Create(recipe_name.c_str(), "w"));
359  dmlc::ostream os(fo.get());
360  auto writer = common::make_unique<dmlc::JSONWriter>(&os);
361  writer->BeginObject();
362  writer->WriteObjectKeyValue("target", basename);
363  writer->WriteObjectKeyValue("sources", source_list);
364  writer->EndObject();
365  // force flush before fo destruct.
366  os.set_stream(nullptr);
367  }
368  /* write Makefile if on Linux */
369 #ifdef __linux__
370  {
371  std::string library_name = basename + ".so";
372  std::ostringstream oss;
373  oss << "all: " << library_name << std::endl << std::endl
374  << library_name << ": ";
375  for (const auto& e : object_list) {
376  oss << e << " ";
377  }
378  oss << std::endl
379  << "\tgcc -shared -O3 -o $@ $? -fPIC -std=c99 -flto"
380  << std::endl << std::endl;
381  for (size_t i = 0; i < object_list.size(); ++i) {
382  oss << object_list[i] << ": "
383  << source_list[i]["name"] << ".c" << std::endl
384  << "\tgcc -c -O3 -o $@ $? -fPIC -std=c99 -flto" << std::endl;
385  }
386  oss << std::endl
387  << "clean:" << std::endl
388  << "\trm -fv " << library_name << " ";
389  for (const auto& e : object_list) {
390  oss << e << " ";
391  }
392  common::WriteToFile(dirpath_ + "/Makefile", {oss.str()});
393  if (verbose > 0) {
394  LOG(INFO) << "Writing " << dirpath_ + "/Makefile ...";
395  }
396  }
397 #endif
398  API_END();
399 }
400 
402  API_BEGIN();
403  delete static_cast<CompilerHandleImpl*>(handle);
404  API_END();
405 }
406 
407 int TreeliteLoadLightGBMModel(const char* filename,
408  ModelHandle* out) {
409  API_BEGIN();
410  Model* model = new Model(std::move(frontend::LoadLightGBMModel(filename)));
411  *out = static_cast<ModelHandle>(model);
412  API_END();
413 }
414 
415 int TreeliteLoadXGBoostModel(const char* filename,
416  ModelHandle* out) {
417  API_BEGIN();
418  Model* model = new Model(std::move(frontend::LoadXGBoostModel(filename)));
419  *out = static_cast<ModelHandle>(model);
420  API_END();
421 }
422 
423 int TreeliteLoadXGBoostModelFromMemoryBuffer(const void* buf, size_t len,
424  ModelHandle* out) {
425  API_BEGIN();
426  Model* model = new Model(std::move(frontend::LoadXGBoostModel(buf, len)));
427  *out = static_cast<ModelHandle>(model);
428  API_END();
429 }
430 
431 int TreeliteLoadProtobufModel(const char* filename,
432  ModelHandle* out) {
433  API_BEGIN();
434  Model* model = new Model(std::move(frontend::LoadProtobufModel(filename)));
435  *out = static_cast<ModelHandle>(model);
436  API_END();
437 }
438 
440  API_BEGIN();
441  delete static_cast<Model*>(handle);
442  API_END();
443 }
444 
446  API_BEGIN();
447  auto builder = new frontend::TreeBuilder();
448  *out = static_cast<TreeBuilderHandle>(builder);
449  API_END();
450 }
451 
453  API_BEGIN();
454  delete static_cast<frontend::TreeBuilder*>(handle);
455  API_END();
456 }
457 
459  API_BEGIN();
460  auto builder = static_cast<frontend::TreeBuilder*>(handle);
461  return (builder->CreateNode(node_key)) ? 0 : -1;
462  API_END();
463 }
464 
466  API_BEGIN();
467  auto builder = static_cast<frontend::TreeBuilder*>(handle);
468  return (builder->DeleteNode(node_key)) ? 0 : -1;
469  API_END();
470 }
471 
473  API_BEGIN();
474  auto builder = static_cast<frontend::TreeBuilder*>(handle);
475  return (builder->SetRootNode(node_key)) ? 0 : -1;
476  API_END();
477 }
478 
480  int node_key, unsigned feature_id,
481  const char* opname,
482  float threshold, int default_left,
483  int left_child_key,
484  int right_child_key) {
485  API_BEGIN();
486  auto builder = static_cast<frontend::TreeBuilder*>(handle);
487  CHECK_GT(optable.count(opname), 0)
488  << "No operator `" << opname << "\" exists";
489  return (builder->SetNumericalTestNode(node_key, feature_id,
490  optable.at(opname),
491  static_cast<tl_float>(threshold),
492  (default_left != 0),
493  left_child_key, right_child_key)) \
494  ? 0 : -1;
495  API_END();
496 }
497 
499  TreeBuilderHandle handle,
500  int node_key, unsigned feature_id,
501  const unsigned char* left_categories,
502  size_t left_categories_len,
503  int default_left,
504  int left_child_key,
505  int right_child_key) {
506  API_BEGIN();
507  auto builder = static_cast<frontend::TreeBuilder*>(handle);
508  std::vector<uint8_t> vec(left_categories_len);
509  for (size_t i = 0; i < left_categories_len; ++i) {
510  CHECK(left_categories[i] <= std::numeric_limits<uint8_t>::max());
511  vec[i] = static_cast<uint8_t>(left_categories[i]);
512  }
513  return (builder->SetCategoricalTestNode(node_key, feature_id, vec,
514  (default_left != 0),
515  left_child_key, right_child_key)) \
516  ? 0 : -1;
517  API_END();
518 }
519 
521  float leaf_value) {
522  API_BEGIN();
523  auto builder = static_cast<frontend::TreeBuilder*>(handle);
524  return (builder->SetLeafNode(node_key, static_cast<tl_float>(leaf_value))) \
525  ? 0 : -1;
526  API_END();
527 }
528 
530  int node_key,
531  const float* leaf_vector,
532  size_t leaf_vector_len) {
533  API_BEGIN();
534  auto builder = static_cast<frontend::TreeBuilder*>(handle);
535  std::vector<tl_float> vec(leaf_vector_len);
536  for (size_t i = 0; i < leaf_vector_len; ++i) {
537  vec[i] = static_cast<tl_float>(leaf_vector[i]);
538  }
539  return (builder->SetLeafVectorNode(node_key, vec)) ? 0 : -1;
540  API_END();
541 }
542 
543 int TreeliteCreateModelBuilder(int num_feature,
544  int num_output_group,
545  int random_forest_flag,
546  ModelBuilderHandle* out) {
547  API_BEGIN();
548  auto builder = new frontend::ModelBuilder(num_feature, num_output_group,
549  (random_forest_flag != 0));
550  *out = static_cast<ModelBuilderHandle>(builder);
551  API_END();
552 }
553 
555  const char* name,
556  const char* value) {
557  API_BEGIN();
558  auto builder = static_cast<frontend::ModelBuilder*>(handle);
559  builder->SetModelParam(name, value);
560  API_END();
561 }
562 
564  API_BEGIN();
565  delete static_cast<frontend::ModelBuilder*>(handle);
566  API_END();
567 }
568 
570  TreeBuilderHandle tree_builder_handle,
571  int index) {
572  API_BEGIN();
573  auto model_builder = static_cast<frontend::ModelBuilder*>(handle);
574  auto tree_builder = static_cast<frontend::TreeBuilder*>(tree_builder_handle);
575  return model_builder->InsertTree(tree_builder, index);
576  API_END();
577 }
578 
580  TreeBuilderHandle *out) {
581  API_BEGIN();
582  auto model_builder = static_cast<frontend::ModelBuilder*>(handle);
583  auto tree_builder = &model_builder->GetTree(index);
584  *out = static_cast<TreeBuilderHandle>(tree_builder);
585  API_END();
586 }
587 
589  API_BEGIN();
590  auto builder = static_cast<frontend::ModelBuilder*>(handle);
591  return (builder->DeleteTree(index)) ? 0 : -1;
592  API_END();
593 }
594 
596  ModelHandle* out) {
597  API_BEGIN();
598  auto builder = static_cast<frontend::ModelBuilder*>(handle);
599  Model* model = new Model();
600  const bool result = builder->CommitModel(model);
601  if (result) {
602  *out = static_cast<ModelHandle>(model);
603  return 0;
604  } else {
605  return -1;
606  }
607  API_END();
608 }
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:479
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
Definition: c_api.cc:554
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:579
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:350
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:415
#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:439
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:588
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:96
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:563
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:445
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:529
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:569
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:139
void Save(dmlc::Stream *fo) const
save branch annotation to a JSON file
Definition: annotator.cc:146
int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle, int node_key)
Set a node as the root of a tree.
Definition: c_api.cc:472
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:452
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:595
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:543
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:423
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:407
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:431
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:520
Branch annotation tools.
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
Definition: c_api.cc:465
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
int TreeliteTreeBuilderSetCategoricalTestNode(TreeBuilderHandle handle, int node_key, unsigned feature_id, const unsigned char *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:498
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:401
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:458