Skip to content

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

Open
jschmied wants to merge 11 commits into
ashhart:mainfrom
jschmied:metal-kv-share-nemotron
Open

jschmied wants to merge 11 commits into
ashhart:mainfrom
jschmied:metal-kv-share-nemotron

Conversation

@jschmied

@jschmied jschmied commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Part of #1293 (step 4). On main (#1837 has landed); this PR's change is its two commits, ab9c1ea88 and 34f66f309. Head f22a537e9c answers the second review: the shared store is cleared when weights move, flushes and imprint asks reach it, and TF_KV_SHARE with --learn is refused. 161e74387e answered the first review: untaught streams bypass the store, and the slide Metal host shim exports the pool. The M3 Pro checks of the first version are in the rerun, where three numbers differ from the ones below under memory pressure.

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.

Shared store integration (f22a537e9c)

  • Core first: the lane host's disk.shared names the prompt_radix store a family keeps on pool pages. lane_host_learn.step clears it when a Sliding Weights step reports changed weights, as it clears h.cache. lane_host_rows takes a flush when either store exists (no 501), and answers flush and the other imprint asks from the shared store when there is no host cache. prompt_radix.Store.clear drops every state and releases the tree's pages. Metal and CUDA set disk.shared next to their adapter (metal.zig, cuda_startup_phase.zig).
  • TF_KV_SHARE with --learn or --learn-conversations on Nemotron Metal is refused by name before any load, so no store, imprint or learned folder is opened. The text is core kv_prompts.learnRefusal, which the CUDA host's --learn-conversations refusal now uses too.
  • Regressions: zig/src/core/lane_host_shared_test.zig (in test-lane-api-host): a retained state survives an unchanged learn step (control) and is gone after a changed step, with its pages back in the pool; a flush without a host cache drops 1 state and 3 bytes, and with no store it is still 501 (control). Planted controls: without the clear in lane_host_learn.zig, the changed-step test fails; with the old h.cache == null flush check, the flush test fails. metal_slide_test.zig (in test-slide-metal-host): the refusal by name for both learn options, no learned folder, and a control without TF_KV_SHARE that reaches the load and fails there.
  • Linux host steps: test-nemotron-metal-share-host test-nemotron-metal-steps-host test-lane-api-host test-kv-pool test-cache-replay-host test-gemma4-shared-host 21/21 steps, 811/811 tests; aarch64-macos cross analysis of test-slide-metal-host clean; zig fmt --check clean. pr_check 0 errors, lean 0 new. Full CI set on x86 at f22a537e9c: green, 2449/2449 steps, 25258/25418 tests (160 skipped), lean 165 vs 165 (0 new). Mac (M3 Pro): planted 5d5a16181f red on exactly the --learn refusal test; f22a537e9c 19/19 steps, 781/781 tests on test-slide-metal-host test-nemotron-metal-share-host test-nemotron-metal-steps-host test-lane-api-host.
  • Not rerun: speed rows and served replies. The default path does not change (the new branches run only with the shared store or both flags set).

Proposed CHANGELOG line: Metal Nemotron: TF_KV_SHARE=n keeps prompt states as core pool pages; untaught requests bypass it, moved weights and flushes clear it, and --learn with it is refused.

@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

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. The port onto today's main kept your four commits under your name and wired the KV-share pass into stepped Nemotron prefill, with the new stepped-pass test. The build shims still export safetensors and now add kv_pool and kv_prompts, so the SAM3 work can keep torch_checkpoint beside them. The earlier GB10 and M5 Ultra runs were green with identical tokens, and the default path is unchanged with the option unset.

Nothing is left for you to change, only the git pull --rebase note before your next push so the port is not lost.

  • Synthena

@ashhart
ashhart force-pushed the metal-kv-share-nemotron branch from 5a9d4a3 to 3b0fdc3 Compare November 19, 2026 17:19
ashhart added a commit to jschmied/TensorFold that referenced this pull request Nov 19, 2026
# Conflicts:
#	CHANGELOG.md
--
2.54.0 (Apple Git-157)
--
2.43.0
ashhart added a commit to jschmied/TensorFold that referenced this pull request Nov 19, 2026
)

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.
@AIFlowML

Copy link
Copy Markdown
Contributor

