qwen4exp: verify/decode pass launch order, prefetch width, chain polling, turnover slots

Lossless (no kernel arithmetic or operand change): the router weight is read beside the mixer
all-reduce; k/v projections before q|gate; the indexer's keys behind the attention launches within
the exact bound; verify-height prefetch at 32 workgroups (`prefetch` takes an optional `blocks`
param) and the shared down arm before the routed down from 2 rows (kVerifyRows); chain waits poll by
grid size (R4D_CHAIN_TINY/SMALL/MID); the mover's kept-free turnover slots follow the swaps the
engine asked for (MoverConfig::turnover_adaptive) instead of a fixed 640.

arch_test: new a_flash_next_decode_step_orders_its_small_launches; the pinned graph fingerprints and
step hashes of qwen4exp_fp8 move (re-pinned). place_test: the adaptive turnover. r4d_selftest: the
`blocks` param of prefetch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Code000andClaude Opus 5.5 authored and StillDeadcode committed 2026-11-17 20:33:07 +01:00
1 parent 946a6f9e2b
commit ca7a7212ec
20 files changed
+598 -44

No files matched your search

+22
View File
@@ -63,6 +63,14 @@ __device__ __forceinline__ const R4dChainRec* r4d_chain_rec() {
// clocks for as long as the wait lasts, slowing whatever else that channel serves. The first
// R4D_CHAIN_FAST polls are ~64 clocks apart, the rest R4D_CHAIN_SLOW x 64: a long wait ends at most
// that much late and puts 1/R4D_CHAIN_SLOW of the load on the channel.
//
// A grid of a few blocks puts little on the channel however often it polls, and a decode step is
// mostly such grids behind one another (a reduction's four blocks, a quantiser's, a router's
// seventeen), each ending its wait up to a slow period late: so up to R4D_CHAIN_TINY blocks keep
// the fast period for the whole wait and up to R4D_CHAIN_SMALL poll every R4D_CHAIN_MID x 64 clocks,
// which is at most the load a 256-block grid puts there at the slow period. Qwen3.8-Flash-Next at
// tp 2, chain 3 with coupling, ~670 dependent launches a verify pass: -0.32% a step; every grid at
// 8 or 4 instead of 16 gave -0.16% and -0.25% for two and four times the polls of the wide ones.
#ifndef R4D_CHAIN_FAST
#define R4D_CHAIN_FAST 8
#endif
@@ -77,17 +85,31 @@ __device__ __forceinline__ void r4d_chain_acquire() {
asm volatile("s_dcache_inv" ::: "memory");
}
#ifndef R4D_CHAIN_TINY
#define R4D_CHAIN_TINY 16
#endif
#ifndef R4D_CHAIN_SMALL
#define R4D_CHAIN_SMALL 64
#endif
#ifndef R4D_CHAIN_MID
#define R4D_CHAIN_MID 4
#endif
__device__ __forceinline__ void r4d_chain_wait() {
const R4dChainRec* r = r4d_chain_rec();
if (!r) return;
if (threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0) {
const unsigned long long target = r->target;
unsigned long long* ctr = r->ctr;
const unsigned nblk = gridDim.x * gridDim.y * gridDim.z;
int n = 0;
while (__hip_atomic_load(ctr, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT) < target) {
if (n < R4D_CHAIN_FAST) {
__builtin_amdgcn_s_sleep(1);
++n;
} else if (nblk <= R4D_CHAIN_TINY) {
__builtin_amdgcn_s_sleep(1);
} else if (nblk <= R4D_CHAIN_SMALL) {
__builtin_amdgcn_s_sleep(R4D_CHAIN_MID);
} else {
__builtin_amdgcn_s_sleep(R4D_CHAIN_SLOW);
}
+10
View File
@@ -73,6 +73,16 @@ extern "C" int r4d_rad_prefetch(const RadArgs* a, RadStream stream) {
if ((uintptr_t)w->data % 16) return R4D_NO(RAD_E_ALIGN);
int nb = r4d_cfg_prefetch_nb(a);
if (nb < 1) nb = 64;
/* The architecture's own count (`blocks`): the read shares the card with the kernels it runs
* beside, whose loads queue behind its own and whose launches wait for the wave slots it
* holds, so the best count follows what those kernels are. Qwen3.8-Flash-Next's delta-net
* update, a latency-bound kernel of a few loads a block, loses more to 64 blocks of reads in
* flight than the projection behind it gains (ms a step at 16 / 24 / 32 / 48 / 64 / 128 / 256
* blocks, against 64: +0.77% / +0.14% / -0.31% / -0.24% / 0 / +2.0% / +7.4%). */
{
const long long blocks = r4d_geti_or(a, "blocks", 0);
if (blocks > 0 && blocks <= 1024) nb = (int)blocks;
}
const bool ch = r4d_geti_or(a, "chain", 0) != 0;
return R4D_EMIT(stream, (ch ? r4dchain_prefetch : prefetch_kernel), dim3((unsigned)nb), dim3(256), 0,
(const int4*)w->data, (size_t)(bytes / 16));
+3
View File
@@ -908,6 +908,9 @@ static const RadOperandSpec oKvLenTrim[] = {
/* `prefetch` reads a weight's leading `bytes` into the cache and writes nothing (docs/OPS.md). */
static const RadParamSpec pPrefetch[] = {
{ "bytes", RAD_P_INT, RAD_REQUIRED },
/* The workgroups reading, where the architecture measured its own count; absent or 0, the
* kernel's (its `nb` axis). */
{ "blocks", RAD_P_INT, RAD_OPTIONAL },
};
static const RadOperandSpec oPrefetch[] = {
{ "w", RAD_OPD_WEIGHT, 0 },
+14
View File
@@ -30766,6 +30766,20 @@ static void device_phase(const Plugin& pl) {
std::vector<RadTensor> ts = { T(d, RAD_U8, { (int64_t)1 << 20 }) };
std::vector<RadParam> ps = { PI("bytes", (int64_t)1 << 20) };
run_case(r, ts, ps);
/* With the architecture's own workgroup count (`blocks`): one block, the count
* Qwen3.8-Flash-Next asks for at a verify, the most the row takes, and absent, 0 and
* one past the most (the kernel's own count each) all launch, and the weight they read
* is the weight afterwards: the row writes nothing at any count. */
std::vector<uint8_t> h((size_t)1 << 20), back((size_t)1 << 20);
for (size_t i = 0; i < h.size(); ++i) h[i] = (uint8_t)(i * 131u + (i >> 9));
st_memcpy(d, h.data(), h.size(), hipMemcpyHostToDevice);
for (const long long nb : { 0ll, 1ll, 32ll, 1024ll, 1025ll }) {
std::vector<RadParam> pb = { PI("bytes", (int64_t)1 << 20), PI("blocks", nb) };
run_case(r, ts, pb);
}
st_memcpy(back.data(), d, back.size(), hipMemcpyDeviceToHost);
ok("prefetch at its caller's workgroup count leaves the weight as it was", back == h,
"the weight's bytes changed");
RadLayout L{};
const int st = r->layout ? r->layout(nullptr, 0, 0, nullptr, nullptr, nullptr, 0, &L) : 0;
ok("prefetch's layout hook takes any stored form (RAD_LAYOUT_ANY)", st == RAD_LAYOUT_ANY,