Skip to content

perf(cbv2): one fused causal SDPA per full-attention prompt chunk - #327

Merged
Gajesh2007 merged 3 commits into
mainfrom
ttft/fused-causal-prefill
Oct 22, 2026
Merged

Gajesh2007 merged 3 commits into
mainfrom
ttft/fused-causal-prefill

Conversation

@Gajesh2007

@Gajesh2007 Gajesh2007 commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Paired provider PR: Layr-Labs/d-inference#1540. Replayed onto current main for publication; the tests below ran on the base named in Validation.

Update for publication (2026-10-21, evening): replayed onto current main 19634dd8 (adds #318 and #314), which d-inference master now pins. Validation below ran on 66f556b7.

Summary

CBv2 attends every prompt chunk longer than 128 queries in 128-query blocks (CBv2AttentionV1.queryBlockSize), and on paged storage each block (Qwen4 QSA aside) also builds an absolute BOOL mask. Blocking bounds the score tensor of MLX's composed path. On a head geometry that MLX serves with a fused kernel there is no score tensor, so blocking only splits one launch into L/128 under-filled launches, plus mask builds and an output concat. GPT-OSS (head_dim 64) paid this on every full-attention layer, and Qwen's head_dim 256 layers could never reach MLX's NAX head-dim-split kernel, which needs 1024+ queries and no array mask.

A full-attention prompt chunk now takes one .causal SDPA call on both KV backends when all of these hold (CBv2AttentionV1.usesFusedCausalPrefill):

  • no window, softcap, span overlay or bidirectional mask, and not MiMo's NAX route;
  • MLX fuses the geometry (mlxFusesCausalAttention): head_dim 64/72/80/96/128 on every Apple GPU, head_dim 256 only on NAX from 1024 queries in bf16/fp16, never qL <= 8;
  • the default stream is a GPU stream.

The keys are the row's visible history and the queries are its trailing columns, which is MLX's bottom-right .causal alignment, so no mask is built. Everything else keeps the blocked path.

The route follows the layer geometry, not a model list:

Model Takes the route
GPT-OSS 20B (head_dim 64 full layers with sinks) Every chip, every prompt chunk over 8 tokens
Nemotron 3.5 Lightning (head_dim 128 attention layers) Every chip, every prompt chunk over 8 tokens. Not measured
Qwen3.5/3.8 dense (head_dim 256 full layers) M5 (NAX) only, chunks of 1024+ tokens (solo prefill stripes are 4096)
Qwen 3.5/3.6 MoE, Bonsai 9B (prism_hadamard_qwen35), Qwen4 Flash-Next dense QSA prefill (head_dim 256) M5 (NAX) only, chunks of 1024+ tokens (MoE solo stripes are 2048). Not measured
Qwen3-VL MoE text layers Every chip when their head_dim is 64-128 (no local config was checked). Not measured
Gemma 4 (head_dim 256 sliding windows, head_dim 512 global), MiMo V2.6 (query/value head_dim 192/128) No

Kill switch: DARKBLOOM_TTFT_FUSED_CAUSAL_PREFILL=0 (exact 0, read once per process) restores the previous routing on both backends.

Commits: ff327738 perf(cbv2): one fused causal SDPA per full-attention prompt chunk and the refactor-pass commit fd17d026 refactor(cbv2): fused causal prefill comments and a route-wiring check, on main @ 66f556b7 (#318). They add Tests/MLXLMTests/Kernel/Cache/CBv2FusedCausalPrefillTests.swift and two fork.yaml sentences. The provider pin is the paired Layr-Labs/d-inference PR (branch ttft/fused-causal-prefill).

Before

flowchart TB
  subgraph SB["Behavior: one full-attention layer, prompt chunk of L queries"]
    direction LR
    pb["Prompt chunk<br/>L queries over its history"]:::input --> kb["Split into L/128<br/>query blocks"]:::slow
    kb --> mb["Paged storage: build an<br/>absolute BOOL mask per block"]:::slow
    mb --> gb{"head_dim"}:::decision
    gb -- "64-128" --> fb["Fused kernel per block<br/>under-filled launches"]:::stock
    gb -- "256" --> cb["Composed SDPA per block<br/>score tensor materialized"]:::slow
    fb --> ob["Concatenate block outputs<br/>GPT-OSS 20B, 32K: 19.5 s TTFT on M3 Ultra"]:::state
    cb --> ob
  end
  subgraph CB["Code flow: CBv2 prompt attention"]
    direction LR
    ua["CBv2AttentionV1<br/>updateAndAttendRow"]:::code --> qa{"shouldBlockQueries(L)"}:::decision
    qa -- "L > 128" --> ab["attendQueryBlocks"]:::stock
    qa -- "L <= 128" --> at["attend: one .causal call"]:::stock
    pa["PagedLayerCache<br/>prefillAttend"]:::code --> pq["Block loop over<br/>kpos <= qpos mask slices"]:::stock
  end
  SB ~~~ CB
  style SB fill:#f8fafc,stroke:#64748b,color:#0f172a
  style CB fill:#f8fafc,stroke:#64748b,color:#0f172a
  classDef input fill:#dbeafe,stroke:#1d4ed8,color:#1e3a8a
  classDef stock fill:#e5e7eb,stroke:#4b5563,color:#111827
  classDef slow fill:#fee2e2,stroke:#b91c1c,color:#7f1d1d
  classDef state fill:#ede9fe,stroke:#6d28d9,color:#3b0764
  classDef code fill:#e0e7ff,stroke:#4338ca,color:#1e1b4b
  classDef decision fill:#fef3c7,stroke:#b45309,color:#78350f
  linkStyle default stroke:#475569,stroke-width:1.5px
  linkStyle 11 stroke:none
Loading

After

flowchart TB
  subgraph SA["Behavior: one full-attention layer, prompt chunk of L queries"]
    direction LR
    pa["Prompt chunk<br/>L queries over its history"]:::input --> ea{"Fused-eligible?<br/>full attention, no window,<br/>softcap, span or bidirectional"}:::decision
    ea -- "head_dim 64-128, any GPU<br/>head_dim 256: NAX, 1024+ queries" --> fa["One .causal SDPA call<br/>fused kernel: no scores, no mask"]:::new
    ea -- "other shapes, or<br/>DARKBLOOM_TTFT_FUSED_CAUSAL_PREFILL=0" --> ba["128-query blocks<br/>unchanged"]:::stock
    fa --> oa["GPT-OSS head_dim 64: bit-identical<br/>head_dim 256: within bf16 rounding<br/>GPT-OSS 20B, 32K: 17.4 s TTFT on M3 Ultra"]:::state
    ba --> oa
  end
  subgraph CA["Code flow: CBv2 prompt attention"]
    direction LR
    ua["CBv2AttentionV1<br/>updateAndAttendRow"]:::code --> pr["usesFusedCausalPrefill<br/>mlxFusesCausalAttention<br/>GPU stream check"]:::new
    pp["PagedLayerCache<br/>prefillAttend"]:::code --> pr
    pr -- "true" --> sc["MLXFast SDPA<br/>mask: .causal"]:::new
    pr -- "false" --> bl["attendQueryBlocks /<br/>paged block loop"]:::stock
  end
  SA ~~~ CA
  style SA fill:#f8fafc,stroke:#64748b,color:#0f172a
  style CA fill:#f8fafc,stroke:#64748b,color:#0f172a
  classDef input fill:#dbeafe,stroke:#1d4ed8,color:#1e3a8a
  classDef stock fill:#e5e7eb,stroke:#4b5563,color:#111827
  classDef new fill:#dcfce7,stroke:#15803d,color:#14532d
  classDef state fill:#ede9fe,stroke:#6d28d9,color:#3b0764
  classDef code fill:#e0e7ff,stroke:#4338ca,color:#1e1b4b
  classDef decision fill:#fef3c7,stroke:#b45309,color:#78350f
  linkStyle default stroke:#475569,stroke-width:1.5px
  linkStyle 9 stroke:none
Loading

Legend: blue is input, grey is unchanged code, red is the cost this PR removes, green is new, amber is a decision, purple is the observable result, indigo is code that is otherwise unchanged.

Measured effect

Logs are in pr/attn-fused/ of the TTFT session scratchpad on the M5 Ultra (/private/tmp/claude-501/-Users-gaj-Documents-Builds-Darkbloom/8eacbef9-e1dd-444e-98a8-972bc1d30258/scratchpad/pr/attn-fused/). Every A/B runs one binary with and without the kill switch, arms interleaved. Times are cold engine TTFT in ms from darkbloom benchmark --scheduler-prefill.

M3 Ultra (no NAX, fleet-representative). Implementation build d92dbc8e (same route code as ff327738), load average 1.1-2.5. Log: evidence/m3u/prefill.log.

Model Prompt tokens Kill switch Route on Change
GPT-OSS 20B 512 220.8, 225.3 218.9, 218.5 -2.0%
2,048 783.6, 783.6 770.9, 770.9 -1.6%
8,192 3,410.6, 3,414.2 3,300.2, 3,305.0 -3.2%
32,768 19,498.5, 19,491.1 17,446.3, 17,431.5 -10.5% (1.12x)
Qwen3.5-9B (no route without NAX) 512 / 8,192 348.4 / 5,487.2 348.0 / 5,498.1 -0.1% / +0.2%
Gemma 4 26B QAT (no route) 512 / 8,192 209.3 / 2,978.0 210.2 / 2,971.3 +0.4% / -0.2%

M3 Ultra load test, GPT-OSS 20B (rate 0.1/s for 300 s, seed 7, 4 in flight, 32 requests, one run per arm; evidence/m3u/load.log): TTFT p50/p90/p99 7.33/15.98/23.03 s with the kill switch, 6.35/13.13/20.28 s with the route; decode p50 52.1 and 53.8 tok/s; no errors.

M5 Ultra (NAX). The GPU was shared with up to three other jobs (load average 30-103), so absolute times are indicative only.

Model Prompt tokens Kill switch Route on Change of means Build and log
GPT-OSS 20B 8,192 1,227.3, 1,259.8 1,066.7, 1,154.7 -10.7% d92dbc8e, evidence/m5u-impl/V1.out
32,768 9,553.1, 8,971.5 5,966.4, 6,435.5 -33.1% (1.49x) same
GPT-OSS 20B 512 / 2,048 medians of 6: 240.1 / 597.1 medians of 6: 228.4 / 551.5 -4.9% / -7.6% (medians) this PR (bd676704), logs/gpu-ticket.log
Qwen3.5-9B 32,768 11,827.1, 9,160.9 9,731.4, 8,015.0 -15.4% d92dbc8e, evidence/m5u-impl/V2.out
8,192 / 32,768 2,810.2, 3,122.2 / 15,524.4, 12,835.3 2,634.6, 1,941.7 / 11,414.6, 8,637.3 -22.9% / -29.3% this PR, logs/gpu-ticket.log

Qwen3.5-9B peak MLX memory at 32,768 tokens drops from 13.86 GB to 12.03 GB in both M5 runs: its head_dim 256 layers no longer materialize a score tensor per block.

Output.

Check Result Log
GPT-OSS 20B teacher-forced scoring, inputs 2/3/4 (1,024/2,048/3,072 prompt tokens, 256 forced tokens), M3 Ultra Mean NLL 4.802980 / 5.074219 / 3.550767 in both arms; plain top-1, every per-token score bit and the repeated diagnostic identical evidence/m3u/quality.log, evidence/m3u/tf-compare.txt
Same, M5 Ultra: input 3 with the route and with the production master binary; input 4 also with the kill switch 5.030523 / 3.475075 in every arm evidence/m5u-impl/SA.out
Qwen3.5-9B greedy generation, 64 tokens after an 1,846-token prompt (one solo chunk, so the head_dim 256 layers take the NAX kernel), M5 Ultra, this PR 64 of 64 tokens identical with and without the kill switch logs/gpu-ticket.log, logs/gen/
head_dim 256 unit case (bf16, 1,030-query chunk after 500 history) against the fp32 absolute-position reference, M5 Ultra max abs error 0.00195 with the route, 0.00391 with the kill switch logs/sdk-test-default.log, logs/sdk-test-default-killswitch.log

Correctness tests

KernelTests.CBv2FusedCausalPrefillTests (kernel type, Tests/MLXLMTests/Kernel/Cache/):

Test Checks
routingMirrorNeverClaimsAComposedShape mlxFusesCausalAttention never claims a shape MLX composes: head_dim 192/512, head_dim 256 below 1024 queries, off NAX or in fp32, mismatched value width, qL <= 8
onlyPlainFullAttentionChunksAreEligible Windows, softcaps, spans and bidirectional chunks stay blocked; sinks are eligible; the kill switch turns every case off
sinkModelPrefillMatchesReferencesOnBothBackends (fp32, bf16) GPT-OSS geometry (head_dim 64, GQA 8, sinks), chunks 384+256+200 on paged and contiguous: within tolerance of an fp32 absolute-position reference and of the blocked BOOL-mask calls the route replaces. The difference from the blocked calls was 0 in all four M5 Ultra configurations, but the test asserts the tolerance, not bit identity
pagedRouteNeverLeaksFutureKeys Perturbing key and value p+1 leaves every query <= p bit-identical and moves the later ones
headDim256ChunkAfterHistoryMatchesReference Qwen geometry, 500 then 1,030 queries, against the fp32 reference on both backends. On NAX MLX widens the contiguous view's kL to the 32-key tile, and the two backends must still agree exactly. With the route enabled on NAX, the 1,030-query output must also equal MLX's own single .causal call bit for bit, which the blocked path does not, so this check fails if the route is not wired

Validation

M5 Ultra (applegpu_g17d), PR clone, SDK commands from libs/mlx-swift-lm. Toolchain: Apple Swift 6.4 (Command Line Tools); XCTest and the Metal toolchain from Xcode 27.1. The GPU commands ran in one shared-queue ticket (gpu-ticket.sh, log logs/gpu-ticket.log).

Command Outcome
nice -n 15 swift build --build-tests Build complete. SwiftPM resolved mlx-swift 81f58d79 (nested MLX 71271f08)
../../scripts/stage-test-metallib.sh "$(swift build --show-bin-path)" Staged mlx.metallib SHA-256 fb1ba8f9… into the test bundles
MLX_ENABLE_TF32=0 swift test --skip-build --filter 'CBv2FusedCausalPrefill|CBv2QueryBlockAttention|CBv2PagedQueryBlocking|CBv2AttentionSinkDtype|CBv2PagedPackedSpan' Passed: 53 tests in 13 suites, and again with DARKBLOOM_TTFT_FUSED_CAUSAL_PREFILL=0
The same filter in the default environment (TF32 on M5) The new suite passes. 5 existing CBv2QueryBlockAttention tests miss their fp32 tolerances (2e-5, 5e-5), the same 5 with the kill switch (145 issues with the route, 156 without)
swift test --skip-build --filter 'CBv2LastQueryPrefill|CBv2AttentionPacket|CBv2PagedKernel|CBv2ModelTests|CBv2RuntimeKVStorage|Gemma4ForceSharedKV|CBv2PagedSegmentDispatchCache|CBv2PagedFrozenChunkGather' (the other suites that build head_dim 256 layers) 119 tests in 20 suites. The same 11 tests fail with the route and with the kill switch, all tight fp32 tolerance checks (2e-6 to 1e-5) under TF32; logs/fail-broad.txt, logs/fail-broad-killswitch.txt
swift format format --configuration .swift-format on the three changed Swift files (swift-format 604.0.0 from Homebrew and the CLT formatter) No changes
python3 scripts/check_forkdiff.py 1,695 changed files, all described
git merge-tree --write-tree origin/main ff327738 against main @ 19634dd8 (#314, landed during preparation) Merges without conflicts

Compatibility

  • No public API change. usesFusedCausalPrefill, mlxFusesCausalAttention and fusedCausalPrefillEnabled are internal statics of CBv2AttentionV1.
  • Peak memory can only drop: the fused kernels materialize no scores.
  • Decode, MTP verification, sliding windows, softcaps, vision spans, bidirectional chunks and MiMo's NAX route are unchanged. Paged layers with live packed (quantized) KV keep their own prompt path (PagedQuantizedLayerOperation).

Limitations

  • Qwen quality evidence is indirect. Teacher-forced scoring prefills in prefillChunkSize (512) chunks (EngineLoopV2.scoreTeacherForced), and the head_dim 256 route needs 1024+ queries, so teacher-forced NLL cannot reach it. The evidence is the identical 64-token greedy output, the unit case against the fp32 reference, and that this is MLX's own kernel for a single .causal call of that shape.
  • head_dim 256 on NAX is not chunk-partition invariant: a chunk below 1024 queries keeps the composed blocks, so one prompt cut two ways can differ within rounding.
  • head_dim 72-128 should be bit-identical for the same reason as head_dim 64 (the same fused kernel; masked tiles add exact zeros), but only head_dim 64 is tested, and no head_dim 72-128 model was measured. Nemotron 3.5 Lightning (head_dim 128) takes the route on every chip without a measurement, and Qwen 3.5/3.6 MoE, Bonsai 9B and Qwen4 Flash-Next take the head_dim 256 route on M5 without one.
  • M5 Ultra times come from a GPU shared with other jobs. One earlier sequential (not interleaved) M5 Ultra GPT-OSS run had the route slower at 512 tokens (95.0, 95.1 against 102.0, 106.8 ms; evidence/m5u-impl/ab-gptoss.txt); the interleaved runs did not reproduce it.
  • The provider LaunchAgent does not forward the kill switch, so it only applies to providers started in the foreground.
  • Both commits use the local identity Darkbloom TTFT <ttft@darkbloom.local> and are not signed; re-sign before publication. The figures were rendered locally with mmdc 11.15.0 in light and dark themes; GitHub rendering is not verified until push.

Landing order

  1. Land this SDK PR first (squash).
  2. Re-pin the paired provider PR's libs/mlx-swift-lm to the squash commit. The provider PR moves libs/mlx-swift to 81f58d79 and libs/mlx to 71271f08 because main @ 66f556b7 needs them. main has since moved to 0351444e (GLM-5.3-Flash native: NVFP4 hybrid, fused serving kernels, MTP, images #314, Pin mlx-swift main 67dc7528 #325, perf(nemotron-super): fused MTP verify window, window submission, NAX prefill kernels on M5 (onto main) #323), which pins mlx-swift 67dc7528 (nested MLX ad4745a2). If the squash lands there, the provider follows those pins and rebuilds the metallib. ad4745a2 is one commit on 71271f08 that changes quantized kernels and allocator code but not the SDPA dispatch (has_fused_kernel, use_fallback, sdpa_full_self_attention_nax), so mlxFusesCausalAttention still mirrors it.
  3. Sibling TTFT PRs (for example ttft/qwen-gdn-prompt-kernels) re-pin the same gitlinks and conflict only on them. After each squash, re-pin to the SDK main head that contains every landed change.

Not in this PR

The implementation branch also has a second phase: it forces MLX's head-dim-split kernel for head_dim 256 chunks from 256 queries and for Gemma 4 sliding windows on NAX, through a new force_fused entry point in Layr-Labs/mlx-swift and mlx-c. That phase has not been validated and is not part of this PR.

Refactor pass

An independent refactor subagent (d-inference AGENTS.md, "Required Refactor Pass") reviewed this branch before publication and added one behavior-preserving commit, fd17d026:

Change Reason
PagedLayerCache.prefillAttend: the route comment now says every prefillKVWritingChunk view holds keys [start, queryStart + l) It said "both shapes"; the function returns three (frozen replay, chunk only, history plus chunk), and borrowers reuse them
CBv2AttentionV1.attendQueryBlocks: the scope paragraph names the usesFusedCausalPrefill exception It still said every multi-token prompt call is blocked
headDim256ChunkAfterHistoryMatchesReference: on NAX with the route enabled, the 1,030-query paged output must equal MLX's own single .causal call bit for bit No check failed with the route unwired: every numeric expectation also holds on the blocked path, as the kill-switch runs show. The blocked path's error against the reference is 0.0039 and the single call's is 0.0020, so the two outputs differ and the check fails without the wiring on M5. It does not run off NAX and cannot detect head_dim 64-128 wiring, whose output is bit-identical either way
Test header (1,030 queries, not 1,024), tolerances state their reasons, the sinks layer kind is renamed withSinks Accuracy; CONTRIBUTING.md asks each test to state its tolerance and the reason

Both production edits change comments only and keep every line number.

Reviewed and kept:

  • usesFusedCausalPrefill reuses MiMoV26NAXGatherQMM.naxAvailable and gpuStream. A neutral home would read better, but moving them edits MiMo code outside this change.
  • The kill switch's DARKBLOOM_TTFT_ prefix names the initiative, not the subsystem (AGENTS.md avoids wave names in files; the SDK's other switches use DARKBLOOM_CBV2_). Sibling TTFT PRs share the prefix, so a rename is a decision for all of them.
  • useMiMoNAX || in updateAndAttendRow is redundant today (MiMo's 192/128 widths never pass mlxFusesCausalAttention) but states the exclusion.
  • head_dim 64 bit identity with the blocked calls stays measured rather than asserted: asserting it would make a claim for every Apple GPU that was checked only on M5 Ultra.
  • mlxFusesCausalAttention matches has_fused_kernel and use_fallback in nested MLX 71271f08 and in SDK main's ad4745a2.
  • Prompt chunks carry no metadata or packet observer (begin accepts decode shapes only), so the contiguous fall-through to attend captures nothing new. Step-owned paged attention prices blocked work but admits only unequal query and value widths, which the route never takes.

PR body corrections: the route table (Nemotron 3.5 Lightning, Qwen 3.5/3.6 MoE, Bonsai 9B, Qwen4 Flash-Next, Qwen3-VL MoE), the head_dim 64 test row (asserted within tolerance, measured bit-identical), the M5 teacher-forced arms, live packed paged KV, the landing note for SDK main @ 0351444e, and arrow color in the four figures so edges stay visible in the dark theme.

Validation of fd17d026 on the M5 Ultra (GPU ticket refactor/gpu-ticket.sh, log refactor/logs/gpu-ticket.log, both under pr/attn-fused/):

Command Outcome
swift build --build-tests, then stage-test-metallib.sh (build-sdk-tests.sh) Build complete; metallib fb1ba8f9…
The focused filter above with MLX_ENABLE_TF32=0, route and kill switch 53 tests in 13 suites pass in both arms
The same filter, default environment, route and kill switch The new suite passes in both arms, the new check included with the route on. The same 5 existing tests fail with the same issue count per test as the earlier run (145 and 156 issues)
The 8 suites with head_dim 256 layers, route on, MLX_ENABLE_TF32=0 119 tests in 20 suites pass
The same suites, default environment The same 11 tests fail as in the earlier run (TF32 tolerances)
swift-format (Homebrew 604.0.0 and the CLT formatter) on the three changed files No changes
check_forkdiff.py 1,695 changed files, all described
git merge-tree --write-tree origin/main fd17d026 with main @ 0351444e (#325 and #323 landed after #314) No conflicts
mmdc 11.15.0 on the four figures, default and dark themes All render

🤖 Generated with Claude Code

Gajesh2007 and others added 2 commits October 21, 2026 17:00
Prompt chunks were attended in 128-query blocks, and on paged storage
each block also built an absolute BOOL mask. Blocking bounds the score
tensor of MLX's composed path, but on a head geometry MLX fuses it only
splits one well-occupied launch into L/128 under-filled ones, each with
a mask build, plus an output concat. GPT-OSS (head_dim 64) paid that on
every full layer, and Qwen's head_dim 256 layers could never reach the
NAX head-dim-split kernel (it requires qL >= 1024 and no array mask).

Full-attention chunks with no window, softcap, span overlay or
bidirectional chunk now take one `.causal` SDPA call on both backends
when MLX routes the shape to a fused kernel: head_dim 64/72/80/96/128 on
any GPU, head_dim 256 only on NAX from 1024 queries in bf16/fp16. The
keys are the row's visible history and the queries their trailing
columns, which is MLX's bottom-right `.causal` alignment. Sliding
windows, softcaps, vision spans, head_dim 192/512, D256 off NAX and
MiMo's own route keep the blocked path, so Gemma 4 is unchanged. The
NAX and GPU-stream checks reuse `MiMoV26NAXGatherQMM.naxAvailable` and
`gpuStream`.

head_dim 64 output is bit-identical to the blocked BOOL-mask calls (fp32
and bf16, with sinks). The fused kernel materializes no scores, so peak
memory can only drop.

`KernelTests.CBv2FusedCausalPrefillTests` (Tests/MLXLMTests/Kernel/Cache)
checks the routing mirror, eligibility, parity on both backends against
absolute-position and blocked references, and causality by perturbation.
Its head_dim 256 case runs a 500-query chunk and then a 1030-query one
over a contiguous buffer with slack past the visible keys: on NAX MLX
widens that view's kL to the 32-key tile and reads the slack rows, and
the two backends must still agree exactly. fork.yaml describes the route
and the suite.

DARKBLOOM_TTFT_FUSED_CAUSAL_PREFILL=0 restores the previous routing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Refactor pass over the fused causal prefill route. No behavior change.

- `PagedLayerCache.prefillAttend`: `prefillKVWritingChunk` returns three
  key views (frozen replay, chunk only, history plus chunk), not two.
  The route comment now says every view holds `[start, queryStart + l)`.
- `CBv2AttentionV1.attendQueryBlocks`: the scope paragraph names the
  `usesFusedCausalPrefill` exception instead of saying every multi-token
  prompt call is blocked.
- `CBv2FusedCausalPrefillTests`: no check failed with the route unwired,
  because every numeric expectation also holds on the blocked path. On
  NAX, with the route enabled, `headDim256ChunkAfterHistoryMatchesReference`
  now requires the 1030-query paged output to equal MLX's own single
  `.causal` call bit for bit; the blocked composed path differs in the low
  bits. The header says 1030 queries (not 1024), the tolerances state
  their reasons, and a layer kind named `sinks` is now `withSinks`.

Both production edits change comments only and keep every line number.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T00:36:57.406832Z 684cf53 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Gajesh2007
Gajesh2007 merged commit 1bee572 into main Oct 22, 2026
4 checks passed
@Gajesh2007
Gajesh2007 deleted the ttft/fused-causal-prefill branch October 22, 2026 00:31
Gajesh2007 added a commit to Layr-Labs/d-inference that referenced this pull request Oct 22, 2026
Layr-Labs/mlx-swift-lm#327 squash-merged as 1bee5728 ("perf(cbv2): one fused causal SDPA per full-attention prompt chunk (#327)").
Move the gitlink from the PR branch head fd17d026 to that commit on SDK main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Gajesh2007 added a commit to Layr-Labs/d-inference that referenced this pull request Oct 22, 2026
* perf(native): fused causal SDPA for full-attention prompt chunks

Bump libs/mlx-swift-lm to the CBv2 change that attends a full-attention
prompt chunk with one `.causal` SDPA call whenever MLX serves the head
geometry with a fused kernel (head_dim 64/72/80/96/128 on every Apple
GPU; head_dim 256 on NAX from 1024 queries), instead of 128-query blocks
with explicit masks. GPT-OSS full layers and the Qwen full-attention
layers of M5 solo prefill stripes take the new route; Gemma 4 does not.

The SDK pin moves from 19634dd8 to ff327738: SDK main (#316, #312, #315,
#317, #318) plus the change. SDK main requires mlx-swift 81f58d79
(nested MLX 71271f08), so libs/mlx-swift moves from 6923a80f and
libs/mlx from cb77239b; the Metal library built from the nested MLX is
byte-identical.

Document the SDK-read kill switch DARKBLOOM_TTFT_FUSED_CAUSAL_PREFILL=0
in the configuration reference and the prompt-chunk attention route in
the inference architecture, and add a changelog fragment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(native): name the unmeasured models the fused causal route reaches

Refactor pass over the fused causal prefill change.

Pin libs/mlx-swift-lm to fd17d026, the head of the paired SDK branch: one
refactor commit over ff327738 that changes comments and the route's test,
not compiled code.

The route follows layer geometry, so it also reaches the Nemotron 3.5
Lightning attention layers (head_dim 128, every chip) and the head_dim
256 full-attention layers of the other Qwen-family models in M5 solo
prefill stripes. The changelog fragment now names them and says they were
not measured. The inference and configuration rows note that Qwen4 QSA
blocks build no absolute mask and that live packed paged KV keeps its own
prompt path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Pin libs/mlx-swift-lm to the SDK change on master's SDK commit

The paired SDK PR branch (ttft/fused-causal-prefill) was published on SDK main 0351444e, which
also carries mlx-swift-lm #323 and #325 that d-inference master does not pin
yet. Pin ttft/fused-causal-prefill-pin (6d1321ce) instead: the same commits cherry-picked onto
0351444e, the SDK commit master pins, so this PR adds only this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Revert "Pin libs/mlx-swift-lm to the SDK change on master's SDK commit"

d-inference master now pins SDK main 0351444e (#1531), the base the paired SDK
PR branch (ttft/fused-causal-prefill) was published on, so the PR head is already the right pin and
the cherry-picked ttft/fused-causal-prefill-pin copy is redundant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Re-pin libs/mlx-swift-lm to the merged SDK change (#327)

Layr-Labs/mlx-swift-lm#327 squash-merged as 1bee5728 ("perf(cbv2): one fused causal SDPA per full-attention prompt chunk (#327)").
Move the gitlink from the PR branch head fd17d026 to that commit on SDK main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(provider): document fused causal prefill memory in hardware support

The fused causal prompt-attention route lowers the prefill transient
(Qwen3.5-9B on M5 Ultra at 32K: 13.86 -> 12.03 GB peak) but only
inference.md described it. Record in hardware-support.md what the route
drops, and that the activation reserve, measured floors, minimum load KV
and slot grants intentionally stay unchanged, with the reasons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(provider): restamp the fused causal prefill docs

inference.md and configuration.md changed for the fused causal prompt
attention route; set their freshness stamps to the UTC date of the
change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
javiech1 added a commit to javiech1/mlx-swift-lm that referenced this pull request Oct 22, 2026
javiech1 pushed a commit to javiech1/mlx-swift-lm that referenced this pull request Oct 24, 2026
* Add Gemma 4 12B unified
javiech1 pushed a commit to javiech1/mlx-swift-lm that referenced this pull request Oct 24, 2026
…rs (Layr-Labs#330)

Mirrors the LLM-side sanitize drop merged in Layr-Labs#342 to the MLXVLM Gemma4
text backbone: KV-shared layers (the last `num_kv_shared_layers`) reuse an
earlier layer's K/V and own no `k_proj`/`v_proj`/`k_norm`. After Layr-Labs#327,
those module slots are optional and not built for shared layers. QAT
checkpoints already omit the redundant tensors; some PTQ checkpoints still
ship them and would fail to load against the smaller tree.

Scope the drop to the text backbone only — `vision_tower` / `audio_tower`
share the `layers.N.self_attn.{k,v}_proj` naming, so an unguarded
predicate would amputate tower layers >= firstKVSharedLayer.
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