19 #include <dmlc/thread_local.h> 27 struct CompilerHandleImpl {
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;
42 const Model* model_ =
static_cast<Model*
>(model);
43 const auto* dmat_ =
static_cast<const DMatrix*
>(dmat);
44 CHECK(dmat_) <<
"Found a dangling reference to DMatrix";
45 annotator->Annotate(*model_, dmat_, nthread, verbose);
54 std::unique_ptr<dmlc::Stream> fo(dmlc::Stream::Create(path,
"w"));
55 annotator->
Save(fo.get());
68 std::unique_ptr<CompilerHandleImpl> compiler{
new CompilerHandleImpl(name)};
77 CompilerHandleImpl* impl =
static_cast<CompilerHandleImpl*
>(handle);
78 auto& cfg_ = impl->cfg;
79 std::string name_(name);
80 std::string value_(value);
82 auto it = std::find_if(cfg_.begin(), cfg_.end(),
83 [&name_](
const std::pair<std::string, std::string>& x) {
84 return x.first == name_;
86 if (it == cfg_.end()) {
87 cfg_.emplace_back(name_, value_);
97 const char* dirpath) {
101 std::to_string(verbose).c_str());
106 const Model* model_ =
static_cast<Model*
>(model);
107 CompilerHandleImpl* impl =
static_cast<CompilerHandleImpl*
>(compiler);
110 const std::string& dirpath_(dirpath);
111 filesystem::CreateDirectoryIfNotExist(dirpath);
114 cparam.Init(impl->cfg, dmlc::parameter::kAllMatch);
118 auto compiled_model = impl->compiler->Compile(*model_);
120 LOG(INFO) <<
"Code generation finished. Writing code to files...";
123 for (
const auto& it : compiled_model.files) {
125 LOG(INFO) <<
"Writing file " << it.first <<
"...";
127 const std::string filename_full = dirpath_ +
"/" + it.first;
128 if (it.second.is_binary) {
129 filesystem::WriteToFile(filename_full, it.second.content_binary);
131 filesystem::WriteToFile(filename_full, it.second.content);
140 delete static_cast<CompilerHandleImpl*
>(handle);
146 std::unique_ptr<Model> model = frontend::LoadLightGBMModel(filename);
153 std::unique_ptr<Model> model = frontend::LoadXGBoostModel(filename);
160 std::unique_ptr<Model> model = frontend::LoadXGBoostJSONModel(filename);
167 std::unique_ptr<Model> model = frontend::LoadXGBoostJSONModelString(json_str, length);
174 std::unique_ptr<Model> model = frontend::LoadXGBoostModel(buf, len);
181 delete static_cast<Model*
>(handle);
187 const auto* model_ =
static_cast<const Model*
>(handle);
188 *out = model_->GetNumTree();
194 const auto* model_ =
static_cast<const Model*
>(handle);
195 *out =
static_cast<size_t>(model_->num_feature);
201 const auto* model_ =
static_cast<const Model*
>(handle);
202 *out =
static_cast<size_t>(model_->task_param.num_class);
208 CHECK_GT(limit, 0) <<
"limit should be greater than 0!";
209 auto* model_ =
static_cast<Model*
>(handle);
210 const size_t num_tree = model_->GetNumTree();
211 CHECK_GE(num_tree, limit) <<
"Model contains less trees(" << num_tree <<
") than limit";
212 model_->SetTreeLimit(limit);
218 std::unique_ptr<frontend::Value> value = std::make_unique<frontend::Value>();
233 std::unique_ptr<frontend::TreeBuilder> builder{
250 CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
258 CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
266 CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
273 ValueHandle threshold,
int default_left,
int left_child_key,
int right_child_key) {
276 CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
278 *static_cast<const frontend::Value*>(threshold),
279 (default_left != 0), left_child_key, right_child_key);
285 const unsigned int* left_categories,
size_t left_categories_len,
int default_left,
286 int left_child_key,
int right_child_key) {
289 CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
290 std::vector<uint32_t> vec(left_categories_len);
291 for (
size_t i = 0; i < left_categories_len; ++i) {
292 CHECK(left_categories[i] <= std::numeric_limits<uint32_t>::max());
293 vec[i] =
static_cast<uint32_t
>(left_categories[i]);
295 builder->SetCategoricalTestNode(node_key, feature_id, vec, (default_left != 0),
296 left_child_key, right_child_key);
303 CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
304 builder->
SetLeafNode(node_key, *static_cast<const frontend::Value*>(leaf_value));
309 const ValueHandle* leaf_vector,
size_t leaf_vector_len) {
312 CHECK(builder) <<
"Detected dangling reference to deleted TreeBuilder object";
313 std::vector<frontend::Value> vec(leaf_vector_len);
314 CHECK(leaf_vector) <<
"leaf_vector argument must not be null";
315 for (
size_t i = 0; i < leaf_vector_len; ++i) {
316 CHECK(leaf_vector[i]) <<
"leaf_vector[" << i <<
"] contains an empty Value handle";
319 builder->SetLeafVectorNode(node_key, vec);
324 int num_feature,
int num_class,
int average_tree_output,
const char* threshold_type,
328 num_feature, num_class, (average_tree_output != 0),
GetTypeInfoByName(threshold_type),
338 CHECK(builder) <<
"Detected dangling reference to deleted ModelBuilder object";
353 CHECK(model_builder) <<
"Detected dangling reference to deleted ModelBuilder object";
355 CHECK(tree_builder) <<
"Detected dangling reference to deleted TreeBuilder object";
356 return model_builder->InsertTree(tree_builder, index);
363 CHECK(model_builder) <<
"Detected dangling reference to deleted ModelBuilder object";
364 auto* tree_builder = model_builder->
GetTree(index);
365 CHECK(tree_builder) <<
"Detected dangling reference to deleted TreeBuilder object";
373 CHECK(builder) <<
"Detected dangling reference to deleted ModelBuilder object";
381 CHECK(builder) <<
"Detected dangling reference to deleted ModelBuilder object";
382 std::unique_ptr<Model> model = builder->
CommitModel();
Some useful math utilities.
int TreeliteQueryNumClass(ModelHandle handle, size_t *out)
Query the number of classes of the model. (1 if the model is binary classifier or regressor) ...
C API of Treelite, used for interfacing with other languages This header is excluded from the runtime...
Parameters for tree compiler.
int TreeliteModelBuilderSetModelParam(ModelBuilderHandle handle, const char *name, const char *value)
Set a model parameter.
int TreeliteModelBuilderGetTree(ModelBuilderHandle handle, int index, TreeBuilderHandle *out)
Get a reference to a tree in the ensemble.
std::unique_ptr< Model > CommitModel()
finalize the model and produce the in-memory representation
Collection of front-end methods to load or construct ensemble model.
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...
void DeleteNode(int node_key)
Remove a node from a tree.
#define API_BEGIN()
macro to guard beginning and end section of all functions
int TreeliteFreeModel(ModelHandle handle)
delete model from memory
int TreeliteAnnotationSave(AnnotationHandle handle, const char *path)
save branch annotation to a JSON file
int TreeliteQueryNumTree(ModelHandle handle, size_t *out)
Query the number of trees in the model.
int TreeliteModelBuilderDeleteTree(ModelBuilderHandle handle, int index)
Remove a tree from the ensemble.
parameters for tree compiler
Input data structure of Treelite.
void SetNumericalTestNode(int node_key, unsigned feature_id, const char *op, Value threshold, bool default_left, int left_child_key, int right_child_key)
Turn an empty node into a numerical test node; the test is in the form [feature value] OP [threshold]...
int TreeliteCompilerSetParam(CompilerHandle handle, const char *name, const char *value)
set a parameter for a compiler
int TreeliteLoadXGBoostJSON(const char *filename, ModelHandle *out)
load a json model file generated by XGBoost (dmlc/xgboost). The model file must contain a decision tr...
int TreeliteDeleteModelBuilder(ModelBuilderHandle handle)
Delete a model builder from memory.
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 ...
model structure for tree ensemble
void SetModelParam(const char *name, const char *value)
Set a model parameter.
void SetLeafNode(int node_key, Value leaf_value)
Turn an empty node into a leaf node.
void SetRootNode(int node_key)
Set a node as the root of a tree.
int TreeliteAnnotationFree(AnnotationHandle handle)
delete branch annotation from memory
TreeBuilder * GetTree(int index)
Get a reference to a tree in the ensemble.
void Save(dmlc::Stream *fo) const
save branch annotation to a JSON file
int TreeliteTreeBuilderSetNumericalTestNode(TreeBuilderHandle handle, int node_key, unsigned feature_id, const char *opname, ValueHandle 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 ...
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. ...
int TreeliteTreeBuilderCreateValue(const void *init_value, const char *type, ValueHandle *out)
Create a new Value object. Some model builder API functions accept this Value type to accommodate val...
Interface of compiler that compiles a tree ensemble model.
int TreeliteSetTreeLimit(ModelHandle handle, size_t limit)
keep first N trees of model, limit must smaller than number of trees.
void * ValueHandle
handle to a polymorphic value type, used in the model builder API
int TreeliteModelBuilderInsertTree(ModelBuilderHandle handle, TreeBuilderHandle tree_builder_handle, int index)
Insert a tree at specified location.
void * DMatrixHandle
handle to a data matrix
Cross-platform wrapper for common filesystem functions.
int TreeliteTreeBuilderSetRootNode(TreeBuilderHandle handle, int node_key)
Set a node as the root of a tree.
void * TreeBuilderHandle
handle to tree builder class
Error handling for C API.
int TreeliteDeleteTreeBuilder(TreeBuilderHandle handle)
Delete a tree builder from memory.
int TreeliteCreateTreeBuilder(const char *threshold_type, const char *leaf_output_type, TreeBuilderHandle *out)
Create a new tree builder.
void * AnnotationHandle
handle to branch annotation data
int TreeliteModelBuilderCommitModel(ModelBuilderHandle handle, ModelHandle *out)
finalize the model and produce the in-memory representation
int TreeliteTreeBuilderSetLeafNode(TreeBuilderHandle handle, int node_key, ValueHandle leaf_value)
Turn an empty node into a leaf node.
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).
int TreeliteCreateModelBuilder(int num_feature, int num_class, int average_tree_output, const char *threshold_type, const char *leaf_output_type, ModelBuilderHandle *out)
Create a new model builder.
void DeleteTree(int index)
Remove a tree from the ensemble.
int TreeliteLoadXGBoostModelFromMemoryBuffer(const void *buf, size_t len, ModelHandle *out)
load an XGBoost model from a memory buffer.
void * ModelHandle
handle to a decision tree ensemble model
int TreeliteLoadLightGBMModel(const char *filename, ModelHandle *out)
load a model file generated by LightGBM (Microsoft/LightGBM). The model file must contain a decision ...
TypeInfo GetTypeInfoByName(const std::string &str)
conversion table from string to TypeInfo, defined in tables.cc
static Compiler * Create(const std::string &name, const compiler::CompilerParam ¶m)
create a compiler from given name
int TreeliteTreeBuilderDeleteValue(ValueHandle handle)
Delete a Value object from memory.
int TreeliteCompilerCreate(const char *name, CompilerHandle *out)
create a compiler with a given name
int TreeliteTreeBuilderDeleteNode(TreeBuilderHandle handle, int node_key)
Remove a node from a tree.
thin wrapper for tree ensemble model
int TreeliteQueryNumFeature(ModelHandle handle, size_t *out)
Query the number of features used in the model.
void * ModelBuilderHandle
handle to ensemble builder class
int TreeliteTreeBuilderSetLeafVectorNode(TreeBuilderHandle handle, int node_key, const ValueHandle *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...
void CreateNode(int node_key)
Create an empty node within a tree.
void * CompilerHandle
handle to compiler class
int TreeliteLoadXGBoostJSONString(const char *json_str, size_t length, ModelHandle *out)
load a model stored as JSON stringby XGBoost (dmlc/xgboost). The model json must contain a decision t...
int TreeliteCompilerFree(CompilerHandle handle)
delete compiler from memory
#define API_END()
every function starts with API_BEGIN(); and finishes with API_END() or API_END_HANDLE_ERROR ...
int TreeliteTreeBuilderCreateNode(TreeBuilderHandle handle, int node_key)
Create an empty node within a tree.