Skip to content

metal nemotron: KV pages taken as a stream's rows grow (TF_NEMOTRON_METAL_KV_PAGES) - #1837

Merged
ashhart merged 3 commits into
ashhart:mainfrom
jschmied:metal-kv-grow-nemotron
Nov 2, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
jschmied:metal-kv-grow-nemotron

Conversation

@jschmied

@jschmied jschmied commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Part of #1293 (step 3, the follow-up ashhart's status named). With TF_NEMOTRON_METAL_KV_PAGES=1, #1537 kept Nemotron's keys and values in pool pages, but a stream took pages for its whole context when it opened, so paging saved no memory. Now a stream takes pages as its rows grow.

What changes

  • kv_pool.zig: Pool.trim(lease, n) returns a lease's pages past n to its promise. A rolled-back window gives its pages back without ending the stream.
  • Nemotron on Metal: admission still promises the reply's pages, as on CUDA, so a stream can't run out mid-reply. Every pass that writes rows takes their pages just before it runs: prefill chunks, steps, verify windows, the draft head, restores and GPU rounds. metal_kv_pages.zig trims a lane by rows.
  • No kernel changes. Page size, addresses and the page tables' contents at each read are unchanged, so the paged kernels read the same bits.
  • Off by default, as before: unset means main's path.

Checks (M3 Pro)

before after
one short chat 17 1
four short chats 68 5
one 8k prompt 48 32
idle afterwards 388/388 388/388
  • Speed with the flag on, ABBA: decode 1.000 and 1.001×, TTFT 0.999×.
  • Spark (GB10): lean has no new findings; zig build test, the contract, golden, security and fuzz steps pass; git diff --check is clean.

Left for later

  • Admission with only the prompt plus a page ahead needs a mid-reply yield and replay path, so it isn't in this PR.
  • Step 4, prompt-cache states as pool pages on Metal, comes next.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. We read this through, and the bound looks right. In gpu_round.zig (:379, :478), the target writes end at or below len_known + inflight, and the head at or below mtp_known + inflight + depth. The bound adds rows + depth + 1, so every row a queued round can write is already covered at full depth. The other KV-writing passes grow first, and trimming only happens with nothing in flight. With the flag off, it's main's code.

One thing to confirm: keep() runs at max(len, mtp_len), but mtp_len only advances on acceptance. Is a rejected draft's head row ever written past the kept pages before the next grow?

We've queued two Metal runs on Nemotron, flag off against main and flag on against main, both checking for byte-equal replies. We'll post the numbers here. A debug-build assertion that every round's rows sit inside the covered pages would also be a cheap guard, if you'd like to add one.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Numbers from an M5 Ultra on Nemotron 3.5 Lightning, this branch against its main base, warm-up then base, new, new, base:

flag off TF_NEMOTRON_METAL_KV_PAGES=1
prompt tokens identical, 20/20 rows identical, 20/20 rows
2K prefill 9058 → 9038 tok/s (-0.2%) 9085 → 9124 tok/s (+0.4%)
decode after a 2K prompt 236.8 → 234.1 tok/s (-1.1%) 235.0 → 235.1 tok/s (+0.0%), -0.3 to -0.6% in the later cells
decode after an 8K prompt 233.9 → 231.3 tok/s (-1.1%) 233.1 → 232.2 tok/s (-0.4%)
served chat, 1 to 8 streams -0.3% to +0.4% -1.0% to +1.1%

Replies are exact both ways, and served decode is within noise. The one pattern is decode right after a long prompt with the flag off: both new runs were slower than both base runs in every cell, by about 1%. With the flag off this should be main's code, so we're running it a second time to see whether that holds.

In the meantime, does the flag-off path add anything per decode step, such as a check of the flag, a page-cover computation or a wider struct in the hot loop? If it does, hoisting it out of the step would settle it.

@jschmied

jschmied commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for reading it through and for the M5 runs.

A rejected draft's head row. No. It's never written past the kept pages before the next grow. A page holds 256 rows of every attention cache and the head's last (state.zig pagedLayout), so one lane table covers both. The head writes its draft rows from mtp_len on, then sets mtp_len = kept, its old length + 1 (mtp.zig:190, :213). The rejected rows past kept are dead: a pass writes a row before it attends to it, so the next pass rewrites them before anything reads them.

The order is grow, write, land, trim, then grow again before the next write:

  • every pass that writes K/V rows grows first: prompt chunks (backend.zig:313), steps (:381), windows (:426), drafts (:530, to max(len, mtp_len) + max_lanes + max_window), GPU rounds (gpu_round.zig:379, :478) and snapshot restore (snapshot.zig:107);
  • keep() trims to max(len, mtp_len) only with nothing in flight (backend.zig:496), and not at all on a tree path (compact != null).

So a write after a trim always comes after a grow that covers it. I'll add the debug-build assertion you suggest: every queued pass's last row sits inside the lane's table.

Flag off, per decode step. It adds a little, but nothing in the hot loop:

  • grow() is called once per pass, not per row or per layer. With the flag off it returns after two loads (c.attentions, c.kv[0].paged == null).
  • keep() is one check per verify commit.
  • drawOf only moved to forward.zig; the body is the same.
  • No struct got wider; Cache.paged was already there.

That's a few nanoseconds per pass, against about 4.3 ms per token at 235 tok/s; 1 % would be about 43 µs. So I don't think the code explains it; code layout might. Your second run will tell. To take it off the table, I'll make grow/keep inline, so with the flag off each call site is one null check, and run flag-off against main on our M3 Pro.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, that settles it. The second flag-off run (M5 Ultra, Nemotron 3.5 Lightning, against main) doesn't repeat the gap: decode after a 2K prompt is 237.0 -> 236.4 tok/s with the runs overlapping (base 234.4/239.5, new 239.0/233.9), 8K 232.4 -> 233.7, 64K 203.2 -> 205.1, and tokens are identical in all 20 rows. So the first run's 1% was noise. With the flag on also green and replies exact, this is ready, and we'll land it now. The inline and the cover assertion are welcome as a small follow-up PR.

@jschmied

jschmied commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the second run. I'd pushed the inline and the assertion onto this branch before I saw your note, so I've put the branch back to 0118c8521, the head you checked, and they're now the follow-up #1914.

jschmied and others added 3 commits November 2, 2026 09:44
A rolled-back window returns its pages without ending the stream; the
Metal pages trim a lane by rows.
Admission still promises the reply's pages; prefill chunks, steps, verify
windows, the draft head, restores and GPU rounds take theirs just before
the pass that writes them, and a rolled-back window gives them back.
@ashhart
ashhart force-pushed the metal-kv-grow-nemotron branch from 0118c85 to 8aec78e Compare November 2, 2026 09:44
@ashhart
ashhart merged commit e8b3639 into ashhart:main Nov 2, 2026
6 of 9 checks passed
@ashhart

ashhart commented Nov 2, 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

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner

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