Archived
engine: the KV plan split, the host mirror, and --kv-resident bring-up
This commit is contained in:
1 parent
cce687bf2c
commit
a9e627b7ca
44 files changed
+3118
-51
No files matched your search
@@ -0,0 +1,90 @@
|
||||
/* bind.cpp -- see bind.h. */
|
||||
#include "kvstream/bind.h"
|
||||
|
||||
#include "kvstream/plan.h"
|
||||
|
||||
namespace rad {
|
||||
|
||||
void kv_bind_views(const KVGroupPlan& p, KVPoolBinding* b) {
|
||||
if (!b || !p.paged()) return;
|
||||
b->base = p.base;
|
||||
|
||||
b->ring.layer_stride = p.layer_stride();
|
||||
b->ring.bytes = kv_ring_bytes(p, p.pool_blocks());
|
||||
b->ring.depth = 0;
|
||||
b->ring.shape0 = p.pool_blocks();
|
||||
|
||||
b->has_stage = p.depth == 1;
|
||||
if (b->has_stage) {
|
||||
b->stage.layer_stride = p.n_blocks * p.layer_bytes_per_block();
|
||||
b->stage.bytes = b->stage.layer_stride;
|
||||
b->stage.depth = 1;
|
||||
b->stage.shape0 = p.n_blocks;
|
||||
}
|
||||
|
||||
kv_binding_enter(*b, KVView::Ring);
|
||||
}
|
||||
|
||||
int64_t kv_binding_offset(const KVPoolBinding& b, int64_t layer) {
|
||||
return b.depth == 1 ? 0 : layer * b.layer_stride;
|
||||
}
|
||||
|
||||
KVView kv_pass_view(int phase, int64_t n_tok, int64_t n_seq, int n_spec) {
|
||||
/* Not the phase label alone: a nominally decode pass can name more rows a sequence than a
|
||||
* decode step ever does. The serial drafter's history pass (draft_pass -1) runs over the
|
||||
* trunk's hidden states for a whole prefill chunk, so its rows are the chunk's tokens of every
|
||||
* sequence -- a prefill-shaped read whose union is the histories it spans, not one selection a
|
||||
* row. Such a pass issues one selection per row (`resolve_sel` is per query) and its union
|
||||
* cannot be bounded by max_seqs x (1 + n_spec) x sel_pitch, so it must stage. A decode pass
|
||||
* carries at most 1 + n_spec rows a sequence (a verify window) and takes the ring. */
|
||||
if (phase != RAD_PHASE_DECODE) return KVView::Stage;
|
||||
const int64_t rows = (int64_t)n_seq * (1 + (n_spec > 0 ? n_spec : 0));
|
||||
return n_tok > rows ? KVView::Stage : KVView::Ring;
|
||||
}
|
||||
|
||||
void kv_binding_enter(KVPoolBinding& b, KVView view) {
|
||||
const KVViewState& v = (view == KVView::Stage && b.has_stage) ? b.stage : b.ring;
|
||||
b.layer_stride = v.layer_stride;
|
||||
b.bytes = v.bytes;
|
||||
b.depth = v.depth;
|
||||
b.shape[0] = v.shape0;
|
||||
}
|
||||
|
||||
namespace {
|
||||
template <typename T>
|
||||
T* on_rank(const void* base_r, const void* base_0, T* p0) {
|
||||
return p0 ? (T*)((char*)base_r + ((char*)p0 - (char*)base_0)) : nullptr;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
uint64_t kvstream_view_key(const RadKVStream* v) {
|
||||
if (!v) return 0;
|
||||
uint64_t h = 0x9e3779b97f4a7c15ull;
|
||||
auto mix = [&](uint64_t x) {
|
||||
h ^= x + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2);
|
||||
h *= 0xbf58476d1ce4e5b9ull;
|
||||
};
|
||||
auto p = [&](const void* q) { mix((uint64_t)(uintptr_t)q); };
|
||||
p(v->host); p(v->res); p(v->slot_block); p(v->slot_state);
|
||||
p(v->ctl);
|
||||
mix((uint64_t)v->block_bytes); mix((uint64_t)v->host_layer_stride);
|
||||
mix((uint64_t)v->slots); mix((uint64_t)v->host_bytes);
|
||||
return h | 1;
|
||||
}
|
||||
|
||||
const RadKVStream* kvstream_step_view(RadKVStream* out, const RadKVStream* src, bool per_rank,
|
||||
const void* kv0, const void* kvr) {
|
||||
if (!src) return nullptr;
|
||||
*out = *src;
|
||||
/* Rank 0's pointers are the basis; a follower's maps are moved into its own pool by the same
|
||||
* offset its arena buffers take. Passing ring 0 through this leaves every pointer as it was. */
|
||||
if (!per_rank && kv0 && kvr) {
|
||||
out->res = on_rank(kvr, kv0, src->res);
|
||||
out->slot_block = on_rank(kvr, kv0, src->slot_block);
|
||||
out->slot_state = on_rank(kvr, kv0, src->slot_state);
|
||||
out->ctl = on_rank(kvr, kv0, src->ctl);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,71 @@
|
||||
/* bind.h -- the two views of a streamed group's one pool allocation.
|
||||
*
|
||||
* `layer_slot` stays the group-local position of every bound layer, because it is also the host
|
||||
* mirror's row (rad_kv_layer_index -> kv_stream_host). Aliasing is a *depth*: the stage view
|
||||
* reports depth 1, so every layer's operand resolves to offset 0 of the same allocation while the
|
||||
* mirror keeps its per-layer addressing (docs/KV-STREAMING.md §3.5.1).
|
||||
*
|
||||
* A pass selects a view from its phase *and row shape*, before it is recorded or replayed. A
|
||||
* one-row-a-sequence decode step (RAD_PHASE_DECODE within 1 + n_spec rows a sequence) takes the
|
||||
* ring: its selection is rewritten to resident slots and misses are copied. Every other step --
|
||||
* prefill, a mixed prefill+decode step (RAD_PHASE_MIXED), and a decode-classified pass with more
|
||||
* rows a sequence than a decode step can have (the serial drafter's history pass over a chunk) --
|
||||
* takes the stage: the stage is the group's whole context at identity addressing, so a decode row
|
||||
* in a mixed chunk reads its block ids directly, exactly as a prefill row does, and no selection can
|
||||
* overflow a ring. Selecting per pass is visible to the tape because `phase`, `n_tok`, `n_seq` and
|
||||
* `n_spec` are already in the pass key.
|
||||
*/
|
||||
#pragma once
|
||||
#include "mem/kv.h"
|
||||
#include "runtime/ctx.h"
|
||||
|
||||
namespace rad {
|
||||
|
||||
enum class KVView { Ring, Stage };
|
||||
|
||||
/* Fill both fixed views from the plan and make the ring active. Call at bind time, after the
|
||||
* plan's carve has set n_slots, n_blocks and depth. A non-paged group is left untouched. */
|
||||
void kv_bind_views(const KVGroupPlan& p, KVPoolBinding* b);
|
||||
|
||||
/* The offset of a layer's cache in the active view. depth 1 aliases every layer to the pool base;
|
||||
* the `off + layer_stride <= bytes` bound in issue.cpp is what makes that legal, and it is left
|
||||
* exactly as it was. */
|
||||
int64_t kv_binding_offset(const KVPoolBinding& b, int64_t layer);
|
||||
|
||||
/* The view a pass uses. `n_tok`, `n_seq` and `n_spec` are the pass's row shape, not just its
|
||||
* phase: a decode-classified pass with more rows a sequence than a decode step can have (the
|
||||
* serial drafter's history pass over a chunk) reads a prefill-shaped set and must take the stage.
|
||||
* See bind.cpp. */
|
||||
KVView kv_pass_view(int phase, int64_t n_tok, int64_t n_seq, int n_spec);
|
||||
|
||||
/* Make `view` the binding's active view. Stage falls back to the ring when the group has none. */
|
||||
void kv_binding_enter(KVPoolBinding& b, KVView view);
|
||||
|
||||
/* One rank's step view of a group's ring and mirror, as the batch carries it; null when the group
|
||||
* is not streamed. `src` is that rank's own manager view when the caller has per-rank views
|
||||
* (`per_rank`), else rank 0's, whose map pointers are rebased from rank 0's pool (`kv0`) into this
|
||||
* rank's (`kvr`).
|
||||
*
|
||||
* Each rank names its OWN manager's stream view. The device maps live in that rank's KV pool and
|
||||
* the host mirror is that rank's pinned allocation; sharing rank 0's view made two ranks write
|
||||
* different KV shards into the same mirror bytes, which a staged prefill then read back. A caller
|
||||
* that gave no per-rank views (tests) still gets the old offset rebase, so a follower's maps do not
|
||||
* point into its peer's pool.
|
||||
*
|
||||
* The addresses are fixed at bring-up, so a step's view is a copy of pointer values, not a per-step
|
||||
* description; the batch outlives it with the `out` cell it is written into. */
|
||||
const RadKVStream* kvstream_step_view(RadKVStream* out, const RadKVStream* src, bool per_rank,
|
||||
const void* kv0, const void* kvr);
|
||||
|
||||
/* The identity of a step view, for a recorded pass's key (Ctx::pass_key): the memory a kernel reads,
|
||||
* not the address of the struct that carries it. The batch's view is a copy in one of the builder's
|
||||
* Out slots (batch.h kOut, 68 of them with speculation at its maximum), and the slot advances every
|
||||
* step, so keying on `&view` made every streamed decode step a pass the tape store had never seen:
|
||||
* it recorded a new tape each step, the least-recently-used 24 tapes dropped it long before its slot
|
||||
* came round again, and so no streamed pass ever played: the host walked the whole issue path every
|
||||
* step instead of replaying eight packets. The pointers below are fixed at bring-up
|
||||
* (kvstream_step_view copies them), so this is stable across steps and still separates two groups
|
||||
* that hand a kernel different memory. */
|
||||
uint64_t kvstream_view_key(const RadKVStream* v);
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,219 @@
|
||||
/* bringup.cpp -- see bringup.h. */
|
||||
#include "kvstream/bringup.h"
|
||||
|
||||
#include "device/device.h"
|
||||
#include "engine_hostmem.h"
|
||||
#include "kvstream/plan.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
namespace rad {
|
||||
|
||||
/* Every op whose only job is the host mirror or the ring. `kv_store` is not here -- a group that
|
||||
* does not stream uses it -- and neither is the fused `qk_norm_rope_gate_kv_store`, whose streamed
|
||||
* form (below) is a second variant of a fold that already exists without it. */
|
||||
static const char* const kStreamOnlyOps[] = {
|
||||
"kv_store_streamed",
|
||||
"qk_norm_rope_gate_kv_store_streamed",
|
||||
"kv_resolve",
|
||||
};
|
||||
|
||||
bool kvstream_op_gated(const RadBuildCtx& ctx, const char* op) {
|
||||
if (!op) return false;
|
||||
if (ctx.kv_stream_ok != 0 && ctx.kv_resident_blocks > 0) return false; /* a ring is carved */
|
||||
for (const char* s : kStreamOnlyOps)
|
||||
if (std::strcmp(s, op) == 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
int kvstream_configure(const Config& cfg, int64_t ctx, const std::vector<KVGroupInfo>& groups,
|
||||
const Program& prog, KVStreamSetup* out) {
|
||||
*out = KVStreamSetup{};
|
||||
/* --kv-resident is the whole switch: absent or 0 and nothing streams, which is the base
|
||||
* engine's configuration and costs nothing. */
|
||||
if (cfg.kv_resident <= 0) return RAD_OK;
|
||||
/* One group could stream: the paged full-attention group with the largest page. On Flash-Next
|
||||
* that is `kv_attn`; the indexer's `kv_qsa_bkey` is a full group too but a fraction of the
|
||||
* width and `qsa_score` reads it densely over the whole history every step, so it must stay
|
||||
* resident (docs/KV-STREAMING.md §2). The writer maintains the host mirror
|
||||
* (`kv_store_streamed`), so a miss copies a block the engine wrote. A model whose paged groups
|
||||
* are all windowed has nothing a ring could hold back, so a ring on it is not an error: the
|
||||
* flag simply changes nothing. */
|
||||
int32_t g = kv_stream_group(groups, &prog);
|
||||
/* No group has a streamed path, so nothing will stream -- but a ring that would have streamed
|
||||
* still has to name the op the architecture is missing, and the message is written against a
|
||||
* group. Fall back to the old width rule for the report only; kvstream_ops below
|
||||
* then refuses the path exactly as before. */
|
||||
if (g < 0) g = kv_stream_group(groups);
|
||||
if (g < 0) return RAD_OK;
|
||||
const KVGroupInfo& gi = groups[(size_t)g];
|
||||
const int64_t bs = gi.block_size > 0 ? gi.block_size : 1;
|
||||
/* The ring's depth against the group's id space, without deciding anything yet: a ring that
|
||||
* covers the context is not a refusal -- the group stays a plain resident pool, decided here
|
||||
* from --max-num-seqs and --max-model-len, both known long before the first token. */
|
||||
const KVStreamRing shape = kvstream_ring_shape(cfg, ctx, bs);
|
||||
out->asked_tokens = cfg.kv_resident;
|
||||
out->context_tokens = ctx * cfg.max_seqs;
|
||||
out->group_name = gi.name;
|
||||
out->ring_blocks = shape.ring_blocks;
|
||||
out->ring_tokens = shape.ring_blocks * bs;
|
||||
out->block_size = bs;
|
||||
out->ctx_blocks = shape.ctx_blocks;
|
||||
if (!shape.streams()) return RAD_OK;
|
||||
/* Fail safe before anything is allocated: the group the plan wants to stream must have the
|
||||
* ops its streamed path needs, declared by the plugin that emitted this program. Without
|
||||
* kv_resolve a streamed group's `sel` still holds block ids while the attention reads them as
|
||||
* ring slots -- past a ring smaller than the id space, which is a fault or silent corruption,
|
||||
* not a slow path; without kv_store_streamed nothing writes the host mirror, so a miss reads
|
||||
* zeros. Two paths declare both today -- the gated-fp8 attention (arch/common/
|
||||
* rad_block_attn_gated_fp8.h) and GLM-5.3's MLA block (arch/common/rad_block_glm_mla.h),
|
||||
* which resolves at finish after its indexer selects. Ignored with one line, then exactly the
|
||||
* configuration without the flag: no cap, no mirror, no maps, no stage, a null RadKVStream. */
|
||||
const KVStreamOps ops = kvstream_ops(prog, &gi);
|
||||
if (!ops.streams()) {
|
||||
/* Two causes, two sentences. The architecture can declare no streamed path at all (the
|
||||
* gated path emits a plain kv_store and no resolve), or it can declare one the kernel
|
||||
* table has no row for: a packed cache is declared to the core as plain E4M3, so the dtype
|
||||
* gate below cannot see it, and the streamed store's rows are E4M3 and bf16 only. The
|
||||
* second case must not say "the architecture declares no store" -- it did -- and the
|
||||
* operator's answer is the cache layout, not the model. */
|
||||
if ((cfg.kv_cache_dtype == "fp8_v4" || cfg.kv_cache_dtype == "k6_v4") && ops.resolve &&
|
||||
!ops.store && !ops.folded)
|
||||
RAD_WARN("--kv-resident %lld ignored: group '%s' has a %s cache, and the streamed store "
|
||||
"has no writer for that layout (E4M3 and bf16 only), so nothing streams. "
|
||||
"Serving exactly as without --kv-resident: every block of the group stays on "
|
||||
"the card, with no host mirror, no residency map and no stage view",
|
||||
(long long)cfg.kv_resident, gi.name.c_str(), cfg.kv_cache_dtype.c_str());
|
||||
else
|
||||
RAD_WARN("--kv-resident %lld ignored: architecture '%s' declares no %s for group '%s', so "
|
||||
"this model has no streamed path and nothing streams. Serving exactly as without "
|
||||
"--kv-resident: every block of the group stays on the card, with no host mirror, "
|
||||
"no residency map and no stage view",
|
||||
(long long)cfg.kv_resident,
|
||||
prog.meta.arch_id && *prog.meta.arch_id ? prog.meta.arch_id : "(unnamed)",
|
||||
ops.missing(), gi.name.c_str());
|
||||
*out = KVStreamSetup{};
|
||||
return RAD_OK;
|
||||
}
|
||||
/* The streamed store writes the mirror with the cache's own conversion: E4M3 for the fp8 QSA
|
||||
* family, bf16 for the default cache dtype (kv_store_streamed_bf16). Streaming is
|
||||
* dtype-agnostic beyond the two writers -- the mirror and the ring are byte planes of one
|
||||
* paged layout whose pitch is `block_bytes` -- so a bf16 group streams. A packed fp8_v4/k6_v4
|
||||
* group is declared to the core as plain E4M3 and its architecture emits no streamed store for
|
||||
* the layout, so it is warned about and ignored above, never refused here. This gate is the
|
||||
* last line for a group whose core dtype is neither of the two -- f32 or f16, which no
|
||||
* architecture ships a full group in today. Checked only where something streams, so a
|
||||
* configuration that fits is never refused for it. */
|
||||
if (gi.decl.dtype != RAD_F8E4M3 && gi.decl.dtype != RAD_BF16) {
|
||||
RAD_ERR("--kv-resident %lld: group '%s' has a %s cache, and the streamed store has E4M3 "
|
||||
"and bf16 rows only (--kv-cache-dtype fp8 or bf16); a packed cache is a later "
|
||||
"unit's row. Leave --kv-resident off to keep every block on the card",
|
||||
(long long)cfg.kv_resident, out->group_name.c_str(),
|
||||
rad_dtype_name(gi.decl.dtype));
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
/* The ring's depth: a ring below what one decode step can select is raised to it, with the one
|
||||
* line that says what was asked, what it became and why. */
|
||||
KVStreamRing ring;
|
||||
kvstream_ring(cfg, ctx, bs, ops.sel_pitch, &ring);
|
||||
if (ring.raised_blocks > 0)
|
||||
/* Anything under the computed amount is ignored and pulled up to it, the way Strata sizes
|
||||
* this: the engine knows what a decode step names, and --kv-resident is a request for
|
||||
* streaming rather than for one number. A warning, so startup proceeds -- and a different
|
||||
* sentence from the ignored-flag one above, because this model does stream. */
|
||||
RAD_WARN("--kv-resident %lld raised to %lld tokens: a decode step can name %lld blocks of "
|
||||
"%lld blocks each (--max-num-seqs %lld x %lld for speculation x %lld), and the ring "
|
||||
"must hold them all; the ring takes the raised depth and startup proceeds",
|
||||
(long long)out->asked_tokens, (long long)(ring.ring_blocks * bs),
|
||||
(long long)ring.raised_blocks, (long long)bs,
|
||||
(long long)(cfg.max_seqs > 0 ? cfg.max_seqs : 1),
|
||||
(long long)(1 + (cfg.n_spec > 0 ? cfg.n_spec : 0)), (long long)ops.sel_pitch);
|
||||
/* The raised ring may be the whole context, which is not a streamed group at all: it keeps
|
||||
* every block on the card and the report says which context the ring covers. */
|
||||
out->ring_blocks = ring.ring_blocks;
|
||||
out->ring_tokens = ring.ring_blocks * bs;
|
||||
out->raised_tokens = ring.raised_blocks > 0 ? ring.ring_blocks * bs : 0;
|
||||
out->group = ring.streams() ? g : -1;
|
||||
/* Prefill runs on the stage and never resolves: above `qsa_exact_to` one launch's selection is
|
||||
* a chunk's union, which no serving ring could hold, so the pass gets the group's context one
|
||||
* layer deep, addressed by block id. A within-(1+n_spec) decode pass takes the ring and
|
||||
* resolves; a decode-classified pass with more rows a sequence than that (the serial drafter's
|
||||
* history pass over a chunk) is prefill-shaped and stages, because its union is the histories it
|
||||
* spans. */
|
||||
out->stage_blocks = ring.streams() ? ring.ctx_blocks : 0;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
void kvstream_host_add(KVStreamHostUse* use, const KVManager& kv) {
|
||||
const KVStreamCost c = kv.stream_cost();
|
||||
use->sum.groups += c.groups;
|
||||
use->sum.host_bytes += c.host_bytes;
|
||||
use->sum.map_bytes += c.map_bytes;
|
||||
use->max_rank_bytes = std::max(use->max_rank_bytes, c.host_bytes);
|
||||
}
|
||||
|
||||
int kvstream_host_gate(const KVStreamHostUse& use, int n_ranks, int64_t still, int64_t headroom) {
|
||||
if (use.sum.host_bytes <= 0) return RAD_OK;
|
||||
std::string why;
|
||||
if (lockable_room(use.sum.host_bytes, still, headroom, false, &why)) return RAD_OK;
|
||||
RAD_ERR("kv streaming: the host mirror for %lld streamed KV group(s) is up to %s a rank "
|
||||
"(%s over %d ranks) of pinned host memory, and %s. It is sized by "
|
||||
"--max-model-len and --max-num-seqs (one full context a sequence), not by "
|
||||
"--kv-resident, which sizes the card's ring. Lower one of the first two, or "
|
||||
"--kv-resident 0 to keep every block on the card. --host-pool-mib is the "
|
||||
"experts' tier and holding less of it does not make room for this one.",
|
||||
(long long)use.sum.groups, humanb(use.max_rank_bytes).c_str(),
|
||||
humanb(use.sum.host_bytes).c_str(), n_ranks, why.c_str());
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
|
||||
int kvstream_fill(KVManager& kv) {
|
||||
/* After the carve, which is where an elastic pool backs its range: a fill before that
|
||||
* would touch pages the card has not been given. One bring-up stream, drained before
|
||||
* the first pass, since the maps are read from the first step on. */
|
||||
RadStream st = nullptr;
|
||||
int rc = rad_stream_create(&st, 0);
|
||||
if (rc == RAD_OK) rc = kv.init_stream(st);
|
||||
if (rc == RAD_OK) rc = rad_stream_sync(st);
|
||||
rad_stream_destroy(st);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void kvstream_report(const KVStreamSetup& s, const KVStreamHostUse& host, int n_ranks) {
|
||||
/* The flag is not the ring when the ceiling raised it, and this report is what people read
|
||||
* afterwards: the flag's number, the ring's, and why, before anything below quotes the ring. */
|
||||
const int64_t flag = s.raised_tokens > 0 ? s.raised_tokens : s.asked_tokens;
|
||||
if (s.raised_tokens > 0 && s.block_size > 0)
|
||||
RAD_INFO("kv streaming: the ring is the raised one, not the flag's: --kv-resident %lld "
|
||||
"became %lld tokens (%lld -> %lld blocks of %lld tokens each), the fewest blocks "
|
||||
"one decode step's selection fits in; what was carved below is that ring",
|
||||
(long long)s.asked_tokens, (long long)s.raised_tokens,
|
||||
(long long)((s.asked_tokens + s.block_size - 1) / s.block_size),
|
||||
(long long)(s.raised_tokens / s.block_size), (long long)s.block_size);
|
||||
if (host.sum.groups > 0)
|
||||
RAD_INFO("kv streaming: %lld group(s) stream (%s): kv_store_streamed writes the host "
|
||||
"mirror and the resident ring slots, kv_resolve rewrites each selection as ring "
|
||||
"slots, up to %s of pinned host mirror a rank (%s over %d), %s of ring maps in "
|
||||
"the KV pools; the pool is one reservation of max(ring, stage), with the ring "
|
||||
"sized by --kv-resident",
|
||||
(long long)host.sum.groups, s.group_name.c_str(),
|
||||
humanb(host.max_rank_bytes).c_str(), humanb(host.sum.host_bytes).c_str(), n_ranks,
|
||||
humanb(host.sum.map_bytes).c_str());
|
||||
if (s.stage_blocks > 0)
|
||||
RAD_INFO("kv streaming: prefill staging, group '%s' ring %lld blocks, one-layer stage %lld "
|
||||
"blocks (one layer of its %lld-block context): a prefill or mixed step, and a decode "
|
||||
"pass with more rows a sequence than a decode step can have, stages each layer and "
|
||||
"never resolves; only a within-(1+n_spec) decode pass uses the ring",
|
||||
s.group_name.c_str(), (long long)s.ring_blocks,
|
||||
(long long)s.stage_blocks, (long long)s.ctx_blocks);
|
||||
if (s.group < 0 && s.asked_tokens > 0 && s.ctx_blocks > 0)
|
||||
RAD_INFO("kv streaming: nothing streams: --kv-resident %lld covers group '%s''s "
|
||||
"%lld-token context (%lld blocks), so every block keeps its own slot and the "
|
||||
"group runs the engine's ordinary resident path -- no host mirror, no residency "
|
||||
"map, no stage and no resolve. Lower --kv-resident below %lld to stream it",
|
||||
(long long)flag, s.group_name.c_str(), (long long)s.context_tokens,
|
||||
(long long)s.ctx_blocks, (long long)s.context_tokens);
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,107 @@
|
||||
/* bringup.h -- the engine-side bring-up of a streamed group (docs/KV-STREAMING.md §4.1, §5 Phase 4).
|
||||
*
|
||||
* `--kv-resident N` is the whole switch: absent or 0 and nothing streams, and a ring that covers
|
||||
* the group's context streams nothing either, because such a group keeps the base engine's path
|
||||
* with no overhead at all. Which group streams, the two refusals (a model with no full group, a
|
||||
* cache with no streamed store row), the ring's ceiling, the cap that narrows every rank's pool,
|
||||
* the host-RAM gate for the mirrors and the startup report all live here, so
|
||||
* core/engine_bringup.cpp keeps one call at each seam. Nothing touches a device except fill(),
|
||||
* which creates and drains the bring-up stream of the maps the carve has just placed.
|
||||
*/
|
||||
#pragma once
|
||||
#include "mem/kv.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rad {
|
||||
|
||||
struct Config;
|
||||
|
||||
/* The plan --kv-resident resolves to, as Engine::configure_kv carries it to the report. `group` is
|
||||
* -1 when nothing streams, which is every server without the flag and also a server whose ring
|
||||
* covers the group's context; the other fields then describe what was asked for and why nothing
|
||||
* streams, which is all the report needs. */
|
||||
struct KVStreamSetup {
|
||||
int32_t group = -1; /* the group that streams; -1 when nothing does */
|
||||
std::string group_name; /* the group that would have: named by the report */
|
||||
int64_t asked_tokens = 0; /* --kv-resident as given; 0 is no streaming at all */
|
||||
int64_t raised_tokens = 0; /* what the flag became when the ring's ceiling raised
|
||||
* it; 0 when it was not raised */
|
||||
int64_t context_tokens = 0; /* --max-model-len x --max-num-seqs */
|
||||
int64_t ring_tokens = 0; /* the ring that is carved, in tokens: the raised flag,
|
||||
* rounded to whole blocks */
|
||||
int64_t ring_blocks = 0; /* the ring's depth, in blocks */
|
||||
int64_t block_size = 0; /* the group's page, in tokens: the ring's block */
|
||||
int64_t ctx_blocks = 0; /* the group's id space, in blocks */
|
||||
int64_t stage_blocks = 0; /* the stage view's depth, in blocks; 0 when there is none */
|
||||
};
|
||||
|
||||
/* The host tier the mirrors take, summed over the ranks. `host_bytes` is every rank's pinned
|
||||
* mirror; `max_rank_bytes` is the largest, which is the figure an operator acts on (an uneven
|
||||
* tensor-parallel split gives the ranks different shard sizes). */
|
||||
struct KVStreamHostUse {
|
||||
KVStreamCost sum;
|
||||
int64_t max_rank_bytes = 0;
|
||||
};
|
||||
|
||||
/* Whether `op` must not be declared at all (it is not an error: the architecture takes its
|
||||
* non-streaming path). The ops in kStreamOnlyOps (bringup.cpp) exist only to
|
||||
* move KV between host memory and a ring, so a run that carves no ring must not have them: their
|
||||
* declaration alone changes the graph, the arena and which kernels the planner may choose, and an
|
||||
* operator who never asks for streaming must get exactly the build they had before it existed.
|
||||
* True only when the architecture says it streams (RadArchProbe::kv_stream_ok, carried in
|
||||
* RadBuildCtx::kv_stream_ok) AND --kv-resident asked for a ring. One binary serves both runs, so
|
||||
* this is where the two questions meet; every architecture's `rad_op_declared` falls through to
|
||||
* its non-streaming path by itself, and no model file carries the flag. */
|
||||
bool kvstream_op_gated(const RadBuildCtx& ctx, const char* op);
|
||||
|
||||
/* Read --kv-resident, choose the group from rank 0's declarations, decide whether the ring covers
|
||||
* its context (nothing streams then), refuse a cache with no streamed store row, apply the ring's
|
||||
* ceiling (kvstream_ring, with `sel_pitch` read off the declared kv_resolve) and fill `out`.
|
||||
* RAD_OK with group -1 when nothing streams, which is the ordinary configuration and caps nothing.
|
||||
*
|
||||
* The ceiling raises the ring when the flag asks for a ring smaller than one decode step's
|
||||
* selection, and says so in one line: a warning, not a refusal, because the ring is a size the
|
||||
* engine computes and the operator asked for streaming. `out->asked_tokens` and
|
||||
* `out->raised_tokens` keep what the operator typed and what the ring became, for the report;
|
||||
* `--kv-resident` itself is left as given, so nothing else re-reads a number the operator did not
|
||||
* write.
|
||||
*
|
||||
* Fail-safe: when the architecture declares no streamed ops for the group the plan would stream
|
||||
* (kvstream_ops -- no kv_resolve, or no streamed store), the flag is ignored with one warning that
|
||||
* names the architecture and the missing op, and `out` is left exactly as if --kv-resident had not
|
||||
* been given: group -1, so no ring is capped, no mirror or residency map is taken, no stage view is
|
||||
* laid and the batch hands the arch no RadKVStream. That is not the raised ring above: an
|
||||
* architecture without a streamed path cannot be fixed by the operator, while a ring under the
|
||||
* ceiling is fixed here. A cache layout the streamed store has no writer for (--kv-cache-dtype
|
||||
* fp8_v4 / k6_v4, whose architecture declares no streamed store for it) takes the same
|
||||
* warn-and-ignore, with a sentence that names the layout rather than the architecture. Where the
|
||||
* ops are there, the E4M3/bf16 gate still applies: the streamed store has rows for those two only,
|
||||
* so a full group in any other core dtype would have no writer at all. */
|
||||
int kvstream_configure(const Config& cfg, int64_t ctx, const std::vector<KVGroupInfo>& groups,
|
||||
const Program& prog, KVStreamSetup* out);
|
||||
|
||||
/* Accumulate one rank's stream_cost() into the host tier's total. */
|
||||
void kvstream_host_add(KVStreamHostUse* use, const KVManager& kv);
|
||||
|
||||
/* The host-RAM gate for the mirrors: a streamed group's mirror is `max_seqs x max_ctx` of its KV
|
||||
* per rank -- on Flash-Next at 262144 tokens and 2 sequences that is 3.3 GiB a rank, the largest
|
||||
* host allocation in the engine after the experts' pool. Checked once, before any rank takes it,
|
||||
* against everything still to be pinned after it (`still`: the prefix cache's host tier, the
|
||||
* activation arenas, the checkpoint store when its size is already fixed). The card side is the
|
||||
* ring, which carve() sizes from --kv-resident; a refusal names this tier and the flag that started
|
||||
* streaming, because telling an operator to lower --host-pool-mib would be about the wrong pool:
|
||||
* that one holds experts, and shrinking it cannot make room here. */
|
||||
int kvstream_host_gate(const KVStreamHostUse& use, int n_ranks, int64_t still, int64_t headroom);
|
||||
|
||||
/* After the carve: fill the maps on a bring-up stream and drain it. */
|
||||
int kvstream_fill(KVManager& kv);
|
||||
|
||||
/* The startup lines: the raised ring when the ceiling moved it (the flag is no longer what was
|
||||
* carved, and the report is what people read afterwards), which group streams and what it took on
|
||||
* both tiers, the stage, and -- when the flag was given but nothing streams -- which group's
|
||||
* context the ring covers. */
|
||||
void kvstream_report(const KVStreamSetup& s, const KVStreamHostUse& host, int n_ranks);
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,180 @@
|
||||
/* carve.cpp -- see carve.h. */
|
||||
#include "kvstream/carve.h"
|
||||
|
||||
#include "kvstream/mirror.h"
|
||||
#include "kvstream/plan.h"
|
||||
#include "mem/kv.h"
|
||||
|
||||
namespace rad {
|
||||
|
||||
int64_t kvstream_maps_charged(const std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t max_ctx, int64_t max_seqs) {
|
||||
/* One loop for the budget's term and the report's size: KVStreamStore::cost() is the predicate
|
||||
* alloc() uses and the arithmetic kvstream_map_bytes states, so the two cannot drift. */
|
||||
return st.cost(plans, max_ctx, max_seqs).map_bytes;
|
||||
}
|
||||
|
||||
bool kvstream_dense(const KVGroupPlan& p, const std::vector<KVGroupPlan>& plans,
|
||||
const KVStreamStore& st) {
|
||||
if (!p.paged() || p.zero || p.kind != RAD_KV_FULL || !p.dense_read) return false;
|
||||
for (const auto& q : plans)
|
||||
if (!q.zero && q.paged() && st.windowed(q.index)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t kvstream_dense_blocks(const KVGroupPlan& p, int64_t max_ctx, int64_t max_seqs) {
|
||||
if (p.block_size <= 0 || max_ctx <= 0) return 0;
|
||||
const int64_t seqs = max_seqs > 0 ? max_seqs : 1;
|
||||
return seqs * ((max_ctx + p.block_size - 1) / p.block_size);
|
||||
}
|
||||
|
||||
int64_t kvstream_dense_charged(const std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t max_ctx, int64_t max_seqs) {
|
||||
int64_t bytes = 0;
|
||||
for (const auto& p : plans) {
|
||||
if (!kvstream_dense(p, plans, st)) continue;
|
||||
bytes += align_up(kvstream_dense_blocks(p, max_ctx, max_seqs) * p.bytes_per_block,
|
||||
RAD_ALIGN_UNIT);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
void kvstream_split_windows(const std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t max_ctx, int64_t max_seqs, int64_t* remaining,
|
||||
std::vector<int64_t>* out) {
|
||||
for (const auto& p : plans) {
|
||||
if (!p.paged() || p.zero || !st.windowed(p.index)) continue;
|
||||
const int64_t slots = st.resident_blocks(p);
|
||||
*remaining -= kv_pool_bytes(p, slots, st.context_blocks(p, max_ctx, max_seqs));
|
||||
(*out)[p.index] = slots;
|
||||
}
|
||||
/* The maps are the rest of the arrangement. A probe that sizes the pool must pay them even
|
||||
* though it cannot reserve them yet, or the pool it chooses is short by exactly their bytes and
|
||||
* the first resident group carved against it falls short of --max-model-len. */
|
||||
*remaining -= kvstream_maps_charged(plans, st, max_ctx, max_seqs);
|
||||
/* A dense-read group does not take a share of the split: its requirement is every sequence's
|
||||
* context, which is a floor fixed by the run configuration. Charging it here (and leaving it
|
||||
* out of the shared split in paged_split) is what keeps the sibling's host mirror from
|
||||
* promising max_seqs sequences that the indexer cannot address. */
|
||||
for (const auto& p : plans) {
|
||||
if (!kvstream_dense(p, plans, st)) continue;
|
||||
const int64_t blocks = kvstream_dense_blocks(p, max_ctx, max_seqs);
|
||||
*remaining -= align_up(blocks * p.bytes_per_block, RAD_ALIGN_UNIT);
|
||||
(*out)[p.index] = blocks;
|
||||
}
|
||||
}
|
||||
|
||||
int kvstream_window_fit(const std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t fixed, int64_t budget, int64_t max_ctx, int64_t max_seqs) {
|
||||
int64_t window_bytes = 0;
|
||||
int64_t window_groups = 0;
|
||||
for (const auto& p : plans) {
|
||||
if (!p.paged() || p.zero || !st.windowed(p.index)) continue;
|
||||
/* A window group already bounds its own history: the ids it allocates are its window's,
|
||||
* while a streamed group's ids are its whole context. The two cannot both be true, and
|
||||
* context_blocks() would size the table of the first as if it were the second. */
|
||||
if (p.kind != RAD_KV_FULL) {
|
||||
RAD_ERR("KV group '%s' is a %s group and streaming is set for it: only a full "
|
||||
"attention group streams. A window already bounds its own history, so the ids "
|
||||
"it allocates are its window's, and a streamed group's ids are its whole "
|
||||
"context -- the residency map cannot be right for both. Leave this group "
|
||||
"unstreamed (--kv-resident 0 for it).",
|
||||
p.name.c_str(), kv_kind_name(p.kind));
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
window_bytes += kv_pool_bytes(p, st.resident_blocks(p),
|
||||
st.context_blocks(p, max_ctx, max_seqs));
|
||||
++window_groups;
|
||||
}
|
||||
const int64_t map_bytes = kvstream_maps_charged(plans, st, max_ctx, max_seqs);
|
||||
const int64_t dense_bytes = kvstream_dense_charged(plans, st, max_ctx, max_seqs);
|
||||
int64_t dense_groups = 0;
|
||||
for (const auto& p : plans) if (kvstream_dense(p, plans, st)) ++dense_groups;
|
||||
if (fixed + window_bytes + map_bytes + dense_bytes > budget) {
|
||||
RAD_ERR("the resident KV windows and dense-read groups do not fit vram_kv:\n"
|
||||
" resident windows %10s (%lld group(s) streaming)\n"
|
||||
" ring maps %10s (the streamed groups' residency maps)\n"
|
||||
" dense-read groups %10s (%lld group(s) at %lld sequences x %lld tokens)\n"
|
||||
" linear/conv state %10s (%lld sequences)\n"
|
||||
" vram_kv %10s\n"
|
||||
" short by %10s\n"
|
||||
"Lower --kv-resident (the ring), or --max-model-len / --max-num-seqs (the one-layer "
|
||||
"stage and a dense-read group's floor are sized from them), or give the cache more "
|
||||
"of the card: a smaller --gpu-headroom-mib or a larger --host-pool-mib. The fixed "
|
||||
"costs in this table are not what is short: it is a streamed group's ring and "
|
||||
"stage, or the indexer beside it that must hold every sequence's context.",
|
||||
humanb(window_bytes).c_str(), (long long)window_groups,
|
||||
humanb(map_bytes).c_str(), humanb(dense_bytes).c_str(), (long long)dense_groups,
|
||||
(long long)max_seqs, (long long)max_ctx, humanb(fixed).c_str(),
|
||||
(long long)max_seqs, humanb(budget).c_str(),
|
||||
humanb(fixed + window_bytes + map_bytes + dense_bytes - budget).c_str());
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int kvstream_apply(std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t max_ctx, int64_t max_seqs) {
|
||||
for (auto& p : plans) {
|
||||
if (!p.paged()) continue;
|
||||
if (st.windowed(p.index) && !p.zero) {
|
||||
/* A streamed group's ids run over its context, which lives in host memory; the pool
|
||||
* backs only its resident window. So the split above gave this group its window (the
|
||||
* one number the pool may spend), and the context comes from the run configuration.
|
||||
* A window wider than the context is the context: there is nothing to stream. */
|
||||
p.n_blocks = st.context_blocks(p, max_ctx, max_seqs);
|
||||
p.n_slots = st.resident_blocks(p);
|
||||
/* Only a ring smaller than the context needs the second, one-layer view. A ring that
|
||||
* covers the context streams nothing and never reaches this cap (kvstream_ring), so a
|
||||
* windowed group here always has the stage. */
|
||||
p.depth = p.streamed() ? 1 : 0;
|
||||
} else {
|
||||
/* Not streamed: it addresses exactly the blocks it carves. */
|
||||
p.n_slots = p.n_blocks;
|
||||
}
|
||||
if (p.n_slots > p.n_blocks) p.n_slots = p.n_blocks;
|
||||
/* Streaming wins over replication for one group, and the resolution is not an error
|
||||
* because the two claims are about different stores. rad_kv_group_replicated says a tier
|
||||
* slot holds the group once; a streamed group has no tier entry at all (kv_tier_cached) --
|
||||
* its host mirror is the tier -- so the flag would only misdescribe the slot. A plugin
|
||||
* declares replication because the arithmetic of the group is the same on every rank (an
|
||||
* MLA latent), which stays true while it streams. */
|
||||
if (p.streamed()) p.replicated = false;
|
||||
/* A streamed group needs somewhere for the blocks the pool does not back. Refused here,
|
||||
* not at the first attention read: an unbacked id is written past the layer stride and the
|
||||
* result is fluent wrong output with no way to bisect it (docs/KV-STREAMING.md §3.1).
|
||||
* RAD_E_INVAL, not RAD_E_STATE: RAD_E_STATE stringifies as "called in the wrong phase",
|
||||
* and this is a caller handing the manager a cap without the backing for it. */
|
||||
if (p.streamed()) {
|
||||
const KVStreamStore::Map* m = st.map(p.index);
|
||||
const int64_t need = p.n_blocks * p.bytes_per_block;
|
||||
if (!m || !m->host || !m->res || m->host_bytes < need) {
|
||||
RAD_ERR("KV group '%s' streams -- %lld of its %lld blocks resident -- but nothing "
|
||||
"holds the other blocks: a streamed group needs a host copy of %s and a "
|
||||
"residency map before it is carved. This is a wiring bug, not a flag: "
|
||||
"--kv-resident is set only where the engine also allocates both.",
|
||||
p.name.c_str(), (long long)p.n_slots, (long long)p.n_blocks,
|
||||
humanb(need).c_str());
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
std::string kvstream_report_line(const KVGroupPlan& p) {
|
||||
/* A streamed group's pool is smaller than its context, so the two numbers differ and the
|
||||
* group's own line would read as a pool that holds the whole context. Both tiers in one line:
|
||||
* the ring and where it lives (the pool, at its offset), and the host mirror the misses are
|
||||
* copied from. The maps are the group's own small device allocation, taken from the same pool,
|
||||
* so they are named here too rather than being invisible. */
|
||||
return fmt(" %-14s %-6s streaming: %7lld blocks resident in vram_kv at "
|
||||
"+%lld = %9s, %7lld blocks = %9s mirrored in pinned host memory, "
|
||||
"maps %s\n", "", "", (long long)p.pool_blocks(),
|
||||
(long long)p.pool_offset, humanb(p.pool_bytes).c_str(),
|
||||
(long long)p.n_blocks,
|
||||
humanb(p.n_blocks * p.bytes_per_block).c_str(),
|
||||
humanb(kvstream_map_bytes(p.n_blocks, p.pool_blocks(), p.n_layers)).c_str());
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,76 @@
|
||||
/* carve.h -- what a carve does with a streamed group (docs/KV-STREAMING.md §4.1, §4.2).
|
||||
*
|
||||
* carve() spends a pool against the plan, and three of its steps are streaming's: the resident
|
||||
* windows leave the budget before the other groups split the rest; after the split each paged
|
||||
* group's n_slots (and a streamed group's context and depth) is written, with a refusal if nothing
|
||||
* holds the blocks the pool does not back; and the pool report gains the line naming both tiers.
|
||||
* Those three live here, so mem/kv.cpp keeps one call at each seam. Pure arithmetic on the plan:
|
||||
* nothing here touches a device.
|
||||
*/
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rad {
|
||||
|
||||
struct KVGroupPlan;
|
||||
class KVStreamStore;
|
||||
|
||||
/* Bytes a streamed group's residency maps take from the KV pool: exactly what alloc() reserves,
|
||||
* as KVStreamStore::cost() states it. They are part of the group's arrangement, not a side
|
||||
* allocation: the maps are reserved before carve() (KVStreamStore::alloc), so the budget that
|
||||
* sizes the pool must charge them. The budget probe has no pool and cannot reserve them, while
|
||||
* the carve has already taken them; charging them in paged_split() and adding them back to the
|
||||
* carve's budget is what makes both paths see the same bytes (docs/KV-STREAMING.md §3.9). */
|
||||
int64_t kvstream_maps_charged(const std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t max_ctx, int64_t max_seqs);
|
||||
|
||||
/* A dense-read group (rad_kv_group_dense_read: an indexer's block keys) is walked over its whole
|
||||
* live history every decode step, so it cannot stream. While any group does stream, its pool must
|
||||
* hold every sequence's context: the host mirror beside it is sized for max_seqs sequences, and an
|
||||
* indexer funded for one would make the advertised capacity a promise the engine cannot keep. False
|
||||
* when nothing streams -- that configuration deliberately sizes the cache for one full-length
|
||||
* sequence, and charging the floor there would take the experts' VRAM for capacity nothing uses. */
|
||||
bool kvstream_dense(const KVGroupPlan& p, const std::vector<KVGroupPlan>& plans,
|
||||
const KVStreamStore& st);
|
||||
|
||||
/* Blocks a dense-read group must hold: max_seqs sequences, each ceil(max_ctx / block_size)
|
||||
* blocks long. 0 for a group kvstream_dense() does not select. */
|
||||
int64_t kvstream_dense_blocks(const KVGroupPlan& p, int64_t max_ctx, int64_t max_seqs);
|
||||
|
||||
/* The bytes those blocks take from the pool, rounded as reserve() rounds them. */
|
||||
int64_t kvstream_dense_charged(const std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t max_ctx, int64_t max_seqs);
|
||||
|
||||
/* Take the streamed groups' resident windows, their maps and the dense-read groups' full contexts
|
||||
* out of a paged split before the other groups compete for the pool: `remaining` loses their bytes
|
||||
* and `out[group]` becomes the ring's depth (or the dense-read group's context) in blocks. A streamed group does not compete for the pool: its device share is its resident
|
||||
* window, fixed by --kv-resident, and the blocks it addresses are its context, which lives in host
|
||||
* memory. The caller leaves those groups out of the split and reports this as their carved blocks
|
||||
* -- `carve_into` reads it as the pool's depth, while the group's context comes from the run
|
||||
* configuration. */
|
||||
void kvstream_split_windows(const std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t max_ctx, int64_t max_seqs, int64_t* remaining,
|
||||
std::vector<int64_t>* out);
|
||||
|
||||
/* Refuse a resident window that does not fit `budget`, before anything is split. A streamed group's
|
||||
* window leaves the budget before the other groups split the rest, so a window that does not fit
|
||||
* silently leaves them zero blocks and they are refused for a pool too small -- an operator told to
|
||||
* raise --host-pool-mib when the term that is too large is --kv-resident, and who then tunes the
|
||||
* wrong one. Refuse the window itself instead. The fixed costs are their own term and were checked
|
||||
* by the caller. A window group is refused by name here too: it already bounds its own history. */
|
||||
int kvstream_window_fit(const std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t fixed, int64_t budget, int64_t max_ctx, int64_t max_seqs);
|
||||
|
||||
/* After the split: write every paged group's n_slots, which is its carve unless it streams, and for
|
||||
* a streamed group its n_blocks as the context and its depth. Refuse a streamed group whose host
|
||||
* copy or residency map is missing: an unbacked id is written past the layer stride. */
|
||||
int kvstream_apply(std::vector<KVGroupPlan>& plans, const KVStreamStore& st,
|
||||
int64_t max_ctx, int64_t max_seqs);
|
||||
|
||||
/* The report continuation for a streamed group: its pool is smaller than its context, so the
|
||||
* group's own line would read as a pool that holds the whole context. Both tiers in one line. */
|
||||
std::string kvstream_report_line(const KVGroupPlan& p);
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,218 @@
|
||||
/* mirror.cpp -- see mirror.h. */
|
||||
#include "kvstream/mirror.h"
|
||||
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "kvstream/plan.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
namespace rad {
|
||||
|
||||
namespace {
|
||||
int64_t ceil_div(int64_t a, int64_t b) { return b > 0 ? (a + b - 1) / b : 0; }
|
||||
|
||||
/* Each array starts on an 8-byte boundary: the ABI's `ctl` counters are read as u64 pairs, and a
|
||||
* block count or a ring depth that is odd would leave a 4-byte boundary misaligned. The pool's base
|
||||
* is RAD_ALIGN_UNIT (4096) aligned, so rounding every part keeps the absolute addresses aligned. */
|
||||
int64_t align8(int64_t n) { return (n + 7) & ~(int64_t)7; }
|
||||
|
||||
/* The reservation's one layout: the bytes of each array and its offset from the reservation's
|
||||
* base. alloc() cuts the reservation from it, init() resets each part at its own length, and
|
||||
* kvstream_map_bytes() is its total, so the budget, the carve and the reset cannot disagree. */
|
||||
struct MapLayout {
|
||||
int64_t res_bytes = 0, slot_block_bytes = 0, slot_state_bytes = 0, ctl_bytes = 0;
|
||||
int64_t res = 0, slot_block = 0, slot_state = 0, ctl = 0;
|
||||
int64_t bytes = 0; /* the whole reservation */
|
||||
};
|
||||
|
||||
MapLayout map_layout(int64_t blocks, int64_t slots, int64_t layers) {
|
||||
if (layers < 1) layers = 1;
|
||||
MapLayout l;
|
||||
l.res_bytes = layers * blocks * (int64_t)sizeof(int32_t);
|
||||
l.slot_block_bytes = layers * slots * (int64_t)sizeof(int32_t);
|
||||
l.slot_state_bytes = layers * slots * 2 * (int64_t)sizeof(int32_t);
|
||||
l.ctl_bytes = 16 * (int64_t)sizeof(int32_t);
|
||||
l.res = 0; /* res, block -> slot, one map a layer */
|
||||
l.slot_block = l.res + align8(l.res_bytes); /* slot_block, slot -> block */
|
||||
l.slot_state = l.slot_block + align8(l.slot_block_bytes);/* slot_state: epoch, heat */
|
||||
l.ctl = l.slot_state + align8(l.slot_state_bytes);/* ctl, shared by the layers */
|
||||
l.bytes = l.ctl + align8(l.ctl_bytes);
|
||||
return l;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
int64_t kvstream_map_bytes(int64_t blocks, int64_t slots, int64_t layers) {
|
||||
return map_layout(blocks, slots, layers).bytes;
|
||||
}
|
||||
|
||||
void KVStreamStore::cap(int32_t group, int64_t tokens) {
|
||||
if (group < 0) return;
|
||||
if ((size_t)group >= resident_tokens_.size()) resident_tokens_.resize((size_t)group + 1, 0);
|
||||
resident_tokens_[(size_t)group] = tokens > 0 ? tokens : 0;
|
||||
}
|
||||
|
||||
bool KVStreamStore::windowed(int32_t group) const {
|
||||
return group >= 0 && (size_t)group < resident_tokens_.size() &&
|
||||
resident_tokens_[(size_t)group] > 0;
|
||||
}
|
||||
|
||||
int64_t KVStreamStore::resident_blocks(const KVGroupPlan& p) const {
|
||||
const size_t g = (size_t)p.index;
|
||||
if (g >= resident_tokens_.size() || resident_tokens_[g] <= 0 || p.block_size <= 0) return 0;
|
||||
return ceil_div(resident_tokens_[g], p.block_size);
|
||||
}
|
||||
|
||||
int64_t KVStreamStore::context_blocks(const KVGroupPlan& p, int64_t max_ctx,
|
||||
int64_t max_seqs) const {
|
||||
/* One spelling of the id space (kvstream/plan.h): the startup ring decision uses the same
|
||||
* arithmetic, so a ring that covers ctx_blocks covers what the store sizes. */
|
||||
return kv_context_blocks(p.block_size, max_ctx, max_seqs);
|
||||
}
|
||||
|
||||
void KVStreamStore::set_map(int32_t group, const Map& m) {
|
||||
if (group < 0) return;
|
||||
if ((size_t)group >= maps_.size()) maps_.resize((size_t)group + 1);
|
||||
maps_[(size_t)group] = m;
|
||||
}
|
||||
|
||||
const KVStreamStore::Map* KVStreamStore::map(int32_t group) const {
|
||||
if (group < 0 || (size_t)group >= maps_.size()) return nullptr;
|
||||
return &maps_[(size_t)group];
|
||||
}
|
||||
|
||||
const RadKVStream* KVStreamStore::view(const KVGroupPlan* p, int32_t group) const {
|
||||
/* A group whose pool backs only a window of its context, and nothing else: a ring that covers
|
||||
* the context never reaches here (kvstream_ring leaves `group` at -1 and nothing is capped), so
|
||||
* such a group is not a streamed one and gets the base engine's null -- no resolve, no stage,
|
||||
* no mirror read (docs/KV-STREAMING.md §5 Phase 4). */
|
||||
if (!p || !p->streamed() || !windowed(group)) return nullptr;
|
||||
const Map* m = map(group);
|
||||
return m && m->host && m->res ? m : nullptr;
|
||||
}
|
||||
|
||||
KVStreamCost KVStreamStore::cost(const std::vector<KVGroupPlan>& plans, int64_t max_ctx,
|
||||
int64_t max_seqs) const {
|
||||
KVStreamCost c;
|
||||
for (const auto& p : plans) {
|
||||
if (!windowed(p.index) || p.zero || p.kind != RAD_KV_FULL) continue;
|
||||
const int64_t blocks = context_blocks(p, max_ctx, max_seqs);
|
||||
const int64_t slots = std::min(resident_blocks(p), blocks);
|
||||
if (blocks <= 0 || slots <= 0) continue;
|
||||
c.host_bytes += blocks * p.bytes_per_block;
|
||||
c.map_bytes += kvstream_map_bytes(blocks, slots, p.n_layers);
|
||||
++c.groups;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
int KVStreamStore::alloc(const std::vector<KVGroupPlan>& plans, int64_t max_ctx, int64_t max_seqs,
|
||||
MemAllocator& host_alloc, Pool& kv_pool) {
|
||||
for (const auto& p : plans) {
|
||||
if (!windowed(p.index) || p.zero || p.kind != RAD_KV_FULL) continue;
|
||||
const int64_t blocks = context_blocks(p, max_ctx, max_seqs);
|
||||
const int64_t slots = std::min(resident_blocks(p), blocks);
|
||||
if (blocks <= 0 || slots <= 0) continue;
|
||||
|
||||
Map m;
|
||||
m.slots = slots;
|
||||
m.block_bytes = p.layer_bytes_per_block();
|
||||
m.host_layer_stride = blocks * m.block_bytes;
|
||||
m.host_bytes = blocks * p.bytes_per_block;
|
||||
m.host = static_cast<uint8_t*>(host_alloc.alloc(m.host_bytes, RAD_MEM_HOST_MAPPED));
|
||||
if (!m.host) {
|
||||
RAD_ERR("KV group '%s': the host mirror of %s (%lld blocks of %s) could not be "
|
||||
"allocated", p.name.c_str(), humanb(m.host_bytes).c_str(), (long long)blocks,
|
||||
humanb(p.bytes_per_block).c_str());
|
||||
release(host_alloc);
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
/* 2 MiB pages for this mirror are already what the pinned allocator asks for
|
||||
* (device/aql.cpp, pin_pieces), before the pin faults the range in; the switch that turns
|
||||
* them off for the measurement is RADIANCE_NO_HUGE_PAGES there. Nothing to do here. */
|
||||
/* A block the prefix cache has not written yet is read as a miss and copied to the ring
|
||||
* before attention sees it, so the bytes must be defined: an uninitialised block would be
|
||||
* fluent wrong output with nothing to point at. */
|
||||
std::memset(m.host, 0, (size_t)m.host_bytes);
|
||||
|
||||
/* One reservation for the group: the resolve's arrays are a few hundred KiB and separate
|
||||
* reserves would each round up to the pool's alignment. They come out of the KV pool, so
|
||||
* the paged split below sees them and the pool's report names them. */
|
||||
const int64_t layers = p.n_layers > 0 ? p.n_layers : 1;
|
||||
const MapLayout lay = map_layout(blocks, slots, layers);
|
||||
const int64_t map_bytes = lay.bytes;
|
||||
std::string what = "kv_stream_maps:" + p.name;
|
||||
char* at = static_cast<char*>(kv_pool.reserve(what, map_bytes, RAD_ALIGN_UNIT, nullptr));
|
||||
if (!at) {
|
||||
RAD_ERR("KV group '%s': its residency map and ring bookkeeping (%s) did not fit the "
|
||||
"KV pool", p.name.c_str(), humanb(map_bytes).c_str());
|
||||
host_alloc.free(m.host, RAD_MEM_HOST_MAPPED);
|
||||
release(host_alloc);
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
/* The residency map is one slice a layer: a block's ring bytes are per layer, and the
|
||||
* resolve copies one layer's block, so a group-wide map would call a block resident for a
|
||||
* layer whose bytes were never copied (the first decode after a staged prefill). The
|
||||
* control block stays shared: its counters and overflow latch are the group's. */
|
||||
m.res = reinterpret_cast<int32_t*>(at + lay.res);
|
||||
m.slot_block = reinterpret_cast<int32_t*>(at + lay.slot_block);
|
||||
m.slot_state = reinterpret_cast<int32_t*>(at + lay.slot_state);
|
||||
m.ctl = reinterpret_cast<int32_t*>(at + lay.ctl);
|
||||
set_map(p.index, m);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int KVStreamStore::init(const std::vector<KVGroupPlan>& plans, RadStream s) {
|
||||
if (!s) {
|
||||
RAD_ERR("KV streaming: the maps must be filled on a stream this engine owns; a null one "
|
||||
"is not usable");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
for (const auto& p : plans) {
|
||||
const Map* m = map(p.index);
|
||||
if (!m || !m->host || !m->res) continue;
|
||||
/* The reset a resolve launch expects (libref/ref_kvstream.cpp's contract): both maps at
|
||||
* -1, the per-slot state and the control block at zero. There is no reset op because a
|
||||
* residency map lives as long as the pool. */
|
||||
const MapLayout lay = map_layout((int64_t)p.n_blocks, m->slots,
|
||||
p.n_layers > 0 ? p.n_layers : 1);
|
||||
int rc = RAD_OK;
|
||||
/* Every layer has its own map slice (see alloc): a block's bytes are copied per layer, so
|
||||
* residency is per layer too. A streamed group is by definition one whose pool is narrower
|
||||
* than its context, so every block starts absent and the first read of it is a miss. */
|
||||
rc = rad_memset_async(m->res, 0xff, lay.res_bytes, s);
|
||||
if (rc == RAD_OK) rc = rad_memset_async(m->slot_block, 0xff, lay.slot_block_bytes, s);
|
||||
if (rc == RAD_OK) rc = rad_memset_async(m->slot_state, 0, lay.slot_state_bytes, s);
|
||||
if (rc == RAD_OK) rc = rad_memset_async(m->ctl, 0, lay.ctl_bytes, s);
|
||||
if (rc != RAD_OK) {
|
||||
RAD_ERR("KV group '%s': the residency map could not be initialised: %s",
|
||||
p.name.c_str(), rad_dev_last_error());
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
void KVStreamStore::release(MemAllocator& host_alloc) {
|
||||
for (auto& m : maps_) {
|
||||
if (!m.host) continue;
|
||||
host_alloc.free(m.host, RAD_MEM_HOST_MAPPED);
|
||||
/* The device maps are carved from the KV pool and go with it. Clearing the view as well
|
||||
* makes a second call, or a teardown after a failed bring-up, a no-op. */
|
||||
m = Map{};
|
||||
}
|
||||
}
|
||||
|
||||
KVStreamStats KVStreamStore::stats(const std::vector<KVGroupPlan>& plans) const {
|
||||
KVStreamStats st;
|
||||
st.misses = misses_;
|
||||
st.bytes_in = bytes_in_;
|
||||
for (const auto& p : plans)
|
||||
if (p.streamed()) st.resident_blocks += p.pool_blocks();
|
||||
return st;
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,135 @@
|
||||
/* mirror.h -- the host mirror tier and the resident window (docs/KV-STREAMING.md §4.1).
|
||||
*
|
||||
* A streamed group's device pool backs a resident window, not its context; the blocks the pool
|
||||
* does not hold live in a pinned host copy, laid out layer-major and block-indexed exactly like the
|
||||
* pool, and a device residency map says which blocks are in the pool (`-1` for the ones that are
|
||||
* not). This file owns that pair, the cap that narrows a group's pool to a window, and the
|
||||
* arithmetic that sizes both, so mem/kv.{h,cpp} keeps one KVStreamStore member and one call per
|
||||
* method. Nothing here touches a device except init(), which fills the maps on the stream it is
|
||||
* given.
|
||||
*/
|
||||
#pragma once
|
||||
#include "mem/pools.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace rad {
|
||||
|
||||
struct KVGroupPlan;
|
||||
|
||||
/* What the streamed groups need, in bytes, before anything is taken. `host_bytes` is pinned
|
||||
* host memory (`blocks * bytes_per_block` per group, every rank its own shard); `map_bytes` is
|
||||
* the residency map, the ring's reverse map and the control block, all device memory and all
|
||||
* taken from the KV pool itself (a handful of MiB), so they are budgeted with the cache they
|
||||
* describe rather than as an uncharged side allocation.
|
||||
*
|
||||
* Valid after plan_groups() and before carve(); the arithmetic is the one carve() uses. */
|
||||
struct KVStreamCost {
|
||||
int64_t groups = 0;
|
||||
int64_t host_bytes = 0;
|
||||
int64_t map_bytes = 0;
|
||||
};
|
||||
|
||||
/* The resolve's cumulative counters, as metrics read them. A server that does not stream
|
||||
* reports zeros; `resident_blocks` is the ring's depth from the carve. */
|
||||
struct KVStreamStats {
|
||||
int64_t resident_blocks = 0;
|
||||
uint64_t misses = 0; /* blocks placed in the ring by resolve launches */
|
||||
uint64_t bytes_in = 0; /* bytes those placements copied from the host mirror */
|
||||
};
|
||||
|
||||
/* The bytes a streamed group needs, before anything is taken: the mirror is the context the pool
|
||||
* would have held, and the maps are the residency map (one entry a logical block), the ring's
|
||||
* reverse map, its per-slot state and the control block. Only a full
|
||||
* attention group streams -- carve() refuses a window one by name -- so that is what is counted
|
||||
* here; counting a group the carve will refuse would take its mirror first and report a size the
|
||||
* operator cannot act on. */
|
||||
int64_t kvstream_map_bytes(int64_t blocks, int64_t slots, int64_t layers);
|
||||
|
||||
/* A streamed group's storage off the card: the pinned host copy and the device residency map.
|
||||
*
|
||||
* The host copy must cover the group's whole context: `n_blocks * bytes_per_block`, the size the
|
||||
* pool would have had. Needed before carve() for any group cap() narrowed.
|
||||
*
|
||||
* The manager keeps one of these per streamed group and hands it to the run phase as the ABI's
|
||||
* RadKVStream (view), so the fields the plugin reads and the fields alloc() fills are one
|
||||
* definition: `host_bytes` and `slots`/`block_bytes` are the mirror's and the ring's sizes.
|
||||
*
|
||||
* The manager's whole streaming state is here, by group handle, so the policy is in this module and
|
||||
* KVManager only delegates: the resident window cap, the mirror and maps, and the resolve's
|
||||
* counters. Empty vectors and zeros on a manager that does not stream. */
|
||||
class KVStreamStore {
|
||||
public:
|
||||
using Map = RadKVStream;
|
||||
|
||||
/* How much of a paged group's context its device pool backs, in tokens; 0 (the default) is all
|
||||
* of it, which is every group on a server that does not stream. Only the group named, because
|
||||
* a streamed group is usually one whose reads a step can bound -- the sparse attention group --
|
||||
* while its neighbours (an indexer's block keys, read densely over the whole history every
|
||||
* step) must stay where they are (docs/KV-STREAMING.md §4). Rounded up to a whole block and
|
||||
* never above the group's own carve. */
|
||||
void cap(int32_t group, int64_t tokens);
|
||||
|
||||
/* A group whose pool is a resident window rather than its context (cap): its block ids run
|
||||
* over the context and only `n_slots` of them are in device memory. */
|
||||
bool windowed(int32_t group) const;
|
||||
|
||||
/* Blocks of the group's resident window, rounded up to a whole block. A window shorter than one
|
||||
* block is one block: a pool that holds nothing is not a streamed group, it is a group that
|
||||
* cannot run. */
|
||||
int64_t resident_blocks(const KVGroupPlan& p) const;
|
||||
|
||||
/* Blocks the group's context addresses: what the pool would hold if every sequence it may run
|
||||
* reached --max-model-len at once, i.e. the paged part of ceiling(). The block table is already
|
||||
* pitched for this (BatchBuilder sizes it from max_ctx), so the ids exist whether the pool backs
|
||||
* them or not. */
|
||||
int64_t context_blocks(const KVGroupPlan& p, int64_t max_ctx, int64_t max_seqs) const;
|
||||
|
||||
void set_map(int32_t group, const Map& m);
|
||||
const Map* map(int32_t group) const;
|
||||
|
||||
/* The ABI view of a streamed group's ring and mirror, or null for one that is fully resident.
|
||||
* Stable from bring-up: the batch builder copies the pointer into every step's batch, and a
|
||||
* recorded pass keyed by it replays only while the addresses do not move. */
|
||||
const RadKVStream* view(const KVGroupPlan* p, int32_t group) const;
|
||||
|
||||
KVStreamCost cost(const std::vector<KVGroupPlan>& plans, int64_t max_ctx, int64_t max_seqs) const;
|
||||
|
||||
/* Take the mirror and the ring's maps for every streamed group and set_map() each. Call after
|
||||
* plan_groups() and before carve(): the carve refuses a streamed group with no map, and the
|
||||
* maps' bytes leave the pool before its paged split. The mirror is zeroed here, because a
|
||||
* block the prefix cache has not written yet is read as a miss and an uninitialised block
|
||||
* would be fluent wrong output. The device maps are not touched (an elastic pool backs its
|
||||
* range at carve), so init() fills them after it. On failure the mirror is released again;
|
||||
* release() in the engine's teardown covers a partial bring-up. */
|
||||
int alloc(const std::vector<KVGroupPlan>& plans, int64_t max_ctx, int64_t max_seqs,
|
||||
MemAllocator& host_alloc, Pool& kv_pool);
|
||||
|
||||
/* Fill the device maps: `res` and `slot_block` with -1, `slot_state` and `ctl` with zero (the
|
||||
* state a resolve launch expects at bring-up). Call after carve(), with the rank's stream
|
||||
* current, and sync before the first pass. */
|
||||
int init(const std::vector<KVGroupPlan>& plans, RadStream s);
|
||||
|
||||
/* Release what alloc() took. The device maps came from the KV pool and go with it; the host
|
||||
* mirror is outside every pool and is freed here. Safe to call twice and after a failed
|
||||
* bring-up: a group with no mirror is skipped. */
|
||||
void release(MemAllocator& host_alloc);
|
||||
|
||||
KVStreamStats stats(const std::vector<KVGroupPlan>& plans) const;
|
||||
|
||||
/* Charged by whoever reads a resolve launch's ctl: the step path never waits on the card, so
|
||||
* the kernel's cumulative numbers are read where the next unit drains them. */
|
||||
void note_traffic(int64_t misses, int64_t bytes) {
|
||||
if (misses > 0) misses_ += (uint64_t)misses;
|
||||
if (bytes > 0) bytes_in_ += (uint64_t)bytes;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<int64_t> resident_tokens_; /* by group handle: its pool's depth in tokens */
|
||||
std::vector<Map> maps_; /* set_map(), by group handle */
|
||||
uint64_t misses_ = 0; /* note_traffic() */
|
||||
uint64_t bytes_in_ = 0;
|
||||
};
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,209 @@
|
||||
/* plan.cpp -- see plan.h. */
|
||||
#include "kvstream/plan.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace rad {
|
||||
|
||||
int64_t kv_pool_bytes(const KVGroupPlan& p, int64_t slots) {
|
||||
return kv_pool_bytes(p, slots, p.n_blocks);
|
||||
}
|
||||
|
||||
int64_t kv_pool_bytes(const KVGroupPlan& p, int64_t slots, int64_t context_blocks) {
|
||||
if (!p.paged()) return p.n_states * p.bytes_per_state;
|
||||
const int64_t ring = kv_ring_bytes(p, slots);
|
||||
/* slots >= context_blocks is a ring that covers the context, which streams nothing: it is the
|
||||
* group's ordinary one-view pool. A streamed group (slots < context_blocks) also needs its
|
||||
* one-layer stage. The context is passed explicitly because paged_split runs before the carve
|
||||
* writes p.n_blocks. */
|
||||
if (slots <= 0 || slots >= context_blocks) return ring;
|
||||
return std::max(ring, context_blocks * p.layer_bytes_per_block());
|
||||
}
|
||||
|
||||
int64_t kv_pool_bytes(const KVGroupPlan& p) {
|
||||
return kv_pool_bytes(p, p.pool_blocks());
|
||||
}
|
||||
|
||||
int64_t kv_ceiling_bytes(const KVGroupPlan& p, int64_t blocks, int64_t slots) {
|
||||
if (!p.paged() || p.block_size <= 0) return 0;
|
||||
const int64_t ring = slots > 0 ? slots * p.bytes_per_block : 0;
|
||||
if (slots <= 0 || slots >= blocks) return blocks * p.bytes_per_block;
|
||||
/* The stage is context-deep and one layer wide. It and the ring are one reservation, so the
|
||||
* budget pays the larger, not the sum. */
|
||||
return std::max(ring, blocks * p.layer_bytes_per_block());
|
||||
}
|
||||
|
||||
bool kv_tier_cached(const KVGroupPlan& p) {
|
||||
return !p.streamed();
|
||||
}
|
||||
|
||||
namespace {
|
||||
/* The one rule both overloads state: among the groups from index 1 on, a candidate is a full group
|
||||
* that owns bytes and is neither zero nor dense-read, whose declared ops stream it; the widest
|
||||
* page wins. Each overload supplies only how its own container spells those fields. */
|
||||
bool kv_stream_candidate(int32_t kind, bool zero, bool dense_read, int64_t bytes_per_block) {
|
||||
return kind == RAD_KV_FULL && !zero && !dense_read && bytes_per_block > 0;
|
||||
}
|
||||
|
||||
template <class Candidate>
|
||||
int32_t kv_stream_widest(size_t n, const Candidate& candidate) {
|
||||
int32_t best = -1;
|
||||
int64_t widest = 0;
|
||||
for (size_t g = 1; g < n; ++g) {
|
||||
int64_t bytes = 0;
|
||||
if (!candidate(g, &bytes)) continue;
|
||||
if (bytes > widest) {
|
||||
widest = bytes;
|
||||
best = (int32_t)g;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
int32_t kv_stream_group(const KVManager& kv, const Program* prog) {
|
||||
const std::vector<KVGroupPlan>& ps = kv.plans();
|
||||
return kv_stream_widest(ps.size(), [&](size_t g, int64_t* bytes) {
|
||||
const KVGroupPlan& p = ps[g];
|
||||
if (!kv_stream_candidate(p.kind, p.zero, p.dense_read, p.bytes_per_block)) return false;
|
||||
/* Only a group the program wires: a kv_resolve stating this group's per-layer pitch and a
|
||||
* streamed store stating its page. Every caller has the program (the builder's resolved
|
||||
* op list), so there is no width-only fallback here. */
|
||||
const KVGroupInfo* gi = g < prog->kv_groups.size() ? &prog->kv_groups[g] : nullptr;
|
||||
if (!kvstream_ops(*prog, gi).streams()) return false;
|
||||
*bytes = p.bytes_per_block;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
int32_t kv_stream_group(const std::vector<KVGroupInfo>& gs, const Program* prog) {
|
||||
return kv_stream_widest(gs.size(), [&](size_t g, int64_t* bytes) {
|
||||
const KVGroupInfo& gi = gs[g];
|
||||
if (!kv_stream_candidate(gi.decl.kind, gi.zero, gi.dense_read, gi.bytes_per_block))
|
||||
return false;
|
||||
/* The declarations exist before the plans do; a caller with no program (the width-only
|
||||
* report fallback) keeps the width rule. */
|
||||
if (prog && !kvstream_ops(*prog, &gi).streams()) return false;
|
||||
*bytes = gi.bytes_per_block;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
int64_t kvstream_ceiling_blocks(const Config& cfg, int64_t sel_pitch) {
|
||||
if (sel_pitch <= 0 || cfg.max_seqs <= 0) return 0;
|
||||
/* One selection per row: a sequence drafts n_spec tokens and a verify step runs them beside
|
||||
* the accepted position, so under speculation a step is (1 + n_spec) rows a sequence. */
|
||||
const int64_t rows = cfg.max_seqs * (1 + (cfg.n_spec > 0 ? cfg.n_spec : 0));
|
||||
return rows * sel_pitch;
|
||||
}
|
||||
|
||||
KVStreamOps kvstream_ops(const Program& prog, const KVGroupInfo* group) {
|
||||
KVStreamOps ops;
|
||||
/* Whether this declaration has a kernel at all. `bands` being empty does NOT mean it has none:
|
||||
* the builder always leaves at least one band, and for an op no row matched it is a placeholder
|
||||
* with both domains null and a miss reason (core/build/rad_builder.cpp, resolve_bands). The same
|
||||
* predicate as Builder::op_resolved, read off the program a non-builder caller holds. */
|
||||
const auto resolved = [](const OpInfo& o) {
|
||||
for (const Band& b : o.bands)
|
||||
for (int d = 0; d < RAD_N_DOMAINS; ++d)
|
||||
if (b.dom[d]) return true;
|
||||
return false;
|
||||
};
|
||||
/* The group's per-layer block pitch, the figure kv_resolve states as `block_bytes`
|
||||
* (rad_kv_layer_bytes). bytes_per_block covers every bound layer. */
|
||||
const int64_t n_bound = group && !group->layers.empty() ? (int64_t)group->layers.size() : 1;
|
||||
const int64_t layer_bytes =
|
||||
group && group->bytes_per_block > 0 ? group->bytes_per_block / n_bound : 0;
|
||||
for (const OpInfo& op : prog.ops) {
|
||||
/* A declaration that resolved to nothing is not a streamed path: the plugin's handle is
|
||||
* null and nothing would be issued. A declared-but-unresolved store counts as no path,
|
||||
* which is the packed-cache shape -- the architecture emits the op and the kernel table has
|
||||
* no row for the layout -- and letting it count would stream a group whose store never
|
||||
* runs, so the mirror would stay empty and a miss would copy zeros. */
|
||||
if (!resolved(op)) continue;
|
||||
const bool is_resolve = op.op == "kv_resolve";
|
||||
const bool is_store = op.op == "kv_store_streamed" ||
|
||||
op.op == "qk_norm_rope_gate_kv_store_streamed";
|
||||
if (!is_resolve && !is_store) continue;
|
||||
/* The declaration states the geometry it was emitted for; a resolve or a store declared
|
||||
* for a narrower group must not enable streaming of this one. A declaration that states
|
||||
* neither is taken at its word. */
|
||||
long long stated = 0;
|
||||
if (is_resolve && layer_bytes > 0 && op.base.get_i("block_bytes", &stated) &&
|
||||
stated != layer_bytes)
|
||||
continue;
|
||||
if (is_store && group && group->block_size > 0 &&
|
||||
op.base.get_i("block_size", &stated) && stated != group->block_size)
|
||||
continue;
|
||||
if (is_resolve) {
|
||||
long long pitch = 0;
|
||||
if (!op.base.get_i("sel_pitch", &pitch) || pitch <= 0) continue;
|
||||
/* A resolve with no pitch cannot size the ring; the first one that states it wins, as
|
||||
* every layer of the group declares the same. */
|
||||
if (!ops.resolve) ops.sel_pitch = (int64_t)pitch;
|
||||
ops.resolve = true;
|
||||
} else if (op.op == "kv_store_streamed") {
|
||||
ops.store = true;
|
||||
} else {
|
||||
ops.folded = true;
|
||||
}
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
|
||||
KVStreamRing kvstream_ring_shape(const Config& cfg, int64_t ctx, int64_t block_size) {
|
||||
KVStreamRing out;
|
||||
const int64_t bs = block_size > 0 ? block_size : 1;
|
||||
out.ctx_blocks = kv_context_blocks(bs, ctx, cfg.max_seqs);
|
||||
out.ring_blocks = cfg.kv_resident > 0 ? (cfg.kv_resident + bs - 1) / bs : 0;
|
||||
if (out.ring_blocks > out.ctx_blocks) out.ring_blocks = out.ctx_blocks;
|
||||
return out;
|
||||
}
|
||||
|
||||
void kvstream_ring(const Config& cfg, int64_t ctx, int64_t block_size, int64_t sel_pitch,
|
||||
KVStreamRing* out) {
|
||||
*out = kvstream_ring_shape(cfg, ctx, block_size);
|
||||
if (!out->streams()) return; /* absent, 0, or a ring that covers the context */
|
||||
|
||||
/* Anything under the computed amount is ignored and pulled up to it. A ring below what one
|
||||
* decode step can select is not a slower configuration, it is a request served from a
|
||||
* selection table that no longer addresses the pool -- and the engine computes that floor
|
||||
* itself, from --max-num-seqs, the drafter's depth and the arch's own sel_pitch. The operator
|
||||
* asked for streaming, not for a number, so the ring takes the floor and `raised_blocks` says
|
||||
* the flag did not ask for it: the caller prints the one line, and startup proceeds. Silent
|
||||
* here because this runs once a rank on the budget probe and once in bring-up, and the raise
|
||||
* is one startup fact, not three. The ceiling is always positive here: both callers reach this
|
||||
* only after kvstream_ops().streams(), which requires a kv_resolve with sel_pitch > 0, and
|
||||
* --max-num-seqs is parsed >= 1. */
|
||||
const int64_t ceiling = kvstream_ceiling_blocks(cfg, sel_pitch);
|
||||
if (out->ring_blocks < ceiling) {
|
||||
out->ring_blocks = ceiling < out->ctx_blocks ? ceiling : out->ctx_blocks;
|
||||
out->raised_blocks = ceiling;
|
||||
}
|
||||
}
|
||||
|
||||
int kvstream_cap_probe(KVManager& probe, Config& cfg, const Program& prog, int64_t ctx) {
|
||||
cfg.kv_streams = false;
|
||||
if (cfg.kv_resident <= 0) return RAD_OK;
|
||||
const int32_t g = kv_stream_group(probe, &prog);
|
||||
if (g < 0) return RAD_OK;
|
||||
const KVGroupPlan* p = probe.plan(g);
|
||||
if (!p || p->block_size <= 0) return RAD_OK;
|
||||
/* This model's architecture declares no streamed path for the group the plan would stream:
|
||||
* price the pool the ordinary way, exactly as without the flag. The ring's arithmetic is for a
|
||||
* streamed group only, and the one warning this case deserves is printed by kvstream_configure
|
||||
* (this probe runs on every rank). */
|
||||
const KVStreamOps ops =
|
||||
kvstream_ops(prog, g < (int32_t)prog.kv_groups.size() ? &prog.kv_groups[(size_t)g] : nullptr);
|
||||
if (!ops.streams()) return RAD_OK;
|
||||
KVStreamRing ring;
|
||||
/* The ring this prices is the raised one (kvstream_ring), so the budget and the carve take the
|
||||
* same depth. */
|
||||
kvstream_ring(cfg, ctx, p->block_size, ops.sel_pitch, &ring);
|
||||
if (!ring.streams()) return RAD_OK;
|
||||
cfg.kv_streams = true;
|
||||
probe.cap_resident_tokens(g, ring.ring_blocks * p->block_size);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,159 @@
|
||||
/* plan.h -- the KV streaming policy, as pure arithmetic on a KVGroupPlan.
|
||||
*
|
||||
* A streamed group has two device views over one allocation (docs/KV-STREAMING.md §4.1):
|
||||
*
|
||||
* ring n_slots blocks deep, `n_layers` layer stripes, addressed through the residency map;
|
||||
* stage n_blocks blocks deep, ONE layer stripe, addressed by block id.
|
||||
*
|
||||
* The allocation is max(ring_bytes, stage_bytes), never the sum: a decode-only ring is tiny
|
||||
* against one layer of the context, so the stage sets the price and the ring fits inside it.
|
||||
* `depth` on the plan is the stripe count of the staged view (1); the ring view keeps the
|
||||
* group's own `n_layers` stripes. Nothing here touches a device, so this file is testable
|
||||
* beside mem/kv.h's arithmetic.
|
||||
*
|
||||
* The ceiling is the number the VRAM budget is resolved from (Engine::plan, before
|
||||
* cap_resident_tokens() runs). A streamed group must contribute its stage, not its whole
|
||||
* cache, or the phase frees nothing: that is why kvstream_cap_probe() is called on the
|
||||
* throwaway probe before probe.ceiling() (docs/KV-STREAMING.md §4.1, point 5).
|
||||
*/
|
||||
#pragma once
|
||||
#include "mem/kv.h"
|
||||
|
||||
namespace rad {
|
||||
|
||||
/* Bytes of the decode ring view: `slots` blocks, every layer. Inline because it is one
|
||||
* multiplication on the plan and because a caller that only lays a view out (kvstream/bind.cpp)
|
||||
* must not link the probe below, which names KVManager: core/sched/batch.cpp is linked by tests
|
||||
* whose weak device fakes an extracted KVManager would displace. */
|
||||
inline int64_t kv_ring_bytes(const KVGroupPlan& p, int64_t slots) {
|
||||
return slots > 0 ? slots * p.bytes_per_block : 0;
|
||||
}
|
||||
|
||||
/* Blocks the group's context addresses: `max_seqs` sequences at `--max-model-len`, rounded up to a
|
||||
* whole block each. This is the one spelling of the id space, shared by the store's mirror/map
|
||||
* sizing (KVStreamStore::context_blocks) and the startup ring decision, so the two cannot disagree
|
||||
* about whether a ring covers the context. */
|
||||
inline int64_t kv_context_blocks(int64_t block_size, int64_t max_ctx, int64_t max_seqs) {
|
||||
if (block_size <= 0) return 0;
|
||||
const int64_t seqs = max_seqs > 0 ? max_seqs : 1;
|
||||
const int64_t ctx = max_ctx > 0 ? max_ctx : 0;
|
||||
return seqs * ((ctx + block_size - 1) / block_size);
|
||||
}
|
||||
|
||||
/* The one allocation: max(ring, stage). `slots <= 0` or `slots >= n_blocks` is a group that does
|
||||
* not stream (no ring asked for, or one that covers the context), and it keeps its old
|
||||
* single-view cost. A non-paged group is unchanged. */
|
||||
int64_t kv_pool_bytes(const KVGroupPlan& p, int64_t slots);
|
||||
int64_t kv_pool_bytes(const KVGroupPlan& p, int64_t slots, int64_t context_blocks);
|
||||
int64_t kv_pool_bytes(const KVGroupPlan& p);
|
||||
|
||||
/* The pool cost of a group at `blocks` blocks of context and `slots` resident blocks. This is
|
||||
* what ceiling() sums; with no streamed slot it is the old blocks * bytes_per_block. */
|
||||
int64_t kv_ceiling_bytes(const KVGroupPlan& p, int64_t blocks, int64_t slots);
|
||||
|
||||
/* Whether the idle tiers may gather this group. A streamed group's pool holds one layer's bytes,
|
||||
* so a tier gather of layer L would be the last staged layer copied n_layers times
|
||||
* (core/runtime/tierexec.cpp); its mirror is the tier, and the residency map restores a block on
|
||||
* the next resolve (docs/KV-STREAMING.md §4.1). */
|
||||
bool kv_tier_cached(const KVGroupPlan& p);
|
||||
|
||||
/* The group --kv-resident streams: the widest paged FULL group whose selection the plugin has
|
||||
* declared a streamed path for, as configure_kv chooses it. `prog` is the resolved program: a group
|
||||
* is a candidate only if kvstream_ops states its pitch and page, so the choice is a rule rather
|
||||
* than a width coincidence. dense_read groups are never candidates: a step reads them over the
|
||||
* whole live history, so a ring cannot hold them back. -1 when the model has none. */
|
||||
int32_t kv_stream_group(const KVManager& kv, const Program* prog);
|
||||
|
||||
/* The same choice from a rank's declarations, before the plans exist (Engine::configure_kv): the
|
||||
* group by declaration is the one the plans will hold, so the policy is chosen once and the plan
|
||||
* below follows it. */
|
||||
int32_t kv_stream_group(const std::vector<KVGroupInfo>& groups, const Program* prog = nullptr);
|
||||
|
||||
/* What --kv-resident makes of one group: the ring's depth from the flag, the group's id space from
|
||||
* --max-num-seqs and --max-model-len, and whether anything streams at all. Pure arithmetic on
|
||||
* numbers the engine already has, so the budget probe and the carve decision cannot disagree. */
|
||||
struct KVStreamRing {
|
||||
int64_t ring_blocks = 0; /* --kv-resident, rounded up to whole blocks and raised to the
|
||||
* ceiling below when the flag asks for less */
|
||||
int64_t raised_blocks = 0; /* the depth the flag was raised to (kvstream_ceiling_blocks), or 0
|
||||
* when it already asked for that much: the ring is not the
|
||||
* operator's number when this is set */
|
||||
int64_t ctx_blocks = 0; /* max_seqs x ceil(ctx / block_size): the group's whole context */
|
||||
/* A ring that covers the context is not a streamed group. The pool then holds every block
|
||||
* with its own id, so the group keeps the base engine's path exactly: no mirror, no residency
|
||||
* maps, no stage, no resolve, and a null RadKVStream (docs/KV-STREAMING.md §5 Phase 4). */
|
||||
bool streams() const { return ring_blocks > 0 && ring_blocks < ctx_blocks; }
|
||||
};
|
||||
|
||||
/* The blocks one decode step's selections can name, which is what the ring must hold -- and the
|
||||
* depth --kv-resident is raised to when it asks for less. A within-(1+n_spec) decode pass takes
|
||||
* the ring (`kv_pass_view`); a decode-classified pass with more rows a sequence than that (the
|
||||
* serial drafter's history pass over a prefill chunk) stages, so it is not counted here. Under
|
||||
* speculation a decode step issues one selection per row, so a step is `max_seqs x (1 + n_spec)`
|
||||
* rows and each names at most `sel_pitch` distinct blocks -- the arch's `topk + 1`. 0 when the
|
||||
* program declares no kv_resolve: a dense model's attention reads a block id as a pool slot, so
|
||||
* its ceiling is the whole context and nothing can be streamed. */
|
||||
int64_t kvstream_ceiling_blocks(const Config& cfg, int64_t sel_pitch);
|
||||
|
||||
/* The ops a streamed group's issue path actually needs, read off the program the plugin declared
|
||||
* (the builder's resolved op list, not the architecture's name). A streamed group is only safe
|
||||
* when its attention rewrites `sel` from block ids into ring slots (`kv_resolve`) and its store
|
||||
* writes the host mirror (`kv_store_streamed`, or the fused prologue that folds the store in).
|
||||
* Without the resolve, `sel` still holds block ids while the attention reads them as pool page
|
||||
* indices -- past the ring when the pool is smaller than the id space, which is a fault, not a
|
||||
* slow path. Without the store nothing writes the mirror, so a miss reads zeros.
|
||||
*
|
||||
* `group` narrows the match to the group the plan will stream: a declaration states that group's
|
||||
* geometry (`block_bytes` on the resolve, `block_size` on the store), so a resolve emitted for a
|
||||
* narrower group cannot enable streaming of another. Null matches any declaration; a declaration
|
||||
* that states no geometry is accepted as-is, so a plugin that names neither is never refused for
|
||||
* it. A declaration the builder recorded with no kernel in any domain is not a path: the plugin's
|
||||
* handle was null and nothing would be issued (the same test as Builder::op_resolved). That is how
|
||||
* a packed cache reads -- the architecture emits the store and the kernel table has no row for the
|
||||
* layout -- and it must not count, or the group would stream with a null writer. */
|
||||
struct KVStreamOps {
|
||||
bool resolve = false; /* kv_resolve, stating this group's layer bytes and sel_pitch */
|
||||
bool store = false; /* kv_store_streamed: the mirror writer every streamed layer has */
|
||||
bool folded = false; /* qk_norm_rope_gate_kv_store_streamed: the same store, folded */
|
||||
int64_t sel_pitch = 0; /* the blocks one selection row can name, from the resolve */
|
||||
/* The ops are there, so this group can stream. */
|
||||
bool streams() const { return resolve && (store || folded); }
|
||||
/* The first op the plugin is missing, for the one startup line that says why nothing streams;
|
||||
* null when streams(). */
|
||||
const char* missing() const {
|
||||
if (!resolve) return "kv_resolve";
|
||||
if (!store && !folded) return "kv_store_streamed";
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
KVStreamOps kvstream_ops(const Program& prog, const KVGroupInfo* group);
|
||||
|
||||
/* The ring's depth and the group's id space with nothing else decided: whether a ring covers the
|
||||
* context, so that the caller can tell "this model has no streamed path" (a warning) from "this
|
||||
* ring is below what a decode step can select" (a raise) without one hiding the other. */
|
||||
KVStreamRing kvstream_ring_shape(const Config& cfg, int64_t ctx, int64_t block_size);
|
||||
|
||||
/* Read --kv-resident against `group`'s context and fill `out`. A ring below what one decode step
|
||||
* can select is raised to it -- `out->ring_blocks` becomes the ceiling and `out->raised_blocks`
|
||||
* says the flag did not ask for that much -- not refused: the engine computes the floor and the
|
||||
* operator asked for streaming, not for a number. The caller states the one warning, once, and
|
||||
* startup proceeds. A ring that covers the context is not a raise either -- it is
|
||||
* `out->streams() == false`, and the group stays a resident pool. `block_size` is the group's own
|
||||
* page (1 for a group whose page is unknown). Never refuses: both callers reach it only after
|
||||
* kvstream_ops().streams(), so the ceiling is positive, and a caller that names a pitch of its own
|
||||
* on a model with no kv_resolve is a case its caller has already warned about and ignored. */
|
||||
void kvstream_ring(const Config& cfg, int64_t ctx, int64_t block_size, int64_t sel_pitch,
|
||||
KVStreamRing* out);
|
||||
|
||||
/* Teach the budget probe the streamed plan: the ring from --kv-resident and the stage from the
|
||||
* context. Call before probe.ceiling(ctx); the group's ceiling then falls from n_layers of its
|
||||
* context to max(ring, one layer of it), and `cfg.kv_streams` records that the pool is priced that
|
||||
* way (core/mem/vram_budget.cpp). Returns RAD_OK on a model with nothing to stream, before any
|
||||
* weight is loaded. The ring it prices is the raised one (kvstream_ring), so the budget and the
|
||||
* carve take the same depth; the warning for it is kvstream_configure's, which runs once rather
|
||||
* than once a rank. A group whose architecture declares no streamed ops (kvstream_ops) is not
|
||||
* priced for streaming at all: that is the same RAD_OK, with `cfg.kv_streams` left false, so the
|
||||
* configuration is exactly the one without --kv-resident. */
|
||||
int kvstream_cap_probe(KVManager& probe, Config& cfg, const Program& prog, int64_t ctx);
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -0,0 +1,358 @@
|
||||
/* stage.cpp -- see stage.h (the ordering rule lives there, beside the code that would break it). */
|
||||
#include "kvstream/stage.h"
|
||||
|
||||
#include "device/device.h"
|
||||
#include "kvstream/bind.h"
|
||||
#include "runtime/wtab.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace rad {
|
||||
|
||||
namespace {
|
||||
/* The layer staged most recently on this rank thread; reset at every pass. */
|
||||
thread_local int g_last_layer = -1;
|
||||
/* What the pass that just closed staged, and over how many tokens: the counter is read at the next
|
||||
* enter, so the numbers are the ones a request actually paid (docs/KV-STREAMING.md §3.9). */
|
||||
struct PassStage {
|
||||
uint64_t layers = 0, bytes = 0;
|
||||
int64_t tokens = 0, ctx = 0, live_row = 0; /* live_row: the history ONE layer of this pass reads */
|
||||
int64_t bound_blocks = 0; /* the manager's bound the copy was clamped to */
|
||||
int phase = -1;
|
||||
};
|
||||
thread_local PassStage g_pass;
|
||||
bool trace_stage() {
|
||||
static const bool on = std::getenv("RADIANCE_KV_STAGE_TRACE") != nullptr;
|
||||
return on;
|
||||
}
|
||||
|
||||
/* One line per pass that staged: the bytes the pass moved against the bytes the history it read
|
||||
* actually occupies. The ratio is the number that decides whether the stage is a bandwidth problem
|
||||
* or a per-chunk tax (see the note beside kv_stage_layer). */
|
||||
void report_pass() {
|
||||
if (!g_pass.layers) return;
|
||||
const double live = (double)g_pass.live_row * (double)g_pass.layers;
|
||||
RAD_INFO("kv streaming: stage pass phase=%d tokens=%lld ctx=%lld: %llu layers, %.1f MiB moved, "
|
||||
"%.1f MiB of live history (%.1fx), bound %lld blocks",
|
||||
g_pass.phase, (long long)g_pass.tokens, (long long)g_pass.ctx,
|
||||
(unsigned long long)g_pass.layers, (double)g_pass.bytes / (1024.0 * 1024.0),
|
||||
live / (1024.0 * 1024.0), live > 0.0 ? (double)g_pass.bytes / live : 0.0,
|
||||
(long long)g_pass.bound_blocks);
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
/* Forget the last staged layer. A pass visits its layers in order and `resolve_sel` may be called
|
||||
* once per attention launch, so the driver stages a layer once and treats a consecutive repeat as
|
||||
* already done. Called from kvstream_enter() at every pass. */
|
||||
static void kvstage_begin_pass() {
|
||||
report_pass();
|
||||
g_last_layer = -1;
|
||||
g_pass = PassStage();
|
||||
}
|
||||
|
||||
void kvstream_enter(Ctx* c, const RadBatch* b) {
|
||||
/* The stage is filled once per layer per pass; a new pass starts at the first layer again. */
|
||||
kvstage_begin_pass();
|
||||
if (!c || !b) return;
|
||||
g_pass.tokens = b->n_tok;
|
||||
g_pass.ctx = b->max_ctx_len;
|
||||
g_pass.phase = b->phase;
|
||||
for (int i = 0; i < b->n_kv_groups; ++i) {
|
||||
if (!b->kv[i].stream) continue;
|
||||
KVPoolBinding* kb = c->kv_binding(b->kv[i].group);
|
||||
if (!kb || !kb->has_stage) continue;
|
||||
/* A pass selects by phase and row shape (bind.h). */
|
||||
kv_binding_enter(*kb, kv_pass_view(b->phase, b->n_tok, b->n_seq, b->n_spec));
|
||||
/* The history one layer of this pass can read: every block up to the longest sequence's
|
||||
* end, at the same block size the resolve uses. That is the useful part of the stage row
|
||||
* the draw below copies in full. */
|
||||
if (kb->stage.bytes > 0 && b->kv[i].block_size > 0 && b->kv[i].stream->block_bytes > 0) {
|
||||
const int64_t reach = b->max_ctx_len + b->max_q_len;
|
||||
const int64_t live_blocks = (reach + b->kv[i].block_size - 1) / b->kv[i].block_size;
|
||||
const int64_t row = std::min(live_blocks * b->kv[i].stream->block_bytes, kb->stage.bytes);
|
||||
if (row > g_pass.live_row) g_pass.live_row = row;
|
||||
if (kb->stage_live_bytes > 0) {
|
||||
const int64_t bound = kb->stage_live_bytes / b->kv[i].stream->block_bytes;
|
||||
if (bound > g_pass.bound_blocks) g_pass.bound_blocks = bound;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The allocator's high-water mark, as bytes of one layer: every id a table can name is below it
|
||||
* (stage.h, "VOLUME", states the argument and the failure if it is ever too small). */
|
||||
void kvstream_bound_live(Ctx* c, const KVManager& kv) {
|
||||
if (!c) return;
|
||||
for (int g = 0; g < kv.n_groups(); ++g) {
|
||||
KVPoolBinding* b = c->kv_binding(g);
|
||||
if (!b || !b->has_stage) continue;
|
||||
const RadKVStream* st = kv.stream_view(g);
|
||||
if (!st || st->block_bytes <= 0) continue;
|
||||
const int64_t blocks = kv.held_top_blocks(g);
|
||||
b->stage_live_bytes = blocks > 0 ? blocks * st->block_bytes : 0;
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
|
||||
namespace rad {
|
||||
namespace {
|
||||
|
||||
/* Where a staged layer's bytes come from and how big they are: both views start at the pool base, so
|
||||
* the destination is always offset 0 and the source is this layer's row in the mirror, addressed by
|
||||
* the group-local position (`layer_slot`), not by the absolute model layer. */
|
||||
struct StageSrc {
|
||||
void* dst = nullptr;
|
||||
const uint8_t* src = nullptr;
|
||||
int64_t bytes = 0;
|
||||
};
|
||||
|
||||
bool stage_src(Ctx* ctx, int group, const RadKVGroupBatch* kvb, int layer, StageSrc* o) {
|
||||
const KVPoolBinding* b = ctx->kv_binding(group);
|
||||
const RadKVStream* st = kvb ? kvb->stream : nullptr;
|
||||
if (!b || !st) return false;
|
||||
const int64_t li = ctx->kv_layer_index(group, layer);
|
||||
if (!st->host || st->block_bytes <= 0 || st->host_layer_stride <= 0 || li < 0) return false;
|
||||
o->dst = b->base;
|
||||
o->src = st->host + li * st->host_layer_stride;
|
||||
o->bytes = b->bytes;
|
||||
/* Only the live prefix of the row: the block ids above the manager's highest held block cannot
|
||||
* be named by any table this pass reads (stage.h, "VOLUME" -- an underestimate here is wrong
|
||||
* text, not a fault, so this is the bound and not a guess). */
|
||||
if (b->stage_live_bytes > 0 && b->stage_live_bytes < o->bytes) o->bytes = b->stage_live_bytes;
|
||||
if (o->bytes <= 0 || o->bytes > st->host_layer_stride) o->bytes = st->host_layer_stride;
|
||||
return o->bytes > 0;
|
||||
}
|
||||
|
||||
/* The DMA path: one layer of the mirror onto the card without holding a CU slot, overlapped with the
|
||||
* layer before it. `done` is an event the list completes instead of a dispatch; `mark` is the ride
|
||||
* on the compute stream the list waits for, or null (copy now). */
|
||||
bool dma_stage(Ctx* c) {
|
||||
static const bool on = rad_xp_supported(c->device()) != 0;
|
||||
return on;
|
||||
}
|
||||
|
||||
/* One list in flight per rank thread: the layer it fills, and the ride it waits on. */
|
||||
struct StagePipe {
|
||||
RadEvent done = nullptr;
|
||||
int inflight = -1; /* the layer the outstanding list fills */
|
||||
int group = -1;
|
||||
void* mark = nullptr;
|
||||
int64_t bytes = 0; /* what that list was told to move, for the pass's counter */
|
||||
};
|
||||
thread_local StagePipe g_pipe;
|
||||
thread_local bool g_warned_ride = false;
|
||||
|
||||
/* Arm the completion word, mark the compute stream and hand the list over.
|
||||
*
|
||||
* The arm is a write of 1 in stream order (the weight table's patch dispatch, runtime/wtab.h), and
|
||||
* it is also the dispatch the list's ride waits on: so the list starts when the arm has executed,
|
||||
* and the copy's 0 can never precede the 1 the wait is looking for. The arm is behind every
|
||||
* attention launch of the layer before, which is what makes the one-layer buffer safe to refill
|
||||
* (core/kvstream/stage.h). */
|
||||
bool arm_and_submit(Ctx* c, int group, int layer, const StageSrc& s, void* mark) {
|
||||
uint32_t* word = nullptr;
|
||||
if (!g_pipe.done || rad_event_dma(g_pipe.done, c->stream(), &word) < 0) return false;
|
||||
WtabPatch arm;
|
||||
arm.n = 1;
|
||||
arm.dst[0] = (void**)(void*)word;
|
||||
arm.val[0] = (void*)(uintptr_t)1;
|
||||
if (wtab_patch(arm, c->stream()) < 0) return false;
|
||||
const uint64_t src = (uint64_t)(uintptr_t)s.src, dst = (uint64_t)(uintptr_t)s.dst;
|
||||
const uint64_t bytes = (uint64_t)s.bytes;
|
||||
/* No yield: this copy's wait is a barrier on the very stream whose link hold would release it
|
||||
* (docs/KV-STREAMING.md §6.1, the deadlock the prefetch's rule names). */
|
||||
if (rad_xp_list(c->device(), mark, 1, &src, &dst, &bytes, word, 0, RAD_XP_NO_YIELD) != RAD_OK)
|
||||
return false;
|
||||
g_pipe.inflight = layer;
|
||||
g_pipe.group = group;
|
||||
g_pipe.mark = mark;
|
||||
g_pipe.bytes = s.bytes;
|
||||
if (trace_stage())
|
||||
RAD_INFO("kv streaming: handed over layer %d (%lld bytes), behind %lld", layer,
|
||||
(long long)s.bytes, (long long)rad_xp_behind(c->device()));
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The copy on the compute stream, for a backend with no DMA queue (the host backend, a test) and
|
||||
* for a ride the engine would not lend: a list holds no CU slot, a copy does, so this is the path
|
||||
* that pauses the layer rather than the one that rides behind it. */
|
||||
int serial_stage(Ctx* c, const StageSrc& s) {
|
||||
RadStream st = c->stream();
|
||||
const int rc = (s.bytes & 15) == 0
|
||||
? rad_memcpy_ranges_async(s.dst, s.src, s.bytes, nullptr, 0, st)
|
||||
: rad_memcpy_async(s.dst, s.src, s.bytes, st);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void count_stage(Ctx* c, int layer, int64_t bytes) {
|
||||
g_last_layer = layer;
|
||||
g_pass.layers += 1;
|
||||
g_pass.bytes += (uint64_t)bytes;
|
||||
if (trace_stage() && c->rank() == 0)
|
||||
RAD_INFO("kv streaming: stage layer %d (%lld bytes)", layer, (long long)bytes);
|
||||
}
|
||||
|
||||
/* Records the arm and the submit where a recorded pass made them: a DMA list is not a queue packet,
|
||||
* so a tape that replayed without them would read an unstaged layer. */
|
||||
int stage_issue_host(Ctx* c, int group, int layer, bool after) {
|
||||
if (c->recording())
|
||||
return c->tape_kv_stage(after, group, layer);
|
||||
return kvstage_run(c, group, layer, after);
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* The wait and, when the pipeline has nothing for this layer, the copy: issued before the layer's
|
||||
* store, so the store's newer bytes are the last writer of this chunk's positions (the copy reads a
|
||||
* mirror the store has not written yet). 1 when the layer is staged, 0 when the group has no stage
|
||||
* view -- the caller then resolves as usual. A failure is reported with rad_step_fail and returns
|
||||
* 1, so the caller stops rather than reading an unstaged layer. Internal: every caller is the
|
||||
* extern "C" seam below. */
|
||||
static int kvstage_wait(Ctx* c, int group, const RadKVGroupBatch* kvb, int layer) {
|
||||
if (!c || !kvb || !kvb->stream) return 0;
|
||||
const KVPoolBinding* b = c->kv_binding(group);
|
||||
if (!b || !b->has_stage || b->depth != 1) return 0;
|
||||
if (layer == g_last_layer) return 1;
|
||||
return stage_issue_host(c, group, layer, false);
|
||||
}
|
||||
|
||||
/* After a layer's attention has been issued and has finished reading the stage: start the next
|
||||
* bound layer's copy. The ride is completed by the next dispatch on the compute stream, which is
|
||||
* behind every attention launch of `layer`; nothing may read the stage again until the wait that
|
||||
* precedes the next layer's store. 0 always: it reports nothing to the arch, and the exported
|
||||
* rad_kv_stage_next keeps the int for the ABI (every caller (void)s it). Internal: the extern "C"
|
||||
* seam below is its only caller. */
|
||||
static int kvstage_next(Ctx* c, int group, int layer) {
|
||||
if (!c) return 0;
|
||||
const KVPoolBinding* b = c->kv_binding(group);
|
||||
if (!b || !b->has_stage || b->depth != 1) return 0;
|
||||
return stage_issue_host(c, group, layer, true);
|
||||
}
|
||||
|
||||
int kvstage_run(Ctx* c, int group, int layer, bool after) {
|
||||
if (!c) return 0;
|
||||
const RadBatch* b = c->batch();
|
||||
const RadKVGroupBatch* kvb = nullptr;
|
||||
if (b)
|
||||
for (int i = 0; i < b->n_kv_groups; ++i)
|
||||
if ((int)b->kv[i].group == group) { kvb = &b->kv[i]; break; }
|
||||
if (!kvb || !kvb->stream) return 0;
|
||||
const KVPoolBinding* kb = c->kv_binding(group);
|
||||
if (!kb || !kb->has_stage || kb->depth != 1) return 0;
|
||||
|
||||
if (!after) {
|
||||
/* Make `layer`'s stage row whole. Pipelined: the list was handed over at the layer before,
|
||||
* so this is a barrier on the compute stream that the engine's done word releases. */
|
||||
if (g_pipe.inflight >= 0 && (g_pipe.inflight != layer || g_pipe.group != group)) {
|
||||
/* A list is filling the stage for a layer this pass does not read (a pass that ended
|
||||
* before it). Nothing may rewrite the buffer until it lands. */
|
||||
(void)rad_event_wait(c->stream(), g_pipe.done);
|
||||
g_pipe.inflight = -1;
|
||||
g_pipe.bytes = 0;
|
||||
rad_ride_release(g_pipe.mark, nullptr, 0);
|
||||
g_pipe.mark = nullptr;
|
||||
}
|
||||
if (g_pipe.inflight == layer && g_pipe.group == group) {
|
||||
if (rad_event_wait(c->stream(), g_pipe.done) < 0) {
|
||||
rad_step_fail(c, "kv streaming: the compute stream could not wait for the stage copy "
|
||||
"the DMA engine is filling; the layer's attention would read the "
|
||||
"previous layer's bytes");
|
||||
return 1;
|
||||
}
|
||||
/* What the list was handed, not the row's whole size: the counter is what the pass
|
||||
* paid, and the clamp is the whole point of the pass's bound. */
|
||||
const int64_t moved = g_pipe.bytes;
|
||||
g_pipe.inflight = -1;
|
||||
g_pipe.bytes = 0;
|
||||
/* The signal the list polled is nobody's until it goes back to the pool. */
|
||||
rad_ride_release(g_pipe.mark, nullptr, 0);
|
||||
g_pipe.mark = nullptr;
|
||||
count_stage(c, layer, moved);
|
||||
return 1;
|
||||
}
|
||||
StageSrc s;
|
||||
if (!stage_src(c, group, kvb, layer, &s)) {
|
||||
rad_step_fail(c, "kv streaming: a layer needs its prefill stage but the group has no host "
|
||||
"mirror row for it, so the attention would read an unstaged layer");
|
||||
return 1;
|
||||
}
|
||||
/* Nothing was pipelined for this layer (the pass's first, or a group the pipeline has not
|
||||
* seen): copy it on the engine now and wait for it, so it costs the layer's prologue. */
|
||||
if (dma_stage(c)) {
|
||||
if (!g_pipe.done && rad_event_create_local(&g_pipe.done) < 0) g_pipe.done = nullptr;
|
||||
/* The arm dispatch is what the ride below is completed by; it is issued after this
|
||||
* layer's projections, so the copy has that much of a head start. */
|
||||
void* mark = nullptr;
|
||||
if (g_pipe.done && rad_ride(c->stream(), &mark) == RAD_OK) {
|
||||
if (arm_and_submit(c, group, layer, s, mark)) {
|
||||
const bool ok = rad_event_wait(c->stream(), g_pipe.done) >= 0;
|
||||
g_pipe.inflight = -1;
|
||||
rad_ride_release(mark, nullptr, 0);
|
||||
if (ok) {
|
||||
count_stage(c, layer, s.bytes);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
rad_ride_release(mark, nullptr, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (serial_stage(c, s) < 0) {
|
||||
rad_step_fail(c, "kv streaming: the prefill stage copy failed; the layer's attention "
|
||||
"would read the previous layer's bytes");
|
||||
return 1;
|
||||
}
|
||||
count_stage(c, layer, s.bytes);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The layer after this one in this group's binding, if it has one. */
|
||||
int next = -1;
|
||||
const auto& ls = kb->layer_slot;
|
||||
for (int l = layer + 1; l < (int)ls.size(); ++l)
|
||||
if (c->kv_layer_index(group, l) >= 0) { next = l; break; }
|
||||
if (next < 0 || !dma_stage(c)) return 0;
|
||||
if (!g_pipe.done && rad_event_create_local(&g_pipe.done) < 0) g_pipe.done = nullptr;
|
||||
if (!g_pipe.done) return 0;
|
||||
StageSrc s;
|
||||
if (!stage_src(c, group, kvb, next, &s)) return 0;
|
||||
/* No spare ride: the next layer's wait copies it on the compute stream instead. */
|
||||
void* mark = nullptr;
|
||||
if (rad_ride(c->stream(), &mark) != RAD_OK) {
|
||||
if (!g_warned_ride) {
|
||||
g_warned_ride = true;
|
||||
RAD_INFO("rank %d: kv streaming: no spare ride for the stage's DMA list, so its layer "
|
||||
"copies serially", c->rank());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (!arm_and_submit(c, group, next, s, mark)) {
|
||||
rad_ride_release(mark, nullptr, 0);
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
|
||||
extern "C" int rad_kv_stage_layer(RadCtx* c, rad_kvgroup group, const RadKVGroupBatch* kvb,
|
||||
int layer) {
|
||||
using namespace rad;
|
||||
if (!c || !kvb || !kvb->stream) return 0;
|
||||
return kvstage_wait(static_cast<Ctx*>(c), group, kvb, layer);
|
||||
}
|
||||
|
||||
/* The two exported names are the same question ("is this layer staged?"); the frozen ABI keeps
|
||||
* both signatures, but only one body. */
|
||||
extern "C" int rad_kv_stage_wait(RadCtx* c, rad_kvgroup group, const RadKVGroupBatch* kvb,
|
||||
int layer) {
|
||||
return rad_kv_stage_layer(c, group, kvb, layer);
|
||||
}
|
||||
|
||||
extern "C" int rad_kv_stage_next(RadCtx* c, rad_kvgroup group, int layer) {
|
||||
using namespace rad;
|
||||
if (!c) return 0;
|
||||
return kvstage_next(static_cast<Ctx*>(c), group, layer);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/* stage.h -- the one-layer prefill stage: per pass, per layer, copy the live part of the layer's
|
||||
* mirror row into the stage view of the group's pool, on the card's DMA engine, one layer ahead.
|
||||
*
|
||||
* ORDERING RULE -- a correctness invariant, not a preference.
|
||||
*
|
||||
* Aliasing every layer to offset 0 is safe only because, for each layer L, these four happen in
|
||||
* this order:
|
||||
*
|
||||
* 1. the stage copy of L has landed;
|
||||
* 2. L's store writes this chunk's tokens into the stage (the stage copy reads the mirror, which
|
||||
* the store has not written yet, so a copy landing after the store would put the mirror's
|
||||
* older bytes back over them);
|
||||
* 3. L's attention reads the stage;
|
||||
* 4. the stage copy of L+1 starts, and nothing reads the stage until its wait.
|
||||
*
|
||||
* The copy is a list on the card's DMA engine, which holds no CU slot, so the order does not rest
|
||||
* on one stream's issue order alone:
|
||||
*
|
||||
* - 1 before 2 is a barrier the compute stream takes on an event the *list itself* completes
|
||||
* (rad_event_dma hands the list the event's completion word as its `done`), issued immediately
|
||||
* before the layer's store. `rad_event_wait` is a barrier packet, so the wait is device-side and
|
||||
* in issue order: nothing between it and the store can slip in front of it;
|
||||
* - 4 after 3 is a ride (`rad_ride`) taken on the compute stream once every attention launch of
|
||||
* layer L has been issued, so the signal it completes is behind all of L's reads. The list polls
|
||||
* that signal, so it starts when L's attention is done, and crosses the link under L's MLP and
|
||||
* L+1's projections.
|
||||
*
|
||||
* What breaks it, in the order it is likely to happen:
|
||||
*
|
||||
* - taking the ride *before* all of L's attention launches. A prefill layer's attention is issued
|
||||
* in `qsa_gq_rows`-wide chunks, so a ride taken at the first chunk starts the copy of L+1 while
|
||||
* the second chunk still reads the stage. That is why the seam is after the loop
|
||||
* (`rad_kv_stage_next`), not in `resolve_sel`;
|
||||
* - waiting for the copy *after* the layer's store instead of before it. The store's bytes are the
|
||||
* newest ones and the copy's are older ones from the same mirror row;
|
||||
* - arming the completion event on the host instead of on the compute stream. An older 0 from the
|
||||
* previous layer's list would then release this layer's wait early, and a fresh 1 would cut the
|
||||
* previous wait short -- one signal, two uses, no order between them. The record packet puts the
|
||||
* arm in stream order, behind the previous wait;
|
||||
* - the copy on lane 1, or on a bare second stream with no recorded event pair (see §3.5.1);
|
||||
* - re-issuing the copy host-side without `tape_host`. The list is not a queue packet, so a
|
||||
* recorded pass must re-run the arm and the submit where it made them; `Ctx::tape_kv_stage`
|
||||
* does that with the tape paused, as `xprefetch.cpp` does for the prefetch.
|
||||
*
|
||||
* `haz_mark` sees the KV operand as the same byte range for every aliased layer, so a stage and the
|
||||
* previous layer's attention are never marked overlap. That is a relaxation check, not the
|
||||
* protection; do not weaken it (core/runtime/issue.cpp, "haz_mark"). The DMA copy is not a dispatch
|
||||
* at all, so the hazard pass cannot see it: the barriers above are its whole ordering.
|
||||
*
|
||||
* VOLUME -- and the one way this file can be silently wrong.
|
||||
*
|
||||
* The copy moves the mirror row up to the highest block id the coming attention can read, not the
|
||||
* whole row: `n_blocks * block_bytes` is the context allocation, and a 17k-token request was
|
||||
* paying 24 GiB of PCIe to stage 102 MiB of live KV (docs/KV-STREAMING.md §3.5, §5 Phase 3).
|
||||
*
|
||||
* `stage_live_bytes` (kvstream_bound_live, called once per released batch from the engine) is an
|
||||
* UPPER bound on the block ids the pass can name, and an UNDERESTIMATE IS NOT A CRASH: the attention
|
||||
* would read bytes the copy never moved -- whatever the previous layer left there -- and produce
|
||||
* fluent wrong text. It cannot be too small because:
|
||||
*
|
||||
* - every id a block table can hold came out of the group's allocator and is held, and
|
||||
* `KVManager::held_top_blocks` is one bucket past the highest held block it has counted. The
|
||||
* histogram is updated on every acquisition (grab_block, reserve_specific_block) and on the last
|
||||
* release, so it counts sequence blocks and cache-retained blocks alike;
|
||||
* - the stage view is identity-addressed from block 0 for every layer, so copying [0, bound)
|
||||
* covers every id below it, whether or not the ids are dense;
|
||||
* - it is re-evaluated every step, so the chunk that appends a block raises it before the pass
|
||||
* that reads it. A partial tail block and a speculative token live in blocks `ensure` took
|
||||
* before the step was built, so they are held and inside the bound; the bound is in blocks, so
|
||||
* a partial block is copied whole and its unwritten tail is the mirror's zeros.
|
||||
*
|
||||
* Ascending allocation is NOT assumed. The free list is a min-heap, so a fresh sequence usually
|
||||
* takes 0,1,2..., but a prefix-cache-retained block or a restore's `reserve_specific_block` can
|
||||
* leave a live block above a free one, and a reused id need not be the sequence's next. That is
|
||||
* why the bound is the allocator's high-water mark and not `ceil(end / block_size)`.
|
||||
*
|
||||
* What the bound costs: it is the whole group's held set, including blocks another sequence or the
|
||||
* prefix cache holds, so a server with a long cached conversation behind it stages more than the
|
||||
* request reads. It is never less, and a group with nothing held (0) stages the whole row as before.
|
||||
*
|
||||
* A backend with no DMA queue (the host backend, a test) copies on the compute stream exactly as
|
||||
* before, in the same four-step order.
|
||||
*/
|
||||
#pragma once
|
||||
#include "mem/kv.h"
|
||||
#include "runtime/ctx.h"
|
||||
|
||||
namespace rad {
|
||||
|
||||
/* Select the view of every streamed group a pass names, and reset the stage's per-pass state.
|
||||
* Called once before the pass loop (seams in core/runtime/ctx.cpp). Here rather than in bind.h
|
||||
* because it is the pass's opening move -- the driver's, not the layout's -- and because a caller
|
||||
* that only builds a step's view must not link the driver (core/sched/batch.cpp). */
|
||||
void kvstream_enter(Ctx* c, const RadBatch* b);
|
||||
|
||||
/* Carry the block manager's upper bound on live block ids into every streamed group's stage view,
|
||||
* for the step about to run. Called for every rank from Engine::prepare_release, with the ranks
|
||||
* parked, after the step's blocks are ensured and before its layers are staged; rank 0's manager is
|
||||
* the one that hands the ids out. A group with nothing held leaves the view at 0, which copies the
|
||||
* whole row (stage.h, "VOLUME"). */
|
||||
void kvstream_bound_live(Ctx* c, const KVManager& kv);
|
||||
|
||||
/* The driver, as a tape step re-runs it (Ctx::tape_kv_stage). `after` is the seam between the
|
||||
* layer's store and the next layer's copy. */
|
||||
int kvstage_run(Ctx* c, int group, int layer, bool after);
|
||||
|
||||
} /* namespace rad */
|
||||
|
||||
extern "C" {
|
||||
/* Is this layer staged? 1: its row is whole for this pass (the caller must not resolve it). 0: the
|
||||
* group has no stage view, so the caller proceeds normally. Called from `resolve_sel`, which is the
|
||||
* pass's later seam: the wait itself happens before the layer's store, so this only reports. */
|
||||
int rad_kv_stage_layer(RadCtx* c, rad_kvgroup group, const RadKVGroupBatch* kvb, int layer);
|
||||
/* Issue the next bound layer's stage copy, after this layer's attention has been issued. */
|
||||
int rad_kv_stage_next(RadCtx* c, rad_kvgroup group, int layer);
|
||||
}
|
||||
Reference in new issue
Block a user