Repository navigation
GPU.withPrefillRowInvariance(rows:_:): prompt chunks round like the reference rows (opt-in) - #78
Draft
jonathan308 wants to merge 1 commit into
Draft
jonathan308 wants to merge 1 commit into
jonathan308 wants to merge 1 commit into
Conversation
Wraps the core's per-thread prefill-row scope (`mlx_metal_set_thread_prefill_rows`, `mlx_metal_thread_prefill_rows`). Quantized matmuls and attention ops built on the calling thread inside the scope keep its reference rows: on the GPU a quantized `x @ w.T` of two or more rows splits K as a product of that many rows would, so each row of a prompt chunk has the same bits whatever the chunk's length, and head-dim 256 causal or masked attention takes the fused NAX kernel from 16 queries instead of 1024. Ops built outside the scope are unchanged. Moves the mlx submodule to the head of Layr-Labs/mlx perf/prefill-row-invariance (one commit on 72dffa2d) and regenerates the framework headers it changes; mlx-c stays at 2ac897e3. PrefillRowInvarianceTests checks the scope's nesting, restore and thread rules, that short products in the scope equal the same rows of a 4096-row product, and that ops keep the rows of the thread that built them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 of 4 tasks
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. Stacked on Layr-Labs/mlx#71. Layr-Labs/mlx-swift-lm#495 builds against this branch and reports the measurements on builds of the stack.
Proposed changes
GPU.withPrefillRowInvariance(rows:_:)andGPU.prefillRowInvarianceRows, inSource/MLX/GPU+Metal.swiftnext towithQuantizedMatmulSteelTile. They wrap the core'smlx_metal_set_thread_prefill_rows/mlx_metal_thread_prefill_rows, declared inSource/Cmlx/include/mlx/command_buffer_limits.h. The previous value is restored on return and on throw.x @ w.Tof two or more rows splits K as a product of that many rows would. Each row of a prompt chunk then has the same bits whatever the chunk's length.Source/Cmlx/mlxmoves from72dffa2dto5d7fbfa0, the head of [Metal] Prefill-row invariance scope: prompt chunks round like the reference rows (opt-in, per thread) mlx#71 (one commit on72dffa2d).mlx-cstays at2ac897e3..gitmodulesis unchanged.tools/update-mlx-xcodeproj.shregeneratedinclude-framework/mlx-utils.handmlx-primitives.h. At the old pin the tool reproduced the committed headers exactly, except the commit stamp inMetal.hpp, which is left as committed.fork.yaml: a "GPU: prefill-row invariance scope" entry, plus a sentence on the new pin.Bits
Outside a scope, nothing changes. Inside one: short prompt chunks round like a 4,096-row chunk (see Layr-Labs/mlx#71 for the routes and what is not covered).
Tests (this branch, M3 Ultra, 2026-11-21)
PrefillRowInvarianceTests, 6 tests: nesting and restore, restore on throw, negative rows end the scope, the value is per thread, short products in the scope equal the same rows of a 4,096-row product (Qwen 3.5 9B K/V projection, 12 row counts from 2 to 2,048), and ops keep the rows of the thread that built them.swift build --build-system native --build-tests. The metallib was built from the pinnedmlxwith-DMLX_METAL_JIT=OFF. SDK: Command Line Tools MacOSX27.0 (see the note below).PrefillRowInvarianceTests,CommandBufferLimitsTests,Compile*Tests,MLXFastKernelTests,Quantiz*TestsandSortedGatherQuantized*Tests. That is 78 tests: 0 failures, 0 skipped.scripts/check-generated-metal-sources.py: PASS.scripts/check_forkdiff.py: 225 of 225 files described.Note: this Mac runs a macOS 27.2 beta, where binaries built with the Xcode 27.2 SDK crash on
Datecomparisons. The local build therefore used the Command Line Tools 27.0 SDK. CI uses its own toolchain.Owner decisions open
The same three as Layr-Labs/mlx#71: default-on per family, short-prompt bits, and the M5 attention route.
Checklist
swift-formaton the changed Swift files (pre-commititself was not run)AI usage
🤖 Generated with Claude Code