Compute backends

Operations are logical Op variants (matvec, matmul, layer norm, SiLU/GLU, AdamW, sync update, super-linear, RoPE, dequant-matvec, and more). A registry sends each one to the first backend that supports it, in the order KFD → ROCm → CUDA → Vulkan → CPU. A new backend implements one trait. MODGRAD_BACKEND=cpu forces the CPU path.

Backends

backend implementation
CPU rayon-parallel, with AVX-512 / AVX2 paths; the reference every other backend is checked against
AMD ROCm hipBLAS (sgemv, sgemm, strided-batched, bf16 gemmEx), MIOpen (layer-norm, softmax, activations, GLU), and custom HIP kernels for RMSNorm, RoPE and Q4_K/Q5_K/Q6_K dequant-matvec
NVIDIA CUDA cuBLAS via cudarc, loaded at runtime; no nvcc or toolkit needed to build
AMD KFD hand-written RDNA3 assembly kernels with BAR-mapped VRAM. Opt-in (--features kfd plus MODGRAD_ENABLE_KFD=1): it has an unresolved compute-ring stall
Vulkan compute-shader fallback for any Vulkan 1.3 driver (e.g. Steam Deck, Intel Arc, Apple Silicon via MoltenVK)

ROCm takes matvec and matmul only from about 64×64 up; below that the CPU is faster. A HipBatch guard flushes the queue periodically so thousands of small dispatches per step don’t overflow it.

Residency

The main performance lever is keeping weights on the GPU instead of copying them across PCIe on every call. A GpuVec is a device buffer; the resident Linear types cover the common cases:

  • LinearResident (fp32) and LinearResidentBf16 (half the memory)
  • LinearResidentQuantized: Q4_K weights on the host, dequantised into an fp32 device buffer on upload
  • LinearResidentStreaming: weights uploaded on demand and optionally evicted after each call, so layers can take turns in 8 GB of VRAM

Resident matvec at 1024×512 runs 55× and 5.6× faster than the path that copies through the host, on the GPU below.

Measured

End-to-end maze training on an AMD Radeon RX 7600M XT (gfx1102), single runs (docs/GPU_BENCHMARKS.md). Rows differ in maze size and steps, so compare within a row:

d_model maze steps CPU ROCm ROCm time saved
128 11×11 500 140 s 144 s −3% (dispatch overhead dominates)
256 21×21 300 657 s 603 s +8%
384 21×21 200 465 s 393 s +15%
512 21×21 200 696 s 661 s +5%

Eval results were bit-identical between CPU and ROCm in every row.

  • Orchestration-bound, not FLOP-bound. A maze step issues about 23,000 dispatches, and the CPU ran at about half its boost clock under load, so much of the GPU’s gain is taking work off the CPU.
  • Single runs vary up to about 1.5× with CPU clock capping: a repeat of the d_model 384 ROCm run took 594 s instead of 393 s.
  • Not everything helped. Routing the backward weight-gradient matvec (MatvecT) through hipBLAS was 2.3× slower than the CPU at d_model 384, and was reverted.
  • The CUDA path has not been validated on NVIDIA hardware. KFD is reliable only for single-workgroup matvec.

Next: log to modgrad from Rust →