Medial Code Documentation
Loading...
Searching...
No Matches
c_api.h
1#ifndef LIGHTGBM_C_API_H_
2#define LIGHTGBM_C_API_H_
3
4#include <cstdint>
5#include <cstring>
6
15#include <LightGBM/export.h>
16
17typedef void* DatasetHandle;
18typedef void* BoosterHandle;
19
20#define C_API_DTYPE_FLOAT32 (0)
21#define C_API_DTYPE_FLOAT64 (1)
22#define C_API_DTYPE_INT32 (2)
23#define C_API_DTYPE_INT64 (3)
24
25#define C_API_PREDICT_NORMAL (0)
26#define C_API_PREDICT_RAW_SCORE (1)
27#define C_API_PREDICT_LEAF_INDEX (2)
28#define C_API_PREDICT_CONTRIB (3)
29
36LIGHTGBM_C_EXPORT const char* LGBM_GetLastError();
37
38// --- start Dataset interface
39
48LIGHTGBM_C_EXPORT int LGBM_DatasetCreateFromFile(const char* filename,
49 const char* parameters,
50 const DatasetHandle reference,
51 DatasetHandle* out);
52
65LIGHTGBM_C_EXPORT int LGBM_DatasetCreateFromSampledColumn(double** sample_data,
66 int** sample_indices,
67 int32_t ncol,
68 const int* num_per_col,
69 int32_t num_sample_row,
70 int32_t num_total_row,
71 const char* parameters,
72 DatasetHandle* out);
73
81LIGHTGBM_C_EXPORT int LGBM_DatasetCreateByReference(const DatasetHandle reference,
82 int64_t num_total_row,
83 DatasetHandle* out);
84
95LIGHTGBM_C_EXPORT int LGBM_DatasetPushRows(DatasetHandle dataset,
96 const void* data,
97 int data_type,
98 int32_t nrow,
99 int32_t ncol,
100 int32_t start_row);
101
116LIGHTGBM_C_EXPORT int LGBM_DatasetPushRowsByCSR(DatasetHandle dataset,
117 const void* indptr,
118 int indptr_type,
119 const int32_t* indices,
120 const void* data,
121 int data_type,
122 int64_t nindptr,
123 int64_t nelem,
124 int64_t num_col,
125 int64_t start_row);
126
142LIGHTGBM_C_EXPORT int LGBM_DatasetCreateFromCSR(const void* indptr,
143 int indptr_type,
144 const int32_t* indices,
145 const void* data,
146 int data_type,
147 int64_t nindptr,
148 int64_t nelem,
149 int64_t num_col,
150 const char* parameters,
151 const DatasetHandle reference,
152 DatasetHandle* out);
153
169LIGHTGBM_C_EXPORT int LGBM_DatasetCreateFromCSC(const void* col_ptr,
170 int col_ptr_type,
171 const int32_t* indices,
172 const void* data,
173 int data_type,
174 int64_t ncol_ptr,
175 int64_t nelem,
176 int64_t num_row,
177 const char* parameters,
178 const DatasetHandle reference,
179 DatasetHandle* out);
180
193LIGHTGBM_C_EXPORT int LGBM_DatasetCreateFromMat(const void* data,
194 int data_type,
195 int32_t nrow,
196 int32_t ncol,
197 int is_row_major,
198 const char* parameters,
199 const DatasetHandle reference,
200 DatasetHandle* out);
201
213LIGHTGBM_C_EXPORT int LGBM_DatasetCreateFromMats(int32_t nmat,
214 const void** data,
215 int data_type,
216 int32_t* nrow,
217 int32_t ncol,
218 int is_row_major,
219 const char* parameters,
220 const DatasetHandle reference,
221 DatasetHandle* out);
222
232LIGHTGBM_C_EXPORT int LGBM_DatasetGetSubset(
233 const DatasetHandle handle,
234 const int32_t* used_row_indices,
235 int32_t num_used_row_indices,
236 const char* parameters,
237 DatasetHandle* out);
238
246LIGHTGBM_C_EXPORT int LGBM_DatasetSetFeatureNames(
247 DatasetHandle handle,
248 const char** feature_names,
249 int num_feature_names);
250
251
259LIGHTGBM_C_EXPORT int LGBM_DatasetGetFeatureNames(
260 DatasetHandle handle,
261 char** feature_names,
262 int* num_feature_names);
263
264
269LIGHTGBM_C_EXPORT int LGBM_DatasetFree(DatasetHandle handle);
270
277LIGHTGBM_C_EXPORT int LGBM_DatasetSaveBinary(DatasetHandle handle,
278 const char* filename);
279
291LIGHTGBM_C_EXPORT int LGBM_DatasetSetField(DatasetHandle handle,
292 const char* field_name,
293 const void* field_data,
294 int num_element,
295 int type);
296
306LIGHTGBM_C_EXPORT int LGBM_DatasetGetField(DatasetHandle handle,
307 const char* field_name,
308 int* out_len,
309 const void** out_ptr,
310 int* out_type);
311
312
318LIGHTGBM_C_EXPORT int LGBM_DatasetUpdateParam(DatasetHandle handle, const char* parameters);
319
326LIGHTGBM_C_EXPORT int LGBM_DatasetGetNumData(DatasetHandle handle,
327 int* out);
328
335LIGHTGBM_C_EXPORT int LGBM_DatasetGetNumFeature(DatasetHandle handle,
336 int* out);
337
338// --- start Booster interfaces
339
347LIGHTGBM_C_EXPORT int LGBM_BoosterCreate(const DatasetHandle train_data,
348 const char* parameters,
349 BoosterHandle* out);
350
358LIGHTGBM_C_EXPORT int LGBM_BoosterCreateFromModelfile(
359 const char* filename,
360 int* out_num_iterations,
361 BoosterHandle* out);
362
370LIGHTGBM_C_EXPORT int LGBM_BoosterLoadModelFromString(
371 const char* model_str,
372 int* out_num_iterations,
373 BoosterHandle* out);
374
380LIGHTGBM_C_EXPORT int LGBM_BoosterFree(BoosterHandle handle);
381
385LIGHTGBM_C_EXPORT int LGBM_BoosterShuffleModels(BoosterHandle handle, int start_iter, int end_iter);
386
393LIGHTGBM_C_EXPORT int LGBM_BoosterMerge(BoosterHandle handle,
394 BoosterHandle other_handle);
395
402LIGHTGBM_C_EXPORT int LGBM_BoosterAddValidData(BoosterHandle handle,
403 const DatasetHandle valid_data);
404
411LIGHTGBM_C_EXPORT int LGBM_BoosterResetTrainingData(BoosterHandle handle,
412 const DatasetHandle train_data);
413
420LIGHTGBM_C_EXPORT int LGBM_BoosterResetParameter(BoosterHandle handle, const char* parameters);
421
428LIGHTGBM_C_EXPORT int LGBM_BoosterGetNumClasses(BoosterHandle handle, int* out_len);
429
436LIGHTGBM_C_EXPORT int LGBM_BoosterUpdateOneIter(BoosterHandle handle, int* is_finished);
437
446LIGHTGBM_C_EXPORT int LGBM_BoosterRefit(BoosterHandle handle, const int32_t* leaf_preds, int32_t nrow, int32_t ncol);
447
457LIGHTGBM_C_EXPORT int LGBM_BoosterUpdateOneIterCustom(BoosterHandle handle,
458 const float* grad,
459 const float* hess,
460 int* is_finished);
461
467LIGHTGBM_C_EXPORT int LGBM_BoosterRollbackOneIter(BoosterHandle handle);
468
474LIGHTGBM_C_EXPORT int LGBM_BoosterGetCurrentIteration(BoosterHandle handle, int* out_iteration);
475
481LIGHTGBM_C_EXPORT int LGBM_BoosterNumModelPerIteration(BoosterHandle handle, int* out_tree_per_iteration);
482
488LIGHTGBM_C_EXPORT int LGBM_BoosterNumberOfTotalModel(BoosterHandle handle, int* out_models);
489
495LIGHTGBM_C_EXPORT int LGBM_BoosterGetEvalCounts(BoosterHandle handle, int* out_len);
496
503LIGHTGBM_C_EXPORT int LGBM_BoosterGetEvalNames(BoosterHandle handle, int* out_len, char** out_strs);
504
511LIGHTGBM_C_EXPORT int LGBM_BoosterGetFeatureNames(BoosterHandle handle, int* out_len, char** out_strs);
512
518LIGHTGBM_C_EXPORT int LGBM_BoosterGetNumFeature(BoosterHandle handle, int* out_len);
519
530LIGHTGBM_C_EXPORT int LGBM_BoosterGetEval(BoosterHandle handle,
531 int data_idx,
532 int* out_len,
533 double* out_results);
534
544LIGHTGBM_C_EXPORT int LGBM_BoosterGetNumPredict(BoosterHandle handle,
545 int data_idx,
546 int64_t* out_len);
547
558LIGHTGBM_C_EXPORT int LGBM_BoosterGetPredict(BoosterHandle handle,
559 int data_idx,
560 int64_t* out_len,
561 double* out_result);
562
577LIGHTGBM_C_EXPORT int LGBM_BoosterPredictForFile(BoosterHandle handle,
578 const char* data_filename,
579 int data_has_header,
580 int predict_type,
581 int num_iteration,
582 const char* parameter,
583 const char* result_filename);
584
597LIGHTGBM_C_EXPORT int LGBM_BoosterCalcNumPredict(BoosterHandle handle,
598 int num_row,
599 int predict_type,
600 int num_iteration,
601 int64_t* out_len);
602
627LIGHTGBM_C_EXPORT int LGBM_BoosterPredictForCSR(BoosterHandle handle,
628 const void* indptr,
629 int indptr_type,
630 const int32_t* indices,
631 const void* data,
632 int data_type,
633 int64_t nindptr,
634 int64_t nelem,
635 int64_t num_col,
636 int predict_type,
637 int num_iteration,
638 const char* parameter,
639 int64_t* out_len,
640 double* out_result);
641
666LIGHTGBM_C_EXPORT int LGBM_BoosterPredictForCSC(BoosterHandle handle,
667 const void* col_ptr,
668 int col_ptr_type,
669 const int32_t* indices,
670 const void* data,
671 int data_type,
672 int64_t ncol_ptr,
673 int64_t nelem,
674 int64_t num_row,
675 int predict_type,
676 int num_iteration,
677 const char* parameter,
678 int64_t* out_len,
679 double* out_result);
680
702LIGHTGBM_C_EXPORT int LGBM_BoosterPredictForMat(BoosterHandle handle,
703 const void* data,
704 int data_type,
705 int32_t nrow,
706 int32_t ncol,
707 int is_row_major,
708 int predict_type,
709 int num_iteration,
710 const char* parameter,
711 int64_t* out_len,
712 double* out_result);
713
721LIGHTGBM_C_EXPORT int LGBM_BoosterSaveModel(BoosterHandle handle,
722 int start_iteration,
723 int num_iteration,
724 const char* filename);
725
735LIGHTGBM_C_EXPORT int LGBM_BoosterSaveModelToString(BoosterHandle handle,
736 int start_iteration,
737 int num_iteration,
738 int64_t buffer_len,
739 int64_t* out_len,
740 char* out_str);
741
751LIGHTGBM_C_EXPORT int LGBM_BoosterDumpModel(BoosterHandle handle,
752 int start_iteration,
753 int num_iteration,
754 int64_t buffer_len,
755 int64_t* out_len,
756 char* out_str);
757
766LIGHTGBM_C_EXPORT int LGBM_BoosterGetLeafValue(BoosterHandle handle,
767 int tree_idx,
768 int leaf_idx,
769 double* out_val);
770
779LIGHTGBM_C_EXPORT int LGBM_BoosterSetLeafValue(BoosterHandle handle,
780 int tree_idx,
781 int leaf_idx,
782 double val);
783
792LIGHTGBM_C_EXPORT int LGBM_BoosterFeatureImportance(BoosterHandle handle,
793 int num_iteration,
794 int importance_type,
795 double* out_results);
796
805LIGHTGBM_C_EXPORT int LGBM_NetworkInit(const char* machines,
806 int local_listen_port,
807 int listen_time_out,
808 int num_machines);
809
814LIGHTGBM_C_EXPORT int LGBM_NetworkFree();
815
816LIGHTGBM_C_EXPORT int LGBM_NetworkInitWithFunctions(int num_machines, int rank,
817 void* reduce_scatter_ext_fun,
818 void* allgather_ext_fun);
819
820
821#if defined(_MSC_VER)
822#define THREAD_LOCAL __declspec(thread)
823#else
824#define THREAD_LOCAL thread_local
825#endif
826// exception handle and error msg
827static char* LastErrorMsg() { static THREAD_LOCAL char err_msg[512] = "Everything is fine"; return err_msg; }
828
829#pragma warning(disable : 4996)
830inline void LGBM_SetLastError(const char* msg) {
831 std::strcpy(LastErrorMsg(), msg);
832}
833
834#endif // LIGHTGBM_C_API_H_