Skip to content

lfm3 metal: a shared round's qk in one dispatch for every stream - #3079

Merged
ashhart merged 2 commits into
mainfrom
lead/lfm-d821
Nov 21, 2026
Merged

ashhart merged 2 commits into
mainfrom
lead/lfm-d821

Conversation

@ashhart

@ashhart ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

What this changes

In a shared LFM3 round on Metal, lfm3_qk ran once per stream per attention layer. A new kernel, lfm3_qk_segments, finds each row's stream from the round's attention-segment table and runs every stream's rows in one dispatch, so a round saves 11 × (streams − 1) dispatches. lfm3_qk's body moved into a shared inline row function that both kernels call, so the arithmetic per row is the same.

  • The kernel is registered with the kernel contract, with a planted control (every row reads stream 0) and a probe that compares it with lfm3_qk run stream by stream.
  • Issue: n/a (from a read of the LFM Metal decode path).
  • Stacked on: main. It touches lines next to the LFM3 GPU argmax PR (records, kernel names, test imports); whichever lands second rebases.

Checks on this head

  • Lean checker: 345 on the base and on this head, 0 new. zig fmt --check: clean.
  • test-kernel-contract, test-lfm3-host and compile-metal-residency-host: 51/51 steps, 267/267 tests. The contract entry and the engine test compile.
  • Not run here: the Metal shader compile, the device contract (--only "shared qk": bit-equal, control red), the limits run and the LFM3 exactness tool. We'll run them on an M5 Mac before landing.

Receipt

  • Main against branch: owed, on an M5 Mac. LFM3 at 1, 2 and 4 streams, greedy and seeded: replies byte-identical, decode within noise or faster at 2+ streams, level at 1.

Checklist

  • The title and description match the head commit.
  • No precision traded for speed: same arithmetic per row.
  • Comments and docstrings are one line.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

lfm3_qk_segments runs lfm3_qk's body for each row of a shared round,
reading the row's stream, cache length, capacity, keys and values from
the attention table that lfm3_attn_segments already uses. A shared round
now dispatches qk once per attention layer instead of once per stream.
lfm3_qk keeps its bindings and arithmetic through the shared inline row
function, so every row computes the same bytes at the same address.

The contract registers the kernel and a planted control (every row reads
stream 0's entry), with a probe whose replaced path is lfm3_qk stream by
stream; the qk reference takes each row's position. An engine test
compares queries, keys and values byte for byte against the per-stream
dispatches.
@ashhart
ashhart merged commit a37a957 into main Nov 21, 2026
2 of 11 checks passed
@ashhart
ashhart deleted the lead/lfm-d821 branch November 21, 2026 06:14
ashhart added a commit that referenced this pull request Nov 21, 2026
ashhart added a commit that referenced this pull request Nov 21, 2026
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