Skip to content

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.

  1. Master-weight scaled FP8/FP4 layers (nn::fp8_linear, nn::fp8_matmul, nn::fp4_linear, nn::fp4_matmul) dynamically scale f32 inputs and weights into F8E4M3FN (±448) or F4E2M1FN (±6) with f32 accumulation and straight-through gradients back to f32 master weights.
  2. Native FP8-stored weights (w.cast(Dtype::F8E4M3FN)) via nn::quantized_matmul(&x, &w) keep weights in 1-byte FP8 in HBM, automatically dispatch native F8E4M3FN x F8E4M3FN -> BF16 Tensor Core GEMMs on CUDA/TPU (falling back cleanly to BF16 on CPU/Metal), auto-select dynamic per-tensor FP8 backward GEMMs on Blackwell (sm_100) and TPU vs BF16 backward on Hopper (sm_90), and accumulate large stacked for_loop FP8 weight gradients in scaled 1-byte F8E5M2.
// 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.

let out = with_precision(Precision::Highest, || a.matmul(&b));

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.

let qa = a.to_i8();
let qb = b.to_i8();
let acc = qa.matmul_in(&qb, Dtype::I32);