Archived
device and plugin boundary: kv_resolve, the streamed store rows, ABI 30
This commit is contained in:
1 parent
5a9ce4912b
commit
cce687bf2c
34 files changed
+3643
-29
No files matched your search
+17
-3
@@ -3,6 +3,8 @@
|
||||
*/
|
||||
#include <vector>
|
||||
#include "ctx.h"
|
||||
#include "kvstream/bind.h"
|
||||
#include "kvstream/stage.h"
|
||||
#include "xp_state.h"
|
||||
#include "../device/device.h"
|
||||
|
||||
@@ -921,7 +923,7 @@ int Ctx::lane_join(int from, int to) {
|
||||
* (below). */
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 360,
|
||||
"RadBatch changed: check that Ctx::pass_key reads every new field a pass depends on");
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 72,
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 80,
|
||||
"RadKVGroupBatch changed: check that Ctx::pass_key reads every new field");
|
||||
|
||||
uint64_t Ctx::pass_key(const RadBatch* b) const {
|
||||
@@ -957,6 +959,11 @@ uint64_t Ctx::pass_key(const RadBatch* b) const {
|
||||
mix((uint64_t)k.group); ptr(k.slot_mapping); ptr(k.block_table);
|
||||
mix((uint64_t)k.block_table_pitch); ptr(k.seqused); ptr(k.state_index);
|
||||
mix((uint64_t)k.state_index_pitch); mix((uint64_t)k.block_size); mix((uint64_t)k.max_blocks);
|
||||
/* Null on a fully resident group, a fixed set of addresses on a streamed one. Two groups
|
||||
* that differ here hand a kernel different memory, so they are different passes -- and the
|
||||
* view's *address* must not enter the key: the builder's Out slot rotates every step, which
|
||||
* would make every streamed pass a new one (kvstream/bind.h, kvstream_view_key). */
|
||||
mix(kvstream_view_key(k.stream));
|
||||
}
|
||||
mix((uint64_t)level_);
|
||||
mix(dense_epoch_);
|
||||
@@ -1229,6 +1236,12 @@ int Ctx::play_pass(PassTape& t) {
|
||||
rc = xp_ ? xp_step_run(st.a) : RAD_OK;
|
||||
lane_ = l0;
|
||||
break;
|
||||
case kTapeKvStage:
|
||||
case kTapeKvStageAfter:
|
||||
lane_ = st.lane;
|
||||
rc = kvstage_run(this, st.a, st.b, st.kind == kTapeKvStageAfter);
|
||||
lane_ = l0;
|
||||
break;
|
||||
}
|
||||
if (rc < 0) {
|
||||
if (step_status_ >= 0) step_status_ = rc;
|
||||
@@ -1254,7 +1267,8 @@ int Ctx::run_pass(const RadBatch* b) {
|
||||
/* A pass the stager armed for is issued live: see runtime/stager.h. */
|
||||
if (!tape_on_ || staging_) { bump(pc_unkeyed_); arch_step_(this, b); return RAD_OK; }
|
||||
const uint64_t key = pass_key(b);
|
||||
if (key == 0) { bump(pc_unkeyed_); arch_step_(this, b); return RAD_OK; }
|
||||
if (key == 0) { bump(pc_unkeyed_); arch_step_(this, b);
|
||||
return RAD_OK; }
|
||||
if (std::find(tape_refused_.begin(), tape_refused_.end(), key) != tape_refused_.end()) {
|
||||
bump(pc_refused_);
|
||||
arch_step_(this, b);
|
||||
@@ -1320,7 +1334,6 @@ int Ctx::run_pass(const RadBatch* b) {
|
||||
}
|
||||
|
||||
int Ctx::run_step(const RadBatch* b) {
|
||||
|
||||
if (!b) return RAD_E_INVAL;
|
||||
if (!arch_step_) {
|
||||
RAD_WARN("Ctx::run_step: no architecture step function was installed");
|
||||
@@ -1337,6 +1350,7 @@ int Ctx::run_step(const RadBatch* b) {
|
||||
step_status_ = RAD_OK;
|
||||
step_fail_.clear();
|
||||
prof_n_ = 0;
|
||||
kvstream_enter(this, b);
|
||||
{
|
||||
const int rc = run_pass(b);
|
||||
if (rc < 0 && step_status_ >= 0) step_status_ = rc;
|
||||
|
||||
+57
-1
@@ -141,6 +141,18 @@ struct RouteSlot {
|
||||
* LINEAR [n_states, n_head_kv, state_dim[0], state_dim[1]]
|
||||
* CONV [n_states, n_head_kv * head_dim, conv_slots] channel-major, then time
|
||||
*/
|
||||
/* One of the two views a streamed group's allocation is addressed through. `depth` is the number
|
||||
* of layer stripes the view presents: the ring keeps the group's `n_layers`, the stage presents
|
||||
* one and every layer aliases offset 0. The field is a depth and NOT a rewritten `layer_slot`:
|
||||
* that table is also the host mirror's row (rad_kv_layer_index), so collapsing it would write all
|
||||
* layers into one mirror row (docs/KV-STREAMING.md §3.5.1). */
|
||||
struct KVViewState {
|
||||
int64_t layer_stride = 0;
|
||||
int64_t bytes = 0;
|
||||
int64_t depth = 0; /* 0: n_layers; 1: every layer at offset 0 */
|
||||
int64_t shape0 = 0; /* the operand's [blocks] extent for this view */
|
||||
};
|
||||
|
||||
struct KVPoolBinding {
|
||||
void* base = nullptr;
|
||||
int64_t layer_stride = 0; /* bytes between one layer's cache and the next */
|
||||
@@ -152,8 +164,26 @@ struct KVPoolBinding {
|
||||
std::vector<int32_t> layer_slot;
|
||||
uint32_t rank = 0; /* 0 when the group has no plan: the operand stays a flat span */
|
||||
int64_t shape[RAD_MAX_RANK] = {0};
|
||||
/* The active view's stripe count: 0 is n_layers, 1 is the one-layer prefill stage. */
|
||||
int64_t depth = 0;
|
||||
/* Both fixed views over the group's one allocation. The ring is always present; the stage only
|
||||
* for a group whose plan depth is 1 (its ring is smaller than its context). The active fields
|
||||
* above are set from one of these before each pass (kvstream_enter), so a recorded pass that
|
||||
* used a view replays against it: the pass key already carries the phase, which selects it. */
|
||||
KVViewState ring;
|
||||
KVViewState stage;
|
||||
bool has_stage = false;
|
||||
/* Bytes of one layer's mirror row a staged pass must copy, from the block manager's highest
|
||||
* held block (kvstream/stage.h). 0 copies the whole row, which is always correct and is what a
|
||||
* caller with no manager (a test, a harness) leaves it at. Set once per step, before the pass,
|
||||
* because the bound grows as a prefill appends blocks. */
|
||||
int64_t stage_live_bytes = 0;
|
||||
};
|
||||
|
||||
/* The KV stage's driver, run again where a recorded pass made it (kvstream/stage.cpp). `after` is
|
||||
* the call the architecture makes after a layer's attention instead of before it. */
|
||||
int kvstage_run(Ctx* c, int group, int layer, bool after);
|
||||
|
||||
struct CtxDesc {
|
||||
Program* program = nullptr;
|
||||
int rank = 0;
|
||||
@@ -213,6 +243,20 @@ public:
|
||||
int rank() const { return rank_; }
|
||||
/* Whether repeated passes are recorded and played back (prepare() says when they are not). */
|
||||
bool records_passes() const { return tape_on_; }
|
||||
/* Whether the pass being issued is being recorded. Work a tape cannot record -- a DMA
|
||||
* submission -- must be re-run where the pass made it instead (tape_kv_stage). */
|
||||
bool recording() const { return rec_ != nullptr; }
|
||||
/* While recording: a KV stage call becomes a host step that re-runs at every play, because the
|
||||
* DMA list it hands the engine and the stream wait on the list's completion word are neither of
|
||||
* them queue packets (kvstream/stage.cpp; xprefetch.cpp does the same for the prefetch). */
|
||||
int tape_kv_stage(bool after, int group, int layer) {
|
||||
TapeStep st;
|
||||
st.kind = (uint8_t)(after ? kTapeKvStageAfter : kTapeKvStage);
|
||||
st.a = group;
|
||||
st.b = layer;
|
||||
return tape_host(st, [&] { return kvstage_run(this, group, layer, after); });
|
||||
} /* This rank's device ordinal, for the calls that name a card rather than a stream. */
|
||||
int device() const { return device_; }
|
||||
int world_size() const { return world_size_; }
|
||||
RadStream stream() const { return stream_; }
|
||||
Program* program() const { return program_; }
|
||||
@@ -230,6 +274,17 @@ public:
|
||||
|
||||
void* weight_ptr(rad_weight w);
|
||||
void* buf_ptr(rad_buf b);
|
||||
/* The group-local position of absolute model layer `layer` in the binding's layer_slot, or -1.
|
||||
* rad_kv_layer_index (rad_runtime.h) exposes it to a plugin, which needs it for the host
|
||||
* mirror: that is laid out by the group's layers, not by model layer numbers. */
|
||||
int64_t kv_layer_index(rad_kvgroup g, int layer) const;
|
||||
/* The group's pool binding, for the view switch and the stage copy. */
|
||||
const KVPoolBinding* kv_binding(rad_kvgroup g) const {
|
||||
return g < kv_.size() ? &kv_[(size_t)g] : nullptr;
|
||||
}
|
||||
KVPoolBinding* kv_binding(rad_kvgroup g) {
|
||||
return g < kv_.size() ? &kv_[(size_t)g] : nullptr;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------- routing (spec §5.5) */
|
||||
int route_report(int layer, const RadRouting* r);
|
||||
@@ -435,7 +490,8 @@ private:
|
||||
* issue. A difference means the key misses a dependency; the step fails, naming the first
|
||||
* differing dispatch. */
|
||||
enum TapeKind : uint8_t { kTapePlay = 0, kTapeTable = 1, kTapeHost = 2, kTapeRoute = 3,
|
||||
kTapeXp = 4, kTapeXpMark = 5, kTapeXpStep = 6 };
|
||||
kTapeXp = 4, kTapeXpMark = 5, kTapeXpStep = 6,
|
||||
kTapeKvStage = 7, kTapeKvStageAfter = 8 };
|
||||
/* A prefetch call (Ctx::xp_predict), run again where the pass made it. */
|
||||
struct TapeXp {
|
||||
RadPrefetchNext next{};
|
||||
|
||||
+14
-1
@@ -19,6 +19,7 @@
|
||||
#include <cstdlib>
|
||||
#include "ctx.h"
|
||||
#include "device/device.h"
|
||||
#include "kvstream/bind.h"
|
||||
#include "wtab.h"
|
||||
|
||||
#include <cstring>
|
||||
@@ -802,7 +803,7 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
|
||||
const int64_t layer = (o.offset >= 0 && (size_t)o.offset < b.layer_slot.size())
|
||||
? b.layer_slot[(size_t)o.offset]
|
||||
: -1;
|
||||
const int64_t off = layer * b.layer_stride;
|
||||
const int64_t off = kv_binding_offset(b, layer);
|
||||
if (layer < 0 || (b.bytes && off + b.layer_stride > b.bytes))
|
||||
return abort_step_msg(h, RAD_E_INVAL,
|
||||
fmt("operand %d asks KV group %u for layer %lld, which is "
|
||||
@@ -1044,6 +1045,7 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
|
||||
g_iss_lau_p = g_iss_lau;
|
||||
}
|
||||
} else {
|
||||
/* The launch alone, split from the band scan and operand resolution `own` covers above. */
|
||||
s = ip.launch(la, cur_stream());
|
||||
}
|
||||
if (hold) {
|
||||
@@ -1251,6 +1253,17 @@ int rad_step_fail(RadCtx* c, const char* what) {
|
||||
}
|
||||
|
||||
const RadBatch* rad_batch(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->batch() : nullptr; }
|
||||
|
||||
int64_t rad::Ctx::kv_layer_index(rad_kvgroup g, int layer) const {
|
||||
if (g >= kv_.size() || !kv_[g].base || layer < 0) return -1;
|
||||
const std::vector<int32_t>& ls = kv_[g].layer_slot;
|
||||
if ((size_t)layer >= ls.size()) return -1;
|
||||
return ls[(size_t)layer];
|
||||
}
|
||||
|
||||
int rad_kv_layer_index(RadCtx* c, rad_kvgroup group, int layer) {
|
||||
return c ? (int)static_cast<rad::Ctx*>(c)->kv_layer_index(group, layer) : -1;
|
||||
}
|
||||
int rad_rank(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->rank() : 0; }
|
||||
int rad_world_size(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->world_size() : 1; }
|
||||
RadStream rad_stream(RadCtx* c) { return c ? static_cast<rad::Ctx*>(c)->stream() : nullptr; }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* kvbind.cpp -- see kvbind.h. */
|
||||
#include "kvbind.h"
|
||||
|
||||
#include "kvstream/bind.h"
|
||||
#include "mem/kv.h"
|
||||
|
||||
namespace rad {
|
||||
@@ -12,6 +13,8 @@ void bind_kv_pools(const Program& p, const std::function<const KVGroupPlan*(int3
|
||||
const KVGroupPlan* gp = plan_of((int32_t)g);
|
||||
if (!gp) continue;
|
||||
KVPoolBinding& b = (*out)[g];
|
||||
/* Stateful groups keep these as they always were; a paged group's are overwritten by
|
||||
* kv_bind_views with the active view's arithmetic. */
|
||||
b.base = gp->base;
|
||||
b.layer_stride = gp->layer_stride();
|
||||
b.bytes = gp->pool_bytes;
|
||||
@@ -34,10 +37,15 @@ void bind_kv_pools(const Program& p, const std::function<const KVGroupPlan*(int3
|
||||
case RAD_KV_WINDOW:
|
||||
case RAD_KV_RING:
|
||||
b.rank = 4;
|
||||
b.shape[0] = gp->n_blocks;
|
||||
/* `shape[0]` follows the view and is filled by kv_bind_views below: the resident
|
||||
* window's depth for the ring, the group's context for the prefill stage
|
||||
* (docs/KV-STREAMING.md §3.1, §4.1). */
|
||||
b.shape[0] = gp->pool_blocks();
|
||||
b.shape[1] = d.n_head_kv;
|
||||
b.shape[2] = gp->block_size;
|
||||
b.shape[3] = 2 * d.head_dim;
|
||||
/* Both fixed views over the group's one allocation, with the ring active. */
|
||||
kv_bind_views(*gp, &b);
|
||||
break;
|
||||
case RAD_KV_LINEAR:
|
||||
b.rank = 4;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "runtime/tierexec.h"
|
||||
|
||||
#include "rad_internal.h"
|
||||
#include "kvstream/plan.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
@@ -31,12 +32,18 @@ int TierExec::configure(Registry* reg, KVManager* kv, IdleTiers* tiers,
|
||||
for (int32_t g : groups) {
|
||||
const KVGroupPlan* p = kv->plan(g);
|
||||
if (!p || !p->paged()) continue;
|
||||
/* A streamed group's pool is one layer of its context, so a gather of layer L would copy
|
||||
* the last staged layer n_layers times. Its mirror is the tier and the residency map
|
||||
* restores a block on the next resolve (docs/KV-STREAMING.md §4.1). */
|
||||
if (!kv_tier_cached(*p)) continue;
|
||||
GroupInfo gi;
|
||||
gi.g = g;
|
||||
gi.n_layers = p->n_layers;
|
||||
gi.frag = p->layer_bytes_per_block();
|
||||
gi.layer_stride = p->layer_stride();
|
||||
gi.n_blocks = p->n_blocks;
|
||||
/* The pool's depth: a streamed group backs fewer blocks than it addresses, and the tier
|
||||
* gathers fragments of the blocks that are here (docs/KV-STREAMING.md §3.1). */
|
||||
gi.n_blocks = p->pool_blocks();
|
||||
gi.base = p->base;
|
||||
gi.replicated = p->replicated;
|
||||
if (p->zero) {
|
||||
@@ -109,6 +116,12 @@ int TierExec::slot_layout(const std::vector<const KVManager*>& ranks,
|
||||
for (int32_t g : groups) {
|
||||
const KVGroupPlan* p0 = ranks[0]->plan(g);
|
||||
if (!p0 || !p0->paged()) continue;
|
||||
/* A streamed group is not part of a tier entry: its pool is one layer deep, so a gather
|
||||
* of layer L would copy the last staged layer n_layers times, and its host mirror is the
|
||||
* tier (docs/KV-STREAMING.md §4.1). TierExec::stage skips the same groups, so this
|
||||
* arithmetic and its gather offsets agree. A streamed group is never replicated, so
|
||||
* leaving it out of the replicated window too is the safe rule. */
|
||||
if (!kv_tier_cached(*p0)) continue;
|
||||
bool replicated = false;
|
||||
for (size_t i = 0; i < ranks.size(); ++i) {
|
||||
const KVGroupPlan* p = ranks[i]->plan(g);
|
||||
|
||||
@@ -202,7 +202,7 @@ void Ctx::xp_serve(XpState& x) {
|
||||
if (r.mark) rad_ride_release(r.mark, r.ms, kXpMarkHold);
|
||||
continue;
|
||||
}
|
||||
if (rad_xp_list(x.device, r.mark, n, src, dst, bytes, x.done + r.g, r.seq) == RAD_OK) {
|
||||
if (rad_xp_list(x.device, r.mark, n, src, dst, bytes, x.done + r.g, r.seq, 0) == RAD_OK) {
|
||||
++x.lists;
|
||||
if (r.mark) x.marks.emplace_back(r.mark, x.lists);
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user