Archived
Which experts a served model actually uses, per layer, is what deciding how to spend an expert's bits needs, and the core already has it: every routed layer's counts come back to a host bank once a pass, and the heat engine drains the landed banks once a step, one step behind. --expert-stats FILE credits the same rows to a host tally and writes it as JSON every 30 s and at shutdown, through a temporary and a rename (place/xstats.h). No device work, no copy, no synchronisation; where the heat engine is off (every expert in VRAM, a static plan) the drain runs for the tally alone, and at shutdown the streams are drained and the banks still in the ring are credited. Counts are filed by step kind: prefill (no decode rows), decode, mixed (one histogram cannot split it) and draft, which is every routed layer past the trunk (an MTP or DSpark head; its bank holds the step's last draft pass). Each rank files its passes' kinds when it issues them. A histogram row is the rank's own experts from zero, so under expert parallelism (deepseek41) it is the rank's slice. The map from a rank's index to the model's expert id comes from the container: index e is the unit declared with group expert e, and the directory entry of that unit's weight names the expert. An expert several ranks hold (a width slice each) is counted on the lowest; a rank that counts nothing does not drain for the tally. Without directory ids the map falls back to the indices (one rank, or every rank holding the model's count) or to rank order. The file carries the model, the routed layers, each layer's expert count and top_k, tokens and steps issued by kind, histograms, passes, observed tokens and selections per kind and layer, the per-rank drain counters (lost, unmapped, unclassified), and counts[kind][layer][expert]. /server_info names the file. Router weights are not summed: they stay on the card. Tests: xstats_test (the map, the tally's kinds and merge, the file); config_test lists the flag and checks it is off until it names a file.
114 lines
5.6 KiB
C++
114 lines
5.6 KiB
C++
/* engine_priv.h -- shared by the Engine's translation units only.
|
|
*
|
|
* `Engine` is one class split across several files by phase, which share little: bring-up runs
|
|
* once and touches every subsystem, the step loop runs constantly and touches a few, the drafters
|
|
* are extra passes, and the probe is debug-only. The public surface in engine.h is unchanged.
|
|
*
|
|
* `Engine::Rank` is defined here, not in engine.h, because it names every subsystem's concrete
|
|
* type; engine.h is included by main.cpp and the tools, which then need only a forward
|
|
* declaration.
|
|
*/
|
|
#pragma once
|
|
#include "engine.h"
|
|
|
|
#include "mem/pools.h"
|
|
#include "mem/kv.h"
|
|
#include "mem/prefix.h"
|
|
#include "mem/kvtier.h"
|
|
#include "runtime/tierexec.h"
|
|
#include "build/rad_build.h"
|
|
#include "lora/declare.h"
|
|
#include "place/heat.h"
|
|
#include "place/planner.h"
|
|
#include "place/mover.h"
|
|
#include "place/stager.h"
|
|
#include "place/xstats.h"
|
|
#include "runtime/ctx.h"
|
|
#include "sample/sampler.h"
|
|
#include "sched/advance.h"
|
|
|
|
namespace rad {
|
|
|
|
/* ================================================================== per-rank state */
|
|
/* One tensor-parallel rank's state; see Engine::ranks_. */
|
|
struct Engine::Rank {
|
|
int index = 0;
|
|
int device = 0;
|
|
Program program; /* declare runs once per rank: dimensions are already divided */
|
|
/* Runtime LoRA: this rank's sites (core/lora/lora.h), the loader's map from an adapter's
|
|
* modules to this rank's slot bytes. Empty with LoRA off. */
|
|
std::vector<lora::Site> lora_sites;
|
|
Plan plan;
|
|
Pools pools;
|
|
Mover mover;
|
|
/* The policy half of tiered placement, bound to the plan at bring-up and off unless the plan
|
|
* is dynamic. `heat_seen[l]` is the last step whose layer-l histogram was credited: a
|
|
* histogram stays in its bank until the next report overwrites it, and must not be counted
|
|
* twice. */
|
|
HeatEngine heat;
|
|
std::vector<int> heat_seen;
|
|
/* One sweep's histograms, found before any is credited; kept so a sweep allocates nothing. */
|
|
struct HeatRow { int layer; int bank; const int32_t* h; };
|
|
std::vector<HeatRow> heat_rows;
|
|
/* Credited histograms, and layer sweeps that found nothing new. A histogram is skipped
|
|
* either because its copy has not landed (the host ran ahead and the sample was lost) or
|
|
* because its bank holds an already-credited step (the layer did not route). Counting these
|
|
* apart keeps a placement engine starved of evidence from looking like idle layers. */
|
|
uint64_t heat_credited = 0, heat_idle = 0;
|
|
/* Trunk steps in a row whose pass the prefill stager did not arm (Engine::tiering_dispatch). */
|
|
int unstaged_steps = 0;
|
|
KVManager kv;
|
|
PrefixCache prefix;
|
|
/* Idle session tiers (spec 7.3). Rank 0 owns the policy and session index, since a
|
|
* conversation is not sharded; the transfer is per rank, since each holds its own shard of
|
|
* every KV block. Both are inert unless --prefix-cache-mib sizes the host tier. */
|
|
IdleTiers tiers;
|
|
TierExec tierx;
|
|
/* The activation arena's smaller levels, smallest step first (Engine::probe_arena_levels). */
|
|
std::vector<ArenaLevel> arena_levels;
|
|
/* Per Ctx level (0 is the full plan): the bytes at the top of this rank's arena that level
|
|
* leaves to the expert slab. */
|
|
std::vector<int64_t> arena_lent;
|
|
/* Where the prefill stager's ring is at the span's shared level, from the arena's base; -1
|
|
* without that level. */
|
|
int64_t arena_low_off = -1;
|
|
/* Prefill stager (core/place/stager.h): copies the next routed layer's pooled experts into the
|
|
* top of this rank's arena while a full-plan step runs the layer before. */
|
|
PrefillStager stager;
|
|
/* File stager (core/place/stager.h): reads a routed layer's file-tier experts from the
|
|
* container into one of the mover's two file buffers before the layer's first expert op, on
|
|
* every pass. With both stagers in use, the chain hands each the passes it armed for. */
|
|
FileStager fstager;
|
|
StagerChain stagers;
|
|
Ctx ctx;
|
|
Sampler sampler; /* one per rank: each samples its own vocab shard (spec §9) */
|
|
/* Scored rows' logits (Request::token_scores), this rank's vocabulary shard, copied into pinned
|
|
* memory ahead of the sampler. Grown to the most rows a step has scored. */
|
|
float* score_host = nullptr;
|
|
int64_t score_rows_cap = 0;
|
|
rad_buf tok_buf = 0; /* where the chain writes the sampled ids */
|
|
/* The card's step state (sched/advance.h): one StepSlot per scheduler slot, and the sorted
|
|
* end-of-generation ids acceptance stops at. Every rank keeps its own and computes the same
|
|
* values from the same sampled ids. */
|
|
StepSlot* step_slots = nullptr;
|
|
int32_t* eog = nullptr;
|
|
int32_t n_eog = 0;
|
|
};
|
|
|
|
/* The tiers' work in flight (Engine::tier_tick): one copy batch per rank's transfer stream at a
|
|
* time. Each rank's result is kept until all ranks have one, so a landed rank is not polled again
|
|
* and a failure is not lost. */
|
|
struct TierPump {
|
|
std::vector<TierJob> copy;
|
|
std::vector<int> copy_st; /* per rank: 0 in flight, 1 landed, negative failed */
|
|
int64_t max_copy = 0;
|
|
int slow_budget = 0; /* capacity drops one plan may make */
|
|
};
|
|
|
|
/* The buffer probe (engine_probe.cpp): the buffers RADIANCE_DUMP_BUF names, at -vv only. The step
|
|
* loop dumps after a trunk step, and each drafter after its own passes, since a drafter's buffers
|
|
* still hold the previous step's values when the step loop dumps. */
|
|
void dump_buffers(Ctx& ctx, int step, const char* phase = "");
|
|
|
|
} /* namespace rad */
|