Q2_0 group 64: Metal backend - #41
Conversation
There was a problem hiding this comment.
Pull request overview
Adds Metal backend support for the Q2_0 quantization format (group size 64) so quantized weights can participate in common Metal paths (copy/dequant, get_rows, mul_mv, mul_mm, and mul_mat dispatch).
Changes:
- Implements
Q2_0quantize/dequant routines in the Metal shader library and wires them into existing generic kernels (cpy/get_rows/mul_mm). - Adds a
Q2_0-specific dot-product routine and a dedicatedkernel_mul_mv_q2_0_f32path. - Enables
GGML_TYPE_Q2_0in Metal op dispatch / device capability checks and sets up pipeline params for mul_mv/mul_mv_id.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
ggml/src/ggml-metal/ggml-metal.metal |
Adds Q2_0 quantize/dequant, dot-product helper, and kernel/template instantiations for copy/get_rows/mul_mm/mul_mv. |
ggml/src/ggml-metal/ggml-metal-ops.cpp |
Allows Q2_0 to use the small-batch mul-mv-ext path in MUL_MAT when applicable. |
ggml/src/ggml-metal/ggml-metal-impl.h |
Introduces N_R0_Q2_0 / N_SG_Q2_0 constants for pipeline configuration. |
ggml/src/ggml-metal/ggml-metal-device.m |
Updates Metal device op support checks to include Q2_0 for relevant copy/dup/cont paths. |
ggml/src/ggml-metal/ggml-metal-device.cpp |
Adds Q2_0 case for mul_mv and mul_mv_id pipeline configuration (nsg/nr0). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
7c6c628 to
0f07ba4
Compare
a592d04 to
774d555
Compare
0f07ba4 to
a69cff5
Compare
774d555 to
be87981
Compare
a69cff5 to
dc7c932
Compare
be87981 to
7a4a89a
Compare
dc7c932 to
4b7044d
Compare
4b7044d to
ed93eda
Compare
7a4a89a to
b808e95
Compare
|
metal: Q2_0 backend OverviewFollow up PR after merging of Supported ModelsSupported Models: Ternary-Bonsai-{8B, 4B, 1.7B}-Q2_0_g64.gguf, more coming very soon. Note: In our huggingface repos ggufs ending with # downloads to models/Ternary-Bonsai-<size>-Q2_0_g64.gguf
hf download prism-ml/Ternary-Bonsai-1.7B-gguf Ternary-Bonsai-1.7B-Q2_0_g64.gguf --local-dir models
hf download prism-ml/Ternary-Bonsai-4B-gguf Ternary-Bonsai-4B-Q2_0_g64.gguf --local-dir models
hf download prism-ml/Ternary-Bonsai-8B-gguf Ternary-Bonsai-8B-Q2_0_g64.gguf --local-dir modelsCorrectness: Metal vs CPU logits KLThe ensure correctness we run the model twice: 1) Q2_0 CPU 2) Q2_0 Metal. We store the logits for both and compare the KL divergence. Using the standard llama-perplexity tool. # reference logits from the CPU kernel
./build/bin/llama-perplexity -m models/Ternary-Bonsai-<size>-Q2_0_g64.gguf \
-f datasets/wikitext-2-raw/wiki.test.raw -c 512 --chunks 20 -ngl 0 --save-all-logits cpu.bin
# Metal vs CPU
./build/bin/llama-perplexity -m models/Ternary-Bonsai-<size>-Q2_0_g64.gguf \
-f datasets/wikitext-2-raw/wiki.test.raw -c 512 --chunks 20 -ngl 99 \
--kl-divergence --kl-divergence-base cpu.bin
Metal Q2_0 matches the CPU reference (Mean KLD ~0, top-1 ≥99.9%). Speeds:
|
b808e95 to
23bc2e9
Compare
23bc2e9 to
7d06961
Compare
7d06961 to
60dac7f
Compare
DRAFT PR for tesintg and reviews.