ML Runtime.
From Scratch.
A complete machine learning infrastructure written in C. Tensor algebra, automatic differentiation, transformer architecture, RLHF alignment, distributed training, and GPU compute — all implemented without PyTorch or TensorFlow.
Every Operation, Native C.
Eight data types, SIMD-accelerated arithmetic, and a full set of neural network primitives. No Python wrappers. No foreign function interfaces.
Data Types
Arithmetic
add, sub, mul, div, pow, neg, scalar_mul with broadcasting and dtype promotion across all 8 types.
Math
sqrt, rsqrt, log, exp, sum, mean, max, min, var, std — all with axis parameter and keepdim support.
Normalization
LayerNorm, RMSNorm, BatchNorm — the three normalization primitives that cover every modern architecture.
Activations
ReLU, GELU, SiLU, sigmoid, tanh, softmax, log_softmax, leaky_relu — every activation used in production models.
Linear Algebra
matmul, scaled_dot_product_attention with GQA and RoPE. BLAS wrapper for optimized matrix operations.
27 Backward Functions.
A computation graph recorder that tracks every tensor operation and computes exact gradients via reverse-mode automatic differentiation. Supports higher-order derivatives through nested graph traversal.
// Forward pass records the graph let x = tensor([2.0, 3.0]); let w = tensor([0.5, 1.0]).requires_grad(true); let y = (x * w).sum(); // Backward pass computes gradients y.backward(); // w.grad => [2.0, 3.0] (dy/dw = x) // x.grad => [0.5, 1.0] (dy/dx = w)
Full Transformer Stack.
Multi-head attention with GQA, rotary position embeddings, KV caching, and pre-norm blocks. Every component that makes a modern LLM work.
Multi-Head Attention
Grouped-query attention (GQA) with configurable head count. Scaled dot-product attention with optional causal masking.
attention.cRotary Position Embedding
RoPE applied to query and key vectors for relative position encoding. No learned positional embeddings.
rope.cKV Cache
Pre-allocated contiguous tensor for autoregressive generation. Stores key-value pairs across layers for O(1) per-token inference.
kv_cache.cTransformer Block
Pre-norm architecture with RMSNorm or LayerNorm, attention sublayer, and SwiGLU feed-forward network.
transformer_block.cFeed-Forward Network
SwiGLU activation with configurable hidden dimension. The standard FFN used in LLaMA and modern open-source models.
ffn.cToken Sampling
Greedy, top-k, top-p (nucleus), and temperature-controlled sampling for text generation.
sampling.cTraining Infrastructure.
A training loop with checkpointing, gradient accumulation, mixed precision, early stopping, and distributed data parallelism — under active development.
Optimizers
SGD with momentum and Nesterov, Adam, AdamW with decoupled weight decay, Lion.
LR Schedulers
Linear warmup, cosine decay, cosine with warmup, step decay, inverse square root.
Memory Efficiency
Gradient checkpointing, gradient accumulation, mixed precision (FP16/BF16), arena-based tensor allocation.
Checkpointing
Save/load model state, optimizer state, scheduler state, and training metrics. Version tracking with epoch numbers.
Early Stopping
Patience-based early stopping with configurable metric tracking. Prevents overfitting without manual intervention.
Weight Initialization
Zeros, ones, uniform, normal, Xavier (Glorot), Kaiming (He) initialization — matching every initialization scheme used in research.
RLHF Built In.
Reinforcement Learning from Human Feedback isn't an add-on — it's part of the core runtime. Four alignment algorithms, a reward model, and a safety filter.
PPO
Proximal Policy Optimization with clipping, KL penalty, and Generalized Advantage Estimation (GAE).
DPO
Direct Preference Optimization — skip the reward model entirely. Train from preference pairs.
GRPO
Group Relative Policy Optimization — compare outputs within a group for relative ranking.
Safety Filter
Keyword, pattern, and topic blocking on both input and output. Configurable safety weighting.
Six Methods.
From post-training quantization to quantization-aware training, with FP8, weight-only, dynamic, and KV cache quantization. Run larger models on smaller hardware.
// Load from standard formats
let model = llm.load("model.gguf");
let model = llm.load_safetensors("weights.safetensors");
// Convert between formats
model_converter.convert(
source: "hf_config.json",
target: "model.gguf",
quantization: "q4_0"
);
// HuggingFace integration
let config = hf_config.load("config.json");Two GPU Backends.
CUDA Backend
285 linesRuntime compilation via NVRTC. Device memory management, kernel launch, and dedicated CUDA kernels for activations, elementwise ops, and matrix multiplication.
Vulkan Compute
446 linesVulkan compute pipelines with descriptor set management. 7 GLSL compute shaders for core operations.
Scale Across GPUs.
Seven parallelism strategies with NCCL-backed communication. From single-machine data parallelism to multi-node pipeline parallelism.
Data Parallel
Gradient all-reduce across workers
Tensor Parallel
Split matrices across devices
Pipeline Parallel
Micro-batch pipeline scheduling
MoE Routing
Mixture of Experts dispatch
ZeRO Sharding
Optimizer state partitioning
Distributed AMP
Mixed precision across nodes
Dist. Checkpoint
Fault-tolerant saving
See It In Action.
The ML runtime ships as part of the Braid standard library. Import std.tensor, std.nn, and std.train to build and train models natively.