Skip to Content
OchreML

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-detects pyarrow, polars, or pandas only when reading .parquet/.pq files.
  • 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 so eval and compare never 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_SUBMODULES defers loading all 29 public submodules (nn, linear_model, tree, ensemble, svm, etc.) until explicitly accessed.
  • _LAZY_IMPORTS maps estimator names directly to their defining submodule so ochreml train --model Ridge loads only ochreml._linear_base and never imports nn, tree, or ensemble.
  • Internal entry points (_ops, _cli, _mcp) are excluded from top-level imports entirely, keeping import ochreml minimal 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

OperationCPU 4-core Median (ms)CPU Loaded ModulesCUDA 2-GPU Median (ms)CUDA Loaded Modules
import ochreml0.852000.61204
from ochreml.linear_model import Ridge103.4430086.11304
import ochreml.nn106.2630086.45304
import sklearn1,202.191,384987.451,388
from sklearn.linear_model import Ridge1,316.471,4831,098.611,487
import torch1,708.711,1371,497.921,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 WorkloadCPU 4-core Median (ms)CUDA 2-GPU Median (ms)Status
cli: profile --json191.57139.90PASS
cli: eval .dit308.95239.21PASS
cli: inspect .dit311.68241.46PASS
cli: predict .dit317.16244.91PASS
cli: verify .dit319.94234.37PASS
cli: train Ridge327.43236.80PASS
cli: compare (cv=3)335.45276.95PASS
cli: info --json1,942.932,890.92PASS
mcp: 6-step JSON-RPC roundtrip1,002.071,201.37PASS

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 .dit I/O) in 140–335 ms.
  • cli: info --json takes 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.

On this page