On 3b0fdc36, zig build test-metal-kv-pages test-metal-residency-host -Dcpu=apple_m1 -j5 --summary all passed 102/102 on M5 Max (Zig 0.17.0). The trial merge into 9ba855b3 conflicts in the two Drama 3 build shims and zig/src/native/metal.zig. In scratch I kept main's torch_pth and wav shim exports alongside this PR's kv_pool and kv_prompts, and kept the PR's relocation of the Qwen27 startup helpers with their existing behavior.

The same command passed 103/103 on that resolved tree, including the paged Metal fixtures. This uses synthetic KV/device fixtures and host residency tests. I did not rerun the release-checkpoint timing or token parity tests.

Igor Lessio - AIFLOW LABS

ashhart added a commit to jschmied/TensorFold that referenced this pull request Nov 22, 2026
)

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.
@ashhart
ashhart force-pushed the metal-kv-share-nemotron branch from 3b0fdc3 to 3e09d94 Compare November 22, 2026 03:45
@AIFlowML

Copy link
Copy Markdown
Contributor

The latest 3e09d948 head trial-merges cleanly into main 3a02038db. The merge of #1837 is reachable on main, and the previous Drama 3 and Metal shim overlaps are resolved in this revision.

On M5 Max with Zig 0.17.0, the KV page and residency targets passed all 104 tests. The stepped prefill and cut-width targets passed all 15 tests, and the native Metal server built. That includes the stepped-pass fixture that stops at each store mark and returns adopted pages on cancellation.

zig build test-metal-kv-pages test-metal-residency-host -Dcpu=apple_m1 -j2 --summary all
zig build test-nemotron-metal-steps-host test-nemotron-prefill-widths-host native \
  -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j2 --summary all

The release-checkpoint token parity and timing results remain tied to the earlier device receipts.

Igor Lessio - AIFLOW LABS

jschmied and others added 9 commits November 23, 2026 11:35
…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.
@ashhart
ashhart force-pushed the metal-kv-share-nemotron branch from 3e09d94 to a2a725b Compare November 23, 2026 11:38
@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. The shared-page design looks carefully integrated. I found two issues to address before merge.

The slide Metal host target still generates its core shim without the KV pool and prompt-hook exports now required by the shared-page adapter, so that host test target will fail to compile. Please mirror the exports already added to the other Metal shim.

The new shared prompt pass also resumes untaught streams from ordinary taught snapshots. With learned neurons active, that gives an untaught request state it could not produce from a fresh pass. Please bypass shared resume and keep for untaught streams, and add a regression covering taught-prefix-then-untaught reuse.

  • Synthena

…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.
@jschmied

Copy link
Copy Markdown
Contributor Author

Thanks. Both are fixed on 161e74387e. The checks below ran on 22f9a28d6, which differs from it only in one comment folded onto one line.

  • Slide Metal host target: its core shim now exports kv_pool and kv_prompts, as the drama3 and residency shims do. I checked this with an aarch64-macos cross analysis of test-slide-metal-host (no link). On a2a725b8d it fails on cuda_prompt_pages.zig:8 (no kv_prompts). On 22f9a28d6 it passes.
  • Untaught streams: Pass.begin now returns an empty plan for them, so they neither resume from the store nor keep at its marks. The new regression test-nemotron-metal-share-host (zig/src/nemotron_prompt_pages_test.zig) runs a taught pass that keeps 256 rows. The same prompt untaught then adopts no pages and gets no state and no marks. A taught control on that prompt does resume at 256. With the bypass line removed, the test fails at line 89; with it, 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: 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.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. Both requested fixes are present by source: the slide Metal host shim now exports kv_pool and kv_prompts, and untaught streams return an inactive prompt pass before store lookup. The taught-prefix-then-untaught regression also has a taught control that still resumes.

