Skip to content

[Engine] qwen4: with --ple-direct, read PLE rows ahead: a decode window's while its guesses are drafted, a prompt's while its first layers run - #465

Open
drakarah wants to merge 2 commits into
simplexai-labs:mainfrom
drakarah:feat/qwen4-ple-read-ahead
Open

drakarah wants to merge 2 commits into
simplexai-labs:mainfrom
drakarah:feat/qwen4-ple-read-ahead

Conversation

@drakarah

@drakarah drakarah commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem and result

With the opt-in Direct PLE reader (--ple-direct, PR #464; PR #440 made it the default and was closed), every decode step of Qwen4-35B-A3B first reads its tokens' n-gram rows from the SSD (16 rows a token) and blocks in collect() before the card starts the step (prepare_ple() from begin_step()). On an RTX 4060 Laptop with UD-IQ4_XS that wait was about 200 ms over a 500-token answer, roughly 5% of the decode time, and it sits on the critical path of every verify window.

Prompts wait too: on a card that holds few experts a prompt read goes through the layers in turn, so every call's rows were read at layer 1 with the card idle (about 150-230 ms of a 2,048-token prompt in an Nsight Systems trace).

The window's tokens are known one at a time while the MTP block drafts them. Now draft_step() asks the reader for each token's rows as soon as it is known; the reads land in the reader's row cache, and the window's own read finds them there. Measured on the laptop below, 6 interleaved runs each, 500 tokens after a 33-token prompt: 86.05 ± 1.00 tokens/s against 82.88 ± 2.86 with it off (+3.8%), and the reader's collect wait 68 ms against 203 ms a run. The SSD reads are the same in number (4,208 against 4,264 in the frozen runs); only their timing moves.

For prompts, read() now hands the architecture all of a read's tokens before its first call (Engine::read_begins()), and Qwen4 asks the reader for their rows in chunks of 1,024 tokens, up to 4,096 ahead of the call being read. Frozen experts, same tokens: 2,048 tokens 1,031.1 against 987.2 tokens/s (collect wait 148.8 against 259.9 ms), 6,000 tokens 1,138.7 against 1,088.7 (205.1 against 488.2 ms).

Related discussion

PR #464 (the opt-in Direct PLE reader, which this branch is on), PR #440 (closed). PR #466 (the reader's read depth) was closed: STRATA_IO_THREADS=64 already gives it, and docs/models/qwen4.md now says so. No issue.

Approach

  • Qwen4::ple_read_ahead(token, pos), called at the top of draft_step(): the token at pos_ + i (the window's first token, then the guesses that the following draft steps take as input), hashed exactly as prepare_ple() hashes it (the two tokens before it from the earlier window tokens read ahead, then from the ring), and the 16 rows issued to the reader into a small landing buffer (kPleAhead = 8 tokens).
  • ple_drain_ahead(): prepare_ple() collects those tickets before it issues the window's own read, which then copies the rows out of the row cache. The destructor drains them before the reader closes.
  • Only with --ple-direct and a single sequence (rows_of_ null); without the flag (the default, the table mapped), on Metal and in batched steps nothing changes. On Metal the reader and this read-ahead are compiled out (#if !defined(STRATA_GPU_METAL)): Metal's strata-poly does not link ple_reader.cpp. The window's last guess is still read with the window.
  • Prompts: Engine::read_begins(tokens, n) (virtual, empty), called by read() after it binds the sequence, without co-reads. Qwen4::read_begins() copies the tokens, ple_prompt_issue() hashes them as prepare_ple() does and issues one ticket a 1,024-token chunk into its own landing buffer, and prepare_ple() collects the chunks that start before its call's end (ple_prompt_drain()), issues the next ones and then reads as before.
  • STRATA_QWEN4_PLE_AHEAD=0 turns both off (registered as a rollback switch, on only with --ple-direct; its entry added to main's register and docs/SETTINGS.md). docs/models/qwen4.md: the read-ahead, its numbers and STRATA_IO_THREADS=64.
  • Kept out: DFlash2's drafting path; the reader's read depth (STRATA_IO_THREADS).

Validation

Revision eeadc9897 (one commit, the two earlier ones squashed) on PR #464's 8f1703c7e, on main 7cef9524a, without PR #440's commits. The speed numbers are from the earlier revisions 62b988400 and e7f5931a0 (the same read-ahead code, on PR #440's reader then on by default). RTX 4060 Laptop GPU 8 GB (sm_89, PCIe 4.0 x8), i9-12900H, 30 GB RAM, Linux, CUDA 13.4; Qwen4-35B-A3B UD-IQ4_XS from the pinned commit 742bc191 (SHA-256 checked), --kv q8_0 --max-context 8192, default MTP.

Passed:

  • Same greedy tokens: with STRATA_FREEZE_EXPERTS=1 --expert-swaps 0 (the cache split held still), 300 tokens are identical with it on, with =0, and with the build without this commit (57be2e249523).
  • Decode, 6 interleaved runs each, 500 tokens: 86.05 ± 1.00 tokens/s on, 82.88 ± 2.86 off; collect wait 68.3 against 203.1 ms (means of the runs). Again later, 10 interleaved runs each with both commits and the other Qwen4 changes I propose: 86.73 ± 2.25 against 84.13 ± 2.05 with =0 (+3.1%), collect wait 67.8 against 180.8 ms.
  • Prompts, frozen experts: 2,048 and 6,000 tokens as above, the same 16 tokens after each (c50e2dbf, 512f5519).
  • python3 tools/settings_register.py --check and python3 -m unittest tools.test_settings_register: OK.
  • Builds of eeadc9897: CUDA 13.4 (strata-poly, sm_89); HIP with ROCm 7.2.4 (rocm/dev-ubuntu-24.04:7.2.4-complete, the CI's image), preset hip-compile (gfx1100, gfx1201), target strata-poly; HIP on Windows 11, ROCm 7.2 SDK (pip _rocm_sdk_devel), gfx1010 (RX 5700 XT), target strata-poly.
  • Default path on eeadc9897: main and this branch without --ple-direct, frozen experts, the same 300 tokens (57be2e24); with --ple-direct, read-ahead on and with STRATA_QWEN4_PLE_AHEAD=0: the same (57be2e24).

Failed:

  • 256-token chunks: rows of one page that fall in two chunks are read twice, so the SSD read 117.7 instead of 98.1 MB for 2,048 tokens and the wait went down less (190 ms); 1,024-token chunks read 102.4 MB. Not in this branch.

Not run:

  • Metal build (no Mac here). The reader and the read-ahead are compiled out there, and I checked by reading the code that no call to them is left outside the guards.
  • DFlash2 drafting, --no-draft (n-gram lookup guesses), batched serving.
  • Other SSDs; the gain depends on the drive's read latency (here a RS1D0TSSD710).
  • Reproduction by anyone else; the whole test suite.

Limits

  • One card, one SSD, one model size.
  • The window's last guess is not read ahead (it is known only when its draft step ends); the remaining wait is about a third of what it was.
  • A prompt's first chunk only overlaps the first layer and the setup before it, so the prompt's wait halves rather than vanishes; with STRATA_IO_THREADS=64 as well it went from 260 to 72 ms.
  • About 4% more SSD bytes for prompts (pages shared by two chunks).
  • Decode on this laptop varies by several percent between runs (the expert cache's swaps differ run to run), hence the interleaved runs and the spread given above.

🤖 Generated with Claude Code

https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW

@VecSzn

VecSzn commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks @drakarah! Please rebase #465 cleanly onto current main (without the #440 patches/ commits) and verify clean builds across CUDA, HIP, and Metal so we can review it for poly.12!

drakarah and others added 2 commits December 6, 2026 08:57
…and the VRAM reserve does not count them

--ple-direct (off by default, CUDA and HIP) reads the PLE n-gram table's rows straight from disk through
strata::ngram::PleReader, the Direct reader the Flash Next engine uses for its own PLE, in place of the host map of
shard 2: unbuffered reads, a 1M-row (~100 MB) row cache, the SSD keep-alive, one issue a step over its whole row set,
collected in begin_step()'s PLE slot and dequantized from pinned raw rows into h_ple_emb_.  The reader is PR simplexai-labs#440's
(closed); here it is opt-in, since it read prompts about 3.4 times slower than the map on a Windows PC with a PCIe 3.0
SSD (PR simplexai-labs#440's report).  A file the reader cannot open falls back to the map.  Not on Metal: there the map is kept
(the reader is not compiled in), as its gathers need no copy.

With the reader open the table needs no page cache, so fill_expert_cache() no longer counts it among the files whose
page cache its tight-RAM test keeps room for (Engine::direct_file_bytes(), default 0; Qwen4 returns the table's bytes
only while the reader is open).  Before, a 30 GB PC with UD-IQ4_XS (20.5 + 11.5 GB of files) got the doubled reserve.

Without --ple-direct nothing changes: the table is mapped and direct_file_bytes() is 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW
…ow's while its guesses are drafted, a prompt's while its first layers run

With the opt-in Direct PLE reader (--ple-direct) each verify window waited for its tokens' n-gram rows (16 a token)
before its first layer: prepare_ple() issued them in begin_step() and blocked in collect().  Now:

- decode: the window's tokens are known one by one while the MTP block drafts them, so draft_step() asks the reader
  for each token's rows as soon as the token is known (ple_read_ahead()); the window's own read in prepare_ple() first
  waits for them (ple_drain_ahead()) and then takes them from the row cache.  The last guess is read with the window.
- prompt: read() hands an architecture all of a read's tokens before the first call (Engine::read_begins(), one
  sequence, no co-reads).  Qwen4 hashes them as prepare_ple() does and asks for their rows in chunks of 1,024 tokens,
  up to 4,096 tokens ahead of the call being read (ple_prompt_issue()); each call waits for the chunks that start
  before its end (ple_prompt_drain()) and asks for the next ones.

The same rows and sums; only when the reads happen changes.  STRATA_QWEN4_PLE_AHEAD=0 turns both off.  Without
--ple-direct, and on Metal (no reader there), nothing changes.

RTX 4060 Laptop 8 GB, i9-12900H, RS1D0TSSD710, UD-IQ4_XS, --kv q8_0, with the reader: decode, 33-token prompt, 500
tokens, 6 interleaved runs, 86.05 +- 1.00 tokens/s against 82.88 +- 2.86 off, the reader's collect wait 68 against
203 ms a run; frozen experts, 2,048-token prompt 1,031.1 against 987.2 tokens/s, 6,000 tokens 1,138.7 against
1,088.7, the same tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW
@drakarah
drakarah force-pushed the feat/qwen4-ple-read-ahead branch from e7f5931 to eeadc98 Compare December 6, 2026 09:29
@drakarah drakarah changed the title [Engine] qwen4: read PLE rows ahead: a decode window's while its guesses are drafted, a prompt's while its first layers run [Engine] qwen4: with --ple-direct, read PLE rows ahead: a decode window's while its guesses are drafted, a prompt's while its first layers run Dec 6, 2026
@drakarah

drakarah commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! Rebased cleanly: one commit, eeadc9897, on #464's 8f1703c7e (it needs the opt-in --ple-direct reader), on current main, without #440's commits.

  • The read-ahead runs only with --ple-direct; without it nothing changes (the same 300 tokens as main, frozen experts; with --ple-direct, read-ahead on and STRATA_QWEN4_PLE_AHEAD=0 give those tokens too).
  • Builds: CUDA 13.4; HIP with the CI's image (rocm/dev-ubuntu-24.04:7.2.4-complete, hip-compile, gfx1100/gfx1201); HIP on Windows (ROCm 7.2, gfx1010). Metal: not built here (no Mac); the reader and the read-ahead are compiled out there (#if !defined(STRATA_GPU_METAL)).
  • Since [Engine] qwen4: the Direct PLE reader reads 64 rows at once on Linux #466 is closed: docs/models/qwen4.md now points to STRATA_IO_THREADS=64 for the read depth.

Could you add run-backends so the backend jobs (including Metal) run? I can't set labels here.

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