Repository navigation
metal: the bf16 prefill GEMM's f16 form sums in f32, as the quantized one - #59
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
sums, K step 16), named as the quantized gemm_f16.
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.
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.