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:
| ID | Primitive | Core Operation | Primary Consumers |
|---|---|---|---|
| N7 | batched_matmul & transpose_last2 | 3D/4D batched tensor contraction [B, H, M, K] x [B, H, K, N] | MultiHeadAttention, ScaledDotProductAttention, RNN, GRU, LSTM |
| N8 | row_layernorm / row_rmsnorm, gelu / silu, causal_mask | Row-wise normalization (forward + backward), modern activations, upper-triangular causal masking | TransformerEncoder, TransformerDecoder, CausalLM, SwiGLU, deep MLPs |
| N9 | cross_entropy_grad & adamw_step | Fused softmax + NLL loss + dLogits and decoupled AdamW parameter update (m, v, weight_decay) | CrossEntropyLoss, AdamW, Sequential.fit(), CausalLM.train_steps() |
| N10 | im2col / col2im & pool2d | 2D patch-to-matrix transforms enabling Conv2d on top of E1/E2 GEMM without cuDNN | Conv2d, 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, fusedN9loss, 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()andCausalLM.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 ID | Constant | Model Type | Contents |
|---|---|---|---|
| 100 | TAG_NN_SEQUENTIAL | nn.Sequential | Layer architecture descriptors, dtype, weights, biases, and BatchNorm2d running buffers |
| 101 | TAG_NN_TRANSFORMER_LM | nn.CausalLM | Hyperparameters (vocab_size, d_model, num_layers, num_heads, num_kv_heads, rope_theta) and all transformer weights |
| 102 | TAG_NN_STATE_DICT | nn.LoRAAdapterState | Named 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, PyTorch2.11.0+cpu, Python3.13.18, Linux x86_64 (4 CPU cores),device="auto"(cpu).
| Benchmark | Input Shape | OchreML Time (ms) | PyTorch Time (ms) | Speedup | OchreML 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.8x | 0.19 | 86.23 |
nn.Conv2d+BN+Pool (fwd + bwd) | (48, 3, 16, 16) | 8.54 | 196.26 | 23.0x | 0.67 | 11.91 |
nn.RNN (fwd + bwd) | (16, 16, 32) | 2.07 | 34.60 | 16.7x | 0.00 | 1.17 |
nn.GRU (fwd + bwd) | (16, 16, 32) | 3.70 | 31.01 | 8.4x | 0.00 | 1.19 |
nn.LSTM (fwd + bwd) | (16, 16, 32) | 4.32 | 85.47 | 19.8x | 0.00 | 4.16 |
nn.MultiHeadAttention (fwd + bwd) | (4, 32, 64) | 4.59 | 23.28 | 5.1x | 0.00 | 0.49 |
2. Multi-GPU Benchmark (2x CUDA GPU)
- Environment: OchreML
0.3.7a1, PyTorch2.11.0+cu130, Python3.13.16, Linux x86_64 (4 CPU cores + 2x CUDA GPUs:cuda:0,cuda:1,cuda:all),device="auto".
| Benchmark | Input Shape | OchreML Time (ms) | PyTorch Time (ms) | Speedup | OchreML 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.81x | 20.75 | 280.71 |
nn.Conv2d+BN+Pool (fwd + bwd) | (128, 3, 16, 16) | 49.18 | 3943.00 | 80.2x | 26.52 | 359.11 |
nn.RNN (fwd + bwd) | (32, 32, 32) | 4.34 | 181.98 | 42.0x | 1.65 | 45.32 |
nn.GRU (fwd + bwd) | (32, 32, 32) | 4.89 | 18.57 | 3.80x | 1.77 | 4.14 |
nn.LSTM (fwd + bwd) | (32, 32, 32) | 4.76 | 6.10 | 1.28x | 1.84 | 0.12 |
nn.MultiHeadAttention (fwd + bwd) | (8, 64, 64) | 17.77 | 52.26 | 2.94x | 3.12 | 20.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.