Skip to content

feat(lfm2): prompt reuse through the lane host's store - #823

Merged
ashhart merged 12 commits into
ashhart:mainfrom
BlivionIaG:lfm2/3-prompt-reuse
Oct 16, 2026
Merged

ashhart merged 12 commits into
ashhart:mainfrom
BlivionIaG:lfm2/3-prompt-reuse

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

This pull request connects the LFM2 family to the prompt cache of the lane host. It is part 3 of 3 for #820.

It needs part 2, #822. This branch contains the commits of parts 1 and 2. Review only commit acd43b4. The merge commits after it only bring in main and parts 1 and 2.

How it works

A kept state holds:

  • the state of the 30 conv layers,
  • the keys and values of the 10 attention layers, up to a mark.

The engine copies the state to host memory at the marks of the store. The prompt pass restores the state and continues from the mark. --prompt-cache-gib sets the budget, as for the other families.

Prompt rows give the same bits with each chunk size, and decode rows give the same bits as prompt rows (part 2). Thus a resumed reply can be equal to a fresh reply. The test below checks this.

Receipt

Files: lfm2-receipts, run c88bd868: tf_two_turn.json, tf_two_turn_cold.json, mlx_two_turn.json, scripts/two_turn.py.

  • Environment: Apple M4 Pro (20-core GPU), 48 GB, macOS 26.5.1. Checkpoint LFM2.5-24B-A2B-MLX-4bit at 667408d494bde191c2cb9dd4f4549827816b9275. TensorFold at c88bd86: its zig/src and zig/kernels trees are equal to this branch before the merge of main c7137bb (acd43b4). I did not run the two-turn test again after the merge. mlx-lm 0.32.0 server (mlx 0.32.3). Requests: chat, temperature 0, streamed.

  • Exactness: turn one is a system message and a user message with an excerpt of TensorFold's CHANGELOG.md (7,991 prompt tokens). Turn two adds the reply of turn one and one question (8,055 prompt tokens). The reply to turn two with the cache on was equal to the reply with --prompt-cache-gib 0. The repeat of turn two also gave an equal reply.

  • TTFT:

    Server Turn 1 TTFT Turn 2 TTFT Turn 2 cached tokens
    TensorFold, cache on 6.69 s 0.22 s (repeat: 0.056 s) 7,987 of 8,055 (repeat: 8,051)
    TensorFold, --prompt-cache-gib 0 6.42 s 6.53 s (repeat: 6.57 s) 0
    mlx-lm server 6.44 s 0.17 s (repeat: 0.038 s) not reported
  • Decode and prompt speed before and after: with --prompt-cache-gib 0, this change does not run. The cold numbers in part 2 ran with the cache off.

  • Memory: the peak physical footprint of the TensorFold server with the cache on, for this test, was 14.8 GB (tf_cache_footprint.txt).

  • Tests run, after the merge of main c7137bb (head 5a6ee03), each with an empty build cache: zig build test test-golden -Dcpu=apple_m1: 166 of 166 steps succeeded; 1,987 of 1,995 tests passed, 8 skipped, 0 failed. Golden: 0 unexpected differences. Main c7137bb gives 1,981 of 1,989 passed, 8 skipped. zig fmt --check: clean. tools/zig/lean_check.py: 527 problems, the same as main. tf-lfm2-exact with the real weights after the merge: 54 passed, 0 failed, at 4, 6 and 8 bits.

  • Not run, and why: CUDA (this family has no CUDA path). Other Macs: M4 Pro above, M5 Ultra below; I did not test M1 to M3. Eviction under a small budget: I did not test it for this family.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Drafted and concurrent: part 2. Resumed: above.)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (No sums change.)
  • Prompt processing is no slower than the last release. (With the cache off, this change does not run.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (No new test file. The receipt is the two-turn test above.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

Apple M5 Ultra

The same code (5a6ee03) and the same two-turn test on an Apple M5 Ultra (64-core GPU, 256 GB, macOS 27.0). Files: runs/m5-ultra-5a6ee034 on the fork (tf_two_turn.json, tf_two_turn_cold.json, mlx_two_turn.json).

Server Turn 1 TTFT Turn 2 TTFT Turn 2 cached tokens
TensorFold, cache on 1.886 s 0.082 s (repeat: 0.027 s) 7,987 of 8,055 (repeat: 8,051)
TensorFold, --prompt-cache-gib 0 1.841 s 1.859 s (repeat: 1.859 s) 0
mlx-lm server 0.974 s 0.175 s (repeat: 0.027 s) not reported

The reply to turn two with the cache on was equal to the reply with --prompt-cache-gib 0, and so was the repeat.

… their decode twin

Adds core/tile_qmm: 32 x 32 bf16 MMA tiles on the exact MLX affine codes
(4, 6 or 8 bits, groups of 64), with fp32 group sums in k order. Adds
grouped tiles for rows sorted by expert, a fused gate-up GLU tile, and a
twin for 1-32 rows that gives the same bits as the tiles.

tf-tile-check (no model) checks the tiles, the twin and the grouped tiles
bit for bit. tools/zig/tile_qmm_reference.py compares them with float64.
…affine

Adds the LFM2 family on the lane core: 30 short-conv layers, 10 attention
layers and 64 experts with 4 active. Prompt rows use core/tile_qmm, and
decode rows and verify windows use its twin. Attention uses fixed
4,096-key chunks. Thus prompt, decode and window rows give the same bits.

tf-lfm2-exact checks prompt chunks, windows, partial keeps and two streams
in one window against one-row decode on the real weights.
Keeps the conv states and the attention keys and values up to a mark in
host memory, and restores them in the prompt pass. --prompt-cache-gib
sets the budget, as for the other families.
@ashhart

ashhart commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Hello @BlivionIaG,

Thank you for prompt reuse through the LFM2 lane host. An exact-length prompt skips restore, and a failed restore starts the prompt over.

This lands with #822, after #821. We wrap native/lfm2.zig:70, native/lfm2.zig:63, backend.zig:131, snapshot.zig:69, and README.md:177. The gate is the same tf-lfm2-exact run on the named weights.

Thank you.

At 4 bits the twin multiplied input j by 16^-j and left code j in
place. The product is the same unless x * 16^-j leaves the float range:
x[7] = 2^-126 gave 2^-154, which underflows to zero, while the tiles
decode the integer and keep 2^-126. The twin now decodes the integer
code.

tf-tile-check adds the case: 4 bits, K 64, code 7 is 1, scales 1,
biases 0, x[7] = 2^-126. The tiles and the twin at widths 1 to 32 must
all give bf16 0x0080.
…onversion

Each 4-bit word gives its eight codes as exact integers: codes i and
i + 4 go into the mantissas of a half pair as 1024 + code, and 1024 is
subtracted. This replaces the shift, mask and integer-to-float convert
of every code. The codes, the products and the fma order are the same,
so the bits are the same; tf-tile-check, its 0x0080 case included,
gives the same output.
@ashhart
ashhart merged commit d6d0c2c into ashhart:main Oct 16, 2026
4 checks passed
ashhart added a commit that referenced this pull request Oct 16, 2026
…717) onto the candidate

# Conflicts:
#	CHANGELOG.md
#	zig/src/native/metal.zig
ashhart added a commit that referenced this pull request Oct 16, 2026
…717)

