Skip to content

metal: the bf16 prefill GEMM's f16 form sums in f32, as the quantized one - #59

Merged
yhirose merged 2 commits into
masterfrom
metal-bf16-f32sums
Nov 7, 2026
Merged

yhirose merged 2 commits into
masterfrom
metal-bf16-f32sums

Conversation

@yhirose

@yhirose yhirose commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Stacked on #57.

Metal's gemm_bf16_f16acc_ summed in half. On check_qwen's 96-token
prompt (the long-prompt path, forced on for these array-path files,
which normally check a short prompt only), its top-5 logits were
3.2e-2 (Qwen3 0.6B F16), 7.3e-3 (Qwen2.5 0.5B) and 2.6e-3 (Llama 3.2 1B
F16) off the f32 forward's; the check's bound is 2e-2. With f32 sums
over the same half tiles (#57's form, llama.cpp's kernel_mul_mm) they
are 2.8e-4, 3.0e-4 and 8.2e-5.

  • The kernels are gemm_bf16_f16_ and gemm_bf16_f16_32_ (half tiles, f32
    sums, K step 16), named as the quantized gemm_f16.
  • No Metal kernel sums in half now, so STEEL's sum-type parameter (metal: the quantized prefill GEMM over half tiles, its sums f32 #57's
    AT) is gone: SteelMMA always sums in float2, and TL_STEEL_KERNEL_ST
    with it. Half sums cannot come back by a kernel's instantiation.
  • gpu_ops_nn.h's gemm_bf16_nt_f16acc contract says what each backend
    does: CUDA sums in f16, Metal rounds the operands to f16 and sums in
    f32. The op keeps its name, shared with CUDA. Comments in qwen.h,
    check_qwen and test_llm that said Metal sums in f16 are updated.

pp512 (bench_qwen_llama, M1 Pro, tok/s, alternating), half sums -> f32
sums: Qwen3 0.6B F16 3050 -> 2955, Qwen2.5 0.5B 4170 -> 3990, Qwen3.5
0.8B F16 1692 -> 1587, Llama 3.2 1B F16 1716 -> 1615 (3 to 6%). A K step
of 32 for these kernels was within noise (-1% to +4% by model) and is
not taken.

Tests: check_qwen is ALL OK on Qwen3 0.6B F16 and Q4_K_M, Qwen2.5 0.5B,
Qwen3.5 0.8B F16, Llama 3.2 1B F16 and Gemma 4 E2B; Qwen3 0.6B F16's
f16 GEMMs on its own prompt went from 1.0e-2 to 6.4e-4. ctest passes
(15.8 to 17.7 s on a loaded machine; the base measured 18.4 s alongside).
No test is added: summing in half is no longer expressible in STEEL.

Metal's prefill ran a block-quantized weight's GEMM over f32 tiles, its
exact form, since the backend has no f16 activations; a bf16 weight's
already took the f16 trade under f16_acc.

- gpu::gemm_quant_f16 (gpu_ops_nn.h): A and the dequantized weight
  rounded to f16 as they are staged, the sums f32, as llama.cpp's Metal
  kernel_mul_mm. f16's range, so the caller checks for inf as with
  gemm_bf16_nt_f16acc; a backend without the form (CUDA) runs
  gemm_quant's exact one.
- STEEL's SteelMMA and steel_body_ take the sums' type (AT) apart from
  the tiles' (ST). TL_STEEL_KERNEL_ST keeps AT = ST, so
  gemm_bf16_f16acc_ still sums in half.
- Each block format gets gemm_<fmt>_f16_ and _f16_32_ (half tiles, f32
  sums, K step 32). quant_fmt holds its GEMMs as gemm[f16][band32], and
  detail_::gemm_quant_run_ launches either form.
- qwen.h: prefill_sums_f16 becomes prefill_gemm_f16, true for a
  quantized model where the backend has f16 activations or this form;
  the prefill's quantized GEMMs take it when not on f16 activations.
  check_qwen's check_f16_sums becomes check_f16_gemms, and now runs on
  Metal's quantized models.

