Skip to content

bf16 master weights: retire the fp32 masters (SPEC decision #8 successor) #57

Closed

Description

@feitreim

bf16 master weights: retire the fp32 masters (SPEC decision #8 successor)

Phase 2 (SPEC §7, decision #8) kept fp32 masters for gradcheck clarity: every parameter is a GpuTensor<f32> and the aligned linears/experts/head carry packed-bf16 compute copies beside it. The masters cost 16.3 GiB at the 4.39B canonical config; converting them to bf16 recovers ~8.2 GiB of workspace and halves the parameter section of every checkpoint. This issue is the numerics core; #58 (compute-copy collapse) and #59 (activation/gradient audit) build on it.

Scope

Masters → bf16 for: qkv_proj / o_proj (Bf16LinearWeights owners, gpu/model/src/lib.rs:635-639, :776-780), the stacked experts (:1472-1479), lm_head (GpuBf16Head.master, :2079-2086), and embedding (:2012-2015, no compute copy today).

Stays fp32 (numerically necessary, and cheap):

  • Router — fp32 end-to-end is a standing constraint (decision Implement Muon CPU reference optimizer #22: bf16 rounding near a top-k boundary reassigns tokens). 295K params; irrelevant memory.
  • Norm weights — scale parameters, 2·L+1 vectors of D; zero memory leverage, high sensitivity.
  • AdamW moments — GpuAdamWMoments stays {f32, f32} (gpu/tensor-gpu/src/lib.rs:810-823). A bf16 second moment is unsafe (it sits inside a sqrt in the denominator); the first moment is a possible follow-up, not this issue.
  • Muon momentum + Newton–Schulz — the orthogonalization path is deliberately fp32 with f64 norm accumulation (sum_squares, gpu/tensor-gpu/src/lib.rs:146-181); it reads the master directly, so it gains a bf16→f32 read but keeps fp32 internals.
  • All existing accumulator/reduction fp32 (tcgen05 epilogues per decision Optimize RMSNorm with block-parallel kernels #20, softmax/CE, RMSNorm reductions).

The crux: the update write-back

With bf16 weights, w -= lr·update computed in fp32 then rounded RNE silently drops any update smaller than half a bf16 ulp of w — bf16 has 8 mantissa bits, so at lr-scale updates this is not hypothetical: crates/optim/examples/overfit_probe.rs already reproduced a bf16 tie-break plateau on exactly this cliff. Plan:

  1. Extend the existing fused-update template — adamw_master_bf16 (gpu/tensor-gpu/src/lib.rs:509-553) is already the right shape (fp32 math, fp32 moments, bf16 write-back) — to all parameters, replacing the plain fp32 adamw + sync_compute re-quantize pairs in GpuDenseAdamW::update_profiled (gpu/model/src/lib.rs:2618-2734).
  2. Stochastic rounding on the bf16 write-back, deterministically seeded: a device-side splitmix64 (the shared constant from crates/tensor-core/src/rng.rs — no device copy exists yet) keyed on (step, parameter_id, element_index). Never runtime entropy: reruns and checkpoint resume must stay bit-identical.
  3. Decide with evidence, not by default: run the overfit gates (the documented knife-edge at lr 0.02–0.03 is the sensor) with RNE first. If RNE passes all gates robustly across reruns, note it and keep SR behind the same kernel switch; if it stalls, SR is the fix. overfit_probe is the cheap CPU-side testbed — Fp32MasterWeights (crates/optim/src/lib.rs:516-568) already models master/compute splits there.

Checkpoint v5

VERSION: u32 = 4 → 5 (gpu/model/src/checkpoint.rs:21). Parameters serialize as bf16 (moments stay fp32); v4 rejected cleanly by the existing version check (:275-278). "Resumes bit-identically" keeps its gate; if SR lands, the step counter it seeds from is already in the header. Checkpoint shrinks ~8.2 GiB.

CPU reference & parity

The CPU model is pure fp32 (crates/nn/*, no bf16 anywhere). Mirror the master dtype there so GPU/CPU parity stays meaningful — Fp32MasterWeights is the template to generalize, and gpu/model/src/main.rs:176-205/:458-480 (compute copies asserted bit-identical to pack_bf16(master)) become the anchor. Tolerance changes only where a genuine dtype change forces them, each documented; no papering over regressions.

Validation

run.sh model full gate suite (parity, both overfits ×AdamW/Muon, aux-loss overfit, checkpoint v5 bit-identical resume + v4 rejection), rerun-once discipline on the known-flaky overfit gates, BASELINE_REF=<ref> run.sh model profile for VRAM (headline: ~164.3 → ~156 GiB) and the optimizer-span deltas.

Rough size: ~600–900 lines touched (fused kernels + drivers, checkpoint, CPU mirror, parity harness).

Activity

  1. added
    enhancement
    on Jul 25, 2026
  2. feitreim commented on Jul 26, 2026

    @feitreim
    OwnerAuthor

    Landed on main (31e1a9c). Masters for embedding/qkv/o_proj/experts/lm_head are now packed bf16 (GpuBf16Tensor); norms, router, all gradients, AdamW moments, Muon momentum/Newton–Schulz, and tcgen05 epilogues stay fp32. The update math is still fp32 against fp32 moments — bf16 appears only at the write-back. RNE ships: 14 gates × 3 runs with no flake or stall (AdamW overfit 1.5267 → 0.000002, MoE+aux 2.9494 → 0.000021). Deterministic SR is implemented behind model::MASTER_ROUNDING (device splitmix64 keyed on step/param/element, ≤1 ulp from nearest, reproducible) with a documented finding: SR passes single-step parity but diverges 9 ulps on chained gates because GPU/CPU draw independent streams over differently-grouped parameters — adopting SR means re-deciding what the master parity gates compare. Checkpoint v5: params serialize bf16, moments fp32, v4 refused (explicit rejection gate). Measured alone: VRAM 164.3 → 156.1 GiB (−8.2, as predicted), step 607 → 585 ms, optimizer span −42%. Combined with #59 on the same tree: VRAM 164.3 → 145.7 GiB, step 608 → 553 ms. Bonus fix during integration: the expert panel packing predicate now matches the GEMM 256-alignment eligibility exactly (was 128 — latent oracle/packed mismatch at in-between shapes). #58 (collapse the now-redundant compute copies; lm_head.sync_w_t regression 0.15 → 0.24 ms also dies there) is unblocked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions