prefill: key recordings by live split geometry

This commit is contained in:
Constantin authored and StillDeadcode committed 2026-11-03 19:05:30 +01:00
1 parent 61e275e30f
commit ded64c0bd3
16 files changed
+204 -16

No files matched your search

+1
View File
@@ -169,6 +169,7 @@ private:
std::vector<const char*> meta_kv_kp_, meta_kv_vp_;
void (*arch_step_)(RadCtx*, const RadBatch*) = nullptr;
uint64_t (*arch_pass_key_)(RadCtx*, const RadBatch*, const int32_t*) = nullptr;
int load_plugins();
int read_metadata();
+2
View File
@@ -968,6 +968,7 @@ int Engine::load_plugins() {
const Plugin* ap = rad_tools_registry().plugin(plugins_.arch_plugin);
arch_step_ = ap ? ap->arch_step : nullptr;
arch_pass_key_ = ap ? ap->arch_pass_key : nullptr;
return RAD_OK;
}
@@ -3866,6 +3867,7 @@ int Engine::start() {
/* Null for all-in-VRAM: the mover never runs and lookup uses WeightInfo::ptr. */
d.residency = r->plan.strategy == Strategy::AllVram ? nullptr : r->mover.residency();
d.arch_step = arch_step_;
d.arch_pass_key = arch_pass_key_;
d.profile_ops = cfg_.profile_ops;
bind_kv_pools(r->program, [&](int32_t g) { return r->kv.plan(g); }, &d.kv);
d.registry = &rad_tools_registry();
+2
View File
@@ -217,6 +217,8 @@ int Registry::commit_(Staged& s, int order) {
p.arch_declare = dl_fn<int (*)(RadBuilder*, const RadModelMeta*, const RadBuildCtx*)>(
s.handle, "rad_arch_declare");
p.arch_step = dl_fn<void (*)(RadCtx*, const RadBatch*)>(s.handle, "rad_arch_step");
p.arch_pass_key = dl_fn<uint64_t (*)(RadCtx*, const RadBatch*, const int32_t*)>(
s.handle, "rad_arch_pass_key");
if (!id_fn || !p.arch_declare || !p.arch_step) {
err_("architecture plugin '%s' is missing rad_arch_id, rad_arch_declare or "
"rad_arch_step", p.name.c_str());
+1
View File
@@ -82,6 +82,7 @@ struct Plugin {
std::string arch_quant;
int (*arch_declare)(RadBuilder*, const RadModelMeta*, const RadBuildCtx*) = nullptr;
void (*arch_step)(RadCtx*, const RadBatch*) = nullptr;
uint64_t (*arch_pass_key)(RadCtx*, const RadBatch*, const int32_t*) = nullptr;
/* Optional (rad_builder.h); null means every question answers zero. Asked before declare, so
* model facts that are inputs to declaration are still read by the plugin, not the core. */
int (*arch_probe)(const RadModelMeta*, RadArchProbe*) = nullptr;
+10 -1
View File
@@ -31,11 +31,15 @@ int Ctx::init(const CtxDesc& d) {
registry_ = d.registry;
kv_ = d.kv;
arch_step_ = d.arch_step;
arch_pass_key_ = d.arch_pass_key;
profiling_ = d.profile_ops;
/* The rank owns one device. Set it here so allocation lands on the right card, and again from
* the rank thread in bind_thread() because the backend's current device is thread-local. */
RAD_TRY(rad_dev_set(device_));
RadDeviceProps props{};
if (rad_dev_props(device_, &props) >= 0 && !props.is_host_backend && props.n_cu > 0)
wgp_count_ = props.n_cu;
if (d.stream) {
stream_ = d.stream;
@@ -944,7 +948,7 @@ int Ctx::lane_join(int from, int to) {
* field added to RadBatch changes its size: decide whether the key must read it, then update the
* size here. `agen_b`, `vgen_b` and `sgen_b` are set only on generator passes, which are never
* recorded (below). */
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 416,
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 424,
"RadBatch changed: check that Ctx::pass_key reads every new field a pass depends on");
static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 80,
"RadKVGroupBatch changed: check that Ctx::pass_key reads every new field");
@@ -989,6 +993,11 @@ uint64_t Ctx::pass_key(const RadBatch* b) const {
/* A streamed group's view by the memory it names, never its address (kvstream_view_key). */
mix(kvstream_view_key(k.stream));
}
if (arch_pass_key_) {
const uint64_t geometry = arch_pass_key_(const_cast<Ctx*>(this), b, b->kv_used_max_h);
if (!geometry) return 0;
mix(geometry);
}
mix((uint64_t)level_);
mix(dense_epoch_);
mix(domain_epoch_.load(std::memory_order_relaxed));
+4
View File
@@ -192,6 +192,7 @@ struct CtxDesc {
RadStream stream = nullptr; /* null: the Ctx creates its own compute stream */
ResidencyTable* residency = nullptr; /* null in the all-in-VRAM case */
ArchStepFn arch_step = nullptr; /* the architecture plugin's rad_arch_step */
uint64_t (*arch_pass_key)(RadCtx*, const RadBatch*, const int32_t*) = nullptr;
bool profile_ops = false; /* spec §16, and it changes what you measure */
/* Whether a repeated pass may be recorded and played back. False for a caller whose pass
* issues work the batch does not describe (e.g. a test driving another component through
@@ -256,6 +257,7 @@ public:
}
/* This rank's device ordinal, for the calls that name a card rather than a stream. */
int device() const { return device_; }
int wgp_count() const { return wgp_count_; }
int world_size() const { return world_size_; }
RadStream stream() const { return stream_; }
Program* program() const { return program_; }
@@ -653,6 +655,8 @@ private:
bool staging_ = false; /* the stager armed for the pass being issued */
int issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n);
ArchStepFn arch_step_ = nullptr;
uint64_t (*arch_pass_key_)(RadCtx*, const RadBatch*, const int32_t*) = nullptr;
int wgp_count_ = 32;
/* The expert prefetch's state (xp_state.h), null while it is off. */
std::unique_ptr<XpState> xp_;
+3
View File
@@ -1280,6 +1280,9 @@ int rad_kv_layer_index(RadCtx* c, rad_kvgroup group, int layer) {
}
int rad_rank(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->rank() : 0; }
int rad_world_size(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->world_size() : 1; }
int rad_ctx_wgp_count(RadCtx* c) {
return c ? static_cast<rad::Ctx*>(c)->wgp_count() : 32;
}
RadStream rad_stream(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->stream() : nullptr; }
/* The second lane. `rad_lane` selects the lane subsequent issues launch on; `rad_lane_join`
+6
View File
@@ -245,6 +245,7 @@ int BatchBuilder::alloc_set(Set& s, const std::vector<KVGroupInfo>& groups) {
o.kv.assign(ranks_.size(), std::vector<RadKVGroupBatch>(s.groups.size()));
o.stream.assign(ranks_.size(), std::vector<RadKVStream>(s.groups.size()));
o.batch.assign(ranks_.size(), RadBatch{});
o.kv_used_max_h.assign(s.groups.size(), 0);
o.seqs.clear();
o.seqs.reserve((size_t)max_seqs_); /* a build fills it without allocating */
o.has_seqs = false;
@@ -814,6 +815,10 @@ int BatchBuilder::build_groups(const StepPlan& plan, const IKVManager& kv, Set&
(int)g.sidx_pitch);
}
}
int32_t group_used = 0;
for (int64_t i = 0; i < n_seq; ++i)
group_used = std::max(group_used, g.h_used[i]);
o.kv_used_max_h[gi] = group_used;
/* A state group's rows are one slot per sequence: n_seq words, restaged whole. */
if (!L.paged) {
stage(g.bt_fix, np, n_seq);
@@ -1226,6 +1231,7 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
B.max_q_len_decode = max_q_dec;
B.max_q_len_prefill = max_q_pf;
B.max_ctx_len = (int32_t)ctx_bound;
B.kv_used_max_h = o.kv_used_max_h.data();
B.n_kv_groups = (int)o.kv[r].size();
B.kv = o.kv[r].data();
B.draft_pass = plan.draft_pass;
+1
View File
@@ -265,6 +265,7 @@ private:
/* Per rank and group, the RadKVStream a batch names: each rank's own copy, outside the slab. */
std::vector<std::vector<RadKVStream>> stream;
std::vector<RadBatch> batch;
std::vector<int32_t> kv_used_max_h;
/* The build's sequences in row order (step_seqs); none for an encoder pass. */
std::vector<uint64_t> seqs;
bool has_seqs = false;