Summing in half too was 5 to 9% faster still, but on check_qwen's
96-token prompt its top-5 logits were 5.2e-2 (Qwen3 0.6B Q4_K_M) and
4.1e-2 (Qwen3.5 0.8B Q4_K_M) off the f32 forward's, past the check's
2e-2; with f32 sums they are 4.5e-4 and 9.5e-4.

pp512 (bench_qwen_llama, M1 Pro, tok/s, alternating), before -> after:
Gemma 4 E2B 1517 -> 1603, E4B 493 -> 528, Qwen3 0.6B Q4_K_M 2474 ->
2662, Llama 3.2 1B Q4_K_M 1298 -> 1399, Gemma 3 1B 1547 -> 1634,
Qwen3.5 0.8B Q4_K_M 1422 -> 1498.

Tests: test_nn's GEMM over block-quantized rows runs both forms, the f16
one to 3e-4 of the terms' magnitude (9.6e-5 measured; a misread weight
is off by ~1/K), skipped where the backend has no such form. Mutants
fail it: q4k's f16 kernels bound to q6k's, q80's 32-row one to q40's,
the bands swapped, the sums half (42 failures). Tiles left f32 pass it
but fail check_qwen's overflow check (no retry). check_qwen is ALL OK
on Gemma 4 E2B and E4B, Gemma 3 1B, PLaMo 3 0.8B, Qwen3 0.6B Q4_K_M and
F16, Qwen3.5 0.8B Q4_K_M, and Llama 3.2 1B Q4_K_M, Q4_0 and Q5_K_M, f16
GEMMs within 7.2e-5 to 9.5e-4 and one overflow retry each. The test
case takes 0.72 s (0.68 before); ctest 10.5 s.

Not changed here: Metal's bf16 f16-sum GEMM is 3.2e-2 off on the same
96-token prompt (Qwen3 0.6B F16), a check its path never runs.
… one

Stacked on #57.

Metal's gemm_bf16_f16acc_ summed in half. On check_qwen's 96-token
prompt (the long-prompt path, forced on for these array-path files,
which normally check a short prompt only), its top-5 logits were
3.2e-2 (Qwen3 0.6B F16), 7.3e-3 (Qwen2.5 0.5B) and 2.6e-3 (Llama 3.2 1B
F16) off the f32 forward's; the check's bound is 2e-2. With f32 sums
over the same half tiles (#57's form, llama.cpp's kernel_mul_mm) they
are 2.8e-4, 3.0e-4 and 8.2e-5.

- The kernels are gemm_bf16_f16_ and gemm_bf16_f16_32_ (half tiles, f32
  sums, K step 16), named as the quantized gemm_<fmt>_f16_.
- No Metal kernel sums in half now, so STEEL's sum-type parameter (#57's
  AT) is gone: SteelMMA always sums in float2, and TL_STEEL_KERNEL_ST
  with it. Half sums cannot come back by a kernel's instantiation.
- gpu_ops_nn.h's gemm_bf16_nt_f16acc contract says what each backend
  does: CUDA sums in f16, Metal rounds the operands to f16 and sums in
  f32. The op keeps its name, shared with CUDA. Comments in qwen.h,
  check_qwen and test_llm that said Metal sums in f16 are updated.

pp512 (bench_qwen_llama, M1 Pro, tok/s, alternating), half sums -> f32
sums: Qwen3 0.6B F16 3050 -> 2955, Qwen2.5 0.5B 4170 -> 3990, Qwen3.5
0.8B F16 1692 -> 1587, Llama 3.2 1B F16 1716 -> 1615 (3 to 6%). A K step
of 32 for these kernels was within noise (-1% to +4% by model) and is
not taken.

Tests: check_qwen is ALL OK on Qwen3 0.6B F16 and Q4_K_M, Qwen2.5 0.5B,
Qwen3.5 0.8B F16, Llama 3.2 1B F16 and Gemma 4 E2B; Qwen3 0.6B F16's
f16 GEMMs on its own prompt went from 1.0e-2 to 6.4e-4. ctest passes
(15.8 to 17.7 s on a loaded machine; the base measured 18.4 s alongside).
No test is added: summing in half is no longer expressible in STEEL.
@yhirose
yhirose merged commit f3529d0 into master Nov 7, 2026
9 checks passed
@yhirose
yhirose deleted the metal-bf16-f32sums branch November 7, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant