CLI Architecture & Latency Benchmarks
How OchreML achieves sub-millisecond Python imports and fast zero-dependency CLI execution via _ops.py and PEP 562 lazy loading.
The ochreml CLI is designed to feel like a native systems tool while sharing 100% of its core logic with the Python API and the MCP server (o´∀`o).
Zero-Dependency Shared Core (ochreml._ops)
Both ochreml._cli (human/script CLI) and ochreml._mcp (JSON-RPC agent server) are thin transport adapters over a single shared operations engine: python/ochreml/_ops.py.
ochreml <cmd> (python/ochreml/_cli.py) ──┐
├──> python/ochreml/_ops.py ──> Rust _ochreml core
ochreml mcp (python/ochreml/_mcp.py) ──┘_ops.py relies exclusively on Python's standard library (csv, json, math, random, pathlib, os, platform) plus the compiled ochreml Rust extension:
- Tabular ingestion: Parses CSV and TSV directly via
csv.DictReader, with mean imputation for numeric columns and deterministic sorted ordinal encoding for categorical columns. Parquet support is optional and auto-detectspyarrow,polars, orpandasonly when reading.parquet/.pqfiles. - Metrics & CV: Computes classification metrics (
accuracy,f1_macro,precision_macro,recall_macro,log_loss), regression metrics (r2,rmse,mae,mse), and K-fold splits in pure Python soevalandcomparenever pull in NumPy or scikit-learn. - Zero drift: Because CLI commands and MCP tools invoke the exact same
op_*functions (op_device_info,op_dataset_profile,op_inspect_dit,op_verify_dit,op_train,op_predict,op_eval,op_compare,op_bench), output schemas and validation rules stay identical across interfaces.
PEP 562 Lazy Loading for Fast Startup
Traditional Python ML libraries eagerly import dozens of submodules and hundreds of transitive dependencies at import time, making short-lived CLI invocations feel sluggish.
python/ochreml/__init__.py avoids this with PEP 562 module-level __getattr__:
_LAZY_SUBMODULESdefers loading all 29 public submodules (nn,linear_model,tree,ensemble,svm, etc.) until explicitly accessed._LAZY_IMPORTSmaps estimator names directly to their defining submodule soochreml train --model Ridgeloads onlyochreml._linear_baseand never importsnn,tree, orensemble.- Internal entry points (
_ops,_cli,_mcp) are excluded from top-level imports entirely, keepingimport ochremlminimal for regular Python users as well.
Measured Startup & CLI Latency Benchmarks
All numbers below are measured medians from OchreML v0.3.7a1 benchmark runs across two hardware profiles:
- CPU (4-core):
cpu_4c(ochreml_v037a1_benchmark_startup_cli_mcp_cpu_4c.csv) - GPU (2x CUDA):
cuda_2gpu(ochreml_v037a1_benchmark_startup_cli_mcp_cuda_2gpu.csv)
Python Import Startup Comparison
| Operation | CPU 4-core Median (ms) | CPU Loaded Modules | CUDA 2-GPU Median (ms) | CUDA Loaded Modules |
|---|---|---|---|---|
import ochreml | 0.85 | 200 | 0.61 | 204 |
from ochreml.linear_model import Ridge | 103.44 | 300 | 86.11 | 304 |
import ochreml.nn | 106.26 | 300 | 86.45 | 304 |
import sklearn | 1,202.19 | 1,384 | 987.45 | 1,388 |
from sklearn.linear_model import Ridge | 1,316.47 | 1,483 | 1,098.61 | 1,487 |
import torch | 1,708.71 | 1,137 | 1,497.92 | 1,147 |
Top-level import ochreml completes in under 1 ms (~200 modules loaded), and importing a single estimator or ochreml.nn is ~12x–17x faster than importing sklearn or torch.
End-to-End CLI & MCP Subprocess Latency
| Subprocess Workload | CPU 4-core Median (ms) | CUDA 2-GPU Median (ms) | Status |
|---|---|---|---|
cli: profile --json | 191.57 | 139.90 | PASS |
cli: eval .dit | 308.95 | 239.21 | PASS |
cli: inspect .dit | 311.68 | 241.46 | PASS |
cli: predict .dit | 317.16 | 244.91 | PASS |
cli: verify .dit | 319.94 | 234.37 | PASS |
cli: train Ridge | 327.43 | 236.80 | PASS |
cli: compare (cv=3) | 335.45 | 276.95 | PASS |
cli: info --json | 1,942.93 | 2,890.92 | PASS |
mcp: 6-step JSON-RPC roundtrip | 1,002.07 | 1,201.37 | PASS |
Reading the Numbers
- Routine CLI operations (
profile,train,inspect,verify,predict,eval,compare) finish end-to-end (including Python subprocess spawn, CSV loading, Rust compute, and.ditI/O) in 140–335 ms. cli: info --jsontakes 1.94 s on CPU and 2.89 s on 2-GPU CUDA because it intentionally probes every available hardware backend (cuda,wgpu,tpu) and checks optional Python packages (numpy,pyarrow,polars,pandas). Normal training and inference commands skip full environment discovery unless needed.