Skip to content

metal nemotron: TF_KV_SHARE keeps prompt states as core pool pages (stacked on #1837) - #1850

Merged
ashhart merged 24 commits into
ashhart:mainfrom
jschmied:metal-kv-share-nemotron
Dec 5, 2026
Merged

ashhart merged 24 commits into
ashhart:mainfrom
jschmied:metal-kv-share-nemotron

Conversation

@jschmied

@jschmied jschmied commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Head 461d5d4aca answers the Bolt review: the shared store keeps where a prompt chunk's seam meets a pool page's edge, so Metal Bolt starts with TF_KV_SHARE and keeps its 16-row cold passes. b5cdead663 answered the folded-memory review. b7615411d9 (main-rebased, with ashhart's three commits) is the base.

With TF_KV_SHARE=n, Nemotron on Metal keeps prompt states in the core prompt_radix store over the Nemotron KV pool, as CUDA does since #1756. Unset or 0: the host prompt cache as before.

What changes

  • efc4cdc07: a Metal lane can adopt a kept prompt's pages. The engines' core re-exports tensorfold.kv_pool, so Metal and CUDA use the same Pool type, and main's cuda_prompt_pages adapter is used as is.
  • 875c05684: a kept state holds only Nemotron's Mamba states (Snap.pages). Its attention rows stay in the pool pages a later stream adopts, so a shared prefix is held once.
  • Marks on the prompt chunk grid (2,048 rows). The first version kept states at 256-row page-edge marks. Keeping a state at such a mark ended a prefill chunk there, so even cold passes got new chunk seams (990 tokens became 768 + 222), and the gates caught changed tokens (2 of 4 replies against main). Marks now lie on the chunk grid, so a resumed prompt's chunk seams are a cold pass's. The cost: a prompt resumes at a multiple of the chunk size.
  • No kernel changes.

Checks (M3 Pro)

  • Flag off: equal to main.
  • Concurrent equal to solo at 1, 2, 4 and 8 streams (and at 4 without drafts), all equal to main.
  • Resumed equal to cold.
  • Round robin at a cap of 2 and of 8 states, with shedding and reclaim: byte-equal. At cap 8 the shedding was page-pressure eviction (4 states at once at 8/8, then 1 more with 12 pages free), not only the state cap.
  • Idle census: free + kept == total every time.
  • Copy against share, round robin: 63k against 115k of 291k prompt tokens cached; kept state about 74 against about 46 MiB, with prefix pages held once.
  • Speed, ABBA: TTFT 2k 1.003, 8k 1.000; decode at 1 and 4 streams 1.000.
  • lean 0 new; zig build test test-metal-kv-pages test-native: 590/590 steps, 1,312/1,336 tests (24 skipped); contract nemotron: 63 pass gates 1-4 with 1 known finding (main's experts relu2), 29/29 controls red as planted, limits 0 problems.

Untaught streams (22f9a28d6)

  • An untaught stream neither resumes from the store nor keeps at its marks (prompt_pages.zig Pass.begin).
  • Regression test-nemotron-metal-share-host (zig/src/nemotron_prompt_pages_test.zig): a taught pass keeps 256 rows, the same prompt untaught adopts no pages, gets no state and no marks, and a taught control does resume at 256. Planted control: with the bypass line removed it fails at line 89 (!u.on); with it, green.
  • test-slide-metal-host: its core shim gets kv_pool and kv_prompts, as the drama3 and residency shims. Checked by an aarch64-macos cross analysis of that test (no link): at a2a725b8d it fails on cuda_prompt_pages.zig:8 (no kv_prompts), at 22f9a28d6 it passes.
  • On a Mac (M3 Pro), test-slide-metal-host, test-nemotron-metal-share-host and test-nemotron-metal-steps-host pass on 22f9a28d6 (16/16 steps, 27/27 tests). On the parent a2a725b8d, test-slide-metal-host fails to compile: cuda_prompt_pages.zig:8:19: root source file struct 'slide-drama-core' has no member named 'kv_prompts'.
  • Touched host tests on Linux: 21/21. Full CI set (x86): on x86 for 22f9a28d60 (the same code; the head 161e74387e only folds a two-line comment into one): 2449/2449 steps, 24674/24831 tests (157 skipped), check-native-cuda-host compile-cuda-tests 17/17, zig fmt --check clean. Its one finding was that comment (lean 166 vs 165); on 161e74387e, lean is 165 vs 165 and pr_check reports 0 errors
  • Not rerun for this head: speed rows, seeded/served replies. The change is one flag test when a pass opens plus a build shim, and it adds no refusal. The ABBA rows above are from before; they have no noise-floor verdict.

Proposed CHANGELOG line: Metal Nemotron: TF_KV_SHARE=n keeps prompt states as core pool pages; untaught requests bypass it.

Folded memory (b5cdead663)

  • Pass.begin returns an empty pass for s.untaught or s.memory_active (zig/src/families/nemotron/prompt_pages.zig). A folded-memory prompt is zeros where the snapshot goes, then the tail, so two memories with one tail share an artificial key; B could resume A's state and then its own restore failure went unseen.
  • Regression two folded-memory streams of one placeholder prompt neither keep nor resume each other's state (zig/src/nemotron_prompt_pages_test.zig): memory A and memory B on the same placeholder prompt; B takes no pages, no pending state, no marks. Control: a plain stream of that prompt resumes at 256.
  • Planted: 0c94a89dce (fork kvshare-1850c-planted) with the bypass line removed. Red on Linux host: 1 failed, 7/8 passed, on the new test.
  • Linux host on b5cdead663: test-nemotron-metal-share-host test-nemotron-metal-steps-host test-nemotron-prefill-widths-host test-lane-api-host: 12/12 steps, 828/828 tests.
  • Targeted x86 on b5cdead663: green (see the follow-up comment 7249478204).
  • On a Mac (M3 Pro, Zig 0.17.0), b5cdead663 (native md5 e55fddef) and planted 0c94a89dce (md5 dc096f55):
    • test-nemotron-metal-share-host test-nemotron-metal-steps-host test-slide-metal-host: 16/16 steps, 34/34 tests.
    • The planted ref fails exactly the new test (7/8).
  • Metal A/B, served (Nemotron 3.5 Lightning MLX 4-bit d9d758fb, --slide, TF_NEMOTRON_METAL_KV_PAGES=1, greedy):
    • Sequence: teach "The vault code is 4417.", ask one ~2.6k-token question (memory A), delete that fact, teach "The vault code is 9032.", ask the same question (memory B).
    • The two memories differ, and their placeholder prompts and tails are equal.
    • With TF_KV_SHARE=4 on the fix, B's reply byte-equals a cold run without TF_KV_SHARE (sha256 e3cdf831, "9032").
    • Planted, with TF_KV_SHARE=4, B answers "4417": A's reply byte for byte, in 2.9 s against 10.4 s, so it resumed memory A's state.
    • memory_version is 2 for A and 4 for B in all three arms.

Bolt's 16-row chunks (6fe3ad937b)

  • kv_prompts.shareGrid(chunk, page) is the two's least common multiple: 256 for Bolt's 16-row chunks, 2,048 as before for the other Nemotrons' 2,048-row chunk. Adapter.openOnGrid keeps the store on it and no longer refuses a chunk off the pages, so error.ChunkOffPages is gone. Bolt's chunk stays at 16 rows.
  • Regressions: Bolt's 16-row chunks keep on the 256-row page grid, a 2,048-row chunk on its own (zig/src/nemotron_prompt_pages_test.zig, test-nemotron-metal-share-host) and Bolt's 16-row chunks with the store's marks on the 256-row grid cut as a cold pass (zig/src/families/nemotron/prefill_steps_test.zig, test-nemotron-metal-steps-host): with 16-row chunks over a 600-token prompt, marks at 256 and 512 give exactly the cold pass's chunks and a keep at each.
  • Planted: a70e4d1726 (fork kvshare-1850d-planted), the grid back to the chunk itself, the old rule.
  • CUDA is unchanged: only native/metal.zig calls openOnGrid; CUDA opens with Adapter.open, which this does not touch.
  • Linux host on 6fe3ad937b: test-nemotron-metal-share-host test-nemotron-metal-steps-host test-nemotron-prefill-widths-host test-lane-api-host test-gemma4-shared-host: 16/16 steps, 836/836 tests.
  • Planted a70e4d1726 (the grid back to the chunk): red on the new Bolt geometry test, as required.
  • Targeted x86 on 6fe3ad937b: green (all steps rc=0, lean clean, CUDA host 21/21). 3a875d4dc5 only moves two comments; host on it: check-native-cuda-host and the Nemotron share and steps steps, 16/16 steps and 20/20 tests. An earlier head of this commit failed only lean on a two-line comment, now one line.
  • Bolt device check: not run; we have no 64 GB+ M1-M4 Mac, see the reply for the steps we ask a tester to run. The Metal Bolt checkpoint (bytebunker/NVIDIA-Nemotron-3.5-Bolt-74B-A9B-MLX-4bit, 43.3 GB) does not fit our M3 Pro (36 GB), and Bolt refuses an M5 by design.

Shifted chunk schedules (555ca0101b, a6b9e90f21, 461d5d4aca)

  • A pass's store queries (begin and keep, Pass.keepInStore is the one keep call) carry the request's own chunk starts, so a shifted cold schedule keeps and resumes only on its seams. Shared imprint asks use the ask's supplied starts.
  • Regressions: a pass asks the store with its request's own chunk starts (begin), the keep call site asks with the request's starts (keep, planted red at the starts assertion, on 760c501019), a shifted schedule's store marks are its own seams (stepped cuts), and an aligned schedule keeps a seam and resumes there, nonzero (real radix store and imprint ask).
  • Host on 461d5d4aca: 26/26 steps, 841/841 tests. Targeted x86 on the same head: green (all steps rc=0).
  • codex-reviewer found two test-harness gaps (an unheld page, then a missing keep-path check); both fixed, and its recheck on 461d5d4aca reports no findings.
  • Bolt Metal device run (reply exactness): not run, see the earlier comment; the cut and resume points are host-tested only.

CHANGELOG (Unreleased): Metal Nemotron: TF_KV_SHARE=n keeps prompt states as core pool pages, on Bolt too; untaught and folded-memory requests bypass it.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

@jschmied, could you rebase this onto current main without #1837's commits (#1837 is on main now), and leave it as a draft until you're ready? Thanks.

@jschmied
jschmied force-pushed the metal-kv-share-nemotron branch from 875c056 to 34f66f3 Compare November 2, 2026 18:51
@jschmied

jschmied commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main (933b969fa) without #1837's two commits; the branch is now this PR's own two, ab9c1ea88 and 34f66f309 (head). One conflict, in nemotron/kv_pages.zig: main's debug holds assert (from 4675e47d3) stays as it is, and the census takes this PR's comment and its kept-pages check. Adopted pages go into the lease's ids, so holds still covers a resumed stream's rows, and snapshot.zig only calls it when a state copies rows (with Snap.pages it copies none). On Linux (GB10): lean_check 0 new, zig build test, test-kernel-contract test-golden test-server-security test-pull-security fuzz-cortheon and git diff --check pass. The Metal parts don't build on Linux, so the M3 Pro checks in the description still need rerunning on this head. It stays a draft until they do.

@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied for the clean rebase. We read the two commits. TF_KV_SHARE stays opt-in, there are no kernel changes, and putting the marks on the 2,048-row chunk grid is the right fix for the chunk-seam token changes. With it off, it's main's path. When your Mac checks have been rerun on 34f66f309, please post them: flag off equal to main, concurrent equal to solo, resumed equal to cold, the round-robin cap with shedding, and the ABBA speed. If it helps, we can also run them on an Apple M3/M5 Ultra.

@jschmied

jschmied commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. Here are the M3 Pro checks rerun on 34f66f309, against main 933b969fa (this PR's parent, with #1837). Every reply is byte-equal and every gate is green. Three numbers differ from the PR body; they are listed first, with the reason for each.

Where this run differs from the body

  • Round-robin cached tokens.
    • Share at cap 8: 75,776 of 290,846 prompt tokens, from 17 hits. The body says 114,688.
    • The copy store: 0 hits. This server started under memory pressure (the Mac was in normal use), so the host store kept no states, and the copy-against-share comparison didn't reproduce this time.
    • Cap 2 is identical to the earlier run: 45,056.
  • Cap-8 shedding: 4 evictions, all at the 8/8 state cap. No page-pressure eviction happened this time, so the body's "then 1 more with 12 pages free" did not reproduce.
  • Decode is noisy: up to 13 % spread inside one arm, because the box was in use.

Gates

  • Flag off: equal to main.
  • TF_KV_SHARE=4: concurrent equals solo at 1, 2, 4 and 8 streams, and at 4 with --no-drafts. Solo equals main 4/4, and so does turn 2.
  • Resumed (4,096 and 18,432 cached tokens): equal to main's cold replies.
  • Round robin, every reply equal:
    • cap 2: 25 evictions, idle 292 free + 96 kept = 388;
    • cap 8: 4 evictions, idle 84 free + 304 kept = 388.
  • Idle census: 216 lines, free + kept == total on every one, 0 not free.
  • lean 424 on both (0 new); fmt and diff-check clean.
  • zig build test test-metal-kv-pages test-native: 609/609 steps (main has grown since the body's 590). The first try failed one unrelated GLM test for lack of memory while the 20 GB server ran; it passed on the rerun.
  • Contract nemotron: 63 pass gates 1-4, with 1 known finding (main's experts relu2); 29/29 controls red. The one limits problem is the older offline DeepSeek-V4.1 record, outside this diff.

ABBA speed, share against main: medians of 4 servers per arm, 3 repeats each.

cell share / main
TTFT 2k 1.000
TTFT 8k 1.002
decode, 1 stream 1.017
decode, 4 streams 1.026
  • For reference, main's absolute values: TTFT 3.81 s at 2k and 16.43 s at 8k, decode 65.7 tok/s at 1 stream and 60.6 at 4.
  • The decode ratios are noise, not a speed-up: one main server ran at 58.7 tok/s against 64.7 to 67.1 for the others.

Since this Mac runs under daily load, a run on your M3 or M5 Ultra would give cleaner speed numbers and the copy-store comparison. We'd gladly take your offer.

@jschmied
jschmied marked this pull request as ready for review November 4, 2026 09:23
@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, that's a thorough rerun: every reply byte-equal, every gate green, and the three numbers that moved explained by the box's state, not the code. We'll take the Mac run you suggested: on our M3 or M5 Ultra, on this head, flag off against main, TF_KV_SHARE=4 against the copy store with cached tokens, concurrent and resumed equality, the cap-8 round robin, and an ABBA speed run. We'll post the numbers here. One small thing: main has moved around the kv_prompts import in tensorfold.zig, so it needs a one-line rebase.

@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. Here's the Mac run on an M5 Ultra with Nemotron 3.5 Lightning 30B-A3B MLX 4-bit, balanced order, against main:

Arm against main Prompts, 32,768 tokens Decode 1 and 4 streams
flag unset not in this arm not in this arm 4 streams -0.2% to -0.3%
TF_KV_SHARE=1 +0.0% to +0.1% -0.4% to -0.0% -0.5% to -0.0%
TF_KV_SHARE=8 -0.1% to +0.0% +0.0% to +0.3% +0.2% to +0.5%

All three read level. On both TF_KV_SHARE arms, 0 of 20 prompt rows differ across runs.

One correction to what we said we'd run: these arms used 1 and 8, not the 4 you suggested, and this table doesn't report the cached-token counts or the cap-8 round robin separately. It answers the question that matters for landing, that the flag off and on costs nothing here.

This is queued to land, and our prep will carry the one-line rebase around kv_prompts.

@ashhart

ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. Main has moved under this one. Nemotron's prompt loop now cuts its chunks in prompt_flight.zig (rows()), so the pass.cut line in backend.zig no longer applies. And with main's native/metal.zig at 590 lines, this PR would take it to 607. Could you rebase onto current main, put the store's cut where the chunk end is now decided, and move something out of metal.zig so it stays at 600 lines or fewer? Please paste the Nemotron Metal host tests on the rebased head.

@jschmied
jschmied force-pushed the metal-kv-share-nemotron branch from c2854c9 to 96cec73 Compare November 6, 2026 19:08
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Nov 6, 2026
@jschmied

jschmied commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. All done on 96cec733d, rebased onto main c3f64ca8a, 3 commits:

  • The store's cut: prompt_flight.rows() now takes the store's marks (pass.marks, empty without TF_KV_SHARE) and ends a chunk at the first mark past at, after the requested cuts and the decision row and before the cap. It doesn't depend on mark order. Pass.cut and backend.zig's pass.cut line are gone.
  • New test in prompt_flight_test.zig, "Nemotron prefill widths: a chunk ends at every TF_KV_SHARE store mark, in any order, beside the other cuts". 29 rows, cuts {3,20}, decision 12, cap 8, marks {24,16,29,40} give widths 3,8,2,3,4,4,5, and every mark within the prompt is a chunk end. With the mark cut disabled it fails.
  • metal.zig: 597 lines. It now imports cuda_window.zig's modelContext instead of its own identical copy. xpu_nem.zig still has a third copy, which I left.
  • CHANGELOG: on current Unreleased.

Lean checker 210 and 210, new findings 0. zig fmt --check and git diff --check are clean. zig build test --summary all: 715/715 steps, 10,495/10,577 tests passed (82 skipped).

Nemotron Metal host tests on the rebased head (Apple M3 Pro, Zig 0.17.0). The counts are from the first run on the rebased tree; since then only the new test's name changed, and the rerun on 96cec733d is green, from cache:

check-nemotron-prefill-widths-host: Build Summary: 6/6 steps succeeded
test-nemotron-prefill-widths-host: Build Summary: 3/3 steps succeeded; 5/5 tests passed
test-nemotron-round-depth-host: Build Summary: 43/43 steps succeeded; 49/49 tests passed
test-nemotron-mtp-policy-host: Build Summary: 5/5 steps succeeded; 10/10 tests passed
test-nemotron-restored-head-host: Build Summary: 5/5 steps succeeded; 13/13 tests passed
test-nemotron-subset-head-host: Build Summary: 5/5 steps succeeded; 15/15 tests passed
test-nemotron-grants-host: Build Summary: 5/5 steps succeeded; 13/13 tests passed
test-bolt-host: Build Summary: 11/11 steps succeeded; 452/454 tests passed (2 skipped)
test-living-metal-host: Build Summary: 3/3 steps succeeded; 52/53 tests passed (1 skipped)
test-metal-kv-pages: Build Summary: 45/45 steps succeeded; 48/48 tests passed

@ashhart

ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. The cut now sits in rows() beside the other cuts, the new widths test is the right check, and metal.zig at 597 is within the limit. Thanks for the host tests on the rebased head. Two small things: the CHANGELOG hunk needs a rebase onto today's Unreleased lines, and could the line say it needs TF_NEMOTRON_METAL_KV_PAGES=1, since TF_KV_SHARE only acts on the paged pool? Optionally, ChunkOffPages in metal.zig could set problem the way the --learn-conversations refusal does.

@jschmied
jschmied force-pushed the metal-kv-share-nemotron branch from 96cec73 to 2e5cd2c Compare November 6, 2026 22:28
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Nov 6, 2026
@jschmied

jschmied commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. All three are on 2e5cd2cc0, on main 5a45539af:

  • CHANGELOG: rebased onto today's Unreleased lines. It's your commit, kept as it was.
  • The line: "Nemotron on Metal: with TF_NEMOTRON_METAL_KV_PAGES=1, TF_KV_SHARE=n keeps prompt states in the core prompt_radix store over the KV pool, as CUDA does; TF_KV_SHARE acts only on the paged pool, and unset or 0 keeps the host prompt cache (metal nemotron: TF_KV_SHARE keeps prompt states as core pool pages (stacked on #1837) #1850). Thanks to @jschmied."
  • ChunkOffPages: now sets problem before it returns, as the --learn-conversations refusal does, and says why: "TF_KV_SHARE keeps prompts as whole pool pages on the prompt chunks' grid, and this Mac's chunk of {d} rows is not a multiple of the pool's {d}-token pages: serve without TF_KV_SHARE". No test reaches that message, since the chunk can't be set off the page grid from outside (prefillStep is 2048 or 8192).

native/metal.zig is now 600 lines, at the limit.

zig build test: 715/715 steps, 9,553/9,630 tests passed (77 skipped). Lean checker 155 and 155, new findings 0. git diff --check is clean.

@ashhart

ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. Here are the two missing cells on the rebased head 96cec73, against its main parent c3f64ca, on an M5 Ultra with Nemotron 3.5 Lightning 30B-A3B MLX 4-bit.

Store Requests Prompt tokens Cached tokens Fresh-equal replies
copy-main 36 370324 245928 36/36
share4 36 370324 196608 36/36
cap8-round-robin 40 411380 98304 40/40

Every tested reply's token IDs and text equal its fresh run and the other store's reply. Per-request cached tokens are in cached-tokens.tsv. The functional cells used greedy sampling and draft:false.

The server reported 8192-row chunks, so these prompts were longer than that grid.

Cap 8: 12 conversations, 12 hits, 20 evictions; the state count reached 8 and never exceeded it. All 40 replies equal fresh. 112 idle census lines satisfy free + kept == total. The counters do not distinguish page-pressure reclaim from state-cap eviction; we did not force pressure.

With TF_KV_SHARE unset, warm/base/new/new/base, first process excluded:

Cell New vs main
Prompt 2048 +0.23%
Prompt 8192 +0.04%
Prompt 32768 -0.05%
Decode, 1 stream(s), greedy and seeded +0.06% to +0.14%
Decode, 4 stream(s), greedy and seeded +0.02% to +0.17%

All 192 cross-arm prompt/reply keys match. The full report includes both process medians per arm and all speed findings. TF_NEMOTRON_METAL_KV_PAGES=1 was enabled throughout. Share-enabled concurrency/drafting and forced-pressure cases remain unchecked on this head. No source changes or public post were made.

These ran on 96cec733d. Your rebase since keeps that code byte-identical (the same patch apart from CHANGELOG), and the one new commit, 2e5cd2cc0, only words the ChunkOffPages refusal at startup, so the cells stand for the current head. This is all we were waiting on: #1850 is in the landing queue.

@ashhart

ashhart commented Nov 7, 2026

Copy link
Copy Markdown
Owner

@jschmied, this was in tonight's landing train, but on today's main it takes zig/src/native/metal.zig to 603 lines (main is at 593, and this adds 10 net), past the 600 limit, so the light gate stops it. Could you make room? One option: move the TF_KV_SHARE open, from the chunk % page_tokens refusal to h.host.info_.prompt_cache = true, into a small function beside cuda_prompt_pages.zig's adapter, and inline nemo_disk, which is used once. The rest rebases cleanly, with only the CHANGELOG context. It goes back in the queue as soon as it fits.

jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Nov 7, 2026
@jschmied
jschmied force-pushed the metal-kv-share-nemotron branch from 2e5cd2c to 3dca2b1 Compare November 7, 2026 05:11
@jschmied

jschmied commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. Done on 3dca2b131, rebased on main cce0bdea7, with only CHANGELOG context in the rebase.

  • Room: your option. Adapter.openOnGrid in cuda_prompt_pages.zig now holds the chunk % page_tokens refusal, with the same message and error.ChunkOffPages, then Adapter.open and the grid rules. h.host.info_.prompt_cache = true stays in metal.zig, since the adapter doesn't see the lane host. nemo_disk is inlined. native/metal.zig is 595 lines.
  • Behaviour: unchanged by the move. zig build test: 728/728 steps, 9,925/9,998 tests passed (73 skipped). Lean checker 116 and 116, new findings 0. zig fmt --check and git diff --check are clean. The Nemotron host steps and test-metal-kv-pages pass.
  • Served on an M3 Pro: Nemotron 3.5 Lightning with TF_NEMOTRON_METAL_KV_PAGES=1 TF_KV_SHARE=4, --prompt-cache-gib 2, greedy, 64 tokens. A 7,459-token request resumed 6,144 tokens from pool pages after a 7,448-token first turn. Its token_sha256 equals a cold server's, and a second resume gives the same.

ashhart and others added 24 commits December 5, 2026 07:02
…ashhart#1293)

The core prompt_radix store over the Nemotron pool, opened through the CUDA adapter. A kept state is its
Mamba states (Snap.pages); its rows stay in pages a later stream adopts. Marks lie on the prompt chunk grid,
so a resume's chunk seams are a cold pass's. Unset or 0: the host prompt cache as before. The store's marks
end chunks in prompt_flight.rows, where the prompt loop cuts them. metal.zig reads the model's window with
cuda_window's modelContext instead of its own copy.
…e core adapter, so metal.zig stays under 600 lines
)

Port of ashhart#1850 onto main's prefill_metal.zig and prefill_steps.zig: Ops.open
begins the store's pass before the cache opens (its held pages less the lane's
promise) and resumes on the adopted pages before the host cache's restore; the
pass lives per stream across prefill_step calls, its marks cut the chunks, and
it ends ok on finish and not ok on error, cancel and Steps.forget. kv_prompts
takes a Query now: .of(s.prompt()).

Rebase notes on jschmied's 7e98a79: its hunks in backend.zig's old prefillFn
are carried here instead (main moved that loop out in ashhart#3021), its traceLessons
move to slide.zig is dropped (main has it in prefill_metal.zig), and metal.zig
takes modelContext from cuda_window.zig with metal_shared.zig's copy removed,
as the branch's merge of ashhart#2425 did.
… so metal.zig stays within 600 lines

With this PR's prompt-page store on today's main, zig/src/native/metal.zig reached 609 lines. ownsQwen27,
qwen27Context, Qwen27Budget, qwen27_margin and qwen27Budget move unchanged into metal_qwen27.zig, as main splits
metal_admit.zig and metal_spill.zig; metal.zig keeps the same names as aliases, so callers and info's
@hasDecl check are unchanged. No behaviour change.
… main

git's auto-merge of 'TF_KV_SHARE's prompt pass on main's stepped pass' kept the
PR's ops.end(s, false) in retain but main's retain(b, s, at) signature, leaving
ops undeclared; both callers pass it again.
…slide Metal host shim exports the pool

An untaught stream reads the model as shipped, so a taught prompt's kept state is not one it could reach:
its pass neither resumes from the store nor keeps at its marks. test-nemotron-metal-share-host covers a
taught prefix then an untaught reuse of it, with a taught control that does resume.

test-slide-metal-host's core shim gets kv_pool and kv_prompts, as the drama3 and residency shims have.
…--learn with it is refused

A lane host's Disk now names the shared radix store (host.disk.shared) where the family keeps
prompts as pool pages: a Sliding Weights step that moves weights clears it as it clears the host
cache, a flush drops its states and pages instead of answering 501, and where/load asks read its
RAM resume. prompt_radix.Store.clear drops every state and page. The Metal and CUDA hosts set it
beside their adapter. TF_KV_SHARE with --learn or --learn-conversations on Nemotron Metal is now
refused by name before any load (core kv_prompts.learnRefusal, shared with the CUDA host).
Pass false for the pages argument introduced by TF_KV_SHARE, matching the copied-cache and learned-prompt callers. The temporary learner cache is freed after the snapshot is returned.

Add a portable semantic compile check for the actual adapter callbacks so a stale call fails without a GPU or checkpoint.
…ep, as an untaught one does

Two memories with one stripped tail build the same placeholder prompt, so one key named both: B could resume A's kept state
and its own restore then failed unseen.
…o Bolt's 16-row chunks start on 256-row pages

TF_KV_SHARE on Metal Bolt refused every start: its prompt chunk is 16 rows and the store's grid was the chunk itself,
which had to be a multiple of the 256-row page. The grid is now the two's least common multiple: 256 for Bolt, still
2,048 for the other Nemotrons. Bolt keeps its 16-row cold passes.
…uest's chunk starts

A pass asked the store with its prompt alone, so a request whose starts shift its cold seams off the store's grid
(a 2,048-row cap with starts 100, 2148, 4196) could keep and resume at 4,096, a seam its cold pass never has.
…d schedule resumes nonzero on the real store

The reviewer's gap: the keep call in Pass.keep had no test, so reverting its starts still passed. Pass.keep now calls
Pass.keepInStore, the only store keep call, and a test checks the starts it sends. An aligned schedule keeps its seam
at 8 and resumes there, nonzero, from the store and from the imprint ask; the shifted schedule still does not.
The mock store retains each page; the test named pages 0 and 1 without holding them and panicked before its assertion.
…adix_flush.zig, so prompt_radix.zig stays within 600 lines

Store keeps them as declarations of the same names, so every call site reads as before; the bodies are unchanged.
@ashhart
ashhart force-pushed the metal-kv-share-nemotron branch from 28255a8 to 80efbf0 Compare December 5, 2026 07:03
@ashhart
ashhart merged commit 332532e into ashhart:main Dec 5, 2026
9 of 13 checks passed
ashhart added a commit that referenced this pull request Dec 5, 2026
Port of #1850 onto main's prefill_metal.zig and prefill_steps.zig: Ops.open
begins the store's pass before the cache opens (its held pages less the lane's
promise) and resumes on the adopted pages before the host cache's restore; the
pass lives per stream across prefill_step calls, its marks cut the chunks, and
it ends ok on finish and not ok on error, cancel and Steps.forget. kv_prompts
takes a Query now: .of(s.prompt()).

Rebase notes on jschmied's 7e98a79: its hunks in backend.zig's old prefillFn
are carried here instead (main moved that loop out in #3021), its traceLessons
move to slide.zig is dropped (main has it in prefill_metal.zig), and metal.zig
takes modelContext from cuda_window.zig with metal_shared.zig's copy removed,
as the branch's merge of #2425 did.
@ashhart

ashhart commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

ashhart added a commit to joshuaswarren/TensorFold that referenced this pull request Dec 5, 2026
ashhart added a commit that referenced this pull request Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:perf Classified by Synthena as performance improvement synthena:ready Reviewed by Synthena and ready for group train

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants