Commit a9d5674
authored
[MLX][Gemma4] Introduce Q6K kernels (pytorch#20004)
### Summary
Adds fused **GGUF Q6_K** custom Metal kernels to the MLX backend and
wires them into the Gemma 4 31B GGUF export path, so Q6_K-quantized
linear and embedding weights run directly from llama.cpp's packed block
layout instead of taking the slow non-fused dequantize path. Also
shrinks the exported `.pte` (and its in-memory footprint) by
de-duplicating repeated kernel source blobs.
**New custom kernel ops** (`backends/mlx/custom_kernel_ops/gguf/`)
The `gguf/` package is organized as format routers over per-format
implementations, so new GGUF formats (e.g. Q4_K) can be added without
touching the op definitions:
- `gguf/linear.py` / `gguf/embedding.py`: thin **format routers** — each
owns the op identity (`mlx::gguf_linear` / `mlx::gguf_embedding`: custom
op, fake, and lowering registration) and dispatches on the `format` arg.
Only `"q6k"` is supported today; other formats raise
`NotImplementedError`.
- `gguf/q6k/common.py`: shared Q6_K primitives — constants, the
pure-torch `dequantize_q6_k` reference, and the Metal header
(`block_q6_K` struct + dequant helpers). Lightweight (no builder
import), re-exported from `gguf/q6k/__init__.py`.
- `gguf/q6k/linear.py`: `out = x @ dequant(weight)^T (+bias)` against a
raw GGUF `block_q6_K` blob (no repacking). Emits two Metal kernels — a
fused mat-vec for decode (`M==1`, ported from llama.cpp
`kernel_mul_mv_q6_K_f32_impl`) and a tiled simdgroup mat-mat for prefill
(`M>1`). For dynamic/symbolic `M`, both chains are emitted and selected
at runtime via a new `IfNode`.
- `gguf/q6k/embedding.py`: gather counterpart that dequantizes Q6_K rows
directly.
**Runtime / schema**
New `IfNode` in `schema.fbs` (runtime conditional selecting one of two
instruction chains on an integer condition) plus `exec_if` dispatch in
`MLXInterpreter.h`.
**Serialization: smaller `.pte` + lower load-time RAM**
- Serializer de-duplicates identical strings into a single FlatBuffer
offset (shared-string emission in the generated serializers /
`generate.py` / `mlx_graph_serialize.py`). The big repeated
`MetalKernelNode` source/header blobs are now written once. On Gemma 4
31B this cut the MLX graph metadata from ~1.23 MiB to ~0.47 MiB (~62%).
- Loader interns those shared blobs into one `std::shared_ptr<const
std::string>` keyed by the FlatBuffer string pointer (`StringPool` in
`MLXLoader.{h,cpp}.tmpl`; `MLXInterpreter.h` derefs the handle), so a
newly-produced `.pte` also uses less RAM at runtime.
- Fully backward-compatible: no schema/format change. Old `.pte` files
load unchanged (just without the dedup).
**Gemma 4 31B GGUF loader** (`examples/models/gemma4_31b/`)
- `iter_gguf_tensors` now yields the tensor's quant type and can emit
Q6_K tensors as the raw `(N, n_blocks*210)` uint8 blob (`q6k_raw`);
added `_raw_q6_k` helper and made `_unpack_q6_k` accept an
already-materialized tensor.
- New `mlx_gguf_linear.py` carrier modules
(`GGUFLinear`/`GGUFEmbedding`) and `_handle_mlx_q6k` routing: Linear
weights → `gguf_linear`, token embedding → `gguf_embedding`, tied
lm_head reuses the embedding blob via `gguf_linear`, with a
quantized-tensor fallback for any other Q6_K module.
- Removed the `ET_MLX_ALLOW_NON_FUSED_QUANTIZED_OPS` env-var workaround
in `export.py` since the fused path no longer needs it.
**Refactor**
- Renamed `backends/mlx/model_ops/` → `backends/mlx/custom_kernel_ops/`
(with a `test/` subpackage) and updated all imports
(`turboquant_cache.py`, `qwen3_5_moe/mlx_source_transformations.py`).
### Test plan
- New/updated unit tests: `custom_kernel_ops/gguf/test/test_linear.py`,
`test_embedding.py`; `backends/mlx/test/test_serialization_dedup.py`
(asserts identical source/header are written once);
`examples/models/gemma4_31b/quant/tests/test_gguf.py` and
`examples/models/gemma4_31b/tests/test_mlx_pipeline.py`.
- CI (`.github/workflows/mlx.yml`) discovers op tests recursively
(`custom_kernel_ops/**/test/test_*.py`) so per-format subpackage tests
run with no per-op CI edit.
Run locally:
```bash
# Build the op runner once (per CI):
cmake --preset mlx-release -DEXECUTORCH_BUILD_TESTS=ON -DEXECUTORCH_MLX_ENABLE_SANITIZERS=OFF
cmake --build cmake-out --target op_test_runner -j
# GPU op tests (export + run on device):
python -m executorch.backends.mlx.custom_kernel_ops.gguf.test.test_linear run -v
python -m executorch.backends.mlx.custom_kernel_ops.gguf.test.test_embedding run -v
# Pure-Python checks:
python -m pytest backends/mlx/test/test_serialization_dedup.py \
examples/models/gemma4_31b/quant/tests/test_gguf.py \
examples/models/gemma4_31b/tests/test_mlx_pipeline.py -v
```1 parent 2d42918 commit a9d5674
53 files changed
Lines changed: 3819 additions & 829 deletions
File tree
- .github/workflows
- backends/mlx
- builder
- custom_kernel_ops
- gguf
- q4k
- q6k
- test
- test
- llm
- runtime
- serialization
- test
- examples/models
- gemma4_31b
- quant
- tests
- tests
- qwen3_5_moe
- extension/llm/export
- test
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
77 | 78 | | |
78 | 79 | | |
79 | 80 | | |
| 81 | + | |
| 82 | + | |
80 | 83 | | |
81 | 84 | | |
82 | 85 | | |
| |||
89 | 92 | | |
90 | 93 | | |
91 | 94 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
106 | 105 | | |
107 | 106 | | |
108 | 107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
332 | 405 | | |
333 | 406 | | |
334 | 407 | | |
| |||
421 | 494 | | |
422 | 495 | | |
423 | 496 | | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
424 | 525 | | |
425 | 526 | | |
426 | 527 | | |
| |||
File renamed without changes.
File renamed without changes.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
0 commit comments