[PR #2609] [CLOSED] Metal: faster decode GEMVs on M1 / M2 (wider K-quant and IQ4_XS loads, tuned tiles), bitwise the same output #4163

Closed
opened 2026-11-07 15:33:42 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2609
Author: @PiMatyja
Created: 10/15/2026
Status: ❌ Closed

Base: main ← Head: metal-m1m2-gemv


📝 Commits (1)

  • 1c388bd Metal: faster decode GEMVs on M1 / M2 (wider K-quant and IQ4_XS loads, tuned tiles), bitwise the same output

📊 Changes

5 files changed (+422 additions, -24 deletions)

View changed files

➕ metal/kernels/gemv_wide.metal (+124 -0)
📝 metal/src/engine/window.cpp (+99 -19)
📝 metal/src/mtl_rt.h (+2 -0)
📝 metal/src/mtl_rt.mm (+5 -0)
📝 metal/tests/gemv_parity.cpp (+192 -5)

📄 Description

Summary

Wider loads for the Q4_K, Q5_K, Q6_K and IQ4_XS decoders and a tuned tile choice for the dense decode GEMVs, on M1 and M2 only (Apple GPU family 7 and 8). On an M2 Max with IQ3_XXS, decode is about 11% faster and the output is bitwise the same. M3 and newer build exactly what they build today.

What changed

  • metal/kernels/gemv_wide.metal (new): the wide decoders, reusing gemv.metal's templates under new kernel names.
  • metal/src/engine/window.cpp: on M1 / M2, adds that file and picks a tuned tile for 1-3 token windows. The tile never changes the summation order. If a tuned kernel is missing, too big for threadgroup memory, or the tensor isn't 16-byte aligned, it falls back to today's tile.
  • metal/src/mtl_rt.h / .mm: max_threadgroup_memory() and has_function().
  • metal/tests/gemv_parity.cpp: checks every new kernel bitwise against its stock twin; new --sweep.

STRATA_METAL_GEMV=0 turns it off. units.metal, quant.metal and gemv.metal are untouched, so no other kernel changes on any GPU.

Extra Notes

M2 Max (30-core GPU), 64 GB, macOS 26.4.1, IQ3_XXS resident (with #2338), greedy, 3 rounds, decode tok/s:

GEMV=0 default
400 tokens, code 35.3 39.1 (+10.8%)
400 tokens, story 35.2 38.7 (+10.0%)
256 tokens after ~1K 35.0 39.0 (+11.5%)
256 tokens after ~4K 34.6 38.5 (+11.2%)

Same text in both arms on all four prompts. Prompt speed unchanged. gemv_parity and gemv_parity --sweep pass bitwise on the M2 and on an M4 Max, and the other parity tests match main. Not tested on an M1.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/2609 **Author:** [@PiMatyja](https://github.com/PiMatyja) **Created:** 10/15/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `metal-m1m2-gemv` --- ### 📝 Commits (1) - [`1c388bd`](https://github.com/Niko1221/Strata/commit/1c388bdb8ec42eaa79a329848ac78a839a31c219) Metal: faster decode GEMVs on M1 / M2 (wider K-quant and IQ4_XS loads, tuned tiles), bitwise the same output ### 📊 Changes **5 files changed** (+422 additions, -24 deletions) <details> <summary>View changed files</summary> ➕ `metal/kernels/gemv_wide.metal` (+124 -0) 📝 `metal/src/engine/window.cpp` (+99 -19) 📝 `metal/src/mtl_rt.h` (+2 -0) 📝 `metal/src/mtl_rt.mm` (+5 -0) 📝 `metal/tests/gemv_parity.cpp` (+192 -5) </details> ### 📄 Description ## Summary Wider loads for the Q4_K, Q5_K, Q6_K and IQ4_XS decoders and a tuned tile choice for the dense decode GEMVs, on M1 and M2 only (Apple GPU family 7 and 8). On an M2 Max with IQ3_XXS, decode is about 11% faster and the output is bitwise the same. M3 and newer build exactly what they build today. ## What changed - `metal/kernels/gemv_wide.metal` (new): the wide decoders, reusing `gemv.metal`'s templates under new kernel names. - `metal/src/engine/window.cpp`: on M1 / M2, adds that file and picks a tuned tile for 1-3 token windows. The tile never changes the summation order. If a tuned kernel is missing, too big for threadgroup memory, or the tensor isn't 16-byte aligned, it falls back to today's tile. - `metal/src/mtl_rt.h` / `.mm`: `max_threadgroup_memory()` and `has_function()`. - `metal/tests/gemv_parity.cpp`: checks every new kernel bitwise against its stock twin; new `--sweep`. `STRATA_METAL_GEMV=0` turns it off. `units.metal`, `quant.metal` and `gemv.metal` are untouched, so no other kernel changes on any GPU. ## Extra Notes M2 Max (30-core GPU), 64 GB, macOS 26.4.1, IQ3_XXS resident (with #2338), greedy, 3 rounds, decode tok/s: | | GEMV=0 | default | | --- | ---: | ---: | | 400 tokens, code | 35.3 | 39.1 (+10.8%) | | 400 tokens, story | 35.2 | 38.7 (+10.0%) | | 256 tokens after ~1K | 35.0 | 39.0 (+11.5%) | | 256 tokens after ~4K | 34.6 | 38.5 (+11.2%) | Same text in both arms on all four prompts. Prompt speed unchanged. `gemv_parity` and `gemv_parity --sweep` pass bitwise on the M2 and on an M4 Max, and the other parity tests match main. Not tested on an M1. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:33:42 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4163
No description provided.