This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/place/heat.h
T
Deadcode dc339b4cd2 radiance 1.4.0
- Qwen3.8-Flash-Next on one card: the heat engine moves 64 experts a step (was 8), its copies and
  the prefill's expert staging run on the card's DMA engines instead of as copy kernels, and at
  decode the next layer's likeliest experts are copied onto the card ahead of its GEMMs. Against
  1.3.0 at --tp 1: prefill +17-20% from 8K tokens up, throughput under load +16-20%
- deploy/compose/flashnext-tp1.yaml: Qwen3.8-Flash-Next on one card and 64 GB of host RAM
  (4096-token prefill steps, 4 sequences, a 301K-token cache)
- #61: --busy-prefill-chunk (default auto): a long prefill leaves the rest of the step to the
  requests decoding beside it and to new arrivals, instead of taking all of it (baKon)
- LiquidAI's LFM2 and LFM2-VL, and d1's /v1/systemone (after zivius's PR #50)
- a startup report: every option with its value and where it came from, the hardware, and what
  on it costs speed with the fix; /diagnostics serves it (behind the API key; without one, only
  to the same machine)
- #69: a GPU memory fault names the buffer and kernel argument behind it and ends the server at
  once (exit status 70), instead of hanging or writing a core of tens of gigabytes
- #64: a card without Resizable BAR is refused at startup, naming what to turn on, instead of
  crashing at the first launch
- the image is built with ROCm 10.1 (the host's amdgpu driver must support it), with ROCm's
  libLLVM rebuilt so the same source compiles to the same kernels every time; a source build
  takes ROCm 10.1 or 7.2
- #71: the image ships curl, jq and a CA bundle, so --model <HF repo id> works in the container,
  and hfget.sh runs under its busybox (baKon)
- --profile-ops shows how long each op's lane sat idle before it; cardclock.py skips an
  integrated GPU instead of stopping
2026-10-12 11:46:14 +02:00

334 lines
21 KiB
C++

/* heat.h -- the heat engine: which conditional weight should be in VRAM, decided from what the
* router actually did.
*
* EXPERT OFFLOAD IS A PREDICTION PROBLEM. Layer offload is not -- declare enumerated the whole
* access sequence and the planner emits an exact prefetch schedule off it. Routing is
* data-dependent, so for RAD_ACCESS_CONDITIONAL weights there is no schedule to emit and the only
* thing left is a policy. This file is that policy and nothing else: it sees a tier per unit, a
* heat per unit and a dispatch count, and it answers one question -- "swap these two."
*
* Deliberately pure host code with no device call in it and no knowledge of slabs, streams or
* events. Everything about how the bytes get there is mover.cpp. That split is what lets the
* entire decision half be tested on a machine with no card, which is the only reason the
* hysteresis behaviour below is checkable at all.
*
* ---- WHAT IT COSTS TO BE ONE STEP BEHIND, HONESTLY -------------------------------------------
*
* Routing is computed on device and never blocks the compute stream. The histogram is copied back
* asynchronously and this engine consumes it on the NEXT step (spec §5.5), so every placement
* decision is one step stale.
*
* For heat-based promotion that is irrelevant: a hot expert is hot across many steps, and a
* ranking built from the step before ranks almost exactly the same as one built from this step.
* The real cost is different and it is worth being blunt about, because it is what people expect
* this file to do and it does not:
*
* AN EXPERT THAT IS NOT RESIDENT THIS STEP CANNOT BE FETCHED IN TIME. It is served from
* wherever it is -- streamed out of the pinned pool over the link, or staged off the file tier.
* There is no last-moment rescue and there is no point adding one: the fetch would have to
* finish inside the gap between the router's output and the expert GEMM's first block, which
* is microseconds.
*
* Hit rate is therefore a property of the PLACEMENT POLICY, not of a rescue path. That is the
* trade, and what it buys is a run phase with no device round-trip in it.
*
* ---- WHY LFU-WITH-DECAY AND NOT SOMETHING CLEVERER --------------------------------------------
*
* Replaying real routing traces against cache policies settles the shape of this. Runtime
* placement beats static profile placement by a wide margin, so it is worth building. What bounds
* it is the MOVE RATE, not the ranking: held to the same swaps a step at the same step boundaries,
* an oracle that knows every future routing scores no better than this policy (78.0% against 78.4%
* at 64 swaps a step, Qwen3.8-Flash-Next TP1 with 40% of the experts resident). The unrestricted
* Belady figure is far higher -- 92% on the same trace -- because it admits every miss the moment
* it happens, and a miss admitted here is a second copy of bytes the GEMM has just streamed. So
* the decay constant and the guards are not where the wins are, and this does not need to be
* subtle; the budget (RAD_PLACE_MOVES_PER_STEP), the slots behind it, and keeping a prompt's
* routing from swamping the answer's (HeatParams::credit_cap) are.
*/
#pragma once
#include "planner.h"
#include <string>
#include <vector>
namespace rad {
struct HeatParams {
/* Applied to a layer's whole plane each time that layer's histogram is credited. Every layer is
* credited once a step, so all planes decay in lockstep and heats stay comparable ACROSS
* layers -- which the global ranking below depends on.
*
* 0.95 is a half-life of about fourteen steps. At 64 swaps a step the replay is flat from 0.8
* to 0.95 (77.96% to 78.37%) and falls off above it -- 77.1% at 0.98, 74.2% at 0.99 -- because
* a heat that remembers longer ranks the experts a conversation has moved past above the ones
* it routes to now. */
float decay = 0.95f;
/* Promote only when the candidate beats the victim by this ratio. Without it the two ends of
* the ranking trade places on noise and the engine spends the whole link on churn that
* changes nothing. */
float hysteresis = 1.25f;
/* ...and the additive half of the same guard, in units of one activation. It is what stops a
* swap when the victim's heat is ZERO, where any ratio test passes trivially -- and right
* after warmup that describes most of the resident set, so without this the first dispatch
* would try to swap the entire cache.
*
* LINK TRAFFIC IS U-SHAPED IN THIS THRESHOLD. PAST THE BOTTOM OF THE CURVE, RAISING IT MOVES
* MORE BYTES, NOT FEWER -- so do not "fix" it by tightening the bar to save traffic. The
* economics look one-directional -- a promotion costs an H2D plus a D2H and only pays if the
* expert is selected again -- and they are not, because A PROMOTION DECLINED COMES BACK AS A
* MISS, and a miss is a shard streamed over the same link on the same budget. A tighter bar
* then gives up hit rate and buys no traffic back.
*
* What a swap costs is its promotion. The link is close to full duplex -- on an R9700, 28.5
* GB/s up and 25.0 down at once, against 28.8 up alone -- so a demotion's copy rides the idle
* direction, while a promotion shares the upstream one with every miss the GEMM streams. At
* the default budget lower values replay within 0.2 points of hit rate and promote a fifth
* more. */
float min_gain = 1.5f;
/* A BOUNDED MOVE BUDGET PER STEP: step() is called once a step, after every layer's histogram
* has been credited, and proposes at most this many swaps a slab class. The value and why it
* is that are at RAD_PLACE_MOVES_PER_STEP, which also sizes the free slots a swap lands in
* (the shadow and turnover slots); a budget raised here without them is spent on
* `starved_slab`.
*
* The apparent risk is that a high budget makes the mover compete with the tier it is trying
* to shrink -- a promotion and a streamed miss are the same bytes over the same link. At this
* scale that does not bind: each swap moves an expert the ranking says will be read again
* soon, and the misses it saves are many times its own bytes.
*
* IT IS A BUDGET PER SLAB CLASS. A rank holding an uneven slice of every expert has two expert
* classes -- the wider slice of one parity's experts and the narrower slice of the other's --
* each about half a whole expert, and a budget shared between them would move half the bytes
* a step that whole experts do: residency would converge half as fast. Per class, the two
* together move about as many whole experts as one class of whole experts does. */
int moves_per_step = RAD_PLACE_MOVES_PER_STEP;
/* THE MOST ONE HISTOGRAM CREDITS, in selections: a histogram that routed more is credited
* at this total, each expert in proportion to its count. A decode step routes a few rows a
* sequence and stays under it; a prefill chunk routes thousands, and at full weight one chunk
* outweighs dozens of decode steps, so for that many steps after a prompt the resident set is
* the prompt's rather than the answer's. What the resident set buys a prefill is little in
* any case: a chunk reads nearly every expert of a layer, and the stager copies whichever are
* not resident. Replaying a mixed workload at Qwen3.8-Flash-Next TP1, decode rows hit 77.3%
* uncapped, 78.9% at 1024 and 79.0% at 512 or 256. */
float credit_cap = 512.0f;
/* Dispatches to watch before moving anything, and it is 0 because THE GUARD ALREADY DOES THIS
* JOB. min_gain means a promotion needs `hot >= cold*hysteresis + 1.5`, and against a
* zero-heat victim that is two tokens agreeing in one dispatch -- evidence, not noise,
* whatever the dispatch count is. A warmup only postpones convergence. */
int warmup_dispatches = 0;
};
/* A swap is returned as a PAIR even though the mover issues two independent transfers, because
* the DECISION is a comparison between them: promoting without the matching demotion would grow
* the resident set past the slab. */
struct Swap {
int32_t promote = -1; /* unit index */
int32_t demote = -1;
};
/* One movable unit as step() ranks it: its heat, its layer's resident count, its index. */
struct HeatRanked { float h; int32_t rc; int32_t unit; };
struct HeatStats {
uint64_t dispatches = 0;
uint64_t proposed = 0, promotions = 0, demotions = 0;
/* Why a dispatch did NOT move anything, which is the diagnostic that matters once the engine
* looks idle. `refused` is the guard declining -- the healthy steady state, and the number
* that should dominate once the hot set has settled. */
uint64_t refused = 0, no_candidate = 0;
/* Is the churn PROGRESS or THRASH? The same total move count means two opposite things. If
* every promotion is of a unit never promoted before, the engine is still discovering the
* working set and the traffic is an investment. If it keeps re-admitting units it demoted a
* moment ago, the traffic is pure loss: both directions of the link spent to arrive back
* where it started. `readmits` is the second case counted directly, and it is the number that
* decides whether the guards are set right. */
uint64_t distinct_promoted = 0, readmits = 0;
/* WHAT THE ROUTER ASKED FOR, AND WHETHER IT WAS THERE. Counted on the histogram this engine
* already walks, so it is one compare per routed expert and no second pass.
*
* A miss is not an error and nothing stalls on it: the GEMM reads that expert's weights from
* whatever tier holds them, which for a host-pinned unit means pulling them across the link
* on the critical path of the step. So `stream_bytes` is the OTHER traffic the mover's
* promotions were hiding -- a promotion is bytes spent once to stop paying this, and without
* both numbers beside each other there is no way to say whether the policy is winning.
*
* Weighted by the histogram's `weight`, because a draft head's histogram stands for several
* routing passes and each of them really did read its experts. The hit RATE is unaffected --
* both counters scale together -- and the byte figure is right.
*/
uint64_t routed = 0, routed_resident = 0, stream_bytes = 0;
};
class HeatEngine {
public:
/* Binds to the plan. Returns RAD_OK even when the plan is static -- that is a mode, not a
* failure, and every caller would otherwise have to special-case it. `enabled()` says which.
* Under Config::deterministic the plan is static, this stays off, and step() proposes
* nothing for the whole run (spec §17). */
int init(const Plan& plan, const HeatParams& params);
bool enabled() const { return enabled_; }
/* ---- the input: last step's routing histogram ------------------------------------------
*
* `counts[e]` is how many of the step's tokens routed to expert e of this layer. The credit
* is that count and not a flat 1.0: cost is per DISTINCT expert -- a dispatch reads an
* expert's weights once however many tokens chose it -- but the probability of needing it
* again is not. An expert several tokens chose is far likelier to be chosen again soon than
* one a single token chose, and most selections are single-token. Weighting by it moves no
* extra bytes; it only ranks better. */
/* `weight` is how many of that layer's routing passes this histogram stands for -- see
* Ctx::last_histogram_reports. It is 1 for every layer of a non-speculative step and n_spec
* for a draft head, and it belongs on the CREDIT rather than the decay: the caller sweeps
* every layer exactly once a step, so the planes decay in lockstep and heats stay comparable
* across layers. Without it a single histogram silently stands for several passes' worth of
* activations and under-credits the experts a draft head routed to. */
void observe(int32_t layer, const int32_t* counts, int32_t n_expert, float weight = 1.0f);
/* The same, sparse: distinct expert ids and their counts. `counts` may be null for one each. */
void observe_ids(int32_t layer, const int32_t* experts, const int32_t* counts, int32_t n);
/* Starts the fetch of a histogram observe() will read: a row the card copied into host
* memory is a miss on every line, so a caller that knows its next rows asks for them early. */
static void prefetch(const int32_t* counts, int32_t n_expert) {
for (int32_t j = 0; j < n_expert; j += 64 / (int32_t)sizeof(int32_t))
__builtin_prefetch(counts + j);
}
/* ---- the tier map, written by the MOVER when a move PUBLISHES, not when it is issued ----
*
* Between issue and publish both copies of the bytes are valid and the dispatch still reads
* the old one, so this engine must agree with dispatch about where a unit is or it will pick
* the same one twice. `set_busy` is what excludes an in-flight unit instead. */
void set_tier(int32_t unit, Tier t);
void set_busy(int32_t unit, bool busy);
Tier tier(int32_t unit) const { return tier_[(size_t)unit]; }
bool busy(int32_t unit) const { return busy_[(size_t)unit] != 0; }
/* The best swap available, or false when the guards refuse. */
bool pick(Swap* out) const;
/* One step's worth of proposals, at most `moves_per_step` of them a class.
*
* CONTRACT: every unit named in the result is marked busy on the way out. The caller must
* either issue the transfer -- in which case set_tier() on publish and set_busy(false) after
* the quarantine -- or call abandon() for both halves. A proposal that is silently dropped
* leaves the pair busy forever and the engine goes quiet with no counter to say why. */
int step(std::vector<Swap>* out);
void abandon(const Swap& s);
float heat(int32_t unit) const { return heat_[(size_t)pos_[(size_t)unit]]; }
/* How many units of THIS unit's layer are resident. The tiebreak every residency decision
* needs: a global ranking with no layer term leaves some layers holding far more than others,
* and a layer starved of slots misses on nearly every token however good the ranking inside
* it is. `pick` uses it on an exact heat tie and so must anything else that chooses. */
int32_t layer_residents(int32_t unit) const {
const int32_t l = (size_t)unit < layer_of_.size() ? layer_of_[(size_t)unit] : -1;
return l >= 0 && (size_t)l < res_count_.size() ? res_count_[(size_t)l] : 0;
}
const HeatStats& stats() const { return stats_; }
/* The exit banner. The per-layer residency histogram at the end of it is the one number that
* says whether the engine did the thing it exists for: a global profile ranking leaves layers
* with wildly different resident counts, and a layer starved of slots misses on nearly every
* token however good the ranking inside it is. */
std::string report() const;
private:
HeatParams params_;
bool enabled_ = false;
int32_t n_layer_ = 0;
HeatStats stats_;
/* THE HEAT, BY POSITION RATHER THAN BY UNIT. Each layer owns one run of positions, and the
* front of that run is indexed by EXPERT: expert e of layer l sits at slot_lo_[l] + e, whether
* or not the plan has a unit for it and whether or not that unit may move. Then the layer's
* other movable units, then padding to a whole number of lanes. After the layers come the
* movable units with no layer, padded to a whole screening block, and after them everything
* else.
*
* That lines a histogram up with the positions it credits. observe() walks counts[e] and the
* heat at slot_lo_[l] + e side by side, in fixed-width blocks the compiler turns into vector
* code, with no unit number looked up and no branch taken per expert. A position with no unit
* behind it is a hole: never a candidate, never priced, and its heat is read by nothing.
* `pos_` and `unit_at_` translate (-1 for a hole); everything outside the position arrays
* stays indexed by MoveUnit index so this file and the plan cannot disagree about what a
* number means. */
std::vector<float> heat_; /* by position */
std::vector<int32_t> pos_; /* unit -> position */
std::vector<int32_t> unit_at_; /* position -> unit, -1 for a hole */
/* Per position, what step() ranks it as: 0 not a candidate (fixed, in flight, or a hole), 1 a
* candidate off the device, 2 a candidate on it. Kept by set_tier and set_busy. */
std::vector<uint8_t> rank_as_;
/* Per position, what a routed read of it costs: `routed_as_` is 0 for a hole, 1 off the device
* and 2 in VRAM, and `miss_bytes_` is the unit's bytes when it is off the device and 0 when it
* is not. Kept by set_tier, so pricing a histogram is arithmetic on the pass that credits it. */
std::vector<uint8_t> routed_as_;
std::vector<int64_t> miss_bytes_;
/* Slot s's positions are [slot_lo_[s], slot_lo_[s + 1]): layer s, and slot n_layer_ the
* movable units with no layer. Every movable unit is below slot_lo_[n_layer_ + 1], which is a
* whole number of screening blocks. The first n_ex_[l] positions of layer l are its experts. */
std::vector<int32_t> slot_lo_;
std::vector<int32_t> n_ex_;
void set_place(int32_t unit);
/* Unit bytes, from the plan. Here so a miss can be priced where it is detected rather than
* joined back to the plan by every reader. */
std::vector<int64_t> bytes_;
std::vector<Tier> tier_;
std::vector<uint8_t> busy_;
std::vector<uint8_t> movable_;
std::vector<int32_t> layer_of_;
/* Bit 0: promoted before. Bit 1: demoted before. Two bits a unit, so the thrash question
* costs nothing on the dispatch path. */
std::vector<uint8_t> ever_;
/* How many of each layer's units are resident. THE TIE-BREAK IS NOT COSMETIC: most resident
* units are cold, so `heat == 0.0f` exactly is the common case at both ends of the ranking,
* and whatever breaks that tie IS the policy for most of a run. Breaking it by index order
* strips the low-numbered layers first, which replaces the skew this engine exists to fix
* with a different one. So ties go to the layer that can most afford it: take the victim from
* the layer holding the MOST, promote into the one holding the FEWEST. */
std::vector<int32_t> res_count_;
/* A SWAP STAYS INSIDE ONE SLAB CLASS. The promoted unit takes a slot of its own class and the
* demoted one frees a slot of its own, so a pair across two classes would drain one class's
* spare slots into the other's until promotions starve. A model has one expert class when
* every expert is the same size on a rank, and two when a rank holds an uneven slice of each
* expert's width (three 128-column blocks of one parity, two of the other). `cls_` is the
* unit's class as a dense index, -1 for a unit that cannot move. */
std::vector<int32_t> cls_;
int32_t n_cls_ = 1;
/* step()'s two ends of the ranking, `moves_per_step` entries a class, kept between calls so a
* step allocates nothing. */
std::vector<HeatRanked> rank_hot_, rank_cold_;
std::vector<int32_t> n_hot_, n_cold_, m_of_;
/* Per slot, its lowest movable unit index: the bound step()'s layer screen needs on a tie. */
std::vector<int32_t> min_unit_;
/* EACH LAYER'S TWO ENDS, so a dispatch that proposes nothing -- the guard declining the best
* pair, which is nearly every dispatch once the resident set has settled -- is decided from
* one entry a layer rather than a scan of every unit. Over the movable units not in flight:
* the hottest one off the device and the coldest one on it, and how many there are of each.
* Slot n_layer_ is the movable units with no layer.
*
* observe() recomputes a layer's ends on the pass that decays and credits it, where each is
* one more compare on a value already in a register. A change of tier or of being in flight
* invalidates the unit's layer, and an invalid entry is rebuilt from its positions when step()
* next reads it. */
std::vector<float> end_hot_, end_cold_;
std::vector<int32_t> end_n_off_, end_n_on_;
std::vector<int32_t> end_n_cold_; /* units on the device at exactly the cold end's heat */
std::vector<uint8_t> end_ok_;
void end_rebuild(size_t slot);
void end_invalidate(int32_t unit) {
if (end_ok_.empty()) return;
const int32_t l = (size_t)unit < layer_of_.size() ? layer_of_[(size_t)unit] : -1;
end_ok_[l >= 0 ? (size_t)l : (size_t)n_layer_] = 0;
}
bool warm() const { return (int64_t)stats_.dispatches >= params_.warmup_dispatches; }
};
} /* namespace rad */