Skip to content

qwen27 metal: drafted window rows share the decode tail attention in pairs, before M5 - #3661

Merged
ashhart merged 3 commits into
mainfrom
lead/oe1-metal
Nov 17, 2026
Merged

ashhart merged 3 commits into
mainfrom
lead/oe1-metal

Conversation

@ashhart

@ashhart ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Qwen3.8-27B on Metal before M5: a drafted window's rows run the decode attention in pairs over the keys below the window, so each pair shares one threadgroup, one K/V load and one matrix multiply.

The change. The second row of a pair uses the first row's spare tile lines (8 to 13), and every line's arithmetic is the one it runs alone. The paired kernels are a Q27_JOIN build of the same sources. Main's pipelines compile from unchanged text and run every plan that has no pair. TF_QWEN27_JOIN=0 turns it off; the setting is registered, documented and tested. Pairs across streams on shared pages are already in Plan.pair and wait for the store's page sharing, a separate change. Macs with tensor units (M5) run the prefix-kernel plan and are untouched; a join there would be a follow-up.

Exact. tf-qwen27-attention-share checks paired rows against every stream alone and against main's unpaired plan, bit for bit, with a planted control that fails. tf-qwen27-attention-pages still passes. On macOS zig build test passes, including a new Plan.pair host test. pr_check reports 0 errors, and lean is 221 before and after.

Served, M3 Ultra, Qwen3.8-27B with contiguous caches, main against this. Every one of 264 replies is identical to main's.

  • Drafted, 32K prompt: +29% at 8 streams, +24% at 1.
  • Drafted, 8K: +3.5% at 8 streams, +2.6% at 1.
  • Plain is level: an 8K x 8 repeat of 9 runs each gives +0.06% with the join and +0.33% with it switched off.

Kernel, M3 Ultra, 35K keys. Four 8-row drafted windows: 11.2 to 6.0 ms. Eight streams on shared pages: 3.31 to 2.16 ms.

  • Synthena

…ntion in pairs (Q27_JOIN)

Before the M5 the 27B's decode attention runs q27_attn_tail, a threadgroup a (KV head, chunk, row), and with six
query heads a KV head only lines 0-5 of its 16-line tile carry the row: lines 8-15, with their own softmax state,
are padding. A drafted window's rows all read the stream's keys below the window, and streams resumed from one
stored prompt on shared KV pages read the same keys too, yet each row loaded and multiplied every chunk alone.

Plan.pair pairs rows over the chunks wholly below the keys they hold alike, at one chunk width: a stream's rows
below its window, and streams of one share group below the group's keys. The lead's threadgroup carries the
rider's six heads in lines 8-13 (q27_attn_pack_b), runs the joint chunks once for both (q27_attn_tail, the rider's
own threadgroup returning there), and the merges read the rider's joint partials from the lead's slot. Each line's
arithmetic is the one it runs alone: line 8 + i mirrors line i's key columns, shuffles and sum order.

The join is a Q27_JOIN build of the same sources (simd_attention.joined); main's pipelines compile from unchanged
text and run every plan without a pair. The runner builds the join before the M5 with a group of 8 heads or fewer
(attention_join.build) and pairs each round's rows; TF_QWEN27_JOIN=0 keeps main's pipelines. Share groups across
streams wait for shared prompt pages on Metal; until then only a window's own rows pair.

Tests: tf-qwen27-attention-share checks pairs against every stream alone through main's kernels and against main's
unpaired plan, bit for bit, with a planted page a false share must show, and times them; tf-qwen27-attention-pages
still matches contiguous caches bit for bit; a host test covers Plan.pair's rules.

Measured on an M3 Ultra (80-core), attention pass at 34,817 keys, main -> pairs: 4 streams x 8-row drafted windows
11.2 -> 6.0 ms (-46%); with streams on one prefix's pages, 8 x 1 row 3.31 -> 2.16 ms and 4 x 1 1.94 -> 1.43.
Served, the 27B on its default contiguous caches, main -> the join, per-stream tokens a second while all decode,
264 replies identical to main's (drafts off and one stream too): drafted at a 32K prompt +29% at 8 streams and
+24% at one, at 8K +3.5% and +2.6%; plain decode (one row a stream, no pairs, main's pipelines) level, and an 8K x 8
repeat of nine runs each puts the join at +0.06% and TF_QWEN27_JOIN=0 at +0.33% against main.
# Conflicts:
#	CHANGELOG.md
@ashhart
ashhart merged commit 5519c58 into main Nov 17, 2026
3 of 11 checks passed
@ashhart
ashhart deleted the lead/oe1-metal branch November 17, 2026 16:08
ashhart added a commit that referenced this pull request Nov 17, 2026
# Conflicts:
#	CHANGELOG.md
ashhart added a commit to jschmied/TensorFold that referenced this pull request Nov 17, 2026
# Conflicts:
#	CHANGELOG.md
ashhart added a commit to jayleaton/TensorFold that referenced this pull request Nov 17, 2026
# Conflicts:
#	CHANGELOG.md
ashhart added a commit to gilby/TensorFold that referenced this pull request Nov 17, 2026
# Conflicts:
#	CHANGELOG.md
ashhart added a commit to garvrao80/TensorFold that referenced this pull request Nov 17, 2026
# Conflicts:
#	CHANGELOG.md
ashhart added a commit to akol1/TensorFold that referenced this pull request Nov 17, 2026
# Conflicts:
#	CHANGELOG.md
ashhart added a commit to laudney/TensorFold that referenced this pull request Nov 17, 2026
# Conflicts:
#	CHANGELOG.md
ashhart added a commit that referenced this pull request Nov 17, 2026
# Conflicts:
#	CHANGELOG.md
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