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] << ": " << source_list[i]["name"] << std::endl
383  << "\tgcc -c -O3 -o $@ $? -fPIC -std=c99 -flto" << std::endl;
384  }
385  oss << std::endl
386  << "clean:" << std::endl
387  << "\trm -fv " << library_name << " ";
388  for (const auto& e : object_list) {
389  oss << e << " ";
390  }
391  common::WriteToFile(dirpath_ + "/Makefile", {oss.str()});
392  if (verbose > 0) {
393  LOG(INFO) << "Writing " << dirpath_ + "/Makefile ...";
394  }
395  }
396 #endif
397  API_END();
398 }
399 
401  API_BEGIN();
402  delete static_cast<CompilerHandleImpl*>(handle);
403  API_END();
404 }
405 
406 int TreeliteLoadLightGBMModel(const char* filename,
407  ModelHandle* out) {
408  API_BEGIN();
409  Model* model = new Model(std::move(frontend::LoadLightGBMModel(filename)));
410  *out = static_cast<ModelHandle>(model);
411  API_END();
412 }
413 
414 int TreeliteLoadXGBoostModel(const char* filename,
415  ModelHandle* out) {
416  API_BEGIN();
417  Model* model = new Model(std::move(frontend::LoadXGBoostModel(filename)));
418  *out = static_cast<ModelHandle>(model);
419  API_END();
420 }
421 
422 int TreeliteLoadXGBoostModelFromMemoryBuffer(const void* buf, size_t len,
423  ModelHandle* out) {
424  API_BEGIN();
425  Model* model = new Model(std::move(frontend::LoadXGBoostModel(buf, len)));
426  *out = static_cast<ModelHandle>(model);
427  API_END();
428 }
429 
430 int TreeliteLoadProtobufModel(const char* filename,
431  ModelHandle* out) {
432  API_BEGIN();
433  Model* model = new Model(std::move(frontend::LoadProtobufModel(filename)));
434  *out = static_cast<ModelHandle>(model);
435  API_END();
436 }
437 
439  API_BEGIN();
440  delete static_cast<Model*>(handle);
441  API_END();
442 }
443 
445  API_BEGIN();
446  auto builder = new frontend::TreeBuilder();
447  *out = static_cast<TreeBuilderHandle>(builder);
448  API_END();
449 }
450 
452  API_BEGIN();
453  delete static_cast<frontend::TreeBuilder*>(handle);
454  API_END();
455 }
456 
458  API_BEGIN();
459  auto builder = static_cast<frontend::TreeBuilder*>(handle);
460  return (builder->CreateNode(node_key)) ? 0 : -1;
461  API_END();
462 }
463 
465  API_BEGIN();
466  auto builder = static_cast<frontend::TreeBuilder*>(handle);
467  return (builder->DeleteNode(node_key)) ? 0 : -1;
468  API_END();
469 }
470 
472  API_BEGIN();
473  auto builder = static_cast<frontend::TreeBuilder*>(handle);
474  return (builder->SetRootNode(node_key)) ? 0 : -1;
475  API_END();
476 }
477 
479  int node_key, unsigned feature_id,
480  const char* opname,
481  float threshold, int default_left,
482  int left_child_key,
483  int right_child_key) {
484  API_BEGIN();
485  auto builder = static_cast<frontend::TreeBuilder*>(handle);
486  CHECK_GT(optable.count(opname), 0)
487  << "No operator `" << opname << "\" exists";
488  return (builder->SetNumericalTestNode(node_key, feature_id,
489  optable.at(opname),
490  static_cast<tl_float>(threshold),
491  (default_left != 0),
492  left_child_key, right_child_key)) \
493  ? 0 : -1;
494  API_END();
495 }
496 
498  TreeBuilderHandle handle,
499  int node_key, unsigned feature_id,
500  const unsigned char* left_categories,
501  size_t left_categories_len,
502  int default_left,
503  int left_child_key,
504  int right_child_key) {
505  API_BEGIN();
506  auto builder = static_cast<frontend::TreeBuilder*>(handle);
507  std::vector<uint8_t> vec(left_categories_len);
508  for (size_t i = 0; i < left_categories_len; ++i) {
509  CHECK(left_categories[i] <= std::numeric_limits<uint8_t>::max());
510  vec[i] = static_cast<uint8_t>(left_categories[i]);
511  }
512  return (builder->SetCategoricalTestNode(node_key, feature_id, vec,
513  (default_left != 0),
514  left_child_key, right_child_key)) \
515  ? 0 : -1;
516  API_END();
517 }
518 
520  float leaf_value) {
521  API_BEGIN();
522  auto builder = static_cast<frontend::TreeBuilder*>(handle);
523  return (builder->SetLeafNode(node_key, static_cast<tl_float>(leaf_value))) \
524  ? 0 : -1;
525  API_END();
526 }
527 
529  int node_key,
530  const float* leaf_vector,
531  size_t leaf_vector_len) {
532  API_BEGIN();
533  auto builder = static_cast<frontend::TreeBuilder*>(handle);
534  std::vector<tl_float> vec(leaf_vector_len);
535  for (size_t i = 0; i < leaf_vector_len; ++i) {
536  vec[i] = static_cast<tl_float>(leaf_vector[i]);
537  }
538  return (builder->SetLeafVectorNode(node_key, vec)) ? 0 : -1;
539  API_END();
540 }
541 
542 int TreeliteCreateModelBuilder(int num_feature,
543  int num_output_group,
544  int random_forest_flag,
545  ModelBuilderHandle* out) {
546  API_BEGIN();
547  auto builder = new frontend::ModelBuilder(num_feature, num_output_group,
548  (random_forest_flag != 0));
549  *out = static_cast<ModelBuilderHandle>(builder);
550  API_END();
551 }
552 
554  const char* name,
555  const char* value) {
556  API_BEGIN();
557  auto builder = static_cast<frontend::ModelBuilder*>(handle);
558  builder->SetModelParam(name, value);
559  API_END();
560 }
561 
563  API_BEGIN();
564  delete static_cast<frontend::ModelBuilder*>(handle);
565  API_END();
566 }
567 
569  TreeBuilderHandle tree_builder_handle,
570  int index) {
571  API_BEGIN();
572  auto model_builder = static_cast<frontend::ModelBuilder*>(handle);
573  auto tree_builder = static_cast<frontend::TreeBuilder*>(tree_builder_handle);
574  return model_builder->InsertTree(tree_builder, index);
575  API_END();
576 }
577 
579  TreeBuilderHandle *out) {
580  API_BEGIN();
581  auto model_builder = static_cast<frontend::ModelBuilder*>(handle);
582  auto tree_builder = &model_builder->GetTree(index);
583  *out = static_cast<TreeBuilderHandle>(tree_builder);
584  API_END();
585 }
586 
588  API_BEGIN();
589  auto builder = static_cast<frontend::ModelBuilder*>(handle);
590  return (builder->DeleteTree(index)) ? 0 : -1;
591  API_END();
592 }
593 
595  ModelHandle* out) {
596  API_BEGIN();
597  auto builder = static_cast<frontend::ModelBuilder*>(handle);
598  Model* model = new Model();
599  const bool result = builder->CommitModel(model);
600  if (result) {
601  *out = static_cast<ModelHandle>(model);
602  return 0;
603  } else {
604  return -1;
605  }
606  API_END();
607 }
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:478
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
Definition: c_api.cc:553
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:578
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:414
#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:438
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:587
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:562
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:444
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:528
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:568
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:471
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:451
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:594
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:542
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:422
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:406
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:430
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:519
Branch annotation tools.
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
Definition: c_api.cc:464
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:497
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:400
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:457