Skip to Content
OchreML

Architecture and Benchmarks

Deep learning primitives N7-N10, zero-GIL Rust training loops, .dit TAG 100-102 persistence, and verified PyTorch benchmarks on CPU and 2x CUDA GPU.

ochreml.nn achieves high training throughput and a ~21.8 MB Python wheel footprint by extending OchreML's shared Backend<'a> abstraction rather than bundling PyTorch, LLVM/XLA, or cuDNN (^▽^).


Deep Learning Backend Primitives (N7–N10)

Every neural network layer in ochreml.nn compiles down to the existing dense linear algebra primitives (E1–E5, N2) plus four deep learning primitives (N7–N10) in src/backend.rs:

IDPrimitiveCore OperationPrimary Consumers
N7batched_matmul & transpose_last23D/4D batched tensor contraction [B, H, M, K] x [B, H, K, N]MultiHeadAttention, ScaledDotProductAttention, RNN, GRU, LSTM
N8row_layernorm / row_rmsnorm, gelu / silu, causal_maskRow-wise normalization (forward + backward), modern activations, upper-triangular causal maskingTransformerEncoder, TransformerDecoder, CausalLM, SwiGLU, deep MLPs
N9cross_entropy_grad & adamw_stepFused softmax + NLL loss + dLogits and decoupled AdamW parameter update (m, v, weight_decay)CrossEntropyLoss, AdamW, Sequential.fit(), CausalLM.train_steps()
N10im2col / col2im & pool2d2D patch-to-matrix transforms enabling Conv2d on top of E1/E2 GEMM without cuDNNConv2d, DepthwiseConv2d, MaxPool2d, AvgPool2d, ResidualBlock

All primitives dispatch cleanly across:

  • CPU: AVX2 + FMA + Rayon parallelism
  • CUDA: Dynamic libcuda.so.1 + libcublas.so + persistent PTX kernels (zero bundled CUDA toolkit)
  • wgpu: Pure-Rust Vulkan / Metal / DX12 compute shaders
  • TPU: Dynamic OpenXLA PJRT + StableHLO MLIR (single compiled graph per training step)

Zero-GIL Rust Training & Inference

Standard Python deep learning loops re-acquire the Python GIL on every mini-batch, operator dispatch, and optimizer step. For small-to-medium batches, Python dispatch overhead often dominates actual math time.

ochreml.nn eliminates this bottleneck in its high-level entry points:

  • Sequential.fit() runs data shuffling, mini-batch slicing, forward pass, fused N9 loss, backward autograd tape traversal, optional gradient clipping, and optimizer updates 100% inside Rust.
  • RNN.train_steps(), GRU.train_steps(), LSTM.train_steps() execute multi-step BPTT + AdamW in a single FFI call.
  • CausalLM.train_steps() and CausalLM.generate() keep both multi-step language model updates and KV-cached token sampling loop completely free of Python overhead.

Binary .dit Persistence (TAG 100–102)

ochreml.nn extends OchreML's zero-dependency DIT\x01 binary format with three additive type tags so models remain portable across devices without pickle or PyTorch:

Tag IDConstantModel TypeContents
100TAG_NN_SEQUENTIALnn.SequentialLayer architecture descriptors, dtype, weights, biases, and BatchNorm2d running buffers
101TAG_NN_TRANSFORMER_LMnn.CausalLMHyperparameters (vocab_size, d_model, num_layers, num_heads, num_kv_heads, rope_theta) and all transformer weights
102TAG_NN_STATE_DICTnn.LoRAAdapterStateNamed low-rank adapter pairs (lora_a, lora_b, rank, alpha) without duplicating frozen base weights

Verified Benchmarks vs. PyTorch

All figures below come directly from the OchreML v0.3.7a1 benchmark suite against PyTorch 2.11.0 on identical shapes, precision (float32), and device="auto".

1. CPU Benchmark (4-Core x86_64 Linux)

  • Environment: OchreML 0.3.7a1, PyTorch 2.11.0+cpu, Python 3.13.18, Linux x86_64 (4 CPU cores), device="auto" (cpu).
BenchmarkInput ShapeOchreML Time (ms)PyTorch Time (ms)SpeedupOchreML RAM Delta (MB)PyTorch RAM Delta (MB)
nn.Sequential (MLP 32-64-2, fit + pred)(1024, 32)7.52 (7.34 fit / 0.18 pred)6756.34 (6755.95 fit / 0.40 pred)898.8x0.1986.23
nn.Conv2d+BN+Pool (fwd + bwd)(48, 3, 16, 16)8.54196.2623.0x0.6711.91
nn.RNN (fwd + bwd)(16, 16, 32)2.0734.6016.7x0.001.17
nn.GRU (fwd + bwd)(16, 16, 32)3.7031.018.4x0.001.19
nn.LSTM (fwd + bwd)(16, 16, 32)4.3285.4719.8x0.004.16
nn.MultiHeadAttention (fwd + bwd)(4, 32, 64)4.5923.285.1x0.000.49

2. Multi-GPU Benchmark (2x CUDA GPU)

  • Environment: OchreML 0.3.7a1, PyTorch 2.11.0+cu130, Python 3.13.16, Linux x86_64 (4 CPU cores + 2x CUDA GPUs: cuda:0, cuda:1, cuda:all), device="auto".
BenchmarkInput ShapeOchreML Time (ms)PyTorch Time (ms)SpeedupOchreML RAM Delta (MB)PyTorch RAM Delta (MB)
nn.Sequential (MLP 32-64-2, fit + pred)(4096, 32)1290.54 (1290.22 fit / 0.32 pred)6205.12 (6204.65 fit / 0.47 pred)4.81x20.75280.71
nn.Conv2d+BN+Pool (fwd + bwd)(128, 3, 16, 16)49.183943.0080.2x26.52359.11
nn.RNN (fwd + bwd)(32, 32, 32)4.34181.9842.0x1.6545.32
nn.GRU (fwd + bwd)(32, 32, 32)4.8918.573.80x1.774.14
nn.LSTM (fwd + bwd)(32, 32, 32)4.766.101.28x1.840.12
nn.MultiHeadAttention (fwd + bwd)(8, 64, 64)17.7752.262.94x3.1220.82

The trained nn.Sequential MLP (32-64-2) serializes to just 8.84 KB in .dit (Tag 100) format on both CPU and CUDA runs, with up to 13x–450x lower host RAM overhead than PyTorch during training.

On this page