Repository navigation
[provider 0.10.12] Batched Bonsai 27B prompt rows by default; per-row FP32 arithmetic only on request (pairs) - #70
Merged
jonathan308 merged 3 commits intoNov 12, 2026
Conversation
… default A multi-row FP32 call to the 2-bit/group128/block1024 Hadamard profile with FP16 scales must give the bytes of one quantizedMM over all rows with the FP32-widened constants (the batched matrix kernel single-Mac prefill used before the per-row contract), with and without the grouped GDN layout. Red on this head: every row goes through the M=1 kernel instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
HadamardQuantizedLinear gains `independentRows` (default false). FP32 input of more than one row to the 2-bit/group128/block1024 profile with FP16 scales now takes one batched packed matmul unless the layer was built with `independentRows: true`, which keeps fe35554's M=1-per-row reduction (the bytes of separate singleton calls on every chip) for callers whose machines must agree byte for byte, such as the stages of a pair. Single-Mac prefill returns to the batched matrix kernel it used before the per-row contract (TF32 on M5, FP32 steel elsewhere), 2.3-6x faster on long prompts, at the cost of prompt rows no longer matching the singleton decode bytes. Singleton calls, other profiles (block4096 included) and `independentRowsForward` are unchanged. HadamardLayerTests: the singleton-row test builds its layers with `independentRows: true`; the new default test compares the default with one batched quantizedMM and checks that the opt-in leaves a block512 profile on the batched path. fork.yaml describes the opt-in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dentRows Float16ConstantCastTests' oracle still sent every Bonsai FP32 multi-row call through the singleton kernel, so with the batched default its exact repeated-call test failed on CI (Fork Tests 43138178252, shard 2). The oracle now takes the per-row path only for a layer built with `independentRows`, and one batched quantizedMM otherwise (the oracle this test had before fe35554). The repeated-call/mutation and compile-state tests run for both layers, so cache invalidation stays covered on the batched and the per-row paths; the ineligible profiles are checked with the opt-in too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308
added a commit
that referenced
this pull request
Nov 12, 2026
Review S-6: on CI's runner (vector limit 10) batched calls below 10 rows give the per-row bytes, so the byte tests never exercised the 2-vs-3-row boundary; a mutant that drops the single-Mac clause (back to #70), or `<` -> `<=`, survived them. Float16ConstantCastTests (`@testable import MLXNN`) now asserts `takesPerRowPath` itself: true at 2 rows and false at 3, 12 and 512 rows for a single Mac, for decode-shaped [B, 1, H], prompt-shaped [1, S, H] and flat [M, H] calls, and true at every row count with `independentRows`. Nothing is evaluated. Red first: the same test on 49c145c's code (threshold 12) fails at 3 and 12 rows (Fork Tests on test/bonsai-switch-2-3-red); each mutant above fails it too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Base:
release/provider-0.10.12=c633d221d6, the provider's current pin (owner, 11-12)c633d221d6(Preserve Bonsai row arithmetic and native fence ordering #61's head, which provider 0.10.5–0.10.12 pin).Source/Cmlx/mlxstays72dffa2dandSource/Cmlx/mlx-cstays2ac897e3. The diff is 5 files:Source/MLXNN/Hadamard.swift,Source/MLXNN/Quantized.swift(a comment), two test files andfork.yaml. No Metal, C or C++ source changes, somlx.metallibis unchanged (5b97893c…).main(52c36f86ec) is Preserve Bonsai row arithmetic and native fence ordering #61's merge commit. Its tree equalsc633d221d6's (41261958…). Batched Bonsai 27B prompt rows by default; per-row FP32 arithmetic only on request (pairs) #69 carries the same three commits onmainfor the latermainmerge (same resulting tree,15b25f0f…).What
HadamardQuantizedLineargainsindependentRows(init parameter, defaultfalse).(Bonsai 2 27B) takes one batched packed matmul again: the path single-Mac prefill used before
fe35554(provider ≤ 0.10.4). That is the batched matrix kernel: TF32 (NAX) on M5, FP32 steel on M1–M4.
independentRows: true(pairs):fe35554's M=1-per-row reduction, unchanged. Each row gets the bytes of aseparate singleton call on every chip. d-inference's pair stages (
QwenPrismHadamardSource.install) ask for it.QuantizedLinear.independentRowsForward, and the FP16→FP32 constant reuse.Why
fe35554("Preserve singleton FP32 arithmetic for Bonsai packed rows", first shipped in provider 0.10.5 viac633d221) sends every prompt row of the 27B through an M=1 kernel. Engine-measured prefill fell 2.3–6× on every chip:Bonsai's first-token deadline is 9 s + 5 ms per prompt token, so image and tool prompts of about 1–2K tokens and
more miss it, and OpenRouter gets 429s: only 35 % of image requests and 47 % of tool requests are served.
Owner's decision (option i): single-Mac serving goes back to the batched kernel for prompt processing. Pairs keep
the exact per-row arithmetic, because their two machines must agree. Prompt rows on a single Mac are no longer
bit-identical to the singleton decode arithmetic, and concurrent decode rows depend on their cohort, both as before 10-30. The owner accepted both.
Commits
c7cb850bf3testBonsaiFloat32RowsDefaultToOneBatchedPackedMatmul. A default layer's 512-row and 4×128-row FP32 output must equal onequantizedMMover all rows with FP32-widened constants, with and without the grouped GDN layout. On this head every row goes through M=1, so it fails where the two kernels differ. Red: Fork Tests 43138144326 ran on073a7119ed(shard 3, 4 assertions); that commit has the same tree asc7cb850bf3but was built onmain.ac8f03e36cindependentRows, the profile check pulled intoisBlock1024Profile, comments, andfork.yaml("Bonsai packed signed-Hadamard layers"). The singleton-row test now builds its layers withindependentRows: true. The default test also checks that the opt-in leaves a block512 profile on the batched path.d7464f7d08Float16ConstantCastTests: the oracle takes the per-row path only forindependentRowslayers, so the old assumption no longer fails CI (it failed in 43138178252, shard 2, on themain-based twin). The repeated-call/mutation and compile-state tests run both modes. Tests only.Only Swift in
Source/MLXNNchanges: no Metal, C or C++ source, somlx.metallibis unchanged.swift formatis clean on the three changed Swift files.Consumers
0e6ac37885): the pin bump plus the pair stage's opt-in. It pins this PR's head for CI and this branch's merge commit after review. The pair'sbonsai-fp32-row-arithmetic-v2receipt stays byte-identical because the pair's arithmetic is unchanged.PrismHadamardCheckpoint.install) builds the single-Mac layers withthe default.
Qwen35BatchDecodeProjection) runs only underQwen35BatchDecodeScope.enabled, which only cluster sessions set. So a single-Mac decode step with several requests takes one batched matmul too, as in 0.10.4, and a request's exact output can depend on the requests decoding beside it. Pairs (independentRows: true) stay per-row exact.Review
Reviewed and merged at 03:11:45Z: merge commit
1b3520fe94, which d-inference #2022 pins. Review S1 (a test that builds the block1024 case without the argument) is covered from the consumer side in #2022 (BonsaiRowArithmeticTests.aSingleMacLoaderBuildsBatchedProjections). It goes into #69 formain.