Repository navigation
[Metal] Prefill-row invariance scope: prompt chunks round like the reference rows (opt-in, per thread) - #71
Draft
jonathan308 wants to merge 1 commit into
Draft
jonathan308 wants to merge 1 commit into
jonathan308 wants to merge 1 commit into
Conversation
A thread-local scope (`ThreadPrefillRows`, C: `mlx_metal_set_thread_prefill_rows`) names a number of reference rows. `quantized_matmul` and `fast::scaled_dot_product_attention` read it when the op is built and keep it in the primitive, so the route follows the thread that built the graph. `is_equivalent` compares it, autograd keeps it, and the compile cache keeps traces made inside and outside a scope apart. `state()` is unchanged. Inside a scope, a transposed non-batched quantized `x @ w.T` of two or more rows takes `qmm_splitk` with the K split that the reference rows would get and skips the NAX split-K route, so a row's bits do not depend on how many rows share the call. Head-dim 256 causal or masked attention takes the fused NAX kernel from 16 queries instead of 1024. This applies the batch-invariant kernel idea (Thinking Machines, 2025) to prompt chunks. Ops built outside a scope, or with 0 rows, take the routes they took before. One-row products keep `qmv`; attention of 8 or fewer queries keeps the vector kernels. tests/prefill_rows_tests.cpp checks that ops built outside a scope keep their bits, that short products in a scope equal the same rows of a 4096-row product, that compiled functions trace again in a scope, and the head-dim 256 attention route. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Dec 4, 2026
Merged
GumbiiDigital
added a commit
that referenced
this pull request
Dec 4, 2026
…AX path Address jonathan308's review comments on #73: - Default MLX_NAX_KSPLIT to 0 (opt-in) in mlx/utils.h and update docs/src/usage/environment_variables.rst and fork.yaml so M5 output bits at 8..16 affine rows stay identical to production unless explicitly opted in. - Factor the K-split gate into use_qmm_nax_ksplit(bool allow_ksplit = true) and thread an optional allow_ksplit parameter through qmm() -> qmm_nax(), ready for #71 to pass allow_ksplit = (ref_rows <= 0) when qmm_splitk falls through to qmm at split_k <= 1. - Log GPU architecture and metal::is_nax_available() in tests/nax_quantized_kernel_tests.cpp, assert bitwise M<=16 vs M=17 parity when K-split is off (default) and bitwise K-split dispatch on NAX when MLX_NAX_KSPLIT=1, replace ctest nax_ksplit_disabled with nax_ksplit_enabled (MLX_NAX_KSPLIT=1), and run it in .github/workflows/fork-tests.yml. - Document AGENTS.md (from 56e12b0 on main) under Fork tooling in fork.yaml so Fork Diff Check passes after rebasing onto main.
This branch has not been deployed
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.
Draft, not for merge yet. Part of a stack of three: this PR, then Layr-Labs/mlx-swift#78 (submodule bump and Swift scope), then Layr-Labs/mlx-swift-lm#495 (the engine builds each prompt chunk inside the scope).
What changes
A per-thread "prefill rows" scope.
ThreadPrefillRowsin C++,mlx_metal_set_thread_prefill_rows(rows, &previous)andmlx_metal_thread_prefill_rows()in C (next tomlx_metal_set_thread_qmm_steel_tileincommand_buffer_limits.h).quantized_matmulandfast::scaled_dot_product_attentionread the scope when the op is built and store it in the primitive (QuantizedMatmul::prefill_rows(),ScaledDotProductAttention::prefill_rows()). The route follows the thread that built the graph, not the thread that evaluates it. One evaluation can then hold prompt-chunk products built inside the scope and decode products built outside it.is_equivalentcompares the value, so products from inside and outside a scope are never merged. The vjp and jvp of a quantized matmul build their products with the same value. The compile cache keys each trace by the thread's value, so a function traced in a scope is traced again outside it.state()is unchanged, so an exported graph imports with 0.x @ w.Twith two or more rows goes toqmm_splitk, with the K split counted for the reference rows instead ofM. It skips the NAX split-K route, because that route picks its split fromM. With 4,096 reference rows, every product with N >= 128 gets one K partition, the split that a 4,096-row chunk gets today.use_fallback). Nothing changes on GPUs without neural accelerators.The idea is the batch-invariant kernels of Thinking Machines (2025): fix each row's reduction order so that it does not depend on how many rows share the call. Here it is applied to the chunks of a chunked prompt.
Bits
qmv. Attention with 8 or fewer queries keeps the vector kernels. On NAX, chunks of 9-15 queries keep the composed path. Rows up to 32 on NAX takeqmm_naxwith a 32-row tile, and this PR does not test whether that tile rounds like the 64-row one.Tests (this branch, M3 Ultra, 2026-11-21)
tests/prefill_rows_tests.cpp:prefill_rows() == 0,is_equivalentto an op that never saw a scope), also when it is evaluated inside a scope. An op built inside a scope keeps its route after the scope ends.Results (Release build,
-DMLX_METAL_JIT=OFF):tests --test-case="*prefill rows*": 4 of 4 cases, 134 of 134 assertions passed.testsbinary: 327 of 327 cases, 10,746 assertions passed, 0 skipped.ctest(346 entries): 345 passed.test array shared bufferfailed: it prints SUCCESS and then segfaults at process exit, only under ctest. The same ctest entry fails the same way on an older fork build without this change (c6bf6d64), so this change did not cause it.scripts/check_forkdiff.py: 150 of 150 changed files described. Merge-base check not run locally (no upstream remote).Measured evidence (research build, not this branch)
From the local research build (patches with process-wide switches, same routes; FINDINGS 22.53-22.59, 2026-11-20):
Measurements on builds from this stack's branches (M3 Ultra and M5 Max, 2026-11-21) are on Layr-Labs/mlx-swift-lm#495: the same bits as the research build, today's bits with the switch off, and no measurable cost on today's stripes.
Owner decisions open
AI usage
🤖 Generated with Claude Code