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

+3
View File
@@ -1017,6 +1017,9 @@ void Engine::tiering_dispatch(Rank& r, int step) {
* credited; the next unstaged step swaps on them. */
std::vector<Swap> swaps;
if (!r.stager.staged() && r.heat.step(&swaps) < 0) return;
/* What the turnover slots follow (MoverConfig::turnover_adaptive). A staged step proposes
* nothing and holds the turnover filled anyway (below), so it is not a quiet step. */
if (!r.stager.staged()) r.mover.swap_demand((int64_t)swaps.size());
for (const Swap& s : swaps) {
/* Contract: every proposed unit is busy until `issue` takes it or `abandon` returns it;
* a dropped proposal stays busy forever. */
+3
View File
@@ -3737,6 +3737,9 @@ int Engine::load_weights() {
* aligned (rad_format.h), DirectFile's alignment, so stages take the no-bounce path. */
MoverConfig mc{};
mc.container_path = file_ ? cfg_.model : std::string();
/* The lent slots kept free for swaps follow the swaps asked for (mover.h): a rank that
* holds nearly all its experts gets the idle ones back as residents. */
mc.turnover_adaptive = true;
RAD_TRY(r->mover.init(r->program, r->plan, mc, &r->pools));
/* What the mover took, before the first copy. Its slabs are allocated in init, so the pool
* is as full as it will get; a load stopping with zero free is a placement failure, not
+37 -5
View File
@@ -8,6 +8,7 @@
#include "../device/device.h"
#include <algorithm>
#include <cmath>
#include <chrono>
#include <cstring>
#include <map>
@@ -174,6 +175,12 @@ int Mover::init(const Program& prog, const Plan& plan, const MoverConfig& cfg, P
cp.n_work = shadow + sc.n_stage;
cp.n_turn = std::min<int64_t>(std::max(cfg.turnover_slots, 0),
shadow * RAD_PLACE_TURNOVER_SLOTS / RAD_PLACE_SHADOW_SLOTS);
/* Under MoverConfig::turnover_adaptive the kept-free count starts at its floor: a fresh
* placement fills what it can at once, and the first burst of swaps lands in the shadow
* slots and these while balance() frees the rest. */
cp.turn_now = cfg.turnover_adaptive
? std::min<int64_t>(cp.n_turn, std::max(cfg.turnover_floor, 0))
: cp.n_turn;
total_turn += cp.n_turn;
cp.slab.init(slab_slots, sc.stride);
total_slab += slab_slots;
@@ -379,9 +386,10 @@ int Mover::init(const Program& prog, const Plan& plan, const MoverConfig& cfg, P
republish(u);
}
RAD_INFO("mover: %lld slab slots (%lld shadow, %lld more kept free out of a loan), %lld pinned "
"pool slots, %s static vram%s",
(long long)total_slab, (long long)total_shadow, (long long)total_turn,
RAD_INFO("mover: %lld slab slots (%lld shadow, %s%lld more kept free out of a loan%s), %lld "
"pinned pool slots, %s static vram%s",
(long long)total_slab, (long long)total_shadow, cfg.turnover_adaptive ? "up to " : "",
(long long)total_turn, cfg.turnover_adaptive ? " as swaps ask for them" : "",
(long long)total_pool,
humanb(static_bytes_).c_str(), file_.ok() ? ", file tier open" : "");
/* The pageable region is charged to no budget, so this is the only place it is reported. */
@@ -1126,7 +1134,13 @@ int Mover::issue(const Swap& s, HeatEngine* heat) {
if (promote) {
const int64_t slot = cp.slab.take();
if (slot < 0) { ++stats_.starved_slab; return false; }
if (slot < 0) {
++stats_.starved_slab;
/* The slots kept free were too few for this step's swaps: all of them, for a
* while (balance). */
if (cfg_.turnover_adaptive) turn_boost_ = kTurnBoost;
return false;
}
if (promote_to(unit, slot) < 0) { cp.slab.add_free(slot); return false; }
return true;
}
@@ -1625,7 +1639,19 @@ int Mover::balance(HeatEngine* heat, bool turnover) {
if (unit_[(size_t)m.unit].klass != (int32_t)c || !cp.slab.covered(m.slab_slot)) continue;
if (m.frees_slab()) ++pending;
}
const int64_t turn = turnover ? cp.n_turn : 0;
if (cfg_.turnover_adaptive) {
/* Three steps of the mean swaps a step (a swap holds its slot about three steps,
* RAD_PLACE_TURNOVER_SLOTS), between the floor and the configured count; all of
* them while a starved promotion is recent. The mean is over every class, so a
* class keeps a little more than its own share asks for. */
const int64_t floor_ = std::min<int64_t>(cp.n_turn, std::max(cfg_.turnover_floor, 0));
const int64_t want = turn_boost_ > 0 ? cp.n_turn : std::min<int64_t>(
cp.n_turn, std::max<int64_t>(floor_, (int64_t)std::lround(3.0f * demand_mean_)));
cp.turn_now = want >= cp.turn_now ? want : std::max(want, cp.turn_now - kTurnShrink);
} else {
cp.turn_now = cp.n_turn;
}
const int64_t turn = turnover ? cp.turn_now : 0;
int64_t short_by = cp.n_work + turn - (cp.slab.n_free() + pending);
if (short_by > 0) {
drop_cand_.clear();
@@ -1683,6 +1709,12 @@ int Mover::balance(HeatEngine* heat, bool turnover) {
return flush_moves();
}
void Mover::swap_demand(int64_t proposed) {
if (!cfg_.turnover_adaptive) return;
demand_mean_ += ((float)std::max<int64_t>(proposed, 0) - demand_mean_) / kTurnMean;
if (turn_boost_ > 0) --turn_boost_;
}
int Mover::idle_tick(HeatEngine* heat) {
if (!plan_ || !plan_->dynamic) return RAD_OK;
RAD_TRY(retire(heat));
+40
View File
@@ -307,6 +307,23 @@ struct MoverConfig {
* gets proportionally fewer, so a small loan is not left emptier than it is filled. */
int turnover_slots = RAD_PLACE_TURNOVER_SLOTS;
/* Whether the turnover slots a class keeps free follow the swaps the engine lately asked for
* (Mover::swap_demand) instead of always being all of them, and the fewest it keeps then.
*
* The count above is three steps of the full move budget, what a placement that changes
* every step needs (the planner.h figures are for a rank with 40% of its experts on the
* card). A rank that holds nearly all its experts swaps a few units a step once it has
* warmed up, and the 160 free slots a class are then experts left in the pool for nothing:
* Qwen3.8-Flash-Next at tp 2 keeps 640 slots a rank free (0.8 GiB) beside ~1000 units read
* over the link. Following demand, a class keeps three steps of the swaps a step the engine
* has lately proposed (a running mean, so one step's burst lands in the slots already free
* and moves nothing), never fewer than `turnover_floor`; the rest is filled like any other
* lent slot. A promotion that finds no free slot takes all of them back for a while, by
* dropping the coldest lent units, an address change (balance). Off by default, so a caller
* that sizes the turnover itself gets what it set. */
bool turnover_adaptive = false;
int turnover_floor = RAD_PLACE_SHADOW_SLOTS;
/* The container, for the file tier. Empty means no file tier; a unit planned there is then
* an error. */
std::string container_path;
@@ -577,6 +594,17 @@ public:
/* Whether idle ticks keep the turnover slots filled; the engine holds them through a prompt
* and the few steps after it (Engine::tiering_dispatch). */
void hold_turnover(bool held) { hold_turnover_ = held; }
/* The swaps the heat engine proposed this step (all classes), once a step before they are
* issued: what the kept-free turnover follows under MoverConfig::turnover_adaptive, as a
* running mean over about kTurnMean steps. */
void swap_demand(int64_t proposed);
/* The turnover slots kept free now, summed over the slab classes: turnover_slots(), or under
* MoverConfig::turnover_adaptive what the demand asks for. */
int64_t turnover_now() const {
int64_t n = 0;
for (const ClassPools& cp : pools_) n += cp.turn_now;
return n;
}
/* Each class's pinned pool as the card addresses it: [lo, hi) and the unit stride. At most `max`. */
int pool_ranges(uint64_t* lo, uint64_t* hi, uint64_t* stride, int max) const {
int n = 0;
@@ -746,6 +774,8 @@ private:
int64_t n_turn = 0; /* lent slots kept free past those: the fill stops short
* of them and balance() drops lent units to rebuild them
* (MoverConfig::turnover_slots) */
int64_t turn_now = 0; /* of n_turn, the slots kept free now: all of them, or
* what the recent swaps ask for (swap_demand) */
int64_t n_donated = 0; /* units in lent slots that gave their host copy away
* (Mover::donor_slot); at most n_turn of them */
std::vector<char*> flex_ptr;
@@ -803,6 +833,16 @@ private:
/* balance()'s drop candidates, (heat, unit), kept between calls so a step allocates nothing. */
std::vector<std::pair<float, int32_t>> drop_cand_;
bool hold_turnover_ = false;
/* The running mean of swaps proposed a step (swap_demand), and the steps left for which a
* promotion that found no free slab slot keeps every turnover slot free. */
float demand_mean_ = 0.0f;
int64_t turn_boost_ = 0;
static constexpr float kTurnMean = 64.0f;
static constexpr int64_t kTurnBoost = 512;
/* The most slots a class's kept-free count falls by in one balance(): each slot given up is
* a unit copied in over the link, and all of them at once would be hundreds of copies behind
* one step. Growing is immediate (drops cost nothing). */
static constexpr int64_t kTurnShrink = 8;
/* donor_slot()'s candidates per class, by layer, each layer sorted coldest first (hottest at
* the back). Ranked once per dispatch; `at` is that dispatch. */
struct DonorRank { int at = -1; std::vector<std::vector<std::pair<float, int32_t>>> by_layer; };