From 9a5e8770ed5c3283f891b87dc129d0437c2011b6 Mon Sep 17 00:00:00 2001 From: Sarah Date: Fri, 4 Dec 2026 23:39:27 +0100 Subject: [PATCH] [Engine] qwen4: a prompt call's row tables go to the card by a kernel, 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_, f0ab63a90); 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 #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 Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW --- docs/SETTINGS.md | 3 ++- engine/src/poly/qwen4.cpp | 9 ++++++++ terminal/data/settings-register.json | 33 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index d653eace9..eeb8db134 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -383,7 +383,7 @@ Off unless you set them. The last column says where each one is described. | `STRATA_WDDM_BUDGET` | number | AMD, Intel Arc | [AMD_HIP.md](AMD_HIP.md) | | `STRATA_WDDM_MARGIN_MIB` | number | AMD | [AMD_HIP.md](AMD_HIP.md) | -## Switches that go back to earlier behaviour (134) +## Switches that go back to earlier behaviour (135) These behaviours are on by default. Setting the switch returns the earlier behaviour; the notes in the register say which version made it the default. @@ -491,6 +491,7 @@ version made it the default. | `STRATA_QFUSE_HC_LOCAL` | text | NVIDIA and AMD | v0.2.16 | - | | `STRATA_QSA_EARLY_EXIT` | number | NVIDIA and AMD, Intel Arc | v0.2.16 | - | | `STRATA_QSA_FUSE_NR` | on / off | NVIDIA and AMD | v0.2.16 | - | +| `STRATA_QWEN4_KERNEL_TABLES` | on / off | NVIDIA and AMD | - | - | | `STRATA_RDNA_KQ_ALL` | on / off | NVIDIA and AMD | v0.2.19 | - | | `STRATA_REMOTE_SPIN` | on / off | NVIDIA, AMD, Intel Arc | v0.2.16 | - | | `STRATA_RESIDENT_PIN` | on / off | NVIDIA and AMD, Intel Arc | v0.2.16 | [DETAILS.md](DETAILS.md), [TROUBLESHOOTING.md](TROUBLESHOOTING.md), [UNSLOTH_Q4.md](UNSLOTH_Q4.md) | diff --git a/engine/src/poly/qwen4.cpp b/engine/src/poly/qwen4.cpp index db716189a..30ddc5afd 100644 --- a/engine/src/poly/qwen4.cpp +++ b/engine/src/poly/qwen4.cpp @@ -249,6 +249,15 @@ void Qwen4::load_geometry() { clamp_shexp_ = clamp_exp_; lane_slots_ = true; swiglu_q8_ = true; +#if !defined(STRATA_GPU_METAL) && !defined(STRATA_USE_HIP) + // In a prompt's read of several calls, a call's row tables go to the card by a kernel, not behind the experts' + // copies, as Inkling 2's (kernel_tables_): the same tables reach the same buffers. STRATA_QWEN4_KERNEL_TABLES=0 + // turns it off. + { + const char* v = std::getenv("STRATA_QWEN4_KERNEL_TABLES"); + kernel_tables_ = !(v && v[0] == '0'); + } +#endif #if !defined(STRATA_GPU_METAL) && !defined(STRATA_USE_HIP) // Enable B6=2 (bitwise-exact 2-row-per-block multi-column MMVQ for Q4_K/Q5_K/Q6_K/IQ4_XS) on CUDA unless the user // explicitly set STRATA_B6_MMVQ_ROWS; measured +4-5% on Qwen4 DFlash2 with 0 extra VRAM. diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 739763688..6c810bb31 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -42439,6 +42439,39 @@ "retire_after": "", "retired_in": "0.2.16" }, + { + "name": "STRATA_QWEN4_KERNEL_TABLES", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/poly/qwen4.cpp", + "line": 257, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": null, + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": null, + "first_date": null, + "since": null, + "last_commit": null, + "last_date": null, + "status": "rollback", + "owner": "poly", + "note": "Qwen4 (qwen4exp) on CUDA: in a prompt read of several calls, moe_rows() hands a call's row tables to the card by a kernel (copy2) that reads them from pinned memory, as Inkling 2's STRATA_INK_KERNEL_TABLES, instead of a copy that waits behind the layer's expert uploads on the card's one upload copy engine. Same bits, no extra VRAM. Default on; =0 keeps the copies, as before. HIP and Metal never take it (engine/src/poly/qwen4.cpp, engine.cpp moe_rows())", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_RAM_ADAPT", "backends": [