Skip to content

GPU.withPrefillRowInvariance(rows:_:): prompt chunks round like the reference rows (opt-in) - #78

Draft
jonathan308 wants to merge 1 commit into
mainfrom
perf/prefill-row-invariance
Draft

jonathan308 wants to merge 1 commit into
mainfrom
perf/prefill-row-invariance

Conversation

@jonathan308

@jonathan308 jonathan308 commented Nov 21, 2026 •

Copy link
Copy Markdown

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:_:) and GPU.prefillRowInvarianceRows, in Source/MLX/GPU+Metal.swift next to withQuantizedMatmulSteelTile. They wrap the core's mlx_metal_set_thread_prefill_rows / mlx_metal_thread_prefill_rows, declared in Source/Cmlx/include/mlx/command_buffer_limits.h. The previous value is restored on return and on throw.
  • 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. Each row of a prompt chunk then has the same bits whatever the chunk's length.
    • Head-dim 256 causal or masked attention takes the fused NAX kernel from 16 queries instead of 1,024.
    • Ops built outside the scope are unchanged.
  • Source/Cmlx/mlx moves from 72dffa2d to 5d7fbfa0, the head of [Metal] Prefill-row invariance scope: prompt chunks round like the reference rows (opt-in, per thread) mlx#71 (one commit on 72dffa2d). mlx-c stays at 2ac897e3. .gitmodules is unchanged.
  • tools/update-mlx-xcodeproj.sh regenerated include-framework/mlx-utils.h and mlx-primitives.h. At the old pin the tool reproduced the committed headers exactly, except the commit stamp in Metal.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.
  • Run with swift build --build-system native --build-tests. The metallib was built from the pinned mlx with -DMLX_METAL_JIT=OFF. SDK: Command Line Tools MacOSX27.0 (see the note below).
  • Passed: PrefillRowInvarianceTests, CommandBufferLimitsTests, Compile*Tests, MLXFastKernelTests, Quantiz*Tests and SortedGatherQuantized*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.
  • Not run here: the rest of the XCTest suite, Swift Testing, the distributed ring tests and the xcodeproj build. Those are left to CI.

Note: this Mac runs a macOS 27.2 beta, where binaries built with the Xcode 27.2 SDK crash on Date comparisons. 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

  • I have read the CONTRIBUTING document
  • I have run swift-format on the changed Swift files (pre-commit itself was not run)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • AI usage disclosure: Claude Code (Claude Opus 5.5) wrote the code, the tests and this description, and ran the tests, at the request of the repository's maintainer.

🤖 Generated with Claude Code

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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant