Docs · Machine learning
clio-ml API reference.
The Python package is a thin layer over the nanobind extension clio_ml._clio_ml_native. Per-algorithm options, results and predictors are generated from the C++ headers, so their names match the C++ API field for field. Each algorithm page lists its own options and result fields.
- Package
- clio_ml
- Native module
- clio_ml.native
- C++ headers
- clio_ml/dataset.h, clio_ml/assimilate.h
Python package: clio_ml
Module attributes
clio_ml.backend:"cpu","cuda"or"sycl", whichever the extension was built for.clio_ml.has_cae:Truewhen the build links the Context Assimilation Engine client, soassimilateworks.clio_ml.COLUMNS: dict from algorithm name to the number of trailing non-feature columns a row has. 0 when every column is data (kmeans, pca, lda, fim, seq), 1 for one label column, 2 for aft (time, event).Nonefor als, pic, w2v and iso, whose rows are fixed records and take no column argument.clio_ml.native: the extension module. It exposes everyFit*,Fit*Plain,Fit*Reference,Mine*and*Predictfunction, everyOptions,ModelandResultclass,VecStats,GlmKind,Datasetandcae_assimilate.clio_ml.GlmKind: enum withlinear,logisticandsvm.
Runtime
init(): starts the in-process runtime in client mode (CLIO_INIT(RuntimeMode::kClient, true)) and the Context Transfer Engine client. RaisesRuntimeErrorif either fails. Required byDataset,dataset,attach,assimilateand every paged fit. The in-memory fits do not need it.finalize(): stops the runtime (CLIO_RUNTIME_FINALIZE()).
Datasets
dataset(X, tag=None, page_bytes=16384, nblocks=4, cache_pages=None, gpu_ids=(0,)): a pagedDatasetholdingX.Xis converted to a C-contiguous float32 2-D array; anything else raisesValueError. The page size is rounded down to whole rows, the block count to at most one block per page, andset_sizeiscache_pages(all pages whenNone) divided across the blocks. The tag defaults toclio_ml_py_<n>. Ends withDataset.preload(X).attach(tag, n_rows, n_cols, page_bytes=16384, nblocks=4, cache_pages=None, gpu_ids=(0,)): aDatasetover pages that already exist undertag, or that the CAE will fault in, with no preload.n_rows,n_colsandpage_bytesmust match the geometry the pages were written with.assimilate(src, columns, tag=None, cache_pages=None, stage_dir=None, on_fault=True, fill_value=None, page_bytes=16384, nblocks=4, gpu_ids=(0,)): stagescolumnsof the CSV or Parquet filesrcwithstage_table, builds aDatasetwithattach, and maps the staging file onto its tag through the CAE. Returns(dataset, info)whereinfois a dict withtag,n_rows,n_cols,n_pages,staged_pathandon_fault. The tag defaults toclio_ml_<file stem>_<pid>_<n>. The staging file is<tag>.f32instage_dir, else$CLIO_ML_STAGE_DIR, else the source file's directory. RaisesRuntimeErrorwhenhas_caeis false.stage_table(src, columns, out_path, page_bytes, fill_value=None, batch_rows=65536): writescolumnsofsrcas a page-aligned, row-major float32 file with whole rows per page and a zero-padded last page. Returns(n_rows, n_cols, page_bytes, n_pages), wherepage_bytesis the row-aligned size actually used. Needs no runtime. A Parquet source needspyarrow. Emptycolumnsor a file with no rows raiseValueError, an unknown columnKeyError, a non-numeric columnTypeError, and missing valuesValueErrorunlessfill_valueis given.
Dataset class
Dataset(tag, gpu_ids, page_bytes, nblocks, set_size, n_samples, n_features) binds the C++ constructor below. Read-only properties: num_rows, num_features, num_pages, elems_per_page, num_blocks, page_bytes. Method preload(x) writes a C-contiguous float32 array of exactly num_rows * num_features elements into the paged store; a different size raises ValueError.
Fitting
fit_<name>(X, **options) exists for every name in COLUMNS. X is a 2-D array (in memory, no runtime) or a Dataset (paged). Options are keyword arguments; each is set on a fresh instance of the algorithm's native options class, so the defaults are the C++ defaults. An unknown name raises TypeError. A dict value fills a nested options object field by field, for example fit_rf(X, num_trees=4, tree={"max_depth": 3}). The mapping from name to native stem and options class:
kmeans KMeans KMeansOptions 0
linreg Glm (GlmKind.linear) GlmOptions 1
logreg Glm (GlmKind.logistic) GlmOptions 1
svm Glm (GlmKind.svm) GlmOptions 1
nb NaiveBayes NaiveBayesOptions 1
pca Pca PcaOptions 0
knn KnnClassify KnnOptions 1
tree DecisionTree DecisionTreeOptions 1
rf RandomForest RandomForestOptions 1
gbt Gbt GbtOptions 1
gbtc Gbt (logistic=True) GbtOptions 1
als Als AlsOptions fixed records
gmm Gmm GmmOptions 1
mlp Mlp MlpOptions 1
bkm BisectingKMeans BisectingKMeansOptions 1
fm Fm FmOptions 1
pglm PoissonGlm PoissonGlmOptions 1
lda Lda LdaOptions 0
fim FrequentItemsets FrequentItemsetOptions 0 (Mine*)
aft Aft AftOptions 2
pic Pic PicOptions fixed records
w2v Word2Vec Word2VecOptions fixed records
enet ElasticNet ElasticNetOptions 1
iso Isotonic IsotonicOptions fixed records
seq SequentialPatterns SequentialPatternOptions 0 (Mine*)
mlr MultinomialLogistic MultinomialLogisticOptions 1- For a
Dataset,fit_<name>calls the nativeFit<Stem>(ds, ..., options)(Mine<Stem>for fim and seq,KnnClassifyfor knn). For an array it callsFit<Stem>Plain(X.ravel(), n_rows, n_features, elems_per_page, nblocks, ..., options)with a 16 KiB page holding whole rows and at most four blocks; then_featuresargument is omitted for the fixed-record algorithms. - Extra arguments:
fit_kmeans(..., init_centroids=)andfit_gmm(..., init_means=)default to the firstkrows of an array (kdefaults to 8 for kmeans and 2 for gmm when not given) and are required for aDataset.fit_knn(..., queries=)is always required. - Results are the native
*Resultobjects. Paged results carrystats, aVecStatswithfaults,puts,evicts,get_errorsandput_errors.
Prediction
predict(name, result, x) evaluates one feature row with a fitted result. x is converted to a contiguous float32 array (a float for iso). The dispatch:
- linreg, logreg, svm:
GlmPredict(kind, result.weights, x). - pglm:
PoissonGlmPredict(result.coef, x). - iso:
IsotonicPredict(result.model, float(x)). - nb, tree, rf, gbt, gbtc, gmm, mlp, bkm, fm: the algorithm's
*Predict(result.model, x). - mlr:
MultinomialLogisticPredict(result, x); aft:AftPredictMedian(result, x). - Any other name raises
ValueError.
Environment variables
CLIO_ML_DLL_PATH(Windows only): directories, separated by;, added withos.add_dll_directorybefore the extension is imported.CLIO_ML_STAGE_DIR: default directory forassimilatestaging files whenstage_diris not given.CLIO_SERVER_CONF,CLIO_BIND_ADDR,CLIO_SERVER_ADDR: read by the runtime thatinit()starts. See the Context Runtime docs. On Windows set both addresses to127.0.0.1.
C++
clio_ml/dataset.h
namespace clio_ml {
using u32 = clio::run::u32;
using u64 = clio::run::u64;
class Dataset {
public:
Dataset(const std::string &tag_name, const std::vector<int> &gpu_ids,
u64 page_bytes, u32 nblocks, u32 set_size, u64 n_samples,
u32 n_features,
clio::run::PoolId storage_pool_id = clio::run::PoolId::GetNull(),
u32 nsets = 0); // GPU cache sets; 0 = one per block
u64 NumRows() const;
u32 NumFeatures() const;
u64 RowsPerPage() const;
u64 PageBytes() const;
u64 ElemsPerPage() const;
u64 NumElems() const; // n_samples * n_features
u64 NumPages() const;
u32 NumBlocks() const; // drivers must launch exactly this many blocks
u64 RowOffset(u64 r) const;
PagedVec &Vec(); // cpu_vector::Vector<float> or gpu_vector::Vector<float>
clio::cte::gpu_vector::DeviceVector<float> GetDevice(int gpu_id); // GPU builds only
};
}- The constructor throws
std::invalid_argumentunlesspage_bytes / sizeof(float)is a positive multiple ofn_features. Rows are never padded to a page boundary. - On the CPU backend (
CLIO_ML_CPU=1)gpu_idsandnsetsare ignored and the vector is acpu_vectorwith the same geometry. PagedVecandVecStats(PagedVec::Stats) come fromclio_ml/backend.h, which selects the vector namespace fromCLIO_ML_CPU.- Load data with
ds.Vec().Preload(ptr, count), or with the assimilate path below.
clio_ml/assimilate.h
namespace clio_ml {
inline bool HasCae(); // true when built with CLIO_ML_CAE
inline void AssimilateFloat32File(const std::string &tag,
const std::string &path,
std::uint64_t num_pages,
std::uint64_t page_bytes,
bool on_fault);
}tagis the dataset's tag name without theiowarp::prefix;pathis the local staged float32 file;num_pagesandpage_bytesareDataset::NumPages()andDataset::PageBytes().- It submits one CAE
AssimilationCtxwithsrc = "file::" + path,dst = "iowarp::" + tag,format = "binary",range_size = num_pages * page_bytes,chunk_size = page_bytesand an emptyblob_prefix, so blobiholds file bytes[i*page, (i+1)*page), named like the pager's pages. on_fault = truecopies nothing up front and makes the CAE the tag's fault handler; the file is read for the life of the tag.falseingests every page now.- Throws
std::runtime_errorwhen the build has no CAE or the ingest task fails. - Construct the
Datasetfirst (it creates the tag) and do not preload it.
Build-time macros
CLIO_ML_CPU: 1 on the CPU backend, 0 on CUDA and SYCL.CLIO_ML_CAE: 1 when the Python extension links the CAE client; defaults to 0.CTP_ENABLE_SYCL: set on the SYCL backend; the extension reportsbackend = "sycl"when it is defined andCLIO_ML_CPUis 0.