This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/kvstream/bind.cpp
T

91 lines
3.5 KiB
C++

/* 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 */