Skip to content

cuda: out_prod on BF16 tensor cores for quantized 2D weights — ~2x on the backward's frozen-base GEMMs (includes #9's perf cases) - #11

Merged
joelteply merged 2 commits into
feat/props-weight-residencyfrom
continuum/out-prod-f16-gemm
Sep 27, 2026
Merged

joelteply merged 2 commits into
feat/props-weight-residencyfrom
continuum/out-prod-f16-gemm

Conversation

@joelteply

Copy link
Copy Markdown

Two commits: #9's OUT_PROD perf cases (the before) and the BF16 tensor-core path (the after).

When the weight type has a BF16 dequantizer (K-quants, Q4/Q5/Q8, ...), each row block of W goes to BF16, the gradient goes to BF16 once per op (dense, orientation kept), and the blocks accumulate through cublasGemmEx with F32 compute. BF16 rather than F16 because the gradient operand can be tiny and F16 would flush it to zero; BF16 keeps F32's exponent range. Types without a BF16 dequantizer keep the F32 path.

RTX 5090, CUDA 13.2, us/op at 1024 tokens, F32 path -> BF16:

shape before after
1536x1536 Q4_K 101 63
1536x8960 Q4_K 429 281
8960x1536 Q4_K 438 274
5120x5120 Q4_K 745 518
5120x17408 Q4_K 2948 1481
5120x17408 Q8_0 2698 1481
5120x17408 F32 src0 (unchanged path) 2229 2251
17408x5120 Q4_K 3512 1405
5120x151936 head Q4_K 23357 12652

Parity: test-backend-ops test -b CUDA0 -o OUT_PROD against the CPU backend, 234/234, 0 FAIL.

27B reading: a layer's out_prods go from ~12 ms to ~6.4 ms per 1024 tokens; the step's out_prod ceiling moves from ~1.3k to ~2.5k tok/s.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc

joelteply and others added 2 commits September 26, 2026 19:49
…and 27B projections, the output head; Q4_K beside F32 and Q8_0)

Measured on an RTX 5090 (CUDA 13.2): out_prod(W, dy^T) at 1024 tokens, us/run:
1536x1536 Q4_K 101; 1536x8960 429; 8960x1536 438; 5120x5120 745; 5120x17408 2948
(F32 2229, Q8_0 2698); 17408x5120 3512; the 5120x151936 head 23357. The dequantized
copy costs about a third over the F32 GEMM at the ffn shape; the F32 GEMM itself is the
bulk, so an F16 tensor-core GEMM on the dequantized block is the next cut.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc
…es (cublasGemmEx, F32 accumulate)

The backward's frozen-base GEMMs ran as F32 Sgemm on a dequantized copy, ~3x the
forward's int8 tensor-core cost. When the weight type has a BF16 dequantizer, each row
block goes to BF16, the gradient goes to BF16 once per op (dense, orientation kept), and
the blocks accumulate through cublasGemmEx with F32 compute. BF16 not F16: the gradient
can be tiny and F16 would flush it to zero. Other types keep the F32 path.

RTX 5090, CUDA 13.2, us/op at 1024 tokens (F32 path -> BF16):
1536x1536 101 -> 63; 1536x8960 429 -> 281; 8960x1536 438 -> 274; 5120x5120 745 -> 518;
5120x17408 Q4_K 2948 -> 1481, Q8_0 2698 -> 1481 (F32 src0 unchanged 2229);
17408x5120 3512 -> 1405; 5120x151936 head 23357 -> 12652.
test-backend-ops -o OUT_PROD vs CPU: 234/234.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc
@joelteply

Copy link
Copy Markdown
Author

APPROVED at 21bb5e33f. The BF16 path keeps the F32 path's contract:

  • Gradient to BF16 once per op: dense, orientation kept (f = ne10 / ne11 by src1_T, ld_src from the strided dim).
  • Block offsets on the dense copy: r0 * f when op(B) = Bᵀ, r0 when transposed. These are the same k offsets as the F32 row-blocked loop.
  • W dequantized per row block straight to BF16; cublasGemmEx with CUBLAS_COMPUTE_32F accumulating into the F32 dst (beta 0 then 1).
  • The gate on src1's fast dim being contiguous, else the F32 path.
  • BF16 over F16 is the right call for gradients (F32's exponent range, so no flush to zero).

~2x on the 27B shapes, with 234/234 parity, is the measurement.

One consequence to write down, not a blocker: with the gradient rounded to BF16 (8-bit mantissa), a CUDA training run matches the CPU oracle's per-step LOSS only to roughly 1e-3 relative, not the ~1e-5 the F32 path gives. The op-level parity above is within its NMSE bound either way. For the cross-backend step-parity gate (CPU oracle vs CUDA vs Metal on the same data and seed), either compare at that looser tolerance or keep a debug switch that forces the F32 path, so a real kernel bug can't hide inside BF16 rounding.

Co-Authored-By: Claude Opus 5.5 [email protected]
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

@joelteply
joelteply merged commit 4483cb5 into feat/props-weight-residency Sep 27, 2026
11 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant