tpt-ml-proven

Rust

Proven ML algorithms in Rust, built on the TPT math stack, with Python and Go bindings.

0 stars0 forks0 watchersApache License 2.0

Languages

Rust79.6%Python6.4%C4.5%Go3.8%Jupyter Notebook2.6%JavaScript2.6%HTML0.3%Just0.2%Shell0.1%
README

tpt-ml-proven

Classical machine learning in Rust with proven correctness: the algorithms carry requires / ensures contracts, checked by a self-contained QF_LRA solver where the clauses are linear and by runtime checks everywhere else.

Status: pre-release (v0.1 in progress). Most of the planned algorithms are implemented and checked against scikit-learn on committed fixtures. The tree models, the model file format and the bindings are in place. Release steps (publishing, tagging, the Go import path) are not done.

Quickstart

Nothing is published to a registry yet, so install from a checkout of this repository.

Rust, in a crate that depends on tpt-ml-proven by path:

use tpt_ml_proven::prelude::*;

let data = [0.0, 0.0, 0.1, 0.0, 10.0, 10.0, 10.1, 10.0];
let x = DenseView::new(&data, 4, 2).unwrap();
let mut km = KMeans::<f64>::new(2).with_seed(1);
UnsupervisedEstimator::fit(&mut km, x).unwrap();
let labels = Predictor::predict(&km, DenseView::new(&data, 4, 2).unwrap()).unwrap();

Python, after maturin develop --release in ffi/python:

from tpt_ml_proven import KMeans

labels = KMeans(2, seed=1).fit(X).labels_   # X is a 2-D NumPy array

JavaScript, after node scripts/build.mjs in ffi/wasm:

const km = new KMeansModel({ nClusters: 2, seed: 1 });
km.fit(new Float64Array([0, 0, 0.1, 0, 10, 10, 10.1, 10]), 2);
const labels = km.labels;

Algorithms

CrateEstimators
tpt-ml-proven-clusteringKMeans, DBSCAN, HDBSCAN
tpt-ml-proven-decompositionPCA, TruncatedSVD, NMF
tpt-ml-proven-linearRidge, Lasso, ElasticNet, logistic regression (binary and multinomial)
tpt-ml-proven-preprocessingStandardScaler, MinMaxScaler, RobustScaler, OneHotEncoder, OrdinalEncoder, SimpleImputer
tpt-ml-proven-ensembleStumpForest
tpt-ml-proven-treesDecisionTreeRegressor, DecisionTreeClassifier, GradientBoostingRegressor, RandomForestRegressor, RandomForestClassifier

Each estimator's contract is in docs/contracts/. Parity with scikit-learn is recorded in docs/parity.md.

Verification

Each algorithm has a contract in docs/contracts/, written as requires and ensures clauses. The linear clauses are checked by a QF_LRA solver, tpt-solver, and every UNSAT answer is accepted only after its certificate is checked. Clauses outside linear arithmetic (for example monotone inertia descent, or orthogonality) are checked at run time by runtime assertions. CI fails if an implemented algorithm has a contract clause without its runtime check. Results are compared with scikit-learn to 1e-9 or 1e-6, depending on the algorithm, on committed fixtures. The verifier and the solver are separate, dev-only tools; the library crates have no solver dependency.

Tree models add invariants that are easy to state and check: the leaves of a tree partition the training rows, depth is bounded, and each boosting round does not increase the training error.

Model files

Fitted models can be written to and read from a versioned binary format, specified in docs/model-format.md. The format has a magic header, a version, a model kind, little-endian fields and a checksum. Readers refuse newer versions and any file whose contents don't fit the model's invariants. Golden files from version 1 are committed and tested on every run.

The Rust crates, the Python package and the WebAssembly package read and write model files. The C and Go bindings do not expose them yet.

Bindings

DirectoryBindingWhat it covers
ffi/pythonPython (PyO3, abi3 wheels)Every estimator and transformer, with model files
ffi/wasmWebAssembly (wasm-bindgen)Every estimator and transformer, with model files; single precision for KMeans and PCA
ffi/cC ABI (cbindgen header)Every estimator and transformer; single precision for KMeans and PCA
ffi/goGo (cgo over the C ABI)Clustering, decomposition, the linear models and StumpForest; single precision for KMeans and PCA. Trees and transformers are not wrapped yet

See docs/usage.md for examples of each binding.

Documentation

Goals

  • Proven correctness — contracts, not just tests.
  • Modern API — type-safe estimators, zero-copy where possible, no_std + alloc core.
  • Scikit-compatible surface — familiar fit / transform / predict semantics.
  • FFI-first — Python, WebAssembly, C and Go bindings, generated or thin rather than hand-rolled.

See docs/DESIGN.md for the full design specification.

Workspace

Each crate has its own README and changelog.

CrateStatusPurposeDocs
tpt-ml-proven-coreImplementedEstimator traits, contracts, shared errors, model file format (no_std + alloc)README · CHANGELOG
tpt-ml-proven-clusteringKMeans, DBSCAN, HDBSCAN implementedClustering estimatorsREADME · CHANGELOG
tpt-ml-proven-decompositionPCA, TruncatedSVD, NMF implementedMatrix decomposition estimatorsREADME · CHANGELOG
tpt-ml-proven-linearRidge, Lasso, ElasticNet, logistic implementedLinear modelsREADME · CHANGELOG
tpt-ml-proven-preprocessingScalers, encoders, imputers implementedPreprocessing transformersREADME · CHANGELOG
tpt-ml-proven-ensembleStumpForest implementedBagged decision stumpsREADME · CHANGELOG
tpt-ml-proven-treesDecisionTreeRegressor, GradientBoostingRegressor implementedTree models with structural invariantsREADME · CHANGELOG
tpt-ml-proven-ffiNo code of its ownBindings live in ffi/README · CHANGELOG

Workspace-wide changes (contracts, the verifier, parity tests, bindings, the file format) are recorded in the root changelog.

Building

cargo build --workspace
cargo test --workspace

The toolchain is pinned to stable via rust-toolchain.toml. The bindings build from their own directories; see each binding's README.

Licence

Dual-licensed under either of

at your option.

Copyright (c) 2026 TPT Solutions.

Contributing

See CONTRIBUTING.md.