How-To Guides
Hardware Acceleration
Run OchreML across Rayon CPU, NVIDIA CUDA / Multi-GPU (cuda:all), cross-platform wgpu, and Google Cloud TPU (PJRT).
OchreML dispatches dense linear algebra, pairwise distances, reductions, and quantized histogram splits through a unified Backend<'a> abstraction (src/backend.rs). Every hardware backend degrades gracefully to Rayon-parallel CPU (f64) when an accelerator or driver is unavailable. (o´∀`o)
Device Identifiers
Pass device="..." to any estimator constructor or call ochreml.set_default_device(...):
| Device String | Target Backend | Description |
|---|---|---|
"auto" | Best available | Probes hardware capabilities and selects the highest-priority ready accelerator, falling back to "cpu". |
"cpu" | Rayon CPU (f64) | Multi-threaded SIMD-friendly Rust execution across all host CPU cores. |
"cuda" or "cuda:0" | Single NVIDIA GPU | Dynamic CUDA Driver (libcuda.so.1) + cuBLAS (libcublas.so.12 / .11) in native FP64. |
"cuda:0,1" | Selected Multi-GPU | Shards rows across the listed CUDA device indices and reduces results on host. |
"cuda:all" | All NVIDIA GPUs | Uses every visible NVIDIA GPU in parallel via std::thread::scope. |
"wgpu" | Cross-platform GPU | Pure-Rust wgpu compute shaders (Vulkan, Metal, DX12) with Split-K + Kahan summation. |
"tpu" or "tpu:0" | Google Cloud TPU | Dynamic OpenXLA PJRT C API + StableHLO MLIR (libtpu.so / libpjrt_c_api_tpu.so). |
Inspecting Detected Hardware
import ochreml
print(ochreml.get_available_devices())
# Example on dual-GPU machine: ['cpu', 'cuda:0', 'cuda:1', 'cuda:all']
print(ochreml.get_device_info())Set OCHREML_DEBUG=1 in your environment to print diagnostic backend selection and dynamic library loading logs to stderr. Normal runs stay completely silent.
Backend Details
CPU (Rayon f64)
- Always compiled in and always available as the correctness baseline.
- Uses
rayonparallel iterators across all logical CPU cores (std::thread::available_parallelism()). - Ideal for small-to-medium tabular datasets where PCIe transfer latency outweighs GPU compute gains, and for branch-heavy single trees (
DecisionTreeClassifier,DecisionTreeRegressor).
NVIDIA CUDA & Multi-GPU (cuda:all)
- Zero bundled toolkit: Loads
libcuda.so.1andlibcublas.so.12(falling back to.11) dynamically at runtime vialibloading. - Native FP64 precision: Uses
cublasDsyrk_v2,cublasDgemm_v2,cublasDgemv_v2, andcublasDdgmm, plus custom CUDA kernels for reductions (N1), elementwise ops (N2), top-k (N4), andu8quantized histogram splits (N6). - Cached contexts:
CUcontextand cuBLAS handles are cached per device viaOnceLock, avoiding repeated initialization overhead. - Multi-GPU sharding:
cuda:allandcuda:0,1partition row blocks across GPUs in parallel threads and reduce Gram matrices (X^T X,X^T y) on the host.
from ochreml.linear_model import Ridge
model = Ridge(alpha=1.0, device="cuda:all")
model.fit(X_train, y_train)Cross-Platform GPU (wgpu)
- Pure-Rust GPU compute backend targeting Vulkan (Linux/Windows/AMD/Intel/NVIDIA), Metal (macOS Apple Silicon), and DX12 (Windows) without proprietary SDKs.
- Explicitly rejects software rasterizers (
wgpu::DeviceType::Cpu, such asllvmpipe) so CPU Rayon is used instead of slow software emulation. - Uses WGSL
f32compute shaders with Rayon-parallelf64 -> f32host packing, single-upload buffer reuse, Split-K parallel reduction (up to 64 slices), and Kahan compensated summation followed byf64host reduction to preserve numerical stability.
from ochreml.decomposition import PCA
pca = PCA(n_components=16, device="wgpu")
X_proj = pca.fit_transform(X)Google Cloud TPU (tpu:0 via PJRT)
- Dynamically loads
libtpu.soorlibpjrt_c_api_tpu.sovia the OpenXLA PJRT C API. - On Google Colab and Kaggle TPU runtimes (
v5e-1,v6e-1),ochreml/__init__.pyautomatically discovers Python-packagedlibtpu(libtpu.get_library_path()) and setsTPU_LIBRARY_PATHif neitherTPU_LIBRARY_PATHnorPJRT_PLUGIN_LIBRARY_PATHis set. - Emits StableHLO MLIR using
stablehlo.dot_generalwithprecision = [HIGHEST, HIGHEST]and power-of-two padded dimensions to minimize XLA recompilations, enforcing explicit row-major host transfers (minor_to_major = [1, 0]). - Branch-heavy histogram/tree models (
N6) automatically fall back to CPU Rayon on TPU while linear, decomposition, clustering, and distance-matrix models execute on TPU MXUs.
import ochreml
from ochreml.linear_model import LinearRegression
ochreml.set_default_device("tpu:0")
model = LinearRegression()
model.fit(X_train, y_train)Choosing the Right Device
| Workload Profile | Recommended Device |
|---|---|
| Small datasets (under 10k rows) or low-latency scripts | "cpu" or "auto" |
| Large dense linear models, PCA/SVD, KMeans, KNN, or HistGradientBoosting | "cuda:0" or "cuda:all" |
| Apple Silicon (M1–M4), AMD, or Intel GPUs | "wgpu" |
| Large matrix workloads on Colab / Kaggle / GCP TPU VMs | "tpu:0" |