Skip to content

[Engine] poly: opt-in --stage-next: with the staging ring, a layer's call copies the next layer's experts - #462

Closed
drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/poly-stage-next
Closed

drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/poly-stage-next

Conversation

@drakarah

@drakarah drakarah commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Problem and result

A prompt read on a card that holds few of the experts copies each layer's experts outside VRAM into the staging buffer ahead of the routing (moe_rows()). The buffer is at most one layer wide, and with --stage-ring 1 its two halves take turns. The next layer's copy goes out only when the host reaches that layer, after it has waited for this layer's router. On an RTX 4060 Laptop with Qwen4-35B-A3B UD-IQ4_XS (2,443 of 10,496 experts in VRAM), a 2,048-token prompt copies 13.6 GB, and an Nsight Systems trace showed every 38-47 ms layer with the upload copy engine idle for 11-12 ms, kernels busy 60% and copies 45% of the time.

With --stage-next 1, the read's staging buffer is two layers wide and each layer's call copies the next layer's experts into the half it does not use while it multiplies. In the trace the copies now run back to back. Measured (same tokens in every run): 2,048-token prompt 1,224.8 / 1,225.2 tokens/s against 1,039.7 / 1,040.4 with --stage-ring 1 alone (+17.8%), and 948.6 ± 9.6 with neither setting (main + PR #440, 3 runs). Decode after that prompt is unchanged or faster (76.75 ± 0.95 against 73.23 ± 2.53 tokens/s for the full set of Qwen4 changes, 4 runs).

Related discussion

5bddf3729 (the staging ring, --stage-ring). No issue.

