Skip to content

[Engine] qwen4: a prompt call's row tables go to the card by a kernel, as Inkling 2's do - #463

Closed
drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/qwen4-kernel-tables
Closed

drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/qwen4-kernel-tables

Conversation

@drakarah

@drakarah drakarah commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Problem and result

In a prompt read of several calls, moe_rows() copies each call's row tables (about 100 KB) to the card with cudaMemcpyAsync on the main stream. The layer's experts outside VRAM are uploaded ahead of the routing on the up stream, and a GeForce card has one copy engine for uploads, so the tables' copy waits behind those uploads, and every multiplication of the layer, the cached experts' too, waits behind the tables. In an Nsight Systems trace of a 2,048-token prompt (RTX 4060 Laptop, Qwen4-35B-A3B UD-IQ4_XS) a table copy enqueued before 394 expert copies ran after all of them, 27 ms later.

Inkling 2 avoids this since f0ab63a90: its tables are read by a kernel (copy2) from pinned memory (kernel_tables_). Qwen4 now does the same. The same tables reach the same buffers, so the results are the same bits. Alone it is a small gain (+1.5 to +1.7% on a 2,048-token prompt); with the next layer's experts copied ahead (--stage-next, a separate PR) it is what lets that work, since there the tables would wait behind a whole layer's uploads.

Related discussion

f0ab63a90 (Inkling 2's row tables by kernel). No issue.

Approach

  • Qwen4::load_geometry(): kernel_tables_ = true on CUDA, as inkling2.cpp sets it; STRATA_QWEN4_KERNEL_TABLES=0 keeps the copies. HIP and Metal never take it, as for Inkling 2.
  • The switch is registered (terminal/data/settings-register.json, status rollback) and docs/SETTINGS.md is regenerated from it.
  • Kept out: any change to moe_rows() itself, other models.

Validation

Revision 9a5e8770e. 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 (pinned commit 742bc191), --kv q8_0 --max-context 8192, 2,048-token prompt, 2 tokens written.

Passed:

  • This branch (the PLE table mapped, as on main): 1,039.8 ± 34.4 tokens/s against 1,023.9 ± 45.6 with =0, 4 interleaved runs each (the mapped table's page cache makes these noisy).
  • On top of PR [Engine] qwen4: read the PLE n-gram table row by row from disk through the Direct reader #440 (the Direct PLE reader, steadier): 990.6 ± 5.5 against 974.3 ± 6.4, 3 runs each.
  • The same tokens in every run, on and off (01e1e34f); with frozen experts, 200 tokens after a 2,048-token prompt are identical to the build without it.
  • python3 -m unittest tools.test_settings_register: OK.

Failed:

  • none observed.

Not run:

  • HIP and Metal (not taken there).
  • Other cards; a card with two upload copy engines would gain less.
  • Reproduction by anyone else; the whole test suite.

Limits

  • One card and one model size. The gain alone is within the run-to-run spread on main; the trace and the combination with --stage-next are the stronger evidence.

🤖 Generated with Claude Code

…, as Inkling 2's do

In a prompt read of several calls, moe_rows() copies each call's row tables (about 100 KB) to the card with
cudaMemcpyAsync on the main stream.  The layer's experts outside VRAM are copied ahead of the routing on the up
stream, and a GeForce card has one copy engine for uploads, so the tables' copy waited behind those uploads and
the layer's multiplications, the cached experts' too, waited behind the tables.  Inkling 2 has the tables read
by a kernel from pinned memory instead (kernel_tables_, f0ab63a); Qwen4 now does the same.  The same tables
reach the same buffers.  STRATA_QWEN4_KERNEL_TABLES=0 keeps the copies.  CUDA only, as for Inkling 2.

RTX 4060 Laptop 8 GB (PCIe 4.0 x8), UD-IQ4_XS, --kv q8_0, 2,048-token prompt: 1,039.8 +- 34.4 tokens/s against
1,023.9 +- 45.6 with =0 (4 runs each, this branch); on top of PR simplexai-labs#440 990.6 +- 5.5 against 974.3 +- 6.4 (3 runs).
The same tokens in every run.  It matters more with --stage-ring and the next layer's experts copied ahead
(separate change), where the tables otherwise wait behind a whole layer's uploads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW
@VecSzn

VecSzn commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Integrated on main in commit f674f9fd4 (included in upcoming v0.2.20-poly.11). Thanks @drakarah!

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