10 #include <dmlc/thread_local.h> 20 struct TreeliteRuntimeAPIThreadLocalEntry {
26 using TreeliteRuntimeAPIThreadLocalStore
27 = dmlc::ThreadLocalStore<TreeliteRuntimeAPIThreadLocalEntry>;
32 const uint32_t* col_ind,
33 const size_t* row_ptr,
34 size_t num_row,
size_t num_col,
49 delete static_cast<CSRBatch*
>(handle);
54 size_t num_row,
size_t num_col,
75 size_t* out_num_col) {
90 int num_worker_thread,
94 predictor->
Load(library_path);
105 size_t* out_result_size) {
109 const std::string err_msg
110 = std::string(
"Too many columns (features) in the given batch. " 111 "Number of features must not exceed ")
112 + std::to_string(num_feature);
115 CHECK_LE(batch_->
num_col, num_feature) << err_msg;
116 *out_result_size = predictor_->
PredictBatch(batch_, verbose,
117 (pred_margin != 0), out_result);
120 CHECK_LE(batch_->
num_col, num_feature) << err_msg;
121 *out_result_size = predictor_->
PredictBatch(batch_, verbose,
122 (pred_margin != 0), out_result);
130 float* out_result,
size_t* out_result_size) {
134 = predictor_->
PredictInst(inst, (pred_margin != 0), out_result);
180 std::string& ret_str = TreeliteRuntimeAPIThreadLocalStore::Get()->ret_str;
181 ret_str = pred_transform;
182 *out = ret_str.c_str();
Load prediction function exported as a shared library.
void * DenseBatchHandle
handle to batch of dense data rows
size_t QueryNumFeature() const
Get the width (number of features) of each instance used to train the loaded model.
void * CSRBatchHandle
handle to batch of sparse data rows
const uint32_t * col_ind
feature indices
#define API_BEGIN()
macro to guard beginning and end section of all functions
size_t QueryResultSizeSingleInst() const
Query the necessary size of array to hold the prediction for a single data row.
C API of Treelite, used for interfacing with other languages This header is used exclusively by the r...
int TreelitePredictorQueryResultSize(PredictorHandle handle, void *batch, int batch_sparse, size_t *out)
Given a batch of data rows, query the necessary size of array to hold predictions for all data points...
int TreelitePredictorQueryResultSizeSingleInst(PredictorHandle handle, size_t *out)
Query the necessary size of array to hold the prediction for a single data row.
int TreelitePredictorQueryPredTransform(PredictorHandle handle, const char **out)
Get name of post prediction transformation used to train the loaded model.
int TreelitePredictorPredictBatch(PredictorHandle handle, void *batch, int batch_sparse, int verbose, int pred_margin, float *out_result, size_t *out_result_size)
Make predictions on a batch of data rows (synchronously). This function internally divides the worklo...
size_t PredictInst(TreelitePredictorEntry *inst, bool pred_margin, float *out_result)
Make predictions on a single data row (synchronously). The work will be scheduled to the calling thre...
int TreelitePredictorQueryNumFeature(PredictorHandle handle, size_t *out)
Get the width (number of features) of each instance used to train the loaded model.
int TreeliteBatchGetDimension(void *handle, int batch_sparse, size_t *out_num_row, size_t *out_num_col)
get dimensions of a batch
sparse batch in Compressed Sparse Row (CSR) format
const size_t * row_ptr
pointer to row headers; length of [num_row] + 1
void Load(const char *name)
load the prediction function from dynamic shared library.
float QueryGlobalBias() const
Get global bias which adjusting predicted margin scores.
const float * data
feature values
Error handling for C API.
int TreelitePredictorLoad(const char *library_path, int num_worker_thread, PredictorHandle *out)
load prediction code into memory. This function assumes that the prediction code has been already com...
int TreeliteAssembleSparseBatch(const float *data, const uint32_t *col_ind, const size_t *row_ptr, size_t num_row, size_t num_col, CSRBatchHandle *out)
assemble a sparse batch
size_t PredictBatch(const CSRBatch *batch, int verbose, bool pred_margin, float *out_result)
Make predictions on a batch of data rows (synchronously). This function internally divides the worklo...
float missing_value
value representing the missing value (usually nan)
size_t QueryResultSize(const CSRBatch *batch) const
Given a batch of data rows, query the necessary size of array to hold predictions for all data points...
int TreeliteDeleteDenseBatch(DenseBatchHandle handle)
delete a dense batch from memory
int TreeliteDeleteSparseBatch(CSRBatchHandle handle)
delete a sparse batch from memory
int TreeliteAssembleDenseBatch(const float *data, float missing_value, size_t num_row, size_t num_col, DenseBatchHandle *out)
assemble a dense batch
data layout. The value -1 signifies the missing value. When the "missing" field is set to -1...
void * PredictorHandle
handle to predictor class
const float * data
feature values
std::string QueryPredTransform() const
Get name of post prediction transformation used to train the loaded model.
float QuerySigmoidAlpha() const
Get alpha value in sigmoid transformation used to train the loaded model.
size_t num_row
number of rows
size_t QueryNumOutputGroup() const
Get the number of output groups in the loaded model The number is 1 for most tasks; it is greater tha...
int TreelitePredictorQueryNumOutputGroup(PredictorHandle handle, size_t *out)
Get the number of output groups in the loaded model The number is 1 for most tasks; it is greater tha...
predictor class: wrapper for optimized prediction code
int TreelitePredictorFree(PredictorHandle handle)
delete predictor from memory
size_t num_row
number of rows
int TreelitePredictorPredictInst(PredictorHandle handle, union TreelitePredictorEntry *inst, int pred_margin, float *out_result, size_t *out_result_size)
Make predictions on a single data row (synchronously). The work will be scheduled to the calling thre...
size_t num_col
number of columns (i.e. # of features used)
int TreelitePredictorQuerySigmoidAlpha(PredictorHandle handle, float *out)
Get alpha value of sigmoid transformation used to train the loaded model.
#define API_END()
every function starts with API_BEGIN(); and finishes with API_END() or API_END_HANDLE_ERROR ...
int TreelitePredictorQueryGlobalBias(PredictorHandle handle, float *out)
Get global bias which adjusting predicted margin scores.
size_t num_col
number of columns (i.e. # of features used)