Approach

  • Options::stage_next (--stage-next 0|1, STRATA_POLY_STAGE_NEXT=1), with --stage-ring 1.
  • widen_stage(two_layers): read() asks for two layers' width only when the read can use it: ring and next on, the architecture's row tables go by kernel (kernel_tables_), the layers are taken in turn (not read_groups()), and one or two calls (n_packs <= 2; with more, the packs' rows come from pinned memory through the same upload copy engine, and a 6,000-token prompt read 35% slower in a first version). When two layers do not fit (a frozen cache gives no VRAM back), it maps one layer's width (stage_wide1_), as before. stage_two_ records which.
  • moe_rows()'s card(): when this layer's rounds were all copied ahead and the next MoE layer's experts outside VRAM fit in two rounds, it plans them (plan_ahead() does not depend on the routing) and copies each round into the half this call's round two before it leaves, or at once if that half is free (each layer is one round in the two-layer buffer, so the copy goes out before this layer multiplies). A round of the next layer goes into a half this layer's later calls read only from the layer's last call (ring_last_call_, set by the read loop).
  • moe_rows() start: the next layer's call takes those rounds when its experts are the same (ring_next_mine) and copies nothing; the end of a call waits only for the halves it used, not the next layer's.
  • Every place that drops the ring's halves drops the next layer's copy too (drop_ring_next(), which lets the upload stream finish first), and a non-ring stage() waits for that copy before it writes the buffer.
  • Help text, settings register (opt-in) and a paragraph in docs/models/qwen4.md.
  • Kept out: read_groups() (Inkling 2), reads of three or more calls, a default change, HIP-specific tuning.

Validation

Revision c5d7a9ead. RTX 4060 Laptop GPU 8 GB (sm_89, PCIe 4.0 x8), i9-12900H, 30 GB RAM, Linux, CUDA 13.4.

Passed:

  • Qwen4-35B-A3B UD-IQ4_XS (pinned 742bc191), this commit with PR [Engine] qwen4: read the PLE n-gram table row by row from disk through the Direct reader #440 and the Qwen4 row-table kernel, --kv q8_0 --max-context 8192, 2,048-token prompt: 1,224.8 / 1,225.2 tokens/s with --stage-ring 1 --stage-next 1, 1,039.7 / 1,040.4 with --stage-ring 1.
  • Same tokens: identical in every run above (01e1e34f); with frozen experts and the same VRAM reserve, 200 tokens after the 33-token and the 2,048-token prompts are identical to the build without these changes, with and without the settings.
  • 6,000-token prompt (6 calls, inactive by design): 1,136.9 / 1,125.1 against 1,124.9 / 1,114.4; 1,000-token prompt (one call): unchanged.
  • Inkling 2 Mini UD-Q4_K_XL (read_groups(), so inactive), 2,047 tokens: 674.8 / 674.8 against 664.7 / 669.9 with the ring alone.
  • Frozen cache (STRATA_FREEZE_EXPERTS=1): falls back to one layer's width; 1,085.7 against 1,088.0 tokens/s.
  • This branch alone (no row-table kernel for Qwen4): --stage-next stays off, 1,147.5 against 1,158.8 (2 runs).
  • python3 -m unittest tools.test_settings_register, python3 tools/check_links.py: OK.

Failed:

  • A first version (two-round layers, a one-layer buffer) was slower: the next layer's copy could only start after the last call's first round, and the row tables waited behind it. Not in this commit.

Not run:

  • HIP (the code builds on the same CUDA/HIP calls the ring uses, but I have no ROCm build or card here), Metal (not built there), SYCL.
  • Multi-GPU (--layer-split), --vram-pool, co-reads, checkpoints during a read (marks).
  • Cards with two upload copy engines, other models than the two above.
  • Reproduction by anyone else; the whole test suite.

Limits

  • It costs one more layer's experts of VRAM while a read of one or two calls lasts (about 415 MiB here), lent by the expert cache and given back when the answer starts, like the ring's widening.
  • The gain is for prompts of about 1,025 to 2,048 tokens (two calls at the default --prompt-batch); longer prompts take the pinned-memory path and read as before.
  • One card. The read is now bound by the upload copy engine (12.6 GB/s here), so a card with faster PCIe or more experts in VRAM will see a different ratio.

🤖 Generated with Claude Code

…call copies the next layer's experts

A prompt read on a card that holds few of the experts copies a layer's experts outside VRAM ahead of the routing
(moe_rows()), into the staging buffer, which is at most one layer wide; with --stage-ring its two halves take
turns.  The next layer's copy goes out only when the host reaches that layer, after waiting for this layer's
router, so on an RTX 4060 Laptop with Qwen4-35B-A3B UD-IQ4_XS the upload copy engine was idle for 11-12 ms of
every 38-47 ms layer of a 2,048-token prompt, and neither the kernels (60%) nor the copies (45%) kept the card busy.

--stage-next 1 (STRATA_POLY_STAGE_NEXT=1), with --stage-ring 1, in a read of one or two calls through the layers
in turn (not read_groups(), and not with three or more calls, whose packs' rows come from pinned memory through the
same copy engine) and on an architecture whose call row tables go to the card by a kernel (kernel_tables_; a copy
of them would wait behind the next layer's experts):
- widen_stage() maps two layers' experts for the read instead of one (Options::stage_next, stage_wide1_ the one-
  layer width it falls back to when two do not fit, as with a frozen cache), so a layer's experts are one round in
  one half;
- moe_rows()'s card() plans the next MoE layer's rounds (plan_ahead() does not depend on the routing) and copies
  each into the half this call's round two before it leaves, or at once when that half is free; the next layer's
  call finds them (ring_next_mine) and copies nothing;
- the call's end waits for the halves it used, not the next layer's; anything that drops the ring's halves drops
  the next layer's copy too (drop_ring_next(), which lets the upload stream finish first).
Off by default; nothing changes without it.

RTX 4060 Laptop 8 GB (PCIe 4.0 x8), CUDA 13.4, Qwen4-35B-A3B UD-IQ4_XS on this branch plus PR simplexai-labs#440 and the Qwen4
row-table kernel (separate change), --kv q8_0, 2,048-token prompt, 2 runs each: 1,224.8 / 1,225.2 tokens/s against
1,039.7 / 1,040.4 with --stage-ring 1 alone (main and PR simplexai-labs#440 without either setting: 948.6 +- 9.6, 3 runs); the
copies now run back to back.  A 6,000-token prompt 1,136.9 / 1,125.1 against 1,124.9 / 1,114.4 (inactive there by
design).  Inkling 2 Mini UD-Q4_K_XL (read_groups(), so inactive), 2,047 tokens: 674.8 / 674.8 against 664.7 /
669.9.  The same tokens in every pair.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW
VecSzn added a commit that referenced this pull request Dec 6, 2026
@VecSzn

VecSzn commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Integrated on main in commits 837329d59 and 7cef9524a (with follow-up multi-round event wait and read()/end_read() invalidation hardening, live-verified bit-exact at +43.5% prompt throughput on partial offload; included in upcoming v0.2.20-poly.11). Thanks @drakarah!

@VecSzn VecSzn closed this Dec 6, 2026
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