Repository navigation
metal: the quantized prefill GEMM over half tiles, its sums f32 - #57
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.
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.
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.
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.
the tiles' (ST). TL_STEEL_KERNEL_ST keeps AT = ST, so
gemm_bf16_f16acc_ still sums in half.
sums, K step 32). quant_fmt holds its GEMMs as gemm[f16][band32], and
detail_::gemm_quant_run_ launches either form.
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.