Three shared-store integration issues still need changes:

  1. A Sliding Weights step that reports changed weights clears only h.cache in zig/src/core/lane_host_learn.zig:55. The shared branch opens h.prompts but leaves that pointer null (zig/src/native/metal.zig:249-256), so a later taught request can resume pre-update KV pages and Mamba state. Please invalidate the shared radix store on weight changes and add a retained-state/changed-step regression.
  2. Flush and imprint operations still use only h.cache. With the shared branch active, zig/src/core/lane_host_rows.zig:31 rejects flush as 501 even though h.prompts.store has states, and the other imprint queries receive a null store. Please route these operations through the shared adapter and prove flush drops retained shared states.
  3. TF_KV_SHARE with --learn is accepted, but zig/src/native/metal.zig:276 attaches the opened imprint only to h.cache. The shared adapter has no replacement attachment. Please integrate it, or explicitly refuse this combination until supported, and add a startup or persistence regression.

Main has also moved since your last push, and the change that passes TF_QWEN27_TIED_HEAD to qwen27_engine.owns now conflicts with the helper this PR moves to metal_qwen27.zig. You don't need to rebase: we'll carry that argument across when we rebase it for landing.

After these repairs, please rerun the focused slide, shared-page and stepped-prefill host steps and add coverage for the three integration paths above.

  • Synthena

…--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).
@jschmied

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. All three are fixed on f22a537e9c. Most of the fix is in core, so the CUDA host gets it too. No rebase; the TF_QWEN27_TIED_HEAD argument is left for your landing rebase, as you offered. One more overlap for that rebase: main's 5b71334bb1 makes h.closing atomic, and this PR edits the ask line in lane_host_rows.zig too. The merged line keeps both changes: h.closing.load(.acquire) or (a.op == .flush and none).

  1. Changed weights. The lane host's disk.shared now names the prompt_radix store that a family keeps on pool pages. Metal and CUDA set it next to their adapter. When a Sliding Weights step reports changed weights, lane_host_learn.step clears that store as well as h.cache. The new prompt_radix.Store.clear drops every state and gives the tree's pages back to the pool. Regression in zig/src/core/lane_host_shared_test.zig: a retained state survives an unchanged step (the control). After a changed step it is gone, where reports 0, and all 8 pages are free again.
  2. Flush and imprint. lane_host_rows.ask takes a flush when either store exists, so it no longer returns 501. With no host cache, flush and the other asks are answered from the shared store. A flush drops its states and pages; where/load report its RAM resume. Regression: a flush without h.cache drops 1 state (3 bytes), and the pool is whole again. Control: with neither store, the flush still gets 501.
  3. --learn. For now this combination is refused. TF_KV_SHARE with --learn or --learn-conversations on Nemotron Metal is refused by name before any load, so no store, imprint or learned folder is opened. The text lives in core (kv_prompts.learnRefusal), and the CUDA host's existing --learn-conversations refusal now uses it too. Startup regression in metal_slide_test.zig (test-slide-metal-host): both options are refused by name, and no learned folder appears. Control: without TF_KV_SHARE, the same start reaches the load and fails there on the missing checkpoint.

Planted controls, each red before green: without the clear in lane_host_learn.zig, the changed-step test fails. With the old h.cache == null flush check, the flush test fails. With the early refusal removed, the startup test fails on the Mac.

Verified on Linux: test-nemotron-metal-share-host test-nemotron-metal-steps-host test-lane-api-host test-kv-pool test-cache-replay-host test-gemma4-shared-host passed 21/21 steps and 811/811 tests. An aarch64-macos cross analysis of test-slide-metal-host is clean, and zig fmt --check is clean. pr_check 0 errors, lean 165 vs 165 (0 new), git diff --check clean. Full CI set on x86 at f22a537e9c: green, 2449/2449 steps, 25258/25418 tests (160 skipped), lean 165 vs 165 (0 new). On the Mac (M3 Pro, Zig 0.17.0): the planted ref 5d5a16181f is red as intended, with exactly one failing test, "slide admission: TF_KV_SHARE refuses Nemotron's --learn by name before any load or learned folder". On f22a537e9c, test-slide-metal-host test-nemotron-metal-share-host test-nemotron-metal-steps-host test-lane-api-host give 19/19 steps and 781/781 tests.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. The shim and untaught-stream fixes remain present. Changed weights now clear the shared store, flushes and queries reach it, and the unsupported persistent-learning combinations are refused before load. The new controls cover those requests.

We will carry the atomic closing and tied-head changes through integration. Current integrated shared-cache/default reply and timing qualification remains separate from the earlier device receipts.

  • Synthena

This branch has not been deployed

No deployments
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.

4 participants