device and plugin boundary: kv_resolve, the streamed store rows, ABI 30

This commit is contained in:
C_Tamas authored and StillDeadcode committed 2026-10-25 09:58:04 +01:00
1 parent 32881da9fd
commit 486c933161
34 files changed
+3643 -29

No files matched your search

+120
View File
@@ -565,6 +565,126 @@ int ref_kv_store(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ kv_store_streamed */
/* The store of a streamed group: the host mirror is the truth, the device pool is a cache of it.
*
* A streamed group's ring is `slots` blocks deep while the block-id space is `blocks`, and a block
* may be absent from it. This op is `kv_store` with the two copies the group keeps:
*
* host[b] is written ALWAYS, at the block's identity row, whatever the map says;
* kv_cache[res[b]] is written only when `res[b] >= 0`, i.e. the block is resident now.
*
* The residency test belongs in the writer rather than in the resolve, and this is the case that
* decides it: at decode the block this step's tokens land in is the block the last step already
* made resident (a block is `ratio` tokens of one sequence), so a writer that only wrote the
* mirror would leave the ring holding that block's first tokens and every later token missing -
* and the resolve, seeing the block resident, would not copy it. Writing the ring when resident
* and the mirror always means a host block is never stale and a slot never holds a block the
* mirror disagrees with, which is the property the resolve's copy relies on.
*
* The order the caller must keep is store, then resolve, then attention (arch/common/
* rad_block_attn_gated_fp8.h): the block the current step writes may be absent from the ring, and
* the resolve then copies whole blocks out of the mirror, so it must see this step's bytes.
*
* The two planes are the same bytes in the same paged layout
* (`[notional block, kv_heads, block_size, 2 * head_dim]`, K then V in a slot), so one conversion
* writes both: the mirror is described as an E4M3 plane over the same row pitch and the loop
* stores into `host` and, when resident, `kv_cache`.
*
* `res` is read, not written: residency is the resolve's decision (libref/ref_kvstream.cpp). A
* `res` entry outside [-1, slots) for a block this call touches, or a slot naming a block past the
* id space, is a state fault and is refused before anything is written. */
int ref_kv_store_streamed(const RadArgs* a, RadStream) {
if (!have(a, 6)) return RAD_E_INVAL;
const RadTensor *k = &a->t[0], *v = &a->t[1], *sm = &a->t[2], *kv = &a->t[3];
const RadTensor *res = &a->t[4], *host = &a->t[5];
const int64_t head_dim = p_int(a, "head_dim", k->rank >= 3 ? k->shape[k->rank - 1] : 0);
const int64_t block_size = p_int(a, "block_size", 0);
const int64_t kv_heads = p_int(a, "n_head_kv", 0);
if (head_dim <= 0 || block_size <= 0 || kv_heads <= 0) return RAD_E_SHAPE;
if (k->dtype != RAD_BF16 || v->dtype != RAD_BF16) return RAD_E_DTYPE;
if (sm->dtype != RAD_I32 || res->dtype != RAD_I32) return RAD_E_DTYPE;
if (host->dtype != RAD_U8) return RAD_E_DTYPE;
/* E4M3 (the fp8 QSA family) or bf16 (the default cache dtype). Streaming is dtype-agnostic
* beyond the writer: both planes are byte planes of the same paged layout whose pitch is one
* block, so a bf16 group doubles every offset and stores the value itself, as kv_store_bf16
* does. A packed fp8_v4/k6_v4 group has no streamed row and is refused. */
const bool fp8 = kv->dtype == RAD_F8E4M3;
if (!fp8 && kv->dtype != RAD_BF16) return RAD_E_UNSUPPORTED;
if (kv->rank != 4) return RAD_E_SHAPE;
if (kv->shape[1] != kv_heads || kv->shape[2] != block_size ||
kv->shape[3] != 2 * head_dim) return RAD_E_SHAPE;
if (host->rank != 2) return RAD_E_SHAPE;
/* One block of one layer. Both planes are dense rows of it: a padded mirror row would copy a
* block's bytes over its neighbour's, and the two copies must address the same offsets. */
const int64_t EB = fp8 ? 1 : 2;
const int64_t row = kv_heads * block_size * 2 * head_dim * EB;
const int64_t slots = kv->shape[0], blocks = host->shape[0];
if (host->shape[1] != row || host->stride[0] * rad_dtype_bytes(host->dtype, 1) != row)
return RAD_E_STRIDE;
if (kv->stride[0] * rad_dtype_bytes(kv->dtype, 1) != row) return RAD_E_STRIDE;
if (slots <= 0 || blocks <= 0) return RAD_E_SHAPE;
if (numel(res) < blocks || res->stride[0] != 1) return RAD_E_SHAPE;
const int64_t M = numel(sm);
if (M <= 0 || numel(k) < M * kv_heads * head_dim || numel(v) < M * kv_heads * head_dim)
return RAD_E_SHAPE;
const int64_t k_ds = k->rank >= 2 ? k->stride[k->rank - 1] : 1;
const int64_t k_hs = k->rank >= 3 ? k->stride[k->rank - 2] : head_dim * k_ds;
const int64_t k_rs = row_stride(k, kv_heads * head_dim);
const int64_t v_ds = v->rank >= 2 ? v->stride[v->rank - 1] : 1;
const int64_t v_hs = v->rank >= 3 ? v->stride[v->rank - 2] : head_dim * v_ds;
const int64_t v_rs = row_stride(v, kv_heads * head_dim);
/* kv_store_bf16 stores the value itself; only the E4M3 row divides by the reader's descales. */
const float ks = fp8 ? 1.0f / p_f32(a, "k_scale", 1.0f) : 1.0f;
const float vs = fp8 ? 1.0f / p_f32(a, "v_scale", 1.0f) : 1.0f;
/* Everything that can refuse is checked before the first write: a half-written mirror would
* make the next step's copies plausible and wrong. */
const int32_t* const R = (int32_t*)res->data;
for (int64_t t = 0; t < M; ++t) {
const int64_t s = ld_int(sm->dtype, sm->data, offlin(sm, t));
if (s < 0) continue; /* padded row, no cache slot */
const int64_t blk = s / block_size;
if (blk >= blocks) return RAD_E_SHAPE;
if (R[blk] < -1 || R[blk] >= slots) return RAD_E_INVAL;
}
/* The mirror, seen as the plane it holds (E4M3 or bf16): same bytes, same writer, so the two
* copies cannot round differently. */
RadTensor hkv = *host;
hkv.dtype = kv->dtype;
const KVView kvv = kv_view(kv, head_dim, block_size, kv_heads);
const KVView hv = kv_view(&hkv, head_dim, block_size, kv_heads);
REF_PAR_FOR(M * kv_heads * head_dim * 2)
for (int64_t t = 0; t < M; ++t) {
const int64_t s = ld_int(sm->dtype, sm->data, offlin(sm, t));
if (s < 0) continue;
const int64_t blk = s / block_size, off = s % block_size;
const int64_t hb = blk * hv.sb + off * hv.so;
/* A resident block keeps its slot current; a non-resident one is written to the mirror
* alone and the resolve copies the whole block in when it selects it. */
const int64_t cb = R[blk] >= 0 ? R[blk] * kvv.sb + off * kvv.so : -1;
for (int64_t h = 0; h < kv_heads; ++h) {
for (int64_t d = 0; d < head_dim; ++d) {
const float xk = ldt(k, t * k_rs + h * k_hs + d * k_ds) * ks;
const float xv = ldt(v, t * v_rs + h * v_hs + d * v_ds) * vs;
stt(&hkv, hb + h * hv.sh + d * hv.sd, xk);
stt(&hkv, hb + h * hv.sh + (head_dim + d) * hv.sd, xv);
if (cb >= 0) {
stt(kv, cb + h * kvv.sh + d * kvv.sd, xk);
stt(kv, cb + h * kvv.sh + (head_dim + d) * kvv.sd, xv);
}
}
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ rope */
namespace {
+453
View File
@@ -0,0 +1,453 @@
/* ref_kvstream.cpp -- kv_resolve: the residency map between a streamed KV group's block ids and
* the ring slots its device pool actually holds (docs/KV-STREAMING.md §3.4 and §4.3, docs/OPS.md
* "Sparse attention (QSA)").
*
* ============================== What it sits between ==============================
*
* `qsa_select` already dereferences the attention group's block table: it writes
* `sel[q][i] = tb[b]`, the group's POOL PAGE ID (`r4d_qsa_select_bf16.hip:254`, `:686`), and
* `attn_paged` uses each entry as a raw pool page index with no indirection
* (`r4d_attn_decode_h256_gqa6.hip:268`, `:322`; `r4d_attn_paged_gqa8.hip`). When the group's KV
* streams, the device pool is `slots` blocks deep and the block-id space is `blocks`, so a block id
* is no longer an address: `blk * kv_block_stride` would walk out of the layer and silently read
* another layer's bytes. `kv_resolve` turns every selected block id into the ring slot holding it,
* copies the block in from the pinned host mirror when it is not resident, and rewrites `sel` in
* place. Nothing below the rewrite changes: `sel` is the only place the sparse path names KV
* storage, so `attn_paged` reads a resolved table exactly as it reads a fully resident one.
*
* `nsel` and `seqused` stay what `qsa_select` wrote them: the attention's length over the table is
* a token count inside the blocks, not an address, so the rewrite leaves both alone.
*
* ============================== The operands, exactly ==============================
*
* # name role dtype shape what it is
* 0 sel inout i32 [M, sel_pitch] block ids in, ring slots out
* 1 nsel in i32 [M] valid entries of each row
* 2 res inout i32 [blocks] block -> slot, or -1; -2 in flight
* 3 slot_block inout i32 [slots] slot -> block, or -1 free
* 4 slot_state inout i32 [slots, 2] [s,0] epoch of last use, [s,1] heat
* 5 ctl inout i32 [16] the control block (below)
* 6 host in u8 [blocks, block_bytes] the layer's host mirror, identity layout
* 7 kv_cache inout bytes [slots, block_bytes] the layer's ring, as RAD_KV(group, layer)
*
* Order follows the read order of the algorithm, with the cache last as in `kv_store`. `sel` and
* `nsel` keep `qsa_select`'s positions 5 and 6 relative to each other, so the pair reads the same
* way in both ops.
*
* **The miss list is not an operand.** It is a per-call temporary, and a temporary is the row's
* scratch in this codebase (`qsa_select`'s split workspace, docs/OPS.md "qsa_select"): an operand
* would enter the arena planner, the recorded pass's ranges and the tape key for bytes nothing
* outside one launch reads. A device row that needs one declares
* `(next_pow2(slots) + slots) * 4` bytes through `RadKernelInfo::scratch`, laid out
* `int32 miss_block[next_pow2(slots)]` -- the ascending miss list, padded because a launch too wide
* for the kernel's shared claim list sorts it here -- then `int32 miss_slot[slots]`; this reference
* sorts locally and needs none.
*
* ============================== The parameters ==============================
*
* M query rows to resolve; ranged, since a prefill chunk and a decode step differ
* blocks the group's logical block count (the id space, `res`'s extent)
* slots the ring depth (the device pool's extent, `slot_block`'s extent)
* sel_pitch the selection's row pitch in entries (topk + 1); also the bound `nsel` may not pass
* block_bytes bytes one block of ONE layer occupies: the host mirror's row pitch, the pool's slot
* pitch, and the length of every copy
*
* `block_bytes` rather than `block_size` (the block's token count) because the op moves bytes and
* never reads an element: it is the same code at every cache layout (`fp8`, `fp8_v4`, `k6_v4`,
* `bf16`), and it is the number a vectorised copy has to constrain (a block is a whole number of
* 16-byte lanes). It is deliberately NOT a `block_size` key: `Builder::find_kv_consumer` binds a KV
* group to the first unclaimed op after it that carries `block_size`, and the group's block size
* must keep coming off the resolved attention kernel, not off the op that only copies its pages.
*
* `sel_pitch` and `block_bytes` are also pitches the operands carry (`sel.stride[0]`,
* `host.stride[0]`), and this reference requires them to agree: a resolve that read one pitch and
* wrote another would rewrite a row into its neighbour. Stating them in the schema puts them in the
* resolved geometry, where a kernel may constrain them, and in the shape hook, which needs them to
* size `sel` and the two planes.
*
* `slots` is the ring depth, and the OPERAND states it, exactly as `sel`'s extent states `M` and
* its row pitch states `sel_pitch`: `slot_block`'s extent is the ring the call may address and the
* parameter is the ceiling the caller declared. Declare cannot know the ring -- the pool is planned
* after the program is built, and a plugin sees only the context bound -- so the declared `slots` is
* the context and a ring the engine sized smaller (--kv-resident) arrives as a shorter
* operand. A call may never address more than the declaration: `slots > slots_declared` is refused.
*
* ============================== The control block ==============================
*
* `ctl` is 16 int32s, the same [0..9] layout the reference design uses
* (`Strata-main/include/strata/kernels/kv_stream.hpp:78`), 8-byte aligned so the counters can be
* read as u64:
*
* [0] epoch the value the last call used (0 in a fresh block, so the first call is 1)
* [1] hand the CLOCK hand, in [0, slots)
* [2] misses blocks placed by the last call
* [3] overflow 1 once a call could not place every miss; latched, never cleared here
* [4..5] u64 misses every block ever placed
* [6..7] u64 lookups every selection entry ever resolved
* [8..9] u64 calls
* [10] maximum union the largest number of DISTINCT blocks one call has selected
* (hits + misses), so a ring's depth can be sized from measurement
* [11] maximum misses the largest number of blocks one call has placed
* [12] maximum claims the largest number of DISTINCT absent blocks one call has claimed: the
* length of the sorted miss list, so it is what a claim-list capacity has
* to hold. One call's claims are bounded by its lookups, `sum_q nsel[q] <=
* M * sel_pitch` (every claim is a selection entry), and the engine's
* worst case is `max_seqs * sel_pitch`; a device kernel sizes its list from
* those operands rather than a constant (libr4d/r4d_kv_resolve.hip), and
* this is the counter that says whether it did.
* [13..15] zero, reserved
*
* `res`, `slot_block` and `slot_state` are state as well, and the caller brings them up: `res` and
* `slot_block` FILLED WITH -1, `slot_state` and `ctl` ZEROED (one `fill` each, or the group's
* bring-up). That is the reset: there is no separate reset op, because a residency map is fixed at
* bring-up and lives as long as the pool does. Entering a call with any `res` entry of -2, a
* `slot_block` outside [-1, blocks), a `res` outside [-1, slots) or a hand outside [0, slots) is a
* state fault and is refused rather than guessed at. The epoch is a wrap-around i32 counter, only
* ever compared for equality; it would take 2^31 calls - over two years of decoding - for a stale
* stamp to read as a fresh one, and `slot_state[s,1]` would still be the sweep's second chance.
*
* `slot_state[s,1]` is a **two-bit heat counter in [0, 3]** inside an i32 (31 bits unused, so the
* policy needs no new field, no extra per-slot memory and no ABI change). It is bumped by one, and
* clamped at 3, on every use of the slot -- a hit or a placement -- once a slot a call, and halved
* when the sweep passes the slot. Zeroed at bring-up and written nowhere else, so a fresh ring
* starts cold and a negative value is not reachable from a legal state; a reader treats it as cold
* rather than letting a fault strand the sweep.
*
* ============================== The semantics, in order ==============================
*
* 1. **epoch bump.** epoch = ctl[0] + 1.
* 2. **one lookup per distinct block.** For row q ascending, then entry i ascending, over
* i in [0, nsel[q]) (entries at or past nsel[q] are `qsa_select`'s -1 padding and are not
* touched): b = sel[q][i].
* res[b] >= 0 -> a hit: slot_state[res[b],0] = epoch and, the first time this call stamps
* that slot, slot_state[res[b],1] = min(old + 1, 3). One bump a SLOT a
* call, not one an entry: a block twenty rows name gains one step of heat,
* the same as one a single row names, so the counter measures the calls
* that wanted the block and not the width of the selection.
* res[b] == -1 -> a miss, claimed once: res[b] = -2 and b appended to the miss list.
* res[b] == -2 -> already claimed by an earlier entry; nothing. This is what makes the pass
* one lookup per block without a second structure: the map is its own set.
* 3. **the miss list is ascending block id.** Not first-appearance order: a block claimed by row 3
* must not evict a different slot from a block claimed by row 0 depending on which thread got
* there first, or two implementations of one contract would place the same blocks in different
* slots and `sel` would differ. Ascending is a fact about the claim set, not about the map, so
* a device implementation orders the claim list itself (a sort of at most `M * sel_pitch` ids);
* nothing here needs a pass over `blocks`, which is the id space and not the call's size.
* 4. **one victim per miss, in that order: identity, a free slot, then a CLOCK (second chance)
* sweep.** For each missed b in ascending order, if `b < slots` and slot b is available --
* `slot_state[b,0] != epoch` and `slot_block[b]` is either -1 or b itself -- the victim is slot
* b, and the hand does not move. This is a placement preference only: it takes a slot only when
* that slot is free, so it can never evict, and leaving the hand alone means a miss it cannot
* place falls through to exactly the walk below, from exactly the hand it would have seen. Two
* things make it worth having: it maps a block's id to its own slot while the context is
* shorter than the ring, and adjacent block ids then sit in adjacent slots of one layer's
* stripe, which is the order the attention's gather reads them in. Otherwise, visit slots from
* the hand, one at a time, wrapping. A FREE slot -- slot_block[s] == -1 -- is taken before any
* resident block is considered: evicting costs a copy on the next call and a free slot costs
* nothing, so the ring fills its empty region first. A slot with slot_state[s,0] == epoch was
* claimed or hit by this call and is passed, free or not, and keeps its heat counter; this is
* the "never take a slot this call itself uses" rule. When a whole turn finds no free slot, the
* same walk is the CLOCK sweep, with the two-bit heat counter in place of the one-bit
* reference:
* - a slot holding a block whose counter is non-zero: halve it (old >> 1) and pass it. A
* saturated 3 survives exactly two passes (3 -> 1 -> 0), a freshly used 1 exactly one,
* and a slot nothing has wanted for two sweeps is the victim on the next visit.
* - otherwise (a block, counter 0): take it.
* The hand still PICKS the victim -- this is a per-visit decision, not a search -- so the
* policy needs no reduction, no barrier and no extra traffic: an add-and-clamp where a
* constant store was, and a shift where a zero was. That is the whole reason it is affordable
* where an exact LRU is not: the epoch-ordered minimum costs a collective reduction per miss.
* The hand advances one slot per visit, so it ends just past the last slot taken. How many
* slots are free is counted before the sweep; a placement into a free slot makes it live and an
* eviction re-points a live one, so the count only falls to zero and a full ring goes straight
* to CLOCK. The free
* scan is side-effect free when it finds nothing -- it halves no counter and leaves the hand
* where it started -- so a full ring's victim is the sweep's alone. A free slot is taken
* without regard to its stale counter. The CLOCK sweep may
* visit at most 3 * slots slots; every write clamps the counter to 3, so two passes halve any
* value to 0 (docs/KV-STREAMING.md §3.4) and the third places the miss.
* 5. **re-point both maps** as each victim is taken: if the slot held block `old`, res[old] = -1;
* slot_block[s] = b, slot_state[s] = {epoch, min(old + 1, 3)}, res[b] = s. A placement is a
* use, so it carries the same bump a hit does; the victim's counter is 0 by construction (the
* sweep only takes a cold slot), so this is 1 in the common case, and the `min` is what keeps
* the two-bit range true for a slot taken by identity or from the free region.
* 6. **copy**: for each placed (b, s), block_bytes from host[b] into the pool's slot s. A copy is
* a byte copy; nothing is converted, so a streamed block is bit-identical to the block the
* writer put in the mirror.
* 7. **rewrite**: for every entry in [0, nsel[q]), sel[q][i] = res[b] when that is >= 0. Entries
* at or past nsel[q] keep their -1.
*
* `ctl[2] = placed`, `ctl[0] = epoch`, `ctl[1] = hand`, and the counters grow by `placed`, the
* number of entries read (one a lookup, as phase 2 defines a lookup: per entry, not per distinct
* block) and one. The three maxima are the sizes this op exists to measure: `ctl[10]` is the
* largest union one call has selected -- the number a ring must hold -- `ctl[11]` the largest number
* of blocks one call has placed, i.e. how much of that union was not already resident, and
* `ctl[12]` the largest number of claims one call has made, which is the depth the miss list had to
* hold.
*
* ============================== Overflow ==============================
*
* The ring must hold every DISTINCT block one call selects: a hit holds a slot and a miss needs
* another, so the condition is `hits + misses <= slots`. What that union is depends on the launch:
* at decode it is at most `max_seqs * (sel_pitch - 1)`, while above `qsa_exact_to` prefill issues a
* row per query and a launch's union can be a chunk's worth of the history, which is why the engine
* measures it (ctl[10]) instead of assuming a bound.
*
* It is not silent when it does: a miss left unplaced by the sweep reverts to `res[b] = -1`, and
* `ctl[3]` is latched to 1 with `ctl[2]` counting only what was placed. The selection rewrite then
* writes SLOT 0 for every entry whose block is not resident -- a safe address inside the ring, not
* the block's slot. The entry cannot be left holding the block id: on device that value is an
* address past the ring which `attn_paged` dereferences without a bounds check, so a step whose
* resolve overflowed would fault or read another layer's bytes. Slot 0 is defined memory holding a
* real block's KV, so the step produces wrong numbers rather than a fault -- and a caller that finds
* `ctl[3]` set must DISCARD the step, which is the only reading that keeps the engine honest: it
* never serves output computed against a table whose entries do not name their blocks.
*
* ============================== Statefulness ==============================
*
* The op keeps state across calls - the epoch, the hand, the heat counters and its counters - so it
* is not concurrent: two of its launches must never overlap, and the engine must not reorder one
* against another. That is the `rad_kernel_concurrent` promise (`abi/rad_abi.h:493`), which is
* given per row; a plugin's row list lives beside it, and libr4d's is the `kStateful[]` prefix
* table inside `rad_kernel_concurrent` (`libr4d/r4d_rows.cpp:8748-8770`, "A new row with
* stream-ordered state belongs in the list above"). Both kernel rows for this op therefore stay
* out of that list. The absence of the promise is always safe: without it the engine orders the
* launch behind everything before it, which is exactly what the map needs.
*
* Being stateful is not the same as being unrecordable. The op is a pure function of
* device-resident state whose addresses and pitches never change after bring-up (the maps, the
* ring and the mirror are fixed-size from the group's declaration), so a recorded pass replays it
* correctly; rewriting contents is fine, changing a pitch would force a re-record (docs/
* KV-STREAMING.md §3.4, core/runtime/ctx.cpp's pass key).
*
* ============================== Why this file is the oracle ==============================
*
* It is the obvious serial form of the contract above: one pass over the entries, one sorted miss
* list, one second-chance walk of the slots, one memcpy per miss, one rewrite. Two device
* implementations compared against it must agree on which slot every block lands in, which is the
* part a fast parallel sweep could get right on hits and wrong on victims.
*/
#include "ref_common.h"
#include "ref_ops.h"
#include <algorithm>
#include <cstring>
#include <vector>
using namespace ref;
namespace {
/* The heat counter's two writes, the same function in all three implementations: a use bumps by
* one and saturates at 3, a sweep pass halves, and a negative value (never written by a legal call)
* reads as cold rather than letting it survive every pass. */
int32_t heat_up(int32_t h) { return h < 0 ? 1 : (h < 3 ? h + 1 : 3); }
int32_t heat_down(int32_t h) { return h > 0 ? (h >> 1) : 0; }
/* `ctl`'s size, and the index of its first counter (three u64s from [4]). */
enum { kKvCtlInts = 16, kKvCtlMisses = 4, kKvCtlLookups = 6, kKvCtlCalls = 8 };
static_assert(kKvCtlLookups == kKvCtlMisses + 2 && kKvCtlCalls == kKvCtlMisses + 4,
"the three counters are consecutive u64s from ctl[4]");
/* The byte pitch of a rank-2-or-deeper operand's rows, and whether its trailing extents fill
* exactly `bytes` densely. Both the mirror and the pool are one block a row; a padded row would
* copy a block's bytes over its neighbour's. */
int64_t row_bytes(const RadTensor* t, int64_t* dense) {
int64_t acc = 1;
for (int i = (int)t->rank - 1; i >= 1; --i) acc *= t->shape[i];
*dense = acc;
return t->stride[0] * rad_dtype_bytes(t->dtype, 1);
}
} /* namespace */
int ref_kv_resolve(const RadArgs* a, RadStream) {
if (!have(a, 8)) return RAD_E_INVAL;
const RadTensor *sel = &a->t[0], *nsel = &a->t[1], *res = &a->t[2], *sblk = &a->t[3];
const RadTensor *sst = &a->t[4], *ctl = &a->t[5], *host = &a->t[6], *pool = &a->t[7];
const int64_t M = p_int(a, "M", 0), blocks = p_int(a, "blocks", 0);
const int64_t slots_decl = p_int(a, "slots", 0), pitch = p_int(a, "sel_pitch", 0);
const int64_t bbytes = p_int(a, "block_bytes", 0);
/* Zero rows is not a call (docs/OPS.md). */
if (M <= 0) return RAD_E_SHAPE;
if (blocks <= 0 || slots_decl <= 0 || pitch <= 0 || bbytes <= 0) return RAD_E_SHAPE;
/* The map and the counts are one format, written by one op and read by the writer and the
* sweep: i32, dense. */
const RadTensor* state[] = { sel, nsel, res, sblk, sst, ctl };
for (const RadTensor* t : state)
if (t->dtype != RAD_I32) return RAD_E_DTYPE;
if (sel->rank != 2 || sst->rank != 2) return RAD_E_SHAPE;
if (sel->shape[0] < M || sel->shape[1] < pitch) return RAD_E_SHAPE;
if (sel->stride[1] != 1 || sel->stride[0] != pitch) return RAD_E_STRIDE;
if (numel(nsel) < M || nsel->stride[0] != 1) return RAD_E_SHAPE;
if (numel(res) < blocks || res->stride[0] != 1) return RAD_E_SHAPE;
/* The operand states the ring; the parameter is the ceiling it may not pass (see "The
* parameters"). */
const int64_t slots = numel(sblk);
if (slots < 1 || slots > slots_decl || sblk->stride[0] != 1) return RAD_E_SHAPE;
if (sst->shape[0] < slots || sst->stride[1] != 1 || sst->stride[0] < 2) return RAD_E_STRIDE;
if (numel(ctl) < kKvCtlInts || ctl->stride[0] != 1) return RAD_E_SHAPE;
if (host->rank < 2 || pool->rank < 2) return RAD_E_SHAPE;
if (host->shape[0] < blocks || pool->shape[0] < slots) return RAD_E_SHAPE;
/* One block of one layer: the mirror's row and the pool's slot are the same bytes, and the
* copy length `block_bytes` is both pitches. */
int64_t hdense = 0, pdense = 0;
if (row_bytes(host, &hdense) != bbytes || hdense * rad_dtype_bytes(host->dtype, 1) != bbytes)
return RAD_E_STRIDE;
if (row_bytes(pool, &pdense) != bbytes || pdense * rad_dtype_bytes(pool->dtype, 1) != bbytes)
return RAD_E_STRIDE;
int32_t* const R = (int32_t*)res->data;
int32_t* const SB = (int32_t*)sblk->data;
int32_t* const ST = (int32_t*)sst->data;
int32_t* const C = (int32_t*)ctl->data;
const int64_t stp = sst->stride[0];
/* The counters are read and written as u64 in an i32 block. */
if (((uintptr_t)C & 7u) != 0) return RAD_E_STRIDE;
/* ---- everything that can refuse, before anything is written; the selection is read once and
* kept, so phase 7 writes values it did not have to re-read ---- */
std::vector<int64_t> off, ent;
for (int64_t q = 0; q < M; ++q) {
const int64_t n = ld_int(nsel->dtype, nsel->data, q);
if (n < 0 || n > pitch) return RAD_E_SHAPE;
for (int64_t i = 0; i < n; ++i) {
const int64_t b = ld_int(sel->dtype, sel->data, q * pitch + i);
if (b < 0 || b >= blocks) return RAD_E_INVAL;
off.push_back(q * pitch + i);
ent.push_back(b);
}
}
for (int64_t b = 0; b < blocks; ++b)
if (R[b] < -1 || R[b] >= slots) return RAD_E_INVAL; /* -2 is never left by a call */
for (int64_t s = 0; s < slots; ++s)
if (SB[s] < -1 || SB[s] >= blocks) return RAD_E_INVAL;
if (C[1] < 0 || C[1] >= slots) return RAD_E_INVAL;
/* ---- 1. the epoch ---- */
const int32_t epoch = (int32_t)((uint32_t)C[0] + 1u);
/* ---- 2. hits, and one claim per distinct miss (the map is its own set) ---- */
std::vector<int32_t> miss;
for (size_t e = 0; e < ent.size(); ++e) {
const int32_t b = (int32_t)ent[e];
const int32_t s = R[b];
if (s >= 0) {
/* One bump a slot a call (see "The semantics", step 2). */
if (ST[s * stp] != epoch) {
ST[s * stp] = epoch;
ST[s * stp + 1] = heat_up(ST[s * stp + 1]);
}
} else if (s == -1) {
R[b] = -2;
miss.push_back(b);
}
}
const int64_t lookups = (int64_t)ent.size();
/* ---- 3. the canonical miss order ---- */
std::sort(miss.begin(), miss.end());
/* ---- 4. one victim per miss: identity, a free slot, then CLOCK, from the hand, never a slot
* this call uses ---- */
/* Identity first (see "The semantics", step 4): a missed block whose id fits the ring and whose
* own slot is free takes that slot and leaves the hand alone, so the fallback below runs from
* the hand it would always have run from. A free slot is not a victim (see step 4): the ring's
* empty region fills before any resident block is evicted. The free scan halves no counter
* and does not move the hand when it finds nothing, so a full ring takes the CLOCK victim it
* always took. The count below is what lets a full ring go straight to CLOCK: no placement
* frees a slot and no eviction does either, so a count of zero holds for the rest of the call. */
int64_t free_left = 0;
for (int64_t s = 0; s < slots; ++s)
if (SB[s] < 0 && ST[s * stp] != epoch) ++free_left;
int64_t hand = C[1];
int64_t placed = 0;
bool overflow = false;
for (size_t k = 0; k < miss.size(); ++k) {
const int32_t b = miss[k];
int64_t victim = -1;
if ((int64_t)b < slots && ST[(int64_t)b * stp] != epoch && (SB[b] < 0 || SB[b] == b))
victim = b;
if (victim < 0 && free_left > 0) {
int64_t h = hand;
for (int64_t scanned = 0; scanned < slots; ++scanned) {
const int64_t s = h;
h = h + 1 == slots ? 0 : h + 1;
if (ST[s * stp] == epoch) continue; /* used by this call */
if (SB[s] >= 0) continue; /* holds a block */
victim = s;
hand = h;
break;
}
if (victim < 0) free_left = 0;
}
if (victim < 0) {
for (int64_t scanned = 0; scanned < 3 * slots; ++scanned) {
const int64_t s = hand;
hand = hand + 1 == slots ? 0 : hand + 1;
if (ST[s * stp] == epoch) continue; /* used by this call */
if (SB[s] >= 0 && ST[s * stp + 1] > 0) { /* second chance */
ST[s * stp + 1] = heat_down(ST[s * stp + 1]);
continue;
}
victim = s;
break;
}
}
if (victim < 0) { overflow = true; break; }
/* ---- 5. re-point both maps ---- */
if (SB[victim] < 0 && free_left > 0) --free_left; /* a free slot just filled */
if (SB[victim] >= 0) R[SB[victim]] = -1;
SB[victim] = b;
ST[victim * stp] = epoch;
ST[victim * stp + 1] = heat_up(ST[victim * stp + 1]);
R[b] = (int32_t)victim;
++placed;
}
/* A miss the sweep could not place is not resident: undo the claim, so the next call may claim
* it again, and leave its selection entries naming the block (see "Overflow"). */
for (size_t k = (size_t)placed; k < miss.size(); ++k) R[miss[k]] = -1;
/* ---- 6. the copies, one block each, in miss order ---- */
const uint8_t* const hb = (const uint8_t*)host->data;
uint8_t* const pb = (uint8_t*)pool->data;
for (int64_t k = 0; k < placed; ++k) {
const int32_t b = miss[(size_t)k];
std::memcpy(pb + (int64_t)R[b] * bbytes, hb + (int64_t)b * bbytes, (size_t)bbytes);
}
/* ---- 7. the rewrite: every valid entry becomes the slot holding its block ---- */
for (size_t e = 0; e < ent.size(); ++e) {
const int32_t s = R[ent[e]];
/* An entry of a miss the sweep could not place names slot 0, not its block id: the id is
* an address outside the ring (see "Overflow"). ctl[3] is what says the table is unusable. */
st_int(sel->dtype, sel->data, off[e], s >= 0 ? s : 0);
}
/* ---- the bookkeeping ---- */
/* The union is over DISTINCT blocks, so it is counted from the slots this call stamped (every
* block it hit or placed leaves the call's epoch there) plus the misses it could not place:
* entries repeat, a selection names one block per row and many rows share swaths of history. */
int64_t used = 0;
for (int64_t s = 0; s < slots; ++s)
if (ST[s * stp] == epoch) ++used;
const int64_t uni = used + (int64_t)miss.size() - placed;
C[0] = epoch;
C[1] = (int32_t)hand;
C[2] = (int32_t)placed;
if (overflow) C[3] = 1;
/* The sizes the counters report: the union this call selected and the part of it that had to
* be copied in.
* A ring sized for the largest union a launch selects is the whole answer to "how large does the
* ring have to be" (docs/KV-STREAMING.md §3.4). */
if (uni > C[10]) C[10] = (int32_t)uni;
if (placed > C[11]) C[11] = (int32_t)placed;
if ((int64_t)miss.size() > C[12]) C[12] = (int32_t)miss.size();
uint64_t* const ctr = (uint64_t*)(C + kKvCtlMisses);
ctr[0] += (uint64_t)placed;
ctr[1] += (uint64_t)lookups;
ctr[2] += 1u;
return RAD_OK;
}
+6
View File
@@ -153,6 +153,8 @@ int ref_chan_stats (const RadArgs*, RadStream);
/* Unlimited-OCR's SAM tower: windowed attention with decomposed relative position (ref_sam_attn.cpp). */
int ref_sam_attn (const RadArgs*, RadStream);
int ref_kv_store (const RadArgs*, RadStream);
/* the store of a streamed group: the host mirror always, the ring slot when the block is resident */
int ref_kv_store_streamed (const RadArgs*, RadStream);
int ref_rope (const RadArgs*, RadStream);
int ref_rope_table (const RadArgs*, RadStream);
int ref_qk_norm_rope (const RadArgs*, RadStream);
@@ -242,6 +244,8 @@ int ref_qsa_score (const RadArgs*, RadStream);
int ref_qsa_select (const RadArgs*, RadStream);
int ref_qsa_tail_store (const RadArgs*, RadStream);
int ref_qsa_work (const RadArgs*, RadStream);
/* the residency map between a streamed group's block ids and its ring slots (ref_kvstream.cpp) */
int ref_kv_resolve (const RadArgs*, RadStream);
int ref_dflash_conv (const RadArgs*, RadStream);
/* ---- mixture of experts */
@@ -406,6 +410,7 @@ REF_SHAPE_FN(ref_mix_rows);
REF_SHAPE_FN(ref_chan_stats);
REF_SHAPE_FN(ref_sam_attn);
REF_SHAPE_FN(ref_kv_store);
REF_SHAPE_FN(ref_kv_store_streamed);
REF_SHAPE_FN(ref_rope);
REF_SHAPE_FN(ref_rope_table);
REF_SHAPE_FN(ref_qk_norm_rope);
@@ -487,6 +492,7 @@ REF_SHAPE_FN(ref_qsa_score);
REF_SHAPE_FN(ref_qsa_select);
REF_SHAPE_FN(ref_qsa_tail_store);
REF_SHAPE_FN(ref_qsa_work);
REF_SHAPE_FN(ref_kv_resolve);
REF_SHAPE_FN(ref_dflash_conv);
REF_SHAPE_FN(ref_router_topk);
+42
View File
@@ -403,6 +403,17 @@ static const RadParamSpec pKvStore[] = { P_INT("M"), P_INT("head_dim"), P_INT("n
P_F64O("k_scale"), P_F64O("v_scale") };
static const RadOperandSpec oKvStore[] = { OPD("k"), OPD("v"), OPD("slot_mapping"),
INOUT("kv_cache") };
/* The streamed store, `kv_store` with the group's two copies: `host` is the authoritative mirror
* (always written) and `res` is the residency map that says whether the block is in the ring. The
* parameters are kv_store's, because the layout and the conversion are the same; `res` is read,
* never written, since residency is `kv_resolve`'s decision. */
static const RadParamSpec pKvStoreStreamed[] = {
P_INT("M"), P_INT("head_dim"), P_INT("n_head_kv"), P_INT("block_size"), P_STR("kv_dtype"),
P_F64O("k_scale"), P_F64O("v_scale")
};
static const RadOperandSpec oKvStoreStreamed[] = { OPD("k"), OPD("v"), OPD("slot_mapping"),
INOUT("kv_cache"), OPD("res"),
INOUT("host") };
static const RadParamSpec pRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
P_INT("n_head_kv"), P_F64("theta"), P_F64("scale"),
@@ -457,6 +468,17 @@ static const RadParamSpec pQsaWork[] = { P_INT("M"), P_INT("n"), P_INT("ratio"),
static const RadOperandSpec oQsaWork[] = { OPD("k"), OPD("pos"), OPD("cu"), OPD("state"),
OPD("sidx"), OPD("bt"), OPD_O("rope"), OUT("stage"),
OUT("page"), OUT("bpos"), OUT("nc") };
/* The residency map of a streamed group (ref_kvstream.cpp, docs/KV-STREAMING.md §3.4). No
* `block_size`: the op moves whole blocks by the byte and never reads an element, and a
* `block_size` key would make it a KV group's consumer, taking the binding away from the resolved
* attention kernel (Builder::find_kv_consumer). `sel_pitch` and `block_bytes` are the selection's
* row pitch and one block of one layer's byte extent. The miss list is the row's scratch, not an
* operand. */
static const RadParamSpec pKvResolve[] = { P_INT("M"), P_INT("blocks"), P_INT("slots"),
P_INT("sel_pitch"), P_INT("block_bytes") };
static const RadOperandSpec oKvResolve[] = { INOUT("sel"), OPD("nsel"), INOUT("res"),
INOUT("slot_block"), INOUT("slot_state"),
INOUT("ctl"), OPD("host"), INOUT("kv_cache") };
/* DFlash2's grouped convolution, libr4d's schema exactly. */
static const RadParamSpec pDflashConv[] = { P_INT("T"), P_INT("hidden_size"), P_INT("taps"),
@@ -1383,6 +1405,11 @@ static const RadOpSchema kSchemas[] = {
{ "kv_store", ARR(pKvStore), ARR(oKvStore),
"Writes k and v into the paged cache at slot_mapping; a negative slot is skipped. The cache is "
"INOUT rather than the OUT docs/OPS.md draws, because the op touches only the mapped slots." },
{ "kv_store_streamed", ARR(pKvStoreStreamed), ARR(oKvStoreStreamed),
"kv_store for a streamed group: writes the token's k and v into the group's host mirror ALWAYS, "
"at the block's identity row, and into the ring slot only when the residency map says the block "
"is resident. A negative slot is skipped. The mirror is the truth; a block absent from the ring "
"is copied in whole by kv_resolve." },
{ "rope_table", ARR(pRopeTable), ARR(oRopeTable),
"cos/sin for the positions a step names, at each position's own row of a [max_ctx, rot] plane: "
"rot values a row, cos for the first half and sin for the second. The angles are `rope`'s "
@@ -1409,6 +1436,15 @@ static const RadOpSchema kSchemas[] = {
"The blocks completing in this batch: per block its `ratio` raw keys (this step's rows, or the "
"tail's at q % ring) staged contiguously, its block-key page and its first-token position; slots "
"past the count get page -1. nc is each sequence's complete-block count." },
{ "kv_resolve", ARR(pKvResolve), ARR(oKvResolve),
"Makes every block the selections name resident and rewrites them as ring slots: an epoch bump, "
"a hit or a single claim per distinct block, a free slot before any resident one and otherwise "
"one CLOCK (second-chance) victim per miss - never a slot this call uses - the two maps "
"re-pointed, each missed block copied from the host mirror "
"into its slot, then every valid sel entry replaced by the slot holding its block. nsel, and so "
"the attention's length, are unchanged. The ring must hold the call's distinct blocks; a call "
"that cannot place every miss latches ctl[3] and leaves those entries naming blocks, never a "
"fabricated slot." },
{ "dflash_conv", ARR(pDflashConv), ARR(oDflashConv),
"DFlash2's grouped dynamic depthwise convolution: out[t][h] = sum over taps j, j == 0 or "
"t % block_size >= j, of (base[j][h] + delta[t][j][h / group_size]) * x[t - j][h]." },
@@ -2234,6 +2270,9 @@ SIMPLE("ref_sam_attn", "sam_attn", "attn",
ROW("ref_kv_store", "kv_store", "attn", "scatter k and v into the paged cache", DT_ANY,
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])),
nullptr, nullptr, ref_kv_store, nullptr),
ROW("ref_kv_store_streamed", "kv_store_streamed", "attn",
"scatter k and v into the host mirror always and into the resident ring slot", DT_ANY,
nullptr, 0, nullptr, nullptr, ref_kv_store_streamed, nullptr),
SIMPLE("ref_rope", "rope", "attn", "rotary embedding in place", DT_FLOAT, ref_rope),
SIMPLE("ref_qk_norm_rope", "qk_norm_rope", "attn", "per-head QK norm then rotary embedding",
DT_FLOAT, ref_qk_norm_rope),
@@ -2276,6 +2315,9 @@ SIMPLE("ref_qsa_tail_store", "qsa_tail_store", "attn",
ref_qsa_tail_store),
SIMPLE("ref_qsa_work", "qsa_work", "attn",
"QSA work list: completing blocks' keys, pages and positions", DT_FLOAT, ref_qsa_work),
SIMPLE("ref_kv_resolve", "kv_resolve", "attn",
"streamed KV residency map: selected block ids become ring slots, misses copied in",
"i32 maps, u8 block planes", ref_kv_resolve),
SIMPLE("ref_dflash_conv", "dflash_conv", "draft",
"DFlash2 grouped dynamic depthwise convolution", DT_FLOAT, ref_dflash_conv),
SIMPLE("ref_row_topk", "row_topk", "moe", "exact per-row top-R", DT_FLOAT, ref_row_topk),
+56
View File
@@ -1455,6 +1455,36 @@ int ref_kv_store_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
return o.finish("kv_store", operand, out);
}
/* The streamed store. Same geometry as `kv_store`, with the mirror and the residency map beside
* the ring: the mirror is one block a row of the same byte pitch.
*
* The described call touches ONE block, resident in ring slot 0. The draw vocabulary cannot express
* an injective residency map (block -> slot is a permutation, and a draw is a free choice per
* entry), and two blocks drawing one slot would make two tokens write the same ring byte -- a race
* the parallel rerun rightly reports. So the generated case exercises the conversion, the positions
* within a block, the mirror write and the resident ring write; a partly resident map and the
* mirror-only step are built by hand in tests/ref_test.cpp and libavx's hand cases. `M <= block_size`
* keeps every token on its own position of that one block. */
int ref_kv_store_streamed_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0);
const int64_t hd = gi(p, n_p, "head_dim", 0);
const int64_t kvh = gi(p, n_p, "n_head_kv", 0);
const int64_t bs = gi(p, n_p, "block_size", 0);
if (M <= 0 || hd <= 0 || kvh <= 0 || bs <= 0 || M > bs) return RAD_E_UNSUPPORTED;
const uint32_t kv = dt_parse(gs(p, n_p, "kv_dtype"));
const uint32_t KV = kv == (uint32_t)RAD_DT_INVALID ? (uint32_t)RAD_F8E4M3 : kv;
const int64_t row = kvh * bs * 2 * hd * (KV == (uint32_t)RAD_BF16 ? 2 : 1);
Opd o;
o.t((uint32_t)RAD_BF16, { M, kvh, hd }); /* k */
o.t((uint32_t)RAD_BF16, { M, kvh, hd }); /* v */
o.t_idx({ M }, bs, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* slot_mapping, one block */
o.t(KV, { 1, kvh, bs, 2 * hd }); /* kv_cache, INOUT */
o.t_idx({ 1 }, 1, -1, 0); /* res -- resident in slot 0 */
o.t((uint32_t)RAD_U8, { 1, row }); /* host, INOUT */
return o.finish("kv_store_streamed", operand, out);
}
int ref_rope_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0);
const int64_t hd = gi(p, n_p, "head_dim", 0);
@@ -1644,6 +1674,32 @@ int ref_qsa_work_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
return o.finish("qsa_work", operand, out);
}
/* The residency map and the two byte planes. `sel` is described as block ids, which is what
* `qsa_select` leaves there; past `nsel` it is -1 padding the op does not touch.
*
* A described residency map can only be an all-resident one: the description vocabulary draws an
* index in [0, idx_max) or a non-negative constant, and a cold entry is -1 (`res[b] == -1`, an
* absent block), which no draw expresses. So a tool that calls this op cold sees every block
* resident, which exercises the epoch, the rewrite and the bookkeeping but never a miss, an
* eviction or a copy. The service-shaped cases (a partly resident pool, a second chance, an
* eviction, an overflow) are built by hand in tests/ref_test.cpp and in libavx's hand cases. */
int ref_kv_resolve_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0), blocks = gi(p, n_p, "blocks", 0);
const int64_t slots = gi(p, n_p, "slots", 0), pitch = gi(p, n_p, "sel_pitch", 0);
const int64_t bbytes = gi(p, n_p, "block_bytes", 0);
if (M <= 0 || blocks <= 0 || slots <= 0 || pitch <= 0 || bbytes <= 0) return RAD_E_UNSUPPORTED;
Opd o;
o.t_idx({ M, pitch }, blocks, -1, 0); /* sel -- one block id an entry */
o.t_idx({ M }, pitch + 1, -1, 0); /* nsel -- 0 .. sel_pitch */
o.t_idx({ blocks }, slots, -1, 0); /* res -- every block resident, no -2 */
o.t_idx({ slots }, blocks, -1, 0); /* slot_block -- the reverse map */
o.t_idx({ slots, 2 }, 2, 0, 0); /* slot_state -- cold: epoch 0, no reference */
o.t_idx({ 16 }, 2, 0, 0); /* ctl -- cold: epoch 0, hand 0 */
o.t((uint32_t)RAD_U8, { blocks, bbytes }); /* host -- the layer's mirror, a block a row */
o.t((uint32_t)RAD_U8, { slots, bbytes }); /* kv_cache -- the layer's ring, a block a row */
return o.finish("kv_resolve", operand, out);
}
/* `delta` is described as the whole [T, 2 * taps * NG] coefficient projection, of which the op
* reads the first taps * NG columns at the row pitch: the form the architecture issues, and the
* pitch libr4d requires. */