Skip to content

Low-Rank Training (efficient_grad)

Full source in examples/efficient_grad.rs. Run it with cargo run --release --example efficient_grad.

What This Shows

  • Training a 4-layer deep MLP (1-1024-1024-1024-1) and a 4-layer Transformer language model (RmsNorm, SwiGLU, multi-head causal attention) with EfficientAdamW
  • Comparing full-rank AdamW against EfficientAdamW::new (Adafactor-style row/column second moment) and EfficientAdamW::subspace_variance across ranks 128, 64, 32, and 16
  • Measuring gradient memory, optimizer state memory, total non-weight RAM savings (86% to 95%+), convergence parity, and wall-clock time inside compiled for_loop training graphs

Swapping AdamW for EfficientAdamW

EfficientAdamW keeps the same functional loop structure as AdamW. Calling opt.value_and_grad(...) projects matrix gradients into a low-rank subspace (rank) during reverse-mode differentiation and updates factorized optimizer moments directly in that subspace.

let opt = EfficientAdamW::new(&model, lr, rank);

let train = jit(move |(m, o): (DeepMlp, EfficientAdamW<DeepMlp>)| {
    for_loop(steps, (m, o), |_, (mut m, mut o)| {
        let (_, g) = o.value_and_grad(|p| loss_fn(&p), &m);
        o.step(&mut m, &g);
        (m, o)
    })
});

For multi-GPU and multi-node training, EfficientAdamW provides value_and_grad_dp and value_and_grad_hybrid so inter-node AllReduce transfers only the compact rank-r factors instead of full dense gradients.

let (loss, grads) = opt.value_and_grad_dp(
    |p: MiniTransformer| p.loss(&tokens, &targets),
    &model,
    "dp",
);
opt.step(&mut model, &grads);

Running the Comparison

cargo run --release --example efficient_grad
JACOBI_BACKEND=cpu cargo run --release --example efficient_grad

At production scale (apples-to-apples under Fp8UpdateMode::StochasticRounding), rank-32 EfficientAdamW cuts optimizer state by 99.4% on Gemma-4-E4B (0.18 GB vs 29.71 GB), 98.5% on Inkling-2-Mini (0.26 GB/GPU vs 17.81 GB/GPU), and 99.0% on DeepSeek-V4-Flash (1.59 GB/GPU vs 160.97 GB/GPU), enabling 283B multi-node training on 16x H200 where full AdamW exceeds GPU memory even at batch 1 and delivering a 2.42x multi-node RDMA speedup on a 16-layer (106B) DeepSeek slice (140,018 vs 57,865 tok/s).