CHANGELOG in landing order; the other files both sides changed merged clean (LFM2's open branch sits after the
--vision refusal, so an LFM2 checkpoint served with --vision is refused by name).
ashhart added a commit that referenced this pull request Oct 16, 2026
…r families/lfm2_moe

The contributor's commit landed in this release with our wraps and the move to lfm2_moe; its head carries #822's. This keeps the PR's head in the history and changes no file.
ashhart added a commit that referenced this pull request Oct 16, 2026
@ashhart

ashhart commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Thank you, @BlivionIaG. Prompt reuse is what makes the family usable for chats and agents, and keeping the conv states and the bf16 keys and values, restored by the prompt pass itself, matches how Nemotron and the 27B do it.

It ships in TensorFold 1.0.12 under your name (f5f77000c), with our bf09c0630 wrapping long lines at 120 columns; no code changes.

On an M3 Ultra at 4, 6 and 8 bits, two multi-turn chats (three turns, then regenerate, edit and delete) gave the same tokens with the prompt cache on as on a server with it off, 12 of 12 steps at each width, and resumed most of each prompt (7,128 of 7,156 tokens on a second turn). With the cache on, each "draft": false request after a drafted one resumed the same 9k to 13k-token prompt and still gave its tokens, 8 of 8 at each width.

We merged 33bb0ca88 first (cc8f2d818), and your newest head, with the half-pair decode 0cec94543, came in with #867's head (9630b3d81), so this pull request shows as merged.

Posted by Ash's AI Assistant Synthena

BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…ld benchmark (ashhart#717) onto the candidate

# Conflicts:
#	CHANGELOG.md
#	zig/src/native/metal.zig
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…ld benchmark (ashhart#717)

CHANGELOG in landing order; the other files both sides changed merged clean (LFM2's open branch sits after the
--vision refusal, so an LFM2 checkpoint served with --vision is refused by name).
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…ed under families/lfm2_moe

The contributor's commit landed in this release with our wraps and the move to lfm2_moe; its head carries ashhart#822's. This keeps the PR's head in the history and changes no file.
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