Archived
device and plugin boundary: kv_resolve, the streamed store rows, ABI 30
This commit is contained in:
1 parent
32881da9fd
commit
486c933161
34 files changed
+3643
-29
No files matched your search
@@ -1060,6 +1060,102 @@ AVX_KERNEL(kv_store) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ kv_store_streamed */
|
||||
/* kv_store for a group whose KV streams: the host mirror is authoritative and the ring is a cache
|
||||
* of it. `host` is written ALWAYS, at the block's identity row; `kv_cache[res[block]]` only when
|
||||
* the residency map says the block is resident. libref/ref_attn.cpp (ref_kv_store_streamed) states
|
||||
* why the residency test belongs in the writer and not in the resolve: at decode the block a step
|
||||
* writes is usually already resident, so a mirror-only writer would leave the ring's copy of it
|
||||
* short of this step's tokens and the resolve would not re-copy a resident block.
|
||||
*
|
||||
* The two planes are the same bytes in the same paged layout, so one conversion writes both: the
|
||||
* mirror is a view of the same `[block, kv_heads, block_size, 2 * head_dim]` shape whose dtype is
|
||||
* the cache's, so the two copies cannot round differently. E4M3 converts as kv_store_fp8 does;
|
||||
* bf16 stores the value itself, as kv_store_bf16 does. `res` is read, never written. */
|
||||
AVX_KERNEL(kv_store_streamed) {
|
||||
if (!rad_args_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); a packed fp8_v4/k6_v4 group has
|
||||
* no streamed row. */
|
||||
const bool fp8 = kv->dtype == RAD_F8E4M3;
|
||||
if (!fp8 && kv->dtype != RAD_BF16) return RAD_E_UNSUPPORTED;
|
||||
if (kv->rank != 4 || 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, so the same offsets address the
|
||||
* same element of the same block in either. */
|
||||
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);
|
||||
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;
|
||||
|
||||
/* Refused before the first write, exactly as libref refuses it: a half-written mirror makes
|
||||
* the next step's copies plausible and wrong. (The device row cannot return a host status from
|
||||
* inside its kernel and guards these instead; a legal call never takes either path.) */
|
||||
const int32_t* const R = (const int32_t*)res->data;
|
||||
for (int64_t t = 0; t < M; ++t) {
|
||||
const int64_t slot = ldi(sm, offlin(sm, t));
|
||||
if (slot < 0) continue;
|
||||
const int64_t blk = slot / block_size;
|
||||
if (blk >= blocks) return RAD_E_SHAPE;
|
||||
if (R[blk] < -1 || R[blk] >= slots) return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
RadTensor hkv = *host;
|
||||
hkv.dtype = kv->dtype; /* the same bytes, seen as the cache's 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);
|
||||
|
||||
AVX_PARALLEL_FOR_IF(M * kv_heads * head_dim >= AVX_PAR_MIN)
|
||||
for (int64_t t = 0; t < M; ++t) {
|
||||
const int64_t slot = ldi(sm, offlin(sm, t));
|
||||
if (slot < 0) continue;
|
||||
const int64_t blk = slot / block_size, off = slot % block_size;
|
||||
const int64_t hb = blk * hv.sb + off * hv.so;
|
||||
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;
|
||||
}
|
||||
|
||||
/* ================================================================== rotary
|
||||
*
|
||||
* NeoX pairs element i with i + rotary_dim/2; GPT-J ("interleaved") pairs 2i with 2i+1. The style
|
||||
|
||||
Reference in new issue
Block a user