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
+56
-12
@@ -1,6 +1,9 @@
|
||||
/* kv.cpp -- see kv.h. */
|
||||
#include "mem/kv.h"
|
||||
|
||||
#include "kvstream/carve.h"
|
||||
#include "kvstream/plan.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
@@ -71,7 +74,7 @@ std::string BundleComparison::report() const {
|
||||
|
||||
/* ------------------------------------------------------------------ configure */
|
||||
|
||||
static const char* kv_kind_name(int k) {
|
||||
const char* kv_kind_name(int k) {
|
||||
switch (k) {
|
||||
case RAD_KV_FULL: return "full";
|
||||
case RAD_KV_WINDOW: return "window";
|
||||
@@ -133,6 +136,7 @@ int KVManager::plan_groups(const std::vector<KVGroupInfo>& groups, const Config&
|
||||
* other plan-path code has to know the kind. */
|
||||
p.kind = g.decl.kind == RAD_KV_RING ? RAD_KV_WINDOW : g.decl.kind;
|
||||
p.n_layers = (int64_t)g.layers.size();
|
||||
p.dense_read = g.dense_read;
|
||||
|
||||
if (p.n_layers <= 0) {
|
||||
RAD_ERR("KV group '%s': no layers are bound to it, so its page size is unknown "
|
||||
@@ -309,8 +313,11 @@ int64_t KVManager::ceiling(int64_t ctx) const {
|
||||
int64_t blocks = max_seqs_ * ((ctx + p.block_size - 1) / p.block_size);
|
||||
if (p.kind == RAD_KV_WINDOW && !p.runtime_win && p.window_blocks > 0)
|
||||
blocks = std::min(blocks, window_pool_cap(p));
|
||||
need += blocks * p.bytes_per_block;
|
||||
need += kv_ceiling_bytes(p, blocks, stream_.resident_blocks(p));
|
||||
}
|
||||
/* The streamed groups' residency maps are reserved from this same pool; leaving them out makes
|
||||
* a pool given its whole ceiling short by their bytes (kvstream/carve.h). */
|
||||
need += kvstream_maps_charged(plans_, stream_, ctx, max_seqs_);
|
||||
return need;
|
||||
}
|
||||
|
||||
@@ -415,9 +422,15 @@ int64_t KVManager::paged_split(int64_t budget_bytes, std::vector<int64_t>* block
|
||||
/* Zero groups take no part in the split; they get the shared table's block count, which is
|
||||
* what the costed groups settle on here or, under the cap, on the ranks holding the cache. */
|
||||
std::vector<int32_t> open, zeros;
|
||||
for (const auto& p : plans_)
|
||||
if (p.paged()) (p.zero ? zeros : open).push_back(p.index);
|
||||
/* Every paged group zero: this rank limits nothing, so take the cap, or report unbounded. */
|
||||
bool dense = false;
|
||||
for (const auto& p : plans_) {
|
||||
if (!p.paged()) continue;
|
||||
if (p.zero) zeros.push_back(p.index);
|
||||
else if (kvstream_dense(p, plans_, stream_)) dense = true; /* its carve is a floor */
|
||||
else if (!stream_.windowed(p.index)) open.push_back(p.index);
|
||||
}
|
||||
kvstream_split_windows(plans_, stream_, max_ctx_, max_seqs_, &remaining, &out);
|
||||
/* Every paged group is zero: this rank limits nothing, so take the cap, or report unbounded. */
|
||||
if (open.empty() && !zeros.empty()) {
|
||||
const int64_t tokens = tokens_cap_ > 0 ? tokens_cap_ : kUnboundedTokens;
|
||||
if (blocks) {
|
||||
@@ -453,6 +466,10 @@ int64_t KVManager::paged_split(int64_t budget_bytes, std::vector<int64_t>* block
|
||||
}
|
||||
for (int32_t i : open) out[i] = remaining > 0 ? tokens / plans_[i].block_size : 0;
|
||||
for (int32_t i : zeros) out[i] = remaining > 0 ? zero_blocks(plans_[i], tokens) : 0;
|
||||
/* A dense-read group's floor is already paid and has no share of the split, so a pool with no
|
||||
* other open group still pages one full-length sequence: that is the count the report and the
|
||||
* admission cap must see, even though the group addresses max_seqs of them. */
|
||||
if (dense && open.empty() && remaining > 0) tokens = max_ctx_;
|
||||
if (blocks) *blocks = std::move(out);
|
||||
return tokens;
|
||||
}
|
||||
@@ -486,8 +503,13 @@ int64_t KVManager::zero_blocks(const KVGroupPlan& p, int64_t tokens) const {
|
||||
}
|
||||
|
||||
int KVManager::carve_into(Pool& kv_pool) {
|
||||
/* The pool size is the resolver's; this only splits it between the groups. */
|
||||
const int64_t budget_bytes = kv_pool.avail();
|
||||
/* The pool size is the resolver's; this only splits it between the groups. The streamed
|
||||
* groups' maps were reserved from this pool before the carve (KVStreamStore::alloc), so add
|
||||
* them back: paged_split() charges them as part of those groups' windows (kvstream/carve.h),
|
||||
* and a budget that already excludes them would be charged twice and leave the resident
|
||||
* groups one map short. */
|
||||
const int64_t budget_bytes = kv_pool.avail() +
|
||||
kvstream_maps_charged(plans_, stream_, max_ctx_, max_seqs_);
|
||||
const int64_t fixed = fixed_;
|
||||
pool_base_ = kv_pool.base();
|
||||
pool_ = &kv_pool;
|
||||
@@ -505,11 +527,21 @@ int KVManager::carve_into(Pool& kv_pool) {
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
|
||||
/* ---- the resident windows, before anything is split (kvstream/carve.h) ---- */
|
||||
RAD_TRY(kvstream_window_fit(plans_, stream_, fixed, budget_bytes, max_ctx_, max_seqs_));
|
||||
|
||||
/* ---- the paged split; under a cap, the least rank's ---- */
|
||||
std::vector<int64_t> blocks;
|
||||
(void)paged_split(budget_bytes, &blocks);
|
||||
for (auto& p : plans_) if (p.paged()) p.n_blocks = blocks[p.index];
|
||||
|
||||
/* How much of that the device pool backs, and the refusal of a streamed plan with nothing to
|
||||
* back the rest (kvstream/carve.h). */
|
||||
RAD_TRY(kvstream_apply(plans_, stream_, max_ctx_, max_seqs_));
|
||||
|
||||
/* A streamed group's `n_blocks` is its context, not the window the split gave it, so this asks
|
||||
* the right question of it -- can the group address anything at all -- and its window (which is
|
||||
* what `blocks[p.index]` holds) never decides whether the carve goes ahead. */
|
||||
for (auto& p : plans_) {
|
||||
if (!p.paged()) continue;
|
||||
if (p.n_blocks <= 0 && p.zero) {
|
||||
@@ -531,7 +563,7 @@ int KVManager::carve_into(Pool& kv_pool) {
|
||||
/* ---- carve. Every reservation comes from the KV pool only; the mover works inside the
|
||||
* weight slab and cannot reach it (spec §6). ---- */
|
||||
for (auto& p : plans_) {
|
||||
int64_t bytes = p.paged() ? p.n_blocks * p.bytes_per_block
|
||||
int64_t bytes = p.paged() ? kv_pool_bytes(p)
|
||||
: p.n_states * p.bytes_per_state;
|
||||
if (p.zero) {
|
||||
p.base = nullptr;
|
||||
@@ -625,14 +657,19 @@ int KVManager::carve_into(Pool& kv_pool) {
|
||||
return p.runtime_win ? "ring" : kv_kind_name(p.kind);
|
||||
};
|
||||
for (const auto& p : plans_) {
|
||||
if (p.paged())
|
||||
if (p.paged()) {
|
||||
report_ += fmt(" %-14s %-6s %3lld layers block %4lld tok page %8s "
|
||||
"%7lld blocks = %9s (%lld tokens)\n",
|
||||
p.name.c_str(), kind_of(p), (long long)p.n_layers,
|
||||
(long long)p.block_size, humanb(p.bytes_per_block).c_str(),
|
||||
(long long)p.n_blocks, humanb(p.pool_bytes).c_str(),
|
||||
(long long)(p.n_blocks * p.block_size));
|
||||
else
|
||||
/* Both tiers of a streamed group in one line (kvstream/carve.h): its pool is smaller
|
||||
* than its context, so the line above would read as a pool that holds the whole
|
||||
* context. */
|
||||
if (p.streamed())
|
||||
report_ += kvstream_report_line(p);
|
||||
} else
|
||||
report_ += fmt(" %-14s %-6s %3lld layers state %8s %7lld slots = %9s\n",
|
||||
p.name.c_str(), kind_of(p), (long long)p.n_layers,
|
||||
humanb(p.bytes_per_state).c_str(),
|
||||
@@ -758,6 +795,9 @@ int64_t KVManager::total_blocks(int32_t g) const {
|
||||
int64_t KVManager::live_blocks(int32_t g) const {
|
||||
return (g >= 0 && g < (int32_t)live_.size()) ? live_[(size_t)g] : 0;
|
||||
}
|
||||
int64_t KVManager::held_top_blocks(int32_t g) const {
|
||||
return (g >= 0 && g < (int32_t)plans_.size()) ? held_top((size_t)g) : 0;
|
||||
}
|
||||
|
||||
/* The larger of one prefill chunk and the recent shortfall peak (see buffer_bytes in kv.h). */
|
||||
int64_t KVManager::buf_blocks(size_t i) const {
|
||||
@@ -786,8 +826,12 @@ int64_t KVManager::available_blocks(int32_t g) const {
|
||||
|
||||
bool KVManager::lendable(int32_t g) const {
|
||||
const KVGroupPlan* p = plan(g);
|
||||
/* A streamed group never lends. Its pool is the resident window off which a residency map
|
||||
* reads, so a block lent to the expert slab is one the attention has nowhere to put; the
|
||||
* whole point of the group is that the pool is small, and taking part of it away would leave
|
||||
* fewer slots than one launch's selection needs. */
|
||||
return p && p->kind == RAD_KV_FULL && p->n_blocks > 0 && p->block_size > 0 &&
|
||||
p->bytes_per_block > 0;
|
||||
p->bytes_per_block > 0 && !p->streamed();
|
||||
}
|
||||
|
||||
int64_t KVManager::lent_blocks(size_t i, int64_t tokens) const {
|
||||
@@ -905,7 +949,7 @@ int64_t KVManager::pool_bytes(int32_t g) const {
|
||||
int64_t KVManager::pool_bytes_carved(int32_t g) const {
|
||||
const KVGroupPlan* p = plan(g);
|
||||
if (!p) return 0;
|
||||
if (p->paged()) return p->n_blocks * p->bytes_per_block;
|
||||
if (p->paged()) return kv_pool_bytes(*p);
|
||||
if (p->stateful()) return p->n_states * p->bytes_per_state;
|
||||
return 0;
|
||||
}
|
||||
|
||||
+77
-3
@@ -20,6 +20,7 @@
|
||||
* request by the other.
|
||||
*/
|
||||
#pragma once
|
||||
#include "kvstream/mirror.h"
|
||||
#include "mem/hostarena.h"
|
||||
#include "mem/pools.h"
|
||||
#include "rad_core.h"
|
||||
@@ -54,6 +55,10 @@ struct BundleComparison {
|
||||
};
|
||||
BundleComparison compare_bundling(const std::vector<int64_t>& bundles);
|
||||
|
||||
/* The name a refusal or a report uses for a RAD_KV_* kind (kvstream/carve.cpp names the kind it
|
||||
* refuses; KVManager::report() the one it carved). */
|
||||
const char* kv_kind_name(int kind);
|
||||
|
||||
/* ------------------------------------------------------------------ the runtime window */
|
||||
/* A sequence's per-sequence window on a RAD_KV_WINDOW group, set at runtime (R-SWA, the
|
||||
* "reference + ring" attention of Unlimited-OCR: a full causal prefill over the prompt and image
|
||||
@@ -123,11 +128,31 @@ struct KVGroupPlan {
|
||||
* snapshot would be read. */
|
||||
bool win_snap = false;
|
||||
|
||||
int64_t n_blocks = 0; /* paged groups: blocks carved from the pool */
|
||||
int64_t n_blocks = 0; /* paged groups: blocks the group addresses (its context) */
|
||||
/* Paged groups: blocks the device pool actually backs, which may be fewer than `n_blocks`
|
||||
* when the group's KV streams (docs/KV-STREAMING.md). Block ids stay dense over `n_blocks` --
|
||||
* the manager, the refcounts, the block table, the prefix cache and every `id < n_blocks`
|
||||
* guard count in the group's context, because the table a sequence holds names logical blocks.
|
||||
* Only *addressing* uses this: the pool is carved `n_slots` blocks deep and the stride between
|
||||
* layers is `n_slots` blocks long, so a residency map (block -> resident slot) is what turns a
|
||||
* logical block into an address. Equal to `n_blocks` in every other case, so no existing
|
||||
* layout moves. */
|
||||
int64_t n_slots = 0;
|
||||
/* Layer stripes the pool's *staged* view presents: 0 means `n_layers` (the ring, and every
|
||||
* group that does not stream), 1 means one layer and every layer aliases offset 0. It is a
|
||||
* depth, not a rewritten layer table: `layer_slot` is also the host mirror's row
|
||||
* (rad_kv_layer_index), so collapsing it would write every layer into one mirror row
|
||||
* (docs/KV-STREAMING.md §3.5.1). Set by carve() when the group's ring is smaller than its
|
||||
* context. */
|
||||
int64_t depth = 0;
|
||||
/* rad_kv_group_zero: this rank keeps no cache for the group. The plan keeps its position and
|
||||
* block count (one block table serves every rank); its costs are zero and nothing is carved.
|
||||
* No op on this rank names it (issue refuses a group with no pool bound). */
|
||||
bool zero = false;
|
||||
/* An indexer-style group a decode step reads over the whole live history, so it cannot
|
||||
* stream (kvstream/carve.h). When a sibling streams, its carve is a floor of max_seqs full
|
||||
* contexts, not the split's shared token count. */
|
||||
bool dense_read = false;
|
||||
/* rad_kv_group_replicated: every rank holds the same bytes, so a tier slot carries this
|
||||
* group once, in a window at its front that rank 0 writes and every rank reads
|
||||
* (TierExec::slot_layout). The pool is carved as for any group. */
|
||||
@@ -147,13 +172,21 @@ struct KVGroupPlan {
|
||||
|
||||
bool paged() const { return kind == RAD_KV_FULL || kind == RAD_KV_WINDOW; }
|
||||
bool stateful() const { return kind == RAD_KV_LINEAR || kind == RAD_KV_CONV; }
|
||||
/* Blocks of device pool this group owns: `n_slots` for a paged group, which is `n_blocks`
|
||||
* unless the group streams. The one place the two meanings may be confused is addressing, so
|
||||
* every carve and every stride goes through here and every allocation and every guard uses
|
||||
* `n_blocks`. */
|
||||
int64_t pool_blocks() const { return n_slots > 0 ? n_slots : n_blocks; }
|
||||
/* The pool holds less than the group addresses: blocks beyond `n_slots` live off the card and
|
||||
* a residency map decides which are here. */
|
||||
bool streamed() const { return paged() && n_slots > 0 && n_slots < n_blocks; }
|
||||
|
||||
/* The pool is layer-major, as the ABI requires: a RAD_OPK_KV operand carries the layer as its
|
||||
* offset and resolves to `base + layer * layer_stride` (RadOperand in rad_runtime.h,
|
||||
* KVPoolBinding in core/runtime/ctx.h), so a kernel gets one base pointer per layer and
|
||||
* indexes it with the block table and slot mapping.
|
||||
*
|
||||
* Layer i owns [base + i*layer_stride, + layer_stride): n_blocks blocks of
|
||||
* Layer i owns [base + i*layer_stride, + layer_stride): pool_blocks() blocks of
|
||||
* `bytes_per_block / n_layers` for a paged group, n_states states of
|
||||
* `bytes_per_state / n_layers` for a stateful one. */
|
||||
int64_t layer_bytes_per_block() const {
|
||||
@@ -163,7 +196,8 @@ struct KVGroupPlan {
|
||||
return n_layers > 0 ? bytes_per_state / n_layers : bytes_per_state;
|
||||
}
|
||||
int64_t layer_stride() const {
|
||||
return paged() ? n_blocks * layer_bytes_per_block() : n_states * layer_bytes_per_state();
|
||||
return paged() ? pool_blocks() * layer_bytes_per_block()
|
||||
: n_states * layer_bytes_per_state();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -224,6 +258,33 @@ public:
|
||||
* because paged_split is monotone in the budget. */
|
||||
int64_t pool_mib_for_tokens(int64_t tokens, int64_t max_mib) const;
|
||||
void cap_paged_tokens(int64_t tokens) { tokens_cap_ = tokens > 0 ? tokens : 0; }
|
||||
|
||||
/* ---------------- KV streaming (kvstream/mirror.h, kvstream/carve.h) ----------------
|
||||
*
|
||||
* A streamed group's device pool is a resident window and its blocks' authority is a pinned
|
||||
* host copy plus a device residency map. The state, the cap and the mirror's life cycle are
|
||||
* KVStreamStore's, one member of it below; these are the manager's own names for them, so a
|
||||
* caller (the engine, the tests, core/sched/mem_iface.h) does not need the module's header.
|
||||
* See kvstream/mirror.h for what each one promises. */
|
||||
using StreamMap = RadKVStream; /* the ABI's view: the mirror and the ring's maps */
|
||||
using StreamCost = KVStreamCost;
|
||||
using StreamStats = KVStreamStats;
|
||||
void cap_resident_tokens(int32_t group, int64_t tokens) { stream_.cap(group, tokens); }
|
||||
void set_stream_map(int32_t group, const StreamMap& m) { stream_.set_map(group, m); }
|
||||
const StreamMap* stream_map(int32_t group) const { return stream_.map(group); }
|
||||
const RadKVStream* stream_view(int32_t group) const {
|
||||
return stream_.view(plan(group), group);
|
||||
}
|
||||
StreamCost stream_cost() const { return stream_.cost(plans_, max_ctx_, max_seqs_); }
|
||||
int alloc_stream(MemAllocator& host_alloc, Pool& kv_pool) {
|
||||
return stream_.alloc(plans_, max_ctx_, max_seqs_, host_alloc, kv_pool);
|
||||
}
|
||||
int init_stream(RadStream s) { return stream_.init(plans_, s); }
|
||||
void free_stream(MemAllocator& host_alloc) { stream_.release(host_alloc); }
|
||||
StreamStats stream_stats() const { return stream_.stats(plans_); }
|
||||
void note_stream_traffic(int64_t misses, int64_t bytes) {
|
||||
stream_.note_traffic(misses, bytes);
|
||||
}
|
||||
int carve(Pool& kv_pool);
|
||||
|
||||
const std::vector<KVGroupPlan>& plans() const { return plans_; }
|
||||
@@ -368,6 +429,13 @@ public:
|
||||
/* The carve: the range every block id is addressed against. live_blocks() is what the cache
|
||||
* keeps unlent. */
|
||||
int64_t total_blocks(int32_t group) const;
|
||||
/* One past the highest held block, rounded up to a `held_hist_` bucket (0 on a group with
|
||||
* nothing held). Every id a sequence's block table can name is held, so this is an UPPER bound
|
||||
* on those ids -- what a streamed group's prefill stage copies its mirror row up to
|
||||
* (kvstream/stage.h). It counts blocks the prefix cache or another sequence holds too, so it
|
||||
* is exact only while one request is live; the alternative, a scan of the block tables per
|
||||
* layer, is the one thing the stage cannot afford. */
|
||||
int64_t held_top_blocks(int32_t group) const;
|
||||
int64_t free_states(int32_t group) const;
|
||||
|
||||
/* Pool size in bytes, the only unit in which groups can be summed: paged and stateful groups
|
||||
@@ -622,6 +690,12 @@ private:
|
||||
int64_t fixed_ = 0; /* the non-negotiable floor: states, conv slots, padding */
|
||||
int64_t ckpts_ = 0; /* --checkpoint-slots, kept for the warning on a model with none */
|
||||
int64_t tokens_cap_ = 0; /* cap_paged_tokens(): the least any rank can carve; 0 is none */
|
||||
/* KV streaming's state, by group handle: the resident window cap, the mirror and the ring's
|
||||
* maps, and the resolve's counters (kvstream/mirror.h). Empty and zero on a manager that does
|
||||
* not stream, which is every group on a server without --kv-resident and also a group whose
|
||||
* ring covers its context. */
|
||||
KVStreamStore stream_;
|
||||
|
||||
/* declare_runtime_window(): group -> W. Survives forget(), which plan_groups() calls at its
|
||||
* start: it is a declaration about the program, not about a plan. */
|
||||
std::unordered_map<int32_t, int64_t> runtime_decl_;
|
||||
|
||||
@@ -239,6 +239,15 @@ static int resolve(Config& cfg, const std::vector<VramFacts>& per_rank, VramBudg
|
||||
cfg.vram_weights_mib = to_mib_up(b.weights);
|
||||
const int64_t w_over = cfg.vram_weights_mib * kMiB - b.weights;
|
||||
cfg.vram_kv_mib = to_mib(std::max<int64_t>(b.kv - w_over, 0));
|
||||
/* A streamed plan's maps are charged inside kv_one but reserved outside the paged split, so
|
||||
* the MiB truncation here leaves the pool a whole map short of the one conversation kv_one was
|
||||
* sized for -- the target shape's indexer then caps at 47 185 of its 51 200 blocks. kv_one is a
|
||||
* requirement in that case, not a target: round it back up. Only the streamed arms are held to
|
||||
* it; without streaming this is the pre-existing 1 MiB truncation (204 648 for 204 800), which
|
||||
* is a separate issue and not this feature's. */
|
||||
if (cfg.kv_streams && kv_one > 0 &&
|
||||
b.kv >= kv_one && cfg.vram_kv_mib * kMiB < kv_one)
|
||||
cfg.vram_kv_mib = to_mib_up(kv_one);
|
||||
/* A cache given its whole ceiling is rounded up to cover it, out of the headroom as the
|
||||
* weights' MiB is: truncated, a ceiling under a MiB would be an empty pool and one just over a
|
||||
* whole MiB a pool short of its own context. */
|
||||
|
||||
Reference in new issue
Block a user