Skip to content

perf(native): fused causal SDPA for full-attention prompt chunks - #1540

Merged
Gajesh2007 merged 9 commits into
masterfrom
ttft/fused-causal-prefill
Oct 22, 2026
Merged

Gajesh2007 merged 9 commits into
masterfrom
ttft/fused-causal-prefill

Conversation

@Gajesh2007

@Gajesh2007 Gajesh2007 commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Paired SDK PR: Layr-Labs/mlx-swift-lm#327 (merge it first; then re-pin libs/mlx-swift-lm here to its squash commit). Both branches were replayed onto current main/master for publication; the tests below ran on the bases named in Validation.

Update for publication (2026-10-21, evening): d-inference master now pins SDK main 19634dd8, mlx-swift 67dc7528 and MLX ad4745a2 (#1519 and others). This branch was replayed onto current master: it pins libs/mlx-swift-lm to the paired SDK PR (replayed onto current SDK main) and no longer moves libs/mlx-swift or libs/mlx. Validation below ran on the pre-publication bases it names.

Summary

Pins the paired Layr-Labs/mlx-swift-lm PR (fd17d026, branch ttft/fused-causal-prefill): CBv2 attends a full-attention prompt chunk with one fused .causal SDPA call instead of 128-query blocks whenever MLX serves its head geometry with a fused kernel (head_dim 64/72/80/96/128 on every Apple GPU; head_dim 256 on M5 NAX chips from 1024 queries). The route follows layer geometry: GPT-OSS full-attention layers on every chip and the Qwen3.5/3.8 dense full-attention layers of M5 solo prefill stripes take it and were measured; Nemotron 3.5 Lightning attention layers (head_dim 128, every chip) and the head_dim 256 layers of Qwen 3.5/3.6 MoE, Bonsai 9B and Qwen4 Flash-Next dense prefill (M5) take it unmeasured. Gemma 4, sliding windows, softcaps, vision spans and live packed paged KV do not.

Change Path
14661d9b → fd17d026: SDK main @ 66f556b7 (Layr-Labs/mlx-swift-lm#316, #312, #315, #317, #318) plus the route (ff327738) and its refactor-pass commit libs/mlx-swift-lm
6923a80f → 81f58d79, which SDK main requires (Layr-Labs/mlx-swift#52, #57) libs/mlx-swift
cb77239b → 71271f08, the MLX that mlx-swift 81f58d79 nests. The Metal library built from it is byte-identical (SHA-256 fb1ba8f9…) libs/mlx
DARKBLOOM_TTFT_FUSED_CAUSAL_PREFILL row in the engine and scheduler table docs/reference/configuration.md
Prompt-chunk attention row in the scheduler and loop configuration table docs/architecture/inference.md
Changelog fragment changelog.d/fused-causal-prefill-attention.md

Kill switch: DARKBLOOM_TTFT_FUSED_CAUSAL_PREFILL=0, read by the SDK once per process. The LaunchAgent does not forward it, so it applies to foreground providers only.

Commits: e2885e5f3 and the refactor-pass commit 193823342, on master @ eaaf5e615 (#1529).

Before

flowchart TB
  subgraph PB["Behavior: cold prompt prefill on the provider"]
    direction LR
    rb["Request<br/>GPT-OSS 20B, 32K-token prompt"]:::input --> eb["CBv2 engine<br/>full-attention layers in 128-query blocks"]:::slow
    eb --> tb["Cold TTFT on M3 Ultra<br/>19.5 s"]:::state
  end
  subgraph GB["Code flow: pinned native libraries"]
    direction LR
    lb["libs/mlx-swift-lm<br/>14661d9b"]:::stock --> sb["libs/mlx-swift<br/>6923a80f"]:::stock
    sb --> mb["nested MLX cb77239b<br/>libs/mlx cb77239b"]:::stock
    db["docs: no prompt-chunk<br/>attention route"]:::stock
  end
  PB ~~~ GB
  style PB fill:#f8fafc,stroke:#64748b,color:#0f172a
  style GB 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
  linkStyle default stroke:#475569,stroke-width:1.5px
  linkStyle 4 stroke:none
Loading

After

flowchart TB
  subgraph PA["Behavior: cold prompt prefill on the provider"]
    direction LR
    ra["Request<br/>GPT-OSS 20B, 32K-token prompt"]:::input --> ea["CBv2 engine<br/>one fused .causal call per<br/>eligible full-attention chunk"]:::new
    ea --> ta["Cold TTFT on M3 Ultra<br/>17.4 s, output bit-identical"]:::state
    ka["DARKBLOOM_TTFT_FUSED_CAUSAL_PREFILL=0<br/>foreground only"]:::decision -. "restores" .-> oa["128-query blocks"]:::stock
  end
  subgraph GA["Code flow: pinned native libraries"]
    direction LR
    la["libs/mlx-swift-lm<br/>fd17d026: SDK main + route"]:::new --> sa["libs/mlx-swift<br/>81f58d79, required by SDK main"]:::changed
    sa --> ma["nested MLX 71271f08<br/>libs/mlx 71271f08<br/>metallib byte-identical"]:::changed
    da["docs: configuration.md row,<br/>inference.md row, changelog.d"]:::new
  end
  PA ~~~ GA
  style PA fill:#f8fafc,stroke:#64748b,color:#0f172a
  style GA 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 changed fill:#cffafe,stroke:#0e7490,color:#164e63
  classDef state fill:#ede9fe,stroke:#6d28d9,color:#3b0764
  classDef decision fill:#fef3c7,stroke:#b45309,color:#78350f
  linkStyle default stroke:#475569,stroke-width:1.5px
  linkStyle 5 stroke:none
Loading

Legend: blue is input, grey is unchanged, red is the cost removed, green is new, cyan is a pin moved because SDK main needs it, amber is the kill switch, purple is the observable result.

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/). Each A/B runs one binary with and without the kill switch, arms interleaved; times are cold engine TTFT in ms (darkbloom benchmark --scheduler-prefill).

Machine, model Prompt tokens Kill switch Route on Change Log
M3 Ultra, GPT-OSS 20B 8,192 3,410.6, 3,414.2 3,300.2, 3,305.0 -3.2% evidence/m3u/prefill.log
32,768 19,498.5, 19,491.1 17,446.3, 17,431.5 -10.5% (1.12x) same
M3 Ultra, GPT-OSS 20B 512 / 2,048 220.8, 225.3 / 783.6, 783.6 218.9, 218.5 / 770.9, 770.9 -2.0% / -1.6% same
M3 Ultra, Qwen3.5-9B and Gemma 4 26B (no route) 512, 8,192 within ±0.4% same
M5 Ultra, GPT-OSS 20B (shared GPU) 32,768 9,553.1, 8,971.5 5,966.4, 6,435.5 -33.1% (1.49x) evidence/m5u-impl/V1.out
M5 Ultra, Qwen3.5-9B, PR build bd676704 (shared GPU) 32,768 15,524.4, 12,835.3 11,414.6, 8,637.3 -29.3% logs/gpu-ticket.log

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 → 6.35/13.13/20.28 s; decode p50 52.1 → 53.8 tok/s; no errors. On M5 Ultra, Qwen3.5-9B peak MLX memory at 32,768 tokens drops from 13.86 GB to 12.03 GB (evidence/m5u-impl/V2.out, logs/gpu-ticket.log).

Output: GPT-OSS 20B teacher-forced scoring is bit-identical with and without the route (M3 Ultra inputs 2/3/4: every per-token score bit, evidence/m3u/tf-compare.txt; M5 Ultra inputs 3/4: same NLL as the production master binary, evidence/m5u-impl/SA.out). Qwen3.5-9B greedy generation on M5 Ultra, 64 tokens after an 1,846-token prompt, is token-identical with and without the route (logs/gpu-ticket.log).

Validation

M5 Ultra, PR clone, toolchain from toolchains/env.sh (Swift 6.4 CLT, Metal toolchain from Xcode 27.1).

Command Outcome
nice -n 15 swift build -c release --product darkbloom (from provider-swift/) Build complete; darkbloom SHA-256 bd676704… (logs/provider-build.log)
scripts/fetch-metallib.sh release mlx.metallib from nested MLX 71271f08, SHA-256 fb1ba8f9…, the same bytes as the metallib built from master's nested MLX cb77239b
GPU ticket with this binary: Qwen3.5-9B greedy on/off, GPT-OSS 20B and Qwen3.5-9B interleaved TTFT A/Bs Completed, results above (logs/gpu-ticket.log)
SDK suites at the pinned SDK commit (swift test --skip-build, focused attention suites plus 8 suites with head_dim 256 layers) New suite passes; existing suites have identical outcomes with and without the route (details in the SDK PR)
make docs-impact-check BASE=origin/master 6 changed files, canonical documentation coverage OK
make docs-check 186 files OK
python3 scripts/changelog.py check (make changelog-check) 27 valid fragments
make provider-test Not run: this PR changes pins and docs only

Limitations

  • Qwen quality evidence on M5 is indirect: teacher-forced scoring prefills in 512-token chunks and never reaches the head_dim 256 route (1024+ queries). The evidence is the token-identical greedy output above and the SDK unit case against an fp32 reference.
  • Nemotron 3.5 Lightning (head_dim 128 attention layers, every chip) and the head_dim 256 layers of Qwen 3.5/3.6 MoE, Bonsai 9B and Qwen4 Flash-Next dense prefill (M5) take the route without a TTFT or output measurement. head_dim 128 should be bit-identical for the same reason as GPT-OSS's head_dim 64 (one fused kernel; masked tiles add exact zeros), but only head_dim 64 is tested.
  • M5 Ultra times come from a GPU shared with up to three other jobs; the M3 Ultra numbers are the clean ones. The M3 Ultra runs and the M5 Ultra V1.out/V2.out runs use the implementation build d92dbc8e, which has the same route code as ff327738 on the older pins (14661d9b, 6923a80f); fd17d026 changes only comments and tests on top.
  • The kill switch only reaches foreground providers.

Landing order

  1. Land the SDK PR first (squash).
  2. Re-pin libs/mlx-swift-lm here to the squash commit. SDK main moved to 0351444e during preparation (GLM-5.3-Flash native: NVFP4 hybrid, fused serving kernels, MTP, images mlx-swift-lm#314, Harden Swift optional inference sites #325, Update Privacy Policy and ToS legal copy (June 2026) #323) and pins mlx-swift 67dc7528 (nested MLX ad4745a2); if the squash includes them, move libs/mlx-swift and libs/mlx to that pin and its nested MLX, rebuild the metallib (ad4745a2 changes quantized kernels, not the SDPA dispatch the route mirrors) and rerun make docs-impact-check and make docs-check.
  3. Sibling TTFT PRs that re-pin the same gitlinks conflict only on the gitlinks; re-pin to the SDK main head that contains every landed change.

Refactor pass

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

  • Pin libs/mlx-swift-lm to fd17d026, the head of the SDK branch. Its refactor commit changes comments (with unchanged line numbers) and the route's test, not compiled code; see the SDK PR's refactor pass.
  • Changelog fragment: 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 layers of other Qwen-family models on M5. The fragment now names them and says they were not measured, and it says GPT-OSS teacher-forced scores, rather than all output, are bit-identical.
  • inference.md and configuration.md rows: Qwen4 QSA paged blocks build no absolute mask, and live packed paged KV keeps its own prompt path.
  • PR body: the route list, the landing note for SDK main @ 0351444e, and arrow color in both figures so edges stay visible in the dark theme.

Reviewed and kept: the configuration row's place in the engine table (the SDK reads the switch at runtime, so it does not belong with the retired names), and the DARKBLOOM_TTFT_ prefix, which sibling TTFT PRs share.

Validation of 193823342:

Command Outcome
make docs-impact-check BASE=origin/master 6 changed files, canonical documentation coverage OK
make docs-check 186 files OK
make changelog-check, make changelog-preview 27 valid fragments; the fragment renders
SDK suites at fd17d026 (M5 Ultra) Same outcomes as at ff327738; the details are in the SDK PR
mmdc 11.15.0 on both figures, default and dark themes Both render
Provider release build Not rerun. fd17d026 differs from ff327738, which bd676704 was built from, only in comments and test code

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

Gajesh2007 and others added 2 commits October 21, 2026 17:01
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>
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>
@vercel

vercel Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
d-inference Ready Ready Preview Oct 22, 2026 12:34am UTC
d-inference-console-ui-dev Ready Ready Preview Oct 22, 2026 12:34am UTC
d-inference-landing Ready Ready Preview Oct 22, 2026 12:34am UTC
darkbloom-status Ready Ready Preview Oct 22, 2026 12:34am UTC
eigen-homepages-darkbloom Ready Ready Preview Oct 22, 2026 12:34am UTC

Request Review

@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-22T01:26:48.571128Z bd6e963 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.

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Threat model review — advisory

Reviewed head bd6e963fb057 against base a591744a2ed9.

Review not completed for this head. Previous findings are superseded, not confirmed resolved.
Review in progress; coverage is not complete

Models: budgeted Sonnet / selective Opus / selective Sol 6.1. Findings require human validation; this review never requests changes or blocks merging.

Coverage: 0/31 source units; cross-file integration pending. 0 selected depth batch(es) pending. All threat definitions indexed; relevant full definitions selected. Unchanged callers are not scanned.

Cost this run: $0.0000 reported, $0.0000 reserved with unknown cost; 0 paid request(s), 0 reused batch(es), 0 provider cache-hit tokens.

Saved findings and earlier report · Historical findings are not confirmed resolved.

Bedrock: 0 request(s), 0 uncached + 0 cache-read + 0 cache-write input / 0 output tokens; 0 request(s) with unknown usage. OpenRouter backup used 0 time(s). Dollar amounts above cover OpenRouter only; AWS billing is reconciled separately.

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>
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>
Refresh before review: master moved (Nemotron Super and SDK pin updates; it now
pins SDK 0351444e, mlx-swift 67dc7528 and MLX ad4745a2). Keep master's mlx-swift
and MLX pins and pin libs/mlx-swift-lm to fd17d026 (the paired SDK PR head).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0ee7a5b0e5

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread libs/mlx-swift-lm Resolved
Comment thread libs/mlx-swift-lm Resolved
Refresh before review: master moved (Nemotron Super and SDK pin updates; it now
pins SDK 889fe3cc, mlx-swift 67dc7528 and MLX ad4745a2). Keep master's mlx-swift
and MLX pins and pin libs/mlx-swift-lm to .

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cd8243245e

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/architecture/inference.md Resolved
Gajesh2007 and others added 2 commits October 21, 2026 18:16
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>
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>
@Gajesh2007
Gajesh2007 merged commit b004e23 into master Oct 22, 2026
37 of 41 checks passed
@Gajesh2007
Gajesh2007 deleted the ttft/fused-causal-prefill branch October 22, 2026 01:19

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bd6e963fb0

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@@ -0,0 +1,5 @@
### Fused causal prompt attention

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Sign the reviewed commit

Before this commit is submitted, recreate it with an accepted signature: fresh evidence beyond the earlier comment is that the actual reviewed head 744d6ba3280dfafde8e2c8ae627968afc176aba4 has no gpgsig header and git verify-commit exits 1, so it cannot display as GitHub-Verified as required for every PR commit.

AGENTS.md reference: AGENTS.md:L161-L162

Useful? React with 👍 / 👎.

jonathan308 added a commit that referenced this pull request Oct 22, 2026
Brings #1535 (native Flint, prefill projection optimization), #1539 (GPT-OSS
bf16 SDK pin), #1540 (fused causal SDPA for full-attention prompt chunks) and
#1542. libs/mlx-swift-lm stays on the cluster SDK 5d7f7510, which contains
master's pin 889fe3cc; that was the only conflict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants