Mixed Precision and Quantization¶
Jacobi supports native XLA and GPU low-precision and integer dtypes including Dtype::BF16, Dtype::F16, Dtype::F8E4M3FN, Dtype::F8E5M2, Dtype::F4E2M1FN, Dtype::I8, Dtype::I16, and Dtype::U8.
Autocast (jit_autocast and with_autocast)¶
Wrap a compiled training step with jit_autocast(Dtype::BF16, ...) (or .autocast(Dtype::F16)) to automatically cast f32 inputs of matrix multiplications, einsum, and convolutions to bf16 or f16 with f32 accumulation while keeping model weights, reductions, norms, and losses in f32.
let train_step = jit_autocast(Dtype::BF16, move |(mut m, mut o): (MyNet, AdamW<MyNet>)| {
let (loss, grads) = value_and_grad(|p| nn::mse_loss(&p.forward(&x), &y), &m);
o.step(&mut m, &grads);
(m, o, loss)
});
You can also enable autocast inside any closure with with_autocast(Dtype::BF16, || ...).
Manual BF16 and FP16 Casting¶
A common, rock-solid pattern is to keep master weights and optimizer moments in f32, cast the model to bf16 inside the compiled step, and let XLA fuse the entire forward and backward pass.
let train_step = jit(move |(mut m, mut o): (MyNet, AdamW<MyNet>)| {
let (loss, grads) = value_and_grad(
|p| nn::mse_loss(&p.to_bf16().forward(&x.to_bf16()).to_f32(), &y),
&m,
);
o.step(&mut m, &grads);
(m, o, loss)
});
LayerNorm, RmsNorm, GroupNorm, nn::cross_entropy, and nn::scaled_dot_product_attention automatically run reductions and softmax in f32 when given low-precision inputs so training stays numerically stable.
FP8 and FP4 Training¶
For NVIDIA Hopper and Blackwell GPUs (as well as XLA CPU testing), Jacobi supports two complementary FP8/FP4 workflows.
- Master-weight scaled FP8/FP4 layers (
nn::fp8_linear,nn::fp8_matmul,nn::fp4_linear,nn::fp4_matmul) dynamically scalef32inputs and weights intoF8E4M3FN(±448) orF4E2M1FN(±6) withf32accumulation and straight-through gradients back tof32master weights. - Native FP8-stored weights (
w.cast(Dtype::F8E4M3FN)) viann::quantized_matmul(&x, &w)keep weights in 1-byteFP8in HBM, automatically dispatch nativeF8E4M3FN x F8E4M3FN -> BF16Tensor Core GEMMs on CUDA/TPU (falling back cleanly toBF16on CPU/Metal), auto-select dynamic per-tensor FP8 backward GEMMs on Blackwell (sm_100) and TPU vsBF16backward on Hopper (sm_90), and accumulate large stackedfor_loopFP8 weight gradients in scaled 1-byteF8E5M2.
// Master-weight scaled FP8 / FP4 layers
let h = nn::fp8_linear(&self.fc1, &x).relu();
let out_fp4 = nn::fp4_linear(&self.fc2, &h);
// Native FP8-stored weight matmul (auto Tensor Core on CUDA/TPU, BF16 fallback on CPU/Metal)
let y = nn::quantized_matmul(&x_bf16, &w_fp8);
You can also control accumulation precision directly on any matrix multiplication or Einstein summation with matmul_in and einsum_in.
let c = a.to_fp8_e4m3().matmul_in(&b.to_fp8_e4m3(), Dtype::F32);
let e = einsum_in("bhqd,bhkd->bhqk", &[&q_bf16, &k_bf16], Dtype::F32);
Exact Float32 Precision (Precision::Highest)¶
On NVIDIA Ampere, Hopper, and Blackwell GPUs, XLA defaults to TF32 tensor cores for f32 matmuls and convolutions. Wrap sensitive blocks in with_precision(Precision::Highest, || ...) or call set_default_precision(Precision::Highest) to emit StableHLO HIGHEST precision across forward and backward passes.
Integer Dtypes (I8, I16, and U8)¶
Dtype::I8, Dtype::I16 (16-bit PCM audio), and Dtype::U8 work end to end across PJRT, StableHLO, HostTensor, .safetensors, .npy, and .onnx export. Use matmul_in(..., Dtype::I32) or einsum_in(..., Dtype::I32) for integer quantized contractions with i32 accumulation.