Skip to content

perf(flash next cuda): read a prompt pass's n-gram rows before waiting on the previous step's copies - #201

Closed
mcclanahanaman wants to merge 1 commit into
ashhart:mainfrom
mcclanahanaman:perf/flash-next-stage-ahead
Closed

mcclanahanaman wants to merge 1 commit into
ashhart:mainfrom
mcclanahanaman:perf/flash-next-stage-ahead

Conversation

@mcclanahanaman

Copy link
Copy Markdown
Contributor

Branch: perf/flash-next-stage-ahead (one commit, da6caed, on v0.6.0, c464617)

TL;DR: Flash Next's stage() waited for the previous step's copies out of the pinned staging buffers, and only then gathered the pass's n-gram (PLE) rows from the memory-mapped table. The GPU idled through that gather after every prompt pass, and longer when table pages had been evicted. The rows are now gathered before the wait and copied into the buffers after it, so the gather overlaps the previous step's GPU work. The same rows reach the same buffers, so prompts, decode and kept states keep their bits.

The problem

  • stage() called b.staged.synchronize() first. That event is recorded after the previous step's copies out of the pinned buffers. Only then did it gather each layer's n-gram rows (p.table.gather(ids)) and copy them in.
  • The gather runs on the host and touches pages of the mapped n-gram table, about 27 GiB for the NVFP4 checkpoint. An NVFP4 table is never locked (NVFP4Table.lock() returns False), and the memory gate counts its pages as reclaimable.
  • Once memory fills and the kernel evicts table pages, the gather page-faults, and the GPU waits for all of it after every pass.

The change

  • stage() collects the pass's lookups (layer, row ids, first staging row) and gathers the rows for all layers before the wait.
    • After the wait, it writes the token ids and copies the gathered rows into the pinned buffers, as before.
    • stage_ple_rows takes the already-gathered rows (got=). Without them it gathers them itself, as before.
  • EXL3 packs keep their order: stage_ple reads into the pinned buffers directly.
  • TF_FLASH_STAGE_AHEAD=0 restores the old order.
  • The buffers, copies and rows are the same; only the moment of the gather moves.

Measurements

Jetson AGX Thor, one container session (up-u2). Upstream 0.6.0 plus this commit, local-inference-lab/Qwen3.8-Flash-Next-NVFP4 rev 7c4f1bc1, --kv-dtype int8 --parallel 4. The engine reads the switch at each stage(), so both orders run in one process on the same prompts. Cold prefill wall time, every request counted:

Prompt TF_FLASH_STAGE_AHEAD=0 (before) this change Change Head logits
synthetic 8k 903 tok/s 923 tok/s +2.2% identical
synthetic 32k 889 tok/s 890 tok/s +0.1% identical
natural text 32k 862 tok/s 868 tok/s +0.7% identical
natural text 32k, n-gram table dropped from the page cache 773 tok/s 857 tok/s +10.8% identical
synthetic 128k 789 tok/s 792 tok/s +0.4% identical
one-user greedy decode, 512 tokens (median of 3) 66.55 tok/s 66.57 tok/s 0% identical ids

The decode row is in-process greedy decode with MTP drafts, not a served-request metric. The first decode with stage-ahead on was 58.11 tok/s. The median drops it. The other five runs are 66.50-66.59 tok/s with the same ids.

The prefill cells are single runs; a gain under about 1% is within run-to-run noise.

The cold row drops the table's pages before the prompt. The log records madvise(MADV_DONTNEED) (madvise_rc 0) and resident pages falling to about 0. That is what a long-running server sees once other memory has pushed the table out.

Tests

  • New: tests/cuda/test_flashnext_nvfp4_loader.py::test_stage_reads_the_ngram_rows_ahead_with_the_same_bits. With the switch on and off, a prompt in passes of 16 and then 5 rows gives the same first token, staged rows, streams and decoded tokens.
  • An earlier session on the same code (up-u1): test_flashnext_nvfp4_loader.py 26 passed (including the new test); test_flashnext_forward.py plus test_flashnext_multi.py 79 passed, 2 failed. The measurement session (up-u2) did not re-run those full files.
    • The 2 failures are test_flashnext_multi.py::test_packed_passes_keep_each_prompt_one_token_early[bf16] and [int8]. In the measurement session, the same two tests with 0.6.0's forward.py failed the same assertion: stream 4's output is [2033, 2685, 530, 0] and serial_decode continues [2033, 2685, 530, 0, 979, ...].
    • That failure is already present on plain 0.6.0.
  • Not run: DGX Spark, CPU tests.

The commit also applies cleanly to the 0.6.1 branch (pr-141-0.6.1): git cherry-pick merges forward.py and the loader test without conflicts. It was not tested there.

AI disclosure: an AI coding agent (Claude Code) wrote this change and ran the measurements on my Jetson AGX Thor. I approved posting it; I have not reviewed the code line by line.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PRwDEapgChAWSk1ncJ62Yk

…g on the previous step's copies

stage() waited on the pinned staging buffers (the event the previous step records once its copies out of them have
run) and only then gathered the pass's n-gram rows from the memory-mapped table. The GPU therefore idled through the
gather after every prompt pass, and longer once the kernel had evicted table pages: an NVFP4 table is mapped and
never locked (NVFP4Table.lock returns False; other tables lock only when the startup budget has room), and the memory
gate counts its pages as reclaimable.

The rows are now gathered first and copied into the pinned buffers after the wait, so the table's page faults overlap
the previous step's GPU work. The same rows reach the same buffers in the same order: prompts, decode and kept states
keep their bits. EXL3 packs keep their order (stage_ple reads into the pinned buffers directly).
TF_FLASH_STAGE_AHEAD=0 restores the old order.

Jetson AGX Thor, 0.6.0 with this change, local-inference-lab Qwen3.8-Flash-Next-NVFP4 rev 7c4f1bc1, --kv-dtype int8
--parallel 4, the same prompts in one process with the switch on and off (tok/s, cold prefill): natural text 32k with
the n-gram table dropped from the page cache 857 vs 773 (+10.8%); warm 868 vs 862; synthetic 8k / 32k / 128k 923 / 890
/ 792 vs 903 / 889 / 789; head logits identical on every prompt; greedy decode 66.57 vs 66.55 tok/s with the same ids.
tests/cuda/test_flashnext_nvfp4_loader.py: 26 passed, among them a new check that both orders stage the same rows and
give the same tokens and streams.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01PRwDEapgChAWSk1ncJ62Yk
@ashhart

ashhart commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Thank you, @mcclanahanaman. Gathering a prompt pass's n-gram rows before waiting on the previous copies is in TensorFold 0.6.4, exactly as your commit and under your name: https://github.com/ashhart/TensorFold/releases/tag/v0.6.4

On a DGX Spark the combined Flash Next stack (#202, #201, #254, #248, #268) matched one-token decoding in 12 of 12 cells, with prompt speed at parity. Thank you for the careful work.

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.

3 participants