Skip to content

Commit b331ebd

Browse files
authored
[MLX] Small perf improves to Q4K (#20595)
Improves Q4K on Gemma4 from 17 tok/sec to 18 tok/sec on M4 Max. Still slower than using MLX's 4-bit kernels.
1 parent 1acb0da commit b331ebd

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

  • backends/mlx/custom_kernel_ops/gguf/q4k

backends/mlx/custom_kernel_ops/gguf/q4k/linear.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ def _q4k_matvec_source(has_bias: bool) -> str:
9191
9292
for (short row = 0; row < N_R0; ++row) {{
9393
const int r = first_row + row;
94-
if (r >= N) {{ break; }}
95-
9694
device const block_q4_K * blk = xrows + (uint)r * nb + ib;
9795
device const uint16_t * sc = (device const uint16_t *)blk->scales + iq;
9896
device const uint16_t * q1 = (device const uint16_t *)blk->qs + 16 * iq + 4 * ir;
@@ -285,7 +283,9 @@ def _q4k_matmul_source(has_bias: bool) -> str:
285283

286284

287285
# Number of simdgroups per threadgroup for the mat-vec kernel.
288-
_Q4K_MV_NSG = 4
286+
# Matches llama.cpp N_SG_Q4_K=2; nsg=4 launched half as many (fatter) threadgroups,
287+
# hurting occupancy / wave-quantization tail on the bandwidth-bound decode matvec.
288+
_Q4K_MV_NSG = 2
289289
# Tile sizes for the mat-mat kernel (from llama.cpp kernel_mul_mm).
290290
_Q4K_MM_NR0 = 64 # weight/output rows (N dim) per threadgroup
291291
_Q4K_MM_NR1 = 32 # activation rows (M dim) per threadgroup

0 commit comments

Comments
 (0)