Skip to Content
OchreML
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 StringTarget BackendDescription
"auto"Best availableProbes 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 GPUDynamic CUDA Driver (libcuda.so.1) + cuBLAS (libcublas.so.12 / .11) in native FP64.
"cuda:0,1"Selected Multi-GPUShards rows across the listed CUDA device indices and reduces results on host.
"cuda:all"All NVIDIA GPUsUses every visible NVIDIA GPU in parallel via std::thread::scope.
"wgpu"Cross-platform GPUPure-Rust wgpu compute shaders (Vulkan, Metal, DX12) with Split-K + Kahan summation.
"tpu" or "tpu:0"Google Cloud TPUDynamic 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 rayon parallel 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.1 and libcublas.so.12 (falling back to .11) dynamically at runtime via libloading.
  • Native FP64 precision: Uses cublasDsyrk_v2, cublasDgemm_v2, cublasDgemv_v2, and cublasDdgmm, plus custom CUDA kernels for reductions (N1), elementwise ops (N2), top-k (N4), and u8 quantized histogram splits (N6).
  • Cached contexts: CUcontext and cuBLAS handles are cached per device via OnceLock, avoiding repeated initialization overhead.
  • Multi-GPU sharding: cuda:all and cuda:0,1 partition 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 as llvmpipe) so CPU Rayon is used instead of slow software emulation.
  • Uses WGSL f32 compute shaders with Rayon-parallel f64 -> f32 host packing, single-upload buffer reuse, Split-K parallel reduction (up to 64 slices), and Kahan compensated summation followed by f64 host 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.so or libpjrt_c_api_tpu.so via the OpenXLA PJRT C API.
  • On Google Colab and Kaggle TPU runtimes (v5e-1, v6e-1), ochreml/__init__.py automatically discovers Python-packaged libtpu (libtpu.get_library_path()) and sets TPU_LIBRARY_PATH if neither TPU_LIBRARY_PATH nor PJRT_PLUGIN_LIBRARY_PATH is set.
  • Emits StableHLO MLIR using stablehlo.dot_general with precision = [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 ProfileRecommended 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"

On this page