123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213(* Function-level bindings for libxgboost (Phase 1 happy-path subset).
*
* Every C call returns [int] (0=ok, -1=err). The error message is
* recovered via XGBGetLastError on the caller side. NULL out-pointers
* are handled by the layer-C wrapper, not here.
*
* Handles (DMatrixHandle, BoosterHandle) are typedef'd to [void*] in
* c_api.h. We expose them as [unit ptr] here; the high-level OCaml API
* wraps them in record types with phantom-distinguished tags.
*)openCtypesmoduleFunctions(F:Ctypes.FOREIGN)=structopenF(* bst_ulong = uint64_t in c_api.h *)letbst_ulong=uint64_t(* Both DMatrixHandle and BoosterHandle are [void*]. We use [ptr void] for
both; the high-level API recovers safety via phantom typing. *)letdmatrix_handle=ptrvoidletbooster_handle=ptrvoid(* ----- meta ----- *)letxgboost_version=foreign"XGBoostVersion"(ptrint@->ptrint@->ptrint@->returningvoid)letxgb_get_last_error=foreign"XGBGetLastError"(void@->returningstring)letxgb_set_global_config=foreign"XGBSetGlobalConfig"(string@->returningint)(* ----- DMatrix ----- *)letxgdmatrix_create_from_mat=foreign"XGDMatrixCreateFromMat"(ptrfloat@->bst_ulong@->bst_ulong@->float@->ptrdmatrix_handle@->returningint)(* Modern (>=2.0) dense constructor. [data] is a JSON
__array_interface__ string describing the buffer; [config] carries
{"missing": <float>}. Internally faster than the deprecated
XGDMatrixCreateFromMat above (~30-35% on a 100k×100 dense input
in our measurements). *)letxgdmatrix_create_from_dense=foreign"XGDMatrixCreateFromDense"(string@->string@->ptrdmatrix_handle@->returningint)(* Modern (>=2.0) sparse-CSR constructor. [indptr], [indices], [data]
are JSON __array_interface__ strings describing the underlying
buffers (which can be int32 / int64 / float32 / float64 — encoded
in the typestr field). The legacy XGDMatrixCreateFromCSREx was
removed from libxgboost's C API after 3.0 (its declaration is gone
from the 3.4 header), so we bind only this modern entry point. *)letxgdmatrix_create_from_csr=foreign"XGDMatrixCreateFromCSR"(string@->string@->string@->bst_ulong@->string@->ptrdmatrix_handle@->returningint)letxgdmatrix_set_float_info=foreign"XGDMatrixSetFloatInfo"(dmatrix_handle@->string@->ptrfloat@->bst_ulong@->returningint)letxgdmatrix_set_uint_info=foreign"XGDMatrixSetUIntInfo"(dmatrix_handle@->string@->ptruint@->bst_ulong@->returningint)letxgdmatrix_get_float_info=foreign"XGDMatrixGetFloatInfo"(dmatrix_handle@->string@->ptrbst_ulong@->ptr(ptrfloat)@->returningint)letxgdmatrix_get_uint_info=foreign"XGDMatrixGetUIntInfo"(dmatrix_handle@->string@->ptrbst_ulong@->ptr(ptruint)@->returningint)letxgdmatrix_num_row=foreign"XGDMatrixNumRow"(dmatrix_handle@->ptrbst_ulong@->returningint)letxgdmatrix_num_col=foreign"XGDMatrixNumCol"(dmatrix_handle@->ptrbst_ulong@->returningint)letxgdmatrix_num_non_missing=foreign"XGDMatrixNumNonMissing"(dmatrix_handle@->ptrbst_ulong@->returningint)letxgdmatrix_save_binary=foreign"XGDMatrixSaveBinary"(dmatrix_handle@->string@->int@->returningint)letxgdmatrix_free=foreign"XGDMatrixFree"(dmatrix_handle@->returningint)(* Build a child DMatrix containing a row subset of [parent], identified
by [idxset] (an array of [len] int32 row indices into the parent).
The child is a fresh DMatrix; libxgboost copies the relevant rows on
construction. Used by k-fold cross validation. *)letxgdmatrix_slice_dmatrix=foreign"XGDMatrixSliceDMatrix"(dmatrix_handle@->ptrint@->bst_ulong@->ptrdmatrix_handle@->returningint)(* ----- Booster ----- *)letxgbooster_create=foreign"XGBoosterCreate"(ptrdmatrix_handle@->bst_ulong@->ptrbooster_handle@->returningint)letxgbooster_free=foreign"XGBoosterFree"(booster_handle@->returningint)letxgbooster_reset=foreign"XGBoosterReset"(booster_handle@->returningint)letxgbooster_set_param=foreign"XGBoosterSetParam"(booster_handle@->string@->string@->returningint)letxgbooster_get_num_feature=foreign"XGBoosterGetNumFeature"(booster_handle@->ptrbst_ulong@->returningint)letxgbooster_boosted_rounds=foreign"XGBoosterBoostedRounds"(booster_handle@->ptrint@->returningint)letxgbooster_update_one_iter=foreign"XGBoosterUpdateOneIter"(booster_handle@->int@->dmatrix_handle@->returningint)(* Modern (>=2.0) custom-objective training: grad and hess as JSON
__array_interface__ strings instead of raw float* (the deprecated
XGBoosterBoostOneIter took those directly). *)letxgbooster_train_one_iter=foreign"XGBoosterTrainOneIter"(booster_handle@->dmatrix_handle@->int@->string@->string@->returningint)letxgbooster_eval_one_iter=foreign"XGBoosterEvalOneIter"(booster_handle@->int@->ptrdmatrix_handle@->ptrstring@->bst_ulong@->ptrstring@->returningint)letxgbooster_predict=foreign"XGBoosterPredict"(booster_handle@->dmatrix_handle@->int@->uint@->int@->ptrbst_ulong@->ptr(ptrfloat)@->returningint)(* Modern in-place predict path. [values] is a JSON-encoded
__array_interface__ object describing the input pointer and shape.
[m] may be NULL or a proxy DMatrix carrying meta info. The output
shape is written through [out_shape] (a borrowed const bst_ulong*
of length out_dim) and the predictions through [out_result]. *)letxgbooster_predict_from_dense=foreign"XGBoosterPredictFromDense"(booster_handle@->string@->string@->dmatrix_handle@->ptr(ptrbst_ulong)@->ptrbst_ulong@->ptr(ptrfloat)@->returningint)(* ----- Persistence ----- *)letxgbooster_save_model=foreign"XGBoosterSaveModel"(booster_handle@->string@->returningint)letxgbooster_load_model=foreign"XGBoosterLoadModel"(booster_handle@->string@->returningint)letxgbooster_save_model_to_buffer=foreign"XGBoosterSaveModelToBuffer"(booster_handle@->string@->ptrbst_ulong@->ptr(ptrchar)@->returningint)letxgbooster_load_model_from_buffer=foreign"XGBoosterLoadModelFromBuffer"(booster_handle@->ptrvoid@->bst_ulong@->returningint)letxgbooster_save_json_config=foreign"XGBoosterSaveJsonConfig"(booster_handle@->ptrbst_ulong@->ptr(ptrchar)@->returningint)letxgbooster_load_json_config=foreign"XGBoosterLoadJsonConfig"(booster_handle@->string@->returningint)(* Per-feature importance scores. [config] is a JSON string with at
least {"importance_type": "weight"|"gain"|"cover"|...}. The output
is two borrowed arrays: [features] (n_features strings) and
[scores] (a tensor of shape [out_shape] whose product equals the
score length). For non-multiclass models out_dim=1 and the shape
is just [n_features]. *)letxgbooster_feature_score=foreign"XGBoosterFeatureScore"(booster_handle@->string@->ptrbst_ulong(* out_n_features *)@->ptr(ptr(ptrchar))(* out_features *)@->ptrbst_ulong(* out_dim *)@->ptr(ptrbst_ulong)(* out_shape *)@->ptr(ptrfloat)(* out_scores *)@->returningint)end