Archived
models: qwen4exp_fp8 and GLM-5.3 stream their KV
This commit is contained in:
1 parent
a9e627b7ca
commit
c2219fccb0
9 files changed
+1271
-42
No files matched your search
@@ -26,6 +26,17 @@
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
|
||||
/* Whether the streamed store has a row for this cache layout. libr4d's streamed rows are the E4M3
|
||||
* and bf16 ones only (libr4d/r4d_rows.cpp, kv_store_streamed_{fp8,bf16}); a packed cache
|
||||
* (--kv-cache-dtype fp8_v4 / k6_v4) has base-store rows and nothing that writes a host mirror. The
|
||||
* declaration must not be emitted for it: the op is weightless, so a device miss is a hole and
|
||||
* refuses startup even with --kv-resident off. A group that declares no streamed store takes the
|
||||
* base path, and kvstream_configure tells the operator the layout, not the architecture, is why. */
|
||||
inline bool kv_store_streamed_dtype(const char* kv_dtype) {
|
||||
return kv_dtype && (std::strcmp(kv_dtype, "fp8_e4m3") == 0 ||
|
||||
std::strcmp(kv_dtype, "bf16") == 0);
|
||||
}
|
||||
|
||||
struct AttnGatedFP8 {
|
||||
struct Wire {
|
||||
rad_buf x = 0; /* [max_tok, n_embd] the residual stream */
|
||||
@@ -69,6 +80,7 @@ struct AttnGatedFP8 {
|
||||
* block table and context length: same op, same kernel, different list. `sel` is `topk + 1`
|
||||
* wide and the kernel takes its block-table pitch from the operand. */
|
||||
rad_buf qsa_sel = 0;
|
||||
rad_buf qsa_nsel = 0; /* [max_tok] i32, the valid entries of each `qsa_sel` row */
|
||||
rad_buf qsa_sequ = 0;
|
||||
/* Context length up to which dense attention gives the same result, `budget + ratio - 1`.
|
||||
* Below it every complete block is selected, the paths agree to the bit, and dense is cheaper
|
||||
@@ -110,10 +122,23 @@ struct AttnGatedFP8 {
|
||||
|
||||
rad_op op_fused = 0; /* qk_norm_rope_gate, if it resolves */
|
||||
rad_op op_fused_kv = 0; /* the same with kv_store folded in */
|
||||
/* The same with a streamed group's store folded in: the host mirror is written for every token
|
||||
* and the ring slot only where the residency map says the block is resident, exactly as
|
||||
* kv_store_streamed does. Declared beside the pair it replaces; issued only when the batch hands
|
||||
* this group a stream, because the mirror and the map are the batch's. */
|
||||
rad_op op_fused_stream = 0;
|
||||
int64_t fused_rows = 0; /* widest T the fused ops take */
|
||||
rad_op op_q_norm = 0, op_k_norm = 0, op_rope_q = 0, op_rope_k = 0;
|
||||
rad_op op_attn = 0, op_kv_store = 0, op_gq = 0;
|
||||
rad_op op_attn_gq = 0; /* attn_paged + gate_quant_fp8 */
|
||||
/* kv_store_streamed, the store of a group whose KV streams: the host mirror always, the ring
|
||||
* slot when `res` says the block is resident (docs/OPS.md "kv_store_streamed"). Zero when the
|
||||
* rows do not serve it; a streamed group then fails the step rather than write the ring
|
||||
* alone. */
|
||||
rad_op op_kv_stream = 0;
|
||||
/* kv_resolve: this layer's selection made resident and rewritten as ring slots. Declared here,
|
||||
* beside the store it must follow (step), and issued immediately before each sparse launch. */
|
||||
rad_op op_resolve = 0;
|
||||
/* The output gate as `sigmoid` then `mul`: the bf16-linear form (Geom::w_bf16), and with fp8
|
||||
* linears the chain gate_quant_fp8 fuses, where it is not served. `gate` is the sigmoid's
|
||||
* output (the wire's with bf16 linears, the block's own otherwise) and `q_gate` the chain's
|
||||
@@ -143,6 +168,14 @@ struct AttnGatedFP8 {
|
||||
* by passes that only leave layer state behind, such as the MTP head's history pass over a
|
||||
* prompt chunk with no drafting row. */
|
||||
void store_kv(RadCtx* c, const RadBatch* batch) const;
|
||||
/* The cache store of this layer, the one place a layer's KV reaches storage: `kv_store` for a
|
||||
* hosted group, `kv_store_streamed` for a group whose KV streams. Called before the attention,
|
||||
* and before the resolve, of the same layer. */
|
||||
void store_cache(RadCtx* c, const RadKVGroupBatch* kvb, const int32_t* slot, int64_t T,
|
||||
bool staged = false) const;
|
||||
/* The pool store a staged pass needs beside the mirror writer: the same bytes at block-id
|
||||
* identity, which is the addressing a stage view reads. */
|
||||
void store_stage_identity(RadCtx* c, const int32_t* slot, int64_t T) const;
|
||||
|
||||
/* The block's tail, declared last by every rank: the all-reduce and the trailing add. */
|
||||
int declare_out(RadBuilder* b, bool add_out);
|
||||
@@ -162,7 +195,12 @@ struct AttnGatedFP8 {
|
||||
* normed pieces (ArPieces::in_ready): one prefill sequence in two pieces, each the chunk a
|
||||
* span of 1 makes, on the unfused prologue with one rotary component per token (every token's
|
||||
* components agree, so plane 0 of an M-RoPE batch's positions is each token's position) and
|
||||
* the gate quantised by gate_quant_fp8. */
|
||||
* the gate quantised by gate_quant_fp8.
|
||||
*
|
||||
* A streamed group never takes it: the pieces path issues the cache store as `op_kv_store` at
|
||||
* the batch's slot mapping, which for a ring smaller than the context is a block id used as a
|
||||
* slot and never reaches the host mirror, so the split would skip the whole streaming write
|
||||
* (the group's own path in step() is the only one that writes both). */
|
||||
bool takes_pieces(const RadBatch* batch, const ArPieces* pieces) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
|
||||
@@ -170,7 +208,7 @@ struct AttnGatedFP8 {
|
||||
T <= 2 * pieces->rows && !op_add && !op_fill && op_gq && !op_sig && !qsa_sel &&
|
||||
batch->n_seq == 1 && batch->phase == RAD_PHASE_PREFILL &&
|
||||
!(T <= fused_rows) && batch->positions && !rope_mixed(batch) &&
|
||||
kvb && kvb->slot_mapping && kvb->block_table && kvb->seqused;
|
||||
kvb && !kvb->stream && kvb->slot_mapping && kvb->block_table && kvb->seqused;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -326,11 +364,22 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
* ids: an aperture violation outside the cache. rad_op_reads rather than rw() because the list
|
||||
* depends on whether the layer has an indexer. */
|
||||
if (op_attn) {
|
||||
const rad_buf rd[3] = { w.q, qsa_sel, qsa_sequ };
|
||||
rad_op_reads(b, op_attn, rd, (qsa_sel && qsa_sequ) ? 3 : 1);
|
||||
const rad_buf rd[4] = { w.q, qsa_sel, qsa_sequ, qsa_nsel };
|
||||
rad_op_reads(b, op_attn, rd,
|
||||
(qsa_sel && qsa_sequ) ? (qsa_nsel ? 4 : 3) : 1);
|
||||
rad_op_writes(b, op_attn, &w.attn.x, 1);
|
||||
}
|
||||
|
||||
/* This block owns the selection's residency: kv_resolve is issued immediately before each
|
||||
* attention launch and after this layer's store (see step), because the store writes the mirror
|
||||
* and the resident slots and a block that is not resident yet is copied in whole from the
|
||||
* mirror -- so the resolve must see this step's bytes. Its band is the step's widest launch
|
||||
* (`max_tok`: the gated sparse path takes a whole step in one launch) and its selection row is
|
||||
* `qsa_topk + 1` entries wide, which is what the attention op declares too. The geometry is
|
||||
* kv_resolve_declare's (arch/common/rad_arch.h). */
|
||||
if (qsa_sel && qsa_nsel)
|
||||
op_resolve = kv_resolve_declare(b, g, kv, qsa_sel, qsa_nsel, qsa_topk + 1);
|
||||
|
||||
op_kv_store = rw(b, RAD_OP(b, "kv_store",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("n_head_kv", g.n_head_kv),
|
||||
@@ -339,6 +388,23 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
RAD_NOWEIGHTS),
|
||||
{w.k, w.v}, {});
|
||||
|
||||
/* The store of a group whose KV streams: kv_store's operands and parameters, plus the stream's
|
||||
* residency map and the layer's host mirror, which the batch carries and declare cannot see.
|
||||
* Declared beside kv_store and issued instead of it when the batch hands this group a stream
|
||||
* (step, store_cache). Its `block_size` does not take the group's binding: `attn_paged` above
|
||||
* already claimed it. Declared only for a cache the store has a row for: a packed layout
|
||||
* declares neither this nor the fold below, so the group keeps its resident store (see
|
||||
* kv_store_streamed_dtype). */
|
||||
if (kv_store_streamed_dtype(g.kv_dtype) && rad_op_declared(b, "kv_store_streamed"))
|
||||
op_kv_stream = rw(b, RAD_OP(b, "kv_store_streamed",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok),
|
||||
RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("n_head_kv", g.n_head_kv),
|
||||
RAD_INT("block_size", rad_kv_block_size(b, kv)),
|
||||
RAD_STR("kv_dtype", g.kv_dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.k, w.v}, {});
|
||||
|
||||
/* The fused prologue with the cache store folded in: a k head's block already holds its
|
||||
* token's rotated k, so it also writes k and v into the cache, byte-identical to kv_store.
|
||||
* Declared here because the cache block size is read back from the attention op above.
|
||||
@@ -355,6 +421,23 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
RAD_WEIGHTS(w_q_norm, w_k_norm)),
|
||||
{w.qg, w.k, w.cos_sin, w.v}, {w.q, w.k});
|
||||
|
||||
/* The same fold for a group whose KV streams: the store also writes the layer's host mirror and
|
||||
* consults the residency map, whose operands are the batch's, so they are added at issue
|
||||
* (step). E4M3 or bf16, as the streamed store is, and gated on the same layouts for the same
|
||||
* reason; a group that does not stream never issues it. */
|
||||
if (op_fused && kv_store_streamed_dtype(g.kv_dtype) &&
|
||||
rad_op_declared(b, "qk_norm_rope_gate_kv_store_streamed"))
|
||||
op_fused_stream = rw(b, RAD_OP(b, "qk_norm_rope_gate_kv_store_streamed",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, fused_rows),
|
||||
RAD_INT("head_dim", g.head_dim), RAD_INT("n_head", g.n_head),
|
||||
RAD_INT("n_head_kv", g.n_head_kv), RAD_INT("rot", n_rot),
|
||||
RAD_STR("q_dtype", g.dtype), RAD_F64("eps", g.eps),
|
||||
RAD_F64("wadd", g.wadd_qk), RAD_INT("cs_f32", 1),
|
||||
RAD_INT("block_size", rad_kv_block_size(b, kv)),
|
||||
RAD_STR("kv_dtype", g.kv_dtype)),
|
||||
RAD_WEIGHTS(w_q_norm, w_k_norm)),
|
||||
{w.qg, w.k, w.cos_sin, w.v}, {w.q, w.k});
|
||||
|
||||
/* Output gate, multiply and quantiser in one pass (`gate_quant_fp8`); at decode the dispatch
|
||||
* gaps between three small ops are comparable to the work. Each row is a (token, head) pair:
|
||||
* the gate is a fixed stride inside [q|gate], the attention output is contiguous, and the
|
||||
@@ -469,6 +552,45 @@ inline int AttnGatedFP8::declare_out(RadBuilder* b, bool add_out) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
inline void AttnGatedFP8::store_stage_identity(RadCtx* c, const int32_t* slot, int64_t T) const {
|
||||
RAD_ISSUE_N(c, op_kv_store, T, brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T),
|
||||
kv_cache(kv, layer));
|
||||
}
|
||||
|
||||
inline void AttnGatedFP8::store_cache(RadCtx* c, const RadKVGroupBatch* kvb,
|
||||
const int32_t* slot, int64_t T, bool staged) const {
|
||||
const RadKVStream* st = kvb ? kvb->stream : nullptr;
|
||||
if (!st) {
|
||||
RAD_ISSUE(c, op_kv_store,
|
||||
brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, layer));
|
||||
return;
|
||||
}
|
||||
/* A streamed group's cache is the host mirror; the ring slot is written only when the block is
|
||||
* resident (docs/OPS.md "kv_store_streamed"). Falling back to kv_store here would write the
|
||||
* ring alone, which for a ring smaller than the context is a block id used as a slot -- so a
|
||||
* layer that resolves the stream but not the store fails the step instead. */
|
||||
const int64_t bbytes = st->block_bytes;
|
||||
const int64_t blocks = bbytes > 0 ? st->host_layer_stride / bbytes : 0;
|
||||
const int64_t li = rad_kv_layer_index(c, kv, layer);
|
||||
const uint8_t* host = li >= 0 ? st->host + li * st->host_layer_stride : nullptr;
|
||||
const int32_t* res = li >= 0 ? st->res + li * blocks : nullptr;
|
||||
if (!op_kv_stream || !host || !res || blocks <= 0) {
|
||||
rad_step_fail(c, "kv streaming: this layer's KV group streams but the build has no "
|
||||
"kv_store_streamed row to write its host mirror with, so the layer's KV "
|
||||
"would never reach storage");
|
||||
return;
|
||||
}
|
||||
RAD_ISSUE(c, op_kv_stream,
|
||||
brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, layer),
|
||||
praw(res, RAD_I32, blocks), praw2(host, RAD_U8, blocks, bbytes));
|
||||
/* A staged pass reads the pool at block-id identity while kv_store_streamed's device write is
|
||||
* gated on the residency map, and the map holds nothing before the group's first decode: the
|
||||
* mirror would advance and the stage would not, and the attention would read the previous
|
||||
* layer's bytes. The identity store is the same bytes at the addressing this view uses, so a
|
||||
* staged pass issues it too, after the mirror writer. */
|
||||
if (staged) store_stage_identity(c, slot, T);
|
||||
}
|
||||
|
||||
inline void AttnGatedFP8::store_kv(RadCtx* c, const RadBatch* batch) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
if (!ext_in) nq_h.step(c, T);
|
||||
@@ -479,8 +601,9 @@ inline void AttnGatedFP8::store_kv(RadCtx* c, const RadBatch* batch) const {
|
||||
vp.step(c, w.h, w.v, T);
|
||||
RAD_ISSUE_N(c, op_k_norm, T * g.n_head_kv, brows(w.k, T), RAD_W(w_k_norm), brows(w.k, T));
|
||||
RAD_ISSUE(c, op_rope_k, brows(w.k, T), rope_posmc(g, batch, T));
|
||||
RAD_ISSUE(c, op_kv_store,
|
||||
brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, layer));
|
||||
bool staged = false;
|
||||
if (kvb && kvb->stream) staged = kv_stage_wait(c, kv, kvb, layer) != 0;
|
||||
store_cache(c, kvb, slot, T, staged);
|
||||
}
|
||||
|
||||
inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
@@ -555,8 +678,45 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
* token, so a pass with media tokens (differing components) takes the unfused path with the
|
||||
* full [3, T] positions. */
|
||||
const bool fuse_ok = T <= fused_rows && !rope_mixed(batch);
|
||||
const bool fused_kv = op_fused_kv && fuse_ok;
|
||||
if (fused_kv) {
|
||||
/* A hosted group takes the fused store. A streamed group takes the folded *streamed* store
|
||||
* instead (op_fused_stream), whose operands are the batch's mirror and residency map: it writes
|
||||
* the mirror for every token and the ring slot only for a resident block, exactly what
|
||||
* kv_store_streamed does, so the prologue plus a separate store becomes one launch and the
|
||||
* chain between store, resolve and attention loses a hop. */
|
||||
const bool streamed = kvb && kvb->stream;
|
||||
const bool fused_kv = op_fused_kv && fuse_ok && !streamed;
|
||||
/* The map and the mirror, read exactly as store_cache reads them; a layer with none leaves the
|
||||
* pair to store_cache, which fails the step with its own message rather than serving KV that
|
||||
* never reached storage. */
|
||||
const uint8_t* kv_host = nullptr;
|
||||
const int32_t* kv_res = nullptr;
|
||||
int64_t kv_blocks = 0, kv_bbytes = 0;
|
||||
if (streamed && op_fused_stream && fuse_ok) {
|
||||
const RadKVStream* st = kvb->stream;
|
||||
kv_bbytes = st->block_bytes;
|
||||
kv_blocks = kv_bbytes > 0 ? st->host_layer_stride / kv_bbytes : 0;
|
||||
const int64_t li = rad_kv_layer_index(c, kv, layer);
|
||||
kv_host = li >= 0 ? st->host + li * st->host_layer_stride : nullptr;
|
||||
kv_res = li >= 0 ? st->res + li * kv_blocks : nullptr;
|
||||
if (!kv_host || !kv_res || kv_blocks <= 0) kv_host = nullptr;
|
||||
}
|
||||
const bool folded_stream = streamed && kv_host && kv_res;
|
||||
bool staged_folded = false;
|
||||
/* The folded prologue writes the store, so the stage copy must have landed before the launch
|
||||
* rather than after it (core/kvstream/stage.h, the ordering rule). The fallback keeps the wait
|
||||
* where it was, between the prologue and store_cache. */
|
||||
if (folded_stream) staged_folded = kv_stage_wait(c, kv, kvb, layer) != 0;
|
||||
if (folded_stream) {
|
||||
RAD_ISSUE_N(c, op_fused_stream, T,
|
||||
brows(w.qg, T), brows(w.k, T), RAD_B(w.cos_sin), rope_pos1(batch, T),
|
||||
RAD_W(w_q_norm), RAD_W(w_k_norm),
|
||||
brows(w.q, T), brows(w.k, T), RAD_NONE,
|
||||
brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, layer),
|
||||
praw(kv_res, RAD_I32, kv_blocks), praw2(kv_host, RAD_U8, kv_blocks, kv_bbytes));
|
||||
/* The stage view's identity store, for the reason store_cache states: the folded streamed
|
||||
* store writes the ring slot the map names, never the block id a staged pass reads. */
|
||||
if (staged_folded) store_stage_identity(c, slot, T);
|
||||
} else if (fused_kv) {
|
||||
RAD_ISSUE_N(c, op_fused_kv, T,
|
||||
brows(w.qg, T), brows(w.k, T), RAD_B(w.cos_sin), rope_pos1(batch, T),
|
||||
RAD_W(w_q_norm), RAD_W(w_k_norm),
|
||||
@@ -576,9 +736,15 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
RAD_ISSUE(c, op_rope_k, brows(w.k, T), rope_posmc(g, batch, T));
|
||||
}
|
||||
|
||||
if (!fused_kv)
|
||||
RAD_ISSUE(c, op_kv_store,
|
||||
brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, layer));
|
||||
/* A staged group's copy reads the host mirror out of the DMA engine, so it must have landed
|
||||
* before this layer's store writes this chunk's tokens into the stage: the mirror does not hold
|
||||
* them yet, and a copy landing after the store would put its older bytes back over them. The
|
||||
* wait is a barrier on this stream (core/kvstream/stage.h, the ordering rule). A no-op for a
|
||||
* group with no stage view; already taken above when the store is folded into the prologue. */
|
||||
bool staged_un = false;
|
||||
if (streamed && !folded_stream) staged_un = kv_stage_wait(c, kv, kvb, layer) != 0;
|
||||
if (!fused_kv && !folded_stream)
|
||||
store_cache(c, kvb, slot, T, staged_un);
|
||||
|
||||
/* Sparse attention issues one row per query. `attn_paged` indexes its block table and length
|
||||
* by row, and a row need not be a sequence, so T queries issued as T rows of q_len 1, each with
|
||||
@@ -595,6 +761,37 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
const bool sparse = qsa_sel && qsa_sequ &&
|
||||
(T == batch->n_seq || qsa_exact_to <= 0 || reach > qsa_exact_to);
|
||||
const bool gated = sparse && op_attn_gq;
|
||||
/* Make this launch's rows resident and rewrite their `sel` entries before the attention reads
|
||||
* them. The store above must come first: a block this step writes may not be in the ring yet,
|
||||
* and the resolve then copies whole blocks out of the mirror, so it has to see this step's
|
||||
* bytes. What the order is, per layer: store (mirror, and the resident slot) -> resolve (for
|
||||
* each launch's rows) -> attention. The maps, the ring and the mirror are fixed at bring-up,
|
||||
* so the resolve is a pure function of device state and sits on one stream: issue order is the
|
||||
* ordering. `sel` is the same buffer the attention takes, row sliced to this launch. */
|
||||
auto resolve_sel = [&](int64_t off, int64_t rows) {
|
||||
/* A streamed group whose ring is smaller than its context selected the one-layer stage
|
||||
* view for this pass: stage this layer (once) and leave `sel` as block ids, which address
|
||||
* the identity stage directly. No resolve, so no ring can overflow in a prefill. */
|
||||
if (kv_stage_layer(c, kv, kvb, layer)) return;
|
||||
if (!op_resolve || !kvb || !kvb->stream || rows <= 0) return;
|
||||
const RadKVStream* st = kvb->stream;
|
||||
const int64_t bbytes = st->block_bytes;
|
||||
const int64_t blocks = bbytes > 0 ? st->host_layer_stride / bbytes : 0;
|
||||
const int64_t slots = st->slots;
|
||||
const int64_t li = rad_kv_layer_index(c, kv, layer);
|
||||
/* `host` is null exactly when `li < 0`, so it is the whole test for a mirror row. */
|
||||
const uint8_t* host = li >= 0 ? st->host + li * st->host_layer_stride : nullptr;
|
||||
if (!host || blocks <= 0 || slots <= 0) return;
|
||||
RAD_ISSUE_N(c, op_resolve, rows,
|
||||
brow_slice(qsa_sel, off, rows, qsa_topk + 1),
|
||||
brow_slice(qsa_nsel, off, rows, 1),
|
||||
praw(st->res + li * blocks, RAD_I32, blocks),
|
||||
praw(st->slot_block + li * slots, RAD_I32, slots),
|
||||
praw2(st->slot_state + li * slots * 2, RAD_I32, slots, 2),
|
||||
praw(st->ctl, RAD_I32, 16),
|
||||
praw2(host, RAD_U8, blocks, bbytes),
|
||||
kv_cache(kv, layer));
|
||||
};
|
||||
/* A piece at a time where the caller takes the all-reduce that way (ArPieces). On the gated
|
||||
* sparse path every query row is its own (q_len 1) and the gate rides in the merge, so the
|
||||
* attention and out projection can finish a piece's rows and hand off its reduction while the
|
||||
@@ -607,6 +804,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
const int64_t pn = T - p0 < pieces->rows ? T - p0 : pieces->rows;
|
||||
for (int64_t off = p0; off < p0 + pn; off += chunk) {
|
||||
const int64_t rows = (p0 + pn - off) < chunk ? (p0 + pn - off) : chunk;
|
||||
resolve_sel(off, rows);
|
||||
RAD_ISSUE_N(c, op_attn_gq, 1,
|
||||
brow_slice(w.q, off, rows, qw),
|
||||
kv_cache(kv, layer),
|
||||
@@ -621,6 +819,10 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
o.step(c, w.attn, w.h.x, T, p0, pn);
|
||||
if (p0 + pn < T) pieces->hand(p0, pn);
|
||||
}
|
||||
/* Every attention launch of this layer is issued: start the next layer's stage copy now
|
||||
* (the ride it waits on is completed by the next dispatch on this stream, which is behind
|
||||
* all of them). */
|
||||
if (kvb && kvb->stream) kv_stage_next(c, kv, layer);
|
||||
return;
|
||||
}
|
||||
if (gated) {
|
||||
@@ -628,6 +830,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
const int64_t qw = g.q_dim(), hd = g.head_dim;
|
||||
for (int64_t off = 0; off < T; off += chunk) {
|
||||
const int64_t rows = (T - off) < chunk ? (T - off) : chunk;
|
||||
resolve_sel(off, rows);
|
||||
RAD_ISSUE_N(c, op_attn_gq, 1,
|
||||
brow_slice(w.q, off, rows, qw),
|
||||
kv_cache(kv, layer),
|
||||
@@ -644,6 +847,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
const int64_t qw = g.q_dim();
|
||||
for (int64_t off = 0; off < T; off += chunk) {
|
||||
const int64_t rows = (T - off) < chunk ? (T - off) : chunk;
|
||||
resolve_sel(off, rows);
|
||||
RAD_ISSUE_N(c, op_attn, 1,
|
||||
brow_slice(w.q, off, rows, qw),
|
||||
kv_cache(kv, layer),
|
||||
@@ -653,6 +857,11 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
brow_slice(w.attn.x, off, rows, qw));
|
||||
}
|
||||
} else {
|
||||
/* Below qsa_exact_to the dense path reads a block id straight out of the block table and
|
||||
* never resolves. A staged pass addresses blocks by their id, so this layer must be staged
|
||||
* here too or a short prompt on a small ring would read slots out of an unstaged pool
|
||||
* (docs/KV-STREAMING.md §3.5). A no-op for a group with no stage view. */
|
||||
if (kvb && kvb->stream) (void)kv_stage_layer(c, kv, kvb, layer);
|
||||
RAD_ISSUE_N(c, op_attn, batch->max_q_len,
|
||||
brows(w.q, T), kv_cache(kv, layer),
|
||||
praw2(table, RAD_I32, batch->n_seq,
|
||||
@@ -664,6 +873,10 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
brows(w.attn.x, T));
|
||||
}
|
||||
|
||||
/* Every attention launch of this layer is issued: hand the next layer's copy to the DMA engine
|
||||
* riding on the next dispatch here, so it crosses the link under the out projection and the next
|
||||
* layer's own projections (core/kvstream/stage.h). */
|
||||
if (kvb && kvb->stream) kv_stage_next(c, kv, layer);
|
||||
if (gated) {
|
||||
/* the sparse attention gated and quantised in its merge */
|
||||
} else if (op_sig) {
|
||||
|
||||
@@ -90,6 +90,11 @@ struct DsaCache {
|
||||
d.n_head_kv = 1;
|
||||
d.head_dim = 17;
|
||||
*index_group = rad_decl_kv_group(b, nm.f("kv_dsa_index"), &d);
|
||||
/* The indexer reads this group over the whole live history every step (dsa_score), so
|
||||
* a ring cannot hold it back: it stays resident and its carve becomes a floor of
|
||||
* max_seqs full contexts when a sibling group streams (kvstream/carve.cpp). Inert
|
||||
* unless a sibling streams. */
|
||||
RAD_ARCH_TRY(rad_kv_group_dense_read(b, *index_group));
|
||||
}
|
||||
index = *index_group;
|
||||
if (!index) return RAD_E_INVAL;
|
||||
|
||||
@@ -29,6 +29,11 @@ struct MlaLatent {
|
||||
};
|
||||
struct Wire {
|
||||
rad_buf q = 0, q_latent = 0, attn_latent = 0, out = 0, selected = 0, length = 0;
|
||||
/* qsa_select's entry count (selected_count), one int per row. Not part of the attention's
|
||||
* operands and not a declare requirement: only the streamed resolve needs it, and it fails
|
||||
* the step if a streaming group's selection has no count. Never `length`, which is a token
|
||||
* count. */
|
||||
rad_buf nsel = 0;
|
||||
};
|
||||
Geom g{};
|
||||
Config c{};
|
||||
@@ -128,7 +133,7 @@ struct GlmMla {
|
||||
QuantFP8 attn_quant{};
|
||||
MlaLatent core{};
|
||||
rad_weight q_gain = 0, kv_gain = 0;
|
||||
rad_op op_kv_norm = 0, op_store = 0, op_ar = 0;
|
||||
rad_op op_kv_norm = 0, op_store = 0, op_store_stream = 0, op_resolve = 0, op_ar = 0;
|
||||
LinearFP8 rows_q_b{}, rows_output{};
|
||||
QuantFP8 rows_quant{};
|
||||
rad_weight rows_kv_b = 0;
|
||||
@@ -189,6 +194,24 @@ struct GlmMla {
|
||||
RAD_ARCH_TRY(output.declare(b, nm, g, nm.f("blk.%d.attn_output", layer), 4096,
|
||||
GlmTp::mla_width(g.world), RAD_SHARD_COL, grp_layer(layer),
|
||||
{ nm.ckpt("%s.o_proj.weight", prefix) }, w.attn, w.out));
|
||||
/* The store of a group whose KV streams: kv_store's operands and parameters, plus the
|
||||
* stream's residency map and this layer's host mirror, which the batch carries and declare
|
||||
* cannot see (prepare adds them at issue). A streamed group's store must write the mirror,
|
||||
* so falling back to kv_store would leave it empty and a miss would copy zeros. */
|
||||
if (rad_op_declared(b, "kv_store_streamed") && rad_kv_block_size(b, kv) == 4)
|
||||
op_store_stream = rw(b,
|
||||
RAD_OP(b, "kv_store_streamed",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok),
|
||||
RAD_INT("head_dim", 256), RAD_INT("n_head_kv", 1),
|
||||
RAD_INT("block_size", 4),
|
||||
RAD_STR("kv_dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.latent }, {});
|
||||
/* The resolve that rewrites the selection from block ids into ring slots. It is issued in
|
||||
* finish, after this layer's indexer selects and before the attention reads `selected`;
|
||||
* that ordering is the whole correctness argument. `sel_pitch` is the selection row's
|
||||
* pitch, which is what MlaLatent declares its attention with. */
|
||||
op_resolve = kv_resolve_declare(b, g, kv, w.core.selected, w.core.nsel, core.c.blocks);
|
||||
if (reduce) op_ar = GlmTp::reduce(b, g, w.out);
|
||||
if (!op_store || ((reduce && g.world > 1) && !op_ar)) return RAD_E_INVAL;
|
||||
return reduce ? declare_rows(b, nm, prefix) : RAD_OK;
|
||||
@@ -265,20 +288,112 @@ struct GlmMla {
|
||||
kv_a.step(ctx, w.input, w.kv_raw, T);
|
||||
q_norm.step(ctx, T);
|
||||
RAD_ISSUE_N(ctx, op_kv_norm, T, brows(w.kv_raw, T), RAD_W(kv_gain), brows(w.latent, T));
|
||||
/* The row form splits the query projection into finish_rows (per rank), so q_b runs here
|
||||
* only in the head form; the latent store below is full length either way. */
|
||||
if (!rows_take(T)) q_b.step(ctx, w.q_low, w.core.q, T);
|
||||
RAD_ISSUE_N(ctx, op_store, T, bcol(w.latent, 0, 256, T), bcol(w.latent, 256, 256, T),
|
||||
praw(kb->slot_mapping, RAD_I32, T), kv_cache(kv, layer));
|
||||
const RadKVStream* st = kb->stream;
|
||||
if (!st) {
|
||||
RAD_ISSUE_N(ctx, op_store, T, bcol(w.latent, 0, 256, T), bcol(w.latent, 256, 256, T),
|
||||
praw(kb->slot_mapping, RAD_I32, T), kv_cache(kv, layer));
|
||||
return;
|
||||
}
|
||||
/* A streamed group's cache is the host mirror: the ring slot is written only when the block
|
||||
* is already resident. The wait must sit immediately before the store, so this chunk's
|
||||
* bytes are the stage's last writer (core/kvstream/stage.h ORDERING RULE 1-2). Falling back
|
||||
* to kv_store here would write the ring alone, which for a ring smaller than the context is
|
||||
* a block id used as a slot, so the step fails instead. */
|
||||
(void)kv_stage_wait(ctx, kv, kb, layer);
|
||||
const int64_t bbytes = st->block_bytes;
|
||||
const int64_t blocks = bbytes > 0 ? st->host_layer_stride / bbytes : 0;
|
||||
const int64_t li = rad_kv_layer_index(ctx, kv, layer);
|
||||
const uint8_t* host = li >= 0 ? st->host + li * st->host_layer_stride : nullptr;
|
||||
const int32_t* res = li >= 0 ? st->res + li * blocks : nullptr;
|
||||
if (!op_store_stream || !host || !res || blocks <= 0) {
|
||||
rad_step_fail(ctx, "GLM MLA: this layer's KV group streams but the build has no "
|
||||
"kv_store_streamed row to write its host mirror with, so the layer's "
|
||||
"KV would never reach storage");
|
||||
return;
|
||||
}
|
||||
RAD_ISSUE_N(ctx, op_store_stream, T, bcol(w.latent, 0, 256, T),
|
||||
bcol(w.latent, 256, 256, T), praw(kb->slot_mapping, RAD_I32, T),
|
||||
kv_cache(kv, layer), praw(res, RAD_I32, blocks),
|
||||
praw2(host, RAD_U8, blocks, bbytes));
|
||||
/* A staged pass reads the pool at block-id identity, and kv_store_streamed's device write is
|
||||
* gated on the residency map: a block is written only where the *ring* holds it. The map is
|
||||
* empty for every staged prefill before the group's first decode, so without this store the
|
||||
* mirror would advance and the stage would not, and the attention would read the previous
|
||||
* layer's bytes for this chunk. The identity store is the same bytes at the addressing this
|
||||
* view uses, so a staged pass issues it too, after the mirror writer. `kv_stage_layer` is the
|
||||
* predicate that gates the resolve below, answered here by the wait just taken: the layer is
|
||||
* staged and must not resolve. */
|
||||
if (kv_stage_layer(ctx, kv, kb, layer))
|
||||
RAD_ISSUE_N(ctx, op_store, T, bcol(w.latent, 0, 256, T), bcol(w.latent, 256, 256, T),
|
||||
praw(kb->slot_mapping, RAD_I32, T), kv_cache(kv, layer));
|
||||
}
|
||||
void finish(RadCtx* ctx, const RadBatch* batch) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
const auto* kb = kv_batch(batch, kv);
|
||||
if (kb && kb->stream) {
|
||||
/* One `selected` buffer serves the whole model (glm5next.cpp assigns m.iw.selected =
|
||||
* m.aw.core.selected), so this layer's indexer must have re-emitted fresh ids
|
||||
* immediately above. Only one full layer resolves at a time, and only on a pass that
|
||||
* takes the ring; a staged pass addresses block ids directly through the identity
|
||||
* stage. Resolving outside that order makes layer n+1 resolve layer n's ring slots as
|
||||
* block ids: in range, fluent, wrong. */
|
||||
if (kv_stage_layer(ctx, kv, kb, layer)) {
|
||||
/* staged: this pass leaves `selected` as block ids and must not resolve */
|
||||
} else if (!w.core.nsel) {
|
||||
rad_step_fail(ctx, "GLM MLA: the KV group streams but the selection has no entry "
|
||||
"count (selected_count), so kv_resolve cannot rewrite it");
|
||||
return;
|
||||
} else if (!op_resolve) {
|
||||
rad_step_fail(ctx, "GLM MLA: this layer's KV group streams but the build has no "
|
||||
"kv_resolve row to rewrite its selection with, so the attention "
|
||||
"would read block ids as ring slots");
|
||||
return;
|
||||
} else if (T > 0) {
|
||||
const RadKVStream* st = kb->stream;
|
||||
const int64_t bbytes = st->block_bytes;
|
||||
const int64_t blocks = bbytes > 0 ? st->host_layer_stride / bbytes : 0;
|
||||
const int64_t slots = st->slots;
|
||||
const int64_t li = rad_kv_layer_index(ctx, kv, layer);
|
||||
/* `host` is null exactly when `li < 0`, so it is the whole test for a mirror row. */
|
||||
const uint8_t* host = li >= 0 ? st->host + li * st->host_layer_stride : nullptr;
|
||||
if (!host || blocks <= 0 || slots <= 0) {
|
||||
rad_step_fail(ctx, "GLM MLA: the KV group streams but this layer has no host "
|
||||
"mirror row or residency map to resolve against");
|
||||
return;
|
||||
}
|
||||
/* The count operand is qsa_select's second output, the number of entries it wrote,
|
||||
* never `length` (its third output, a token count). The device clamps a too-large
|
||||
* count to sel_pitch and skips the -1 padding, so the wrong buffer serves the right
|
||||
* slots on the card while the reference refuses the same call -- the oracle dies
|
||||
* quietly (tests/arch_test.cpp asserts this handle). */
|
||||
RAD_ISSUE_N(ctx, op_resolve, T,
|
||||
brow_slice(w.core.selected, 0, T, core.c.blocks),
|
||||
brow_slice(w.core.nsel, 0, T, 1),
|
||||
praw(st->res + li * blocks, RAD_I32, blocks),
|
||||
praw(st->slot_block + li * slots, RAD_I32, slots),
|
||||
praw2(st->slot_state + li * slots * 2, RAD_I32, slots, 2),
|
||||
praw(st->ctl, RAD_I32, 16),
|
||||
praw2(host, RAD_U8, blocks, bbytes),
|
||||
kv_cache(kv, layer));
|
||||
}
|
||||
}
|
||||
/* The row form (upstream 1.12.0): a prefill-sized step under --tp-attn-split runs the
|
||||
* attention over a rank's own rows and gathers, so the resolve above already rewrote
|
||||
* `selected` for every row before either form reads it. */
|
||||
if (rows_take(T)) {
|
||||
finish_rows(ctx, T);
|
||||
return;
|
||||
} else {
|
||||
core.step(ctx, T);
|
||||
attn_quant.step(ctx, T);
|
||||
output.step(ctx, w.attn, w.out, T);
|
||||
if (op_ar) RAD_ISSUE_N(ctx, op_ar, T * 4096, brows(w.out, T), RAD_NONE);
|
||||
}
|
||||
core.step(ctx, T);
|
||||
attn_quant.step(ctx, T);
|
||||
output.step(ctx, w.attn, w.out, T);
|
||||
if (op_ar) RAD_ISSUE_N(ctx, op_ar, T * 4096, brows(w.out, T), RAD_NONE);
|
||||
/* Behind every attention launch of this layer, so the next layer's copy starts only once
|
||||
* the last read of the aliased stage is behind it (core/kvstream/stage.h). */
|
||||
if (kb && kb->stream) kv_stage_next(ctx, kv, layer);
|
||||
}
|
||||
/* This rank's rows [first, first + n) end to end; the last rank's run may be short, and the
|
||||
* gather sends its unwritten tail rows, which land past T. */
|
||||
|
||||
@@ -216,6 +216,10 @@ inline int QsaIndexer::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
|
||||
d.head_dim = c.head_dim / (2 * c.ratio);
|
||||
*bk = rad_decl_kv_group(b, nm.f("kv_qsa_bkey"), &d);
|
||||
if (!*bk) return RAD_E_INVAL;
|
||||
/* qsa_score walks every complete block of the live history each step, so this group is
|
||||
* read densely and cannot stream: its carve must hold every sequence's context while a
|
||||
* sibling group's blocks live in host memory (kvstream/carve.h). */
|
||||
RAD_ARCH_TRY(rad_kv_group_dense_read(b, *bk));
|
||||
}
|
||||
kv_bk = *bk;
|
||||
/* Under M-RoPE a block key rotates at its first token's three components, which qsa_work
|
||||
@@ -255,6 +259,17 @@ inline int QsaIndexer::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
|
||||
RAD_INT("ratio", c.ratio), RAD_INT("seqs", g.max_seqs)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.score, w.nc}, {w.sel, w.nsel, w.sequ});
|
||||
|
||||
/* The selection's block ids become ring slots between here and the attention when the
|
||||
* attention group streams. The resolve is declared and issued by the attention block, after
|
||||
* that layer's KV store, not here: the store is what puts this step's bytes in the mirror, and
|
||||
* a resolve that ran before it would copy a block short of the tokens just written
|
||||
* (arch/common/rad_block_attn_gated_fp8.h, AttnGatedFP8::step). The indexer's own read of
|
||||
* `sel` is only the buffer's producer.
|
||||
*
|
||||
* A ring smaller than the group's context is what the engine admits (--kv-resident), so `sel`'s
|
||||
* entries are logical block ids and the resolve rewrites them as ring slots; prefill runs on
|
||||
* the one-layer stage and never resolves (docs/KV-STREAMING.md §3.5). */
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -368,6 +383,10 @@ inline void QsaIndexer::step(RadCtx* c_, const ActFP8& in, const RadBatch* batch
|
||||
* block table and complete-block count are the step's, so each row is scored and selected as
|
||||
* in one pass over the step, and a row two windows share is written twice with the same
|
||||
* bytes. */
|
||||
/* The selection's block ids become ring slots in the attention block, after that layer's KV
|
||||
* store (AttnGatedFP8::step): the store is what puts this step's bytes in the host mirror, and
|
||||
* a resolve issued here -- before the store -- would copy a non-resident block short of the
|
||||
* tokens this step writes. The indexer only produces the table; it never resolves it. */
|
||||
const int64_t PR = c.plane_rows > 0 ? c.plane_rows : T;
|
||||
if (T > PR) {
|
||||
if (S != 1 || !win_cu) {
|
||||
|
||||
@@ -214,6 +214,10 @@ static int indexer_buffers(RadBuilder* b, Model& m) {
|
||||
c.selected = m.aw.core.selected;
|
||||
c.length = m.aw.core.length;
|
||||
c.nsel = decl_b(b, nm.f("selected_count"), RAD_I32, { T });
|
||||
/* The MLA block's resolve names qsa_select's second output; it must never take `length`,
|
||||
* the third output, which is a token count (glm5next.cpp's attention order is store ->
|
||||
* select -> resolve -> attention). */
|
||||
m.aw.core.nsel = c.nsel;
|
||||
if (g.world > 1) {
|
||||
c.cu_part = decl_b(b, nm.f("index_part_bounds"), RAD_I32, { g.max_seqs + 1 });
|
||||
c.sel_part = decl_b(b, nm.f("selected_part"), RAD_I32, { part, 513 });
|
||||
@@ -396,6 +400,10 @@ static int geometry(Geom& g, const RadModelMeta* meta, const RadBuildCtx* ctx, i
|
||||
g.max_ctx = ctx->max_ctx;
|
||||
g.max_seqs = ctx->max_seqs;
|
||||
g.max_spec = ctx->max_spec;
|
||||
/* --kv-resident in tokens. Geom is filled by hand here (not by the shared helper), and the
|
||||
* resolve's declared `slots` is what the ring can address, so leaving this 0 would declare the
|
||||
* whole context as the ring. */
|
||||
g.kv_resident = ctx->kv_resident_blocks;
|
||||
g.max_logit_rows = std::min<int64_t>(g.max_tok, g.max_seqs * (g.max_spec + 1));
|
||||
if (ctx->max_out_rows > g.max_logit_rows) g.max_logit_rows = ctx->max_out_rows;
|
||||
g.eps = meta->rms_eps;
|
||||
@@ -593,6 +601,9 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
}
|
||||
static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
if (!meta || !out) return RAD_E_INVAL;
|
||||
/* A full MLA layer streams: rad_block_glm_mla.h declares the streamed store, which writes the
|
||||
* host mirror, and the resolve that maps the indexer's block ids to ring slots. */
|
||||
out->kv_stream_ok = 1;
|
||||
if (cfg(meta, "num_nextn_predict_layers", 0) == 1) out->draft_depth = GlmMtp::kDepth;
|
||||
// The tower's merged block, so a pass size the core picks itself is whole output rows.
|
||||
if (GlmVision::present(meta)) {
|
||||
|
||||
@@ -1090,6 +1090,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
* paths must give identical text; any difference is a defect. */
|
||||
if (m.qsaw.qk) {
|
||||
lay.attn.qsa_sel = m.qsaw.sel;
|
||||
lay.attn.qsa_nsel = m.qsaw.nsel;
|
||||
lay.attn.qsa_sequ = m.qsaw.sequ;
|
||||
lay.attn.qsa_topk = m.qsacfg.topk;
|
||||
/* The exactness bound, below which prefill issues the cheaper dense attention.
|
||||
@@ -1357,6 +1358,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
m.mtp.attn.kv_block = 4;
|
||||
if (m.qsaw.qk) {
|
||||
m.mtp.attn.qsa_sel = m.qsaw.sel;
|
||||
m.mtp.attn.qsa_nsel = m.qsaw.nsel;
|
||||
m.mtp.attn.qsa_sequ = m.qsaw.sequ;
|
||||
m.mtp.attn.qsa_topk = m.qsacfg.topk;
|
||||
m.mtp.attn.qsa_exact_to = m.qsacfg.budget + m.qsacfg.ratio - 1;
|
||||
@@ -1692,6 +1694,9 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
* mapped table). */
|
||||
static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
if (!meta || !out) return RAD_E_INVAL;
|
||||
/* A full QSA layer streams: rad_block_attn_gated_fp8.h declares the streamed store (fp8 or
|
||||
* bf16, plain or folded) and the resolve that maps its block ids to ring slots. */
|
||||
out->kv_stream_ok = 1;
|
||||
out->draft_depth = 0;
|
||||
if (meta_int2(meta, "qwen4exp.mtp_num_hidden_layers", "mtp_num_hidden_layers", 0) > 0)
|
||||
out->draft_depth = 3;
|
||||
|
||||
+61
-1
@@ -71,7 +71,8 @@ struct RecMap {
|
||||
std::vector<std::string> src;
|
||||
int concat_dim = 0;
|
||||
};
|
||||
struct RecKV { std::string name; RadKVGroupDecl d{}; bool zero = false; bool replicated = false; };
|
||||
struct RecKV { std::string name; RadKVGroupDecl d{}; bool zero = false; bool replicated = false;
|
||||
bool dense = false; };
|
||||
struct RecIssue {
|
||||
rad_op op = 0;
|
||||
std::vector<RadOperand> opd;
|
||||
@@ -147,6 +148,11 @@ struct RadBuilder {
|
||||
const RecOp* op_of(rad_op h) const { return h ? &ops[h - 1] : nullptr; }
|
||||
};
|
||||
|
||||
struct StageRec {
|
||||
size_t issue = 0; /* the issue count when the stage copy was issued */
|
||||
int layer = -1;
|
||||
};
|
||||
|
||||
struct RadCtx {
|
||||
RadBuilder* b = nullptr;
|
||||
const RadBatch* batch = nullptr;
|
||||
@@ -160,6 +166,13 @@ struct RadCtx {
|
||||
/* Device copies into a buffer (rad_memcpy_async to a rad_buf_ptr address): the buffer and the
|
||||
* issue number the copy came before. The step lint counts each as a write there. */
|
||||
std::vector<std::pair<size_t, rad_buf>> copies;
|
||||
/* The prefill-stage test: a stage call is recorded at the current issue index so the test can
|
||||
* assert it sits between the layer's store and its attention. */
|
||||
bool stage_on = false;
|
||||
int stage_last = -1;
|
||||
std::vector<StageRec> stages;
|
||||
/* kv_stage_next: after which issue index the next layer's copy was handed over. */
|
||||
std::vector<StageRec> stages_next;
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------- the declare-side ABI */
|
||||
@@ -237,6 +250,14 @@ extern "C" rad_kvgroup rad_decl_kv_group(RadBuilder* b, const char* name,
|
||||
|
||||
extern "C" int64_t rad_kv_block_size(RadBuilder* b, rad_kvgroup g) { (void)g; return b->kv_block; }
|
||||
|
||||
extern "C" int64_t rad_kv_layer_bytes(RadBuilder* b, rad_kvgroup g) {
|
||||
if (!b || !g || g > b->kv_groups.size()) return 0;
|
||||
const RadKVGroupDecl& d = b->kv_groups[g - 1].d;
|
||||
if (d.kind != RAD_KV_FULL && d.kind != RAD_KV_WINDOW) return 0;
|
||||
const int64_t nh = d.n_head_kv > 0 ? d.n_head_kv : 1;
|
||||
return rad_dtype_bytes(d.dtype, 2 * b->kv_block * nh * d.head_dim);
|
||||
}
|
||||
|
||||
extern "C" int rad_bind_layer_kv(RadBuilder* b, int layer, rad_kvgroup g) {
|
||||
b->binds.push_back({layer, g});
|
||||
return RAD_OK;
|
||||
@@ -346,6 +367,15 @@ extern "C" int rad_kv_group_zero(RadBuilder* b, rad_kvgroup g) {
|
||||
b->kv_groups[g - 1].zero = true;
|
||||
return RAD_OK;
|
||||
}
|
||||
/* Recorded like zero, and like the real builder refused on a non-full group. Not part of
|
||||
* graph_hash(): the flag changes how much of the group the carve funds, not the graph, so the
|
||||
* pinned graphs stand. */
|
||||
extern "C" int rad_kv_group_dense_read(RadBuilder* b, rad_kvgroup g) {
|
||||
if (!b || g == 0 || g > b->kv_groups.size()) return RAD_E_INVAL;
|
||||
if (b->kv_groups[g - 1].d.kind != RAD_KV_FULL) return RAD_E_INVAL;
|
||||
b->kv_groups[g - 1].dense = true;
|
||||
return RAD_OK;
|
||||
}
|
||||
/* Recorded like zero, and like the real builder refused on a zero or unpaged group. Not part of
|
||||
* graph_hash(): the flag changes what a host tier slot holds, not the graph, so the pinned
|
||||
* graphs stand. */
|
||||
@@ -435,6 +465,36 @@ extern "C" int rad_lane_join(RadCtx* c, int from, int to) {
|
||||
c->joins.push_back({ c->issues.size(), from, to });
|
||||
return RAD_OK;
|
||||
}
|
||||
/* No pool binding here, so a layer is at the index it names: the arch test uses layer 0 where it
|
||||
* checks the mirror offset. */
|
||||
extern "C" int rad_kv_layer_index(RadCtx* c, rad_kvgroup g, int layer) {
|
||||
(void)c; (void)g;
|
||||
return layer;
|
||||
}
|
||||
/* The prefill stage: no device here, so record the call at its place in the issue stream. Off
|
||||
* unless a test selects the stage view, which leaves the Phase 0b resolve test's path unchanged. */
|
||||
extern "C" int rad_kv_stage_layer(RadCtx* c, rad_kvgroup g, const RadKVGroupBatch* kvb, int layer) {
|
||||
(void)g;
|
||||
if (!c || !kvb || !kvb->stream || !c->stage_on) return 0;
|
||||
return 1; /* the wait before the layer's store did the work */
|
||||
}
|
||||
/* The wait comes before the layer's store (a stage copy that landed after it would overwrite the
|
||||
* tokens the store just wrote), so its seam is what the stage test records. */
|
||||
extern "C" int rad_kv_stage_wait(RadCtx* c, rad_kvgroup g, const RadKVGroupBatch* kvb, int layer) {
|
||||
(void)g;
|
||||
if (!c || !kvb || !kvb->stream || !c->stage_on) return 0;
|
||||
if (layer == c->stage_last) return 1;
|
||||
c->stage_last = layer;
|
||||
c->stages.push_back(StageRec{ c->issues.size(), layer });
|
||||
return 1;
|
||||
}
|
||||
/* The next layer's copy is handed to the DMA engine after this layer's attention. */
|
||||
extern "C" int rad_kv_stage_next(RadCtx* c, rad_kvgroup g, int layer) {
|
||||
(void)g;
|
||||
if (!c || !c->stage_on) return 0;
|
||||
c->stages_next.push_back(StageRec{ c->issues.size(), layer });
|
||||
return 0;
|
||||
}
|
||||
/* A block that refuses the batch records the reason for the tests. */
|
||||
extern "C" int rad_step_fail(RadCtx* c, const char* what) {
|
||||
c->step_fail = what ? what : "";
|
||||
|
||||
+628
-19
@@ -1325,6 +1325,521 @@ TEST(qwen4exp_yarn_reaches_attention_indexer_and_compressed_keys) {
|
||||
CHECK(direct > 0); CHECK(table > 0); CHECK(compressed > 0);
|
||||
}
|
||||
|
||||
/* A streamed attention group's selection must be made resident and rewritten between
|
||||
* `qsa_select` and the sparse attention, and the layer's KV store must come first: a block this
|
||||
* step writes may not be in the ring, and the resolve copies such a block whole out of the host
|
||||
* mirror, so the mirror write precedes the resolve. The order per layer is therefore
|
||||
* store -> resolve -> attention, with the same `sel` buffer in the resolve and the attention, and
|
||||
* exactly one resolve per attention launch -- a second one for one launch would read slots as block
|
||||
* ids, and one for an earlier launch would let a later launch's evictions move the blocks it names.
|
||||
*
|
||||
* The indexer issues none: it produces `sel`, and a resolve between `qsa_select` and the store
|
||||
* would copy the mirror before this step's tokens reached it. */
|
||||
TEST(a_streamed_qwen4exp_group_stores_then_resolves_then_attends) {
|
||||
RadModelMeta meta = qwen4exp_meta();
|
||||
RadBuildCtx c = build_ctx(0, 1);
|
||||
c.max_tok = 64; c.max_seqs = 4; c.max_spec = 3; c.max_ctx = 200000;
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
|
||||
|
||||
/* The parity configuration's declaration: blocks == slots and the block's own byte pitch. */
|
||||
const RecOp* rop = nullptr;
|
||||
for (const RecOp& o : b.ops) if (o.op == "kv_resolve") { rop = &o; break; }
|
||||
REQUIRE(rop != nullptr);
|
||||
int64_t blocks = 0, bbytes = 0, pitch = 0;
|
||||
for (const RecParam& p : rop->p) {
|
||||
if (p.key == "blocks") blocks = p.ival;
|
||||
if (p.key == "block_bytes") bbytes = p.ival;
|
||||
if (p.key == "sel_pitch") pitch = p.ival;
|
||||
}
|
||||
REQUIRE(blocks > 0);
|
||||
REQUIRE(bbytes > 0);
|
||||
REQUIRE(pitch > 0);
|
||||
|
||||
std::unique_ptr<Kit> k = kit(b, { 1 }, {});
|
||||
std::vector<int32_t> res((size_t)blocks, -1), sblk((size_t)blocks, -1),
|
||||
sst((size_t)blocks * 2, 0), ctl(16, 0);
|
||||
RadKVStream st{};
|
||||
st.host = (uint8_t*)res.data();
|
||||
st.res = res.data();
|
||||
st.slot_block = sblk.data();
|
||||
st.slot_state = sst.data();
|
||||
st.ctl = ctl.data();
|
||||
st.block_bytes = bbytes;
|
||||
st.host_layer_stride = blocks * bbytes;
|
||||
st.slots = blocks;
|
||||
st.host_bytes = blocks * bbytes * (int64_t)b.kv_groups.size();
|
||||
int streamed = 0;
|
||||
for (size_t gi = 0; gi < b.kv_groups.size(); ++gi)
|
||||
if (b.kv_groups[gi].name.find("kv_attn") != std::string::npos) {
|
||||
k->kv[gi].stream = &st;
|
||||
++streamed;
|
||||
}
|
||||
CHECK_EQ(streamed, 1);
|
||||
|
||||
RadCtx ctx;
|
||||
ctx.b = &b;
|
||||
ctx.batch = &k->b;
|
||||
qwen4exp_fp8::step(&ctx, &k->b);
|
||||
|
||||
int selects = 0, stores = 0, resolves = 0, attn = 0;
|
||||
size_t last_select = 0, last_store = 0;
|
||||
for (size_t i = 0; i < ctx.issues.size(); ++i) {
|
||||
const RecOp* o = b.op_of(ctx.issues[i].op);
|
||||
if (!o) continue;
|
||||
if (o->op == "qsa_select") { ++selects; last_select = i; }
|
||||
else if (o->op == "kv_store_streamed" ||
|
||||
o->op == "qk_norm_rope_gate_kv_store_streamed") {
|
||||
++stores;
|
||||
last_store = i;
|
||||
/* The mirror and the residency map are the stream's raw planes, and they are the
|
||||
* operands the layer's KV reaches storage through. A streamed group's store is folded
|
||||
* into the prologue whenever the fused pair resolves and the step fits its rows, so
|
||||
* both forms are accepted and each is checked at its own operand positions. */
|
||||
const bool folded = o->op == "qk_norm_rope_gate_kv_store_streamed";
|
||||
const int ro = folded ? 12 : 4, ho = folded ? 13 : 5;
|
||||
CHECK_EQ(ctx.issues[i].opd[ro].kind, (int)RAD_OPK_RAW); /* res */
|
||||
CHECK_EQ(ctx.issues[i].opd[ro].dtype, (uint32_t)RAD_I32);
|
||||
CHECK_EQ(ctx.issues[i].opd[ro].rows, blocks);
|
||||
CHECK_EQ(ctx.issues[i].opd[ho].kind, (int)RAD_OPK_RAW); /* host, this layer's row */
|
||||
CHECK_EQ(ctx.issues[i].opd[ho].dtype, (uint32_t)RAD_U8);
|
||||
CHECK_EQ(ctx.issues[i].opd[ho].rows, blocks);
|
||||
CHECK_EQ(ctx.issues[i].opd[ho].cols, bbytes);
|
||||
} else if (o->op == "kv_resolve") {
|
||||
++resolves;
|
||||
CHECK(i > last_store); /* the mirror write precedes the copy */
|
||||
CHECK(i > last_select); /* and the selection precedes the rewrite */
|
||||
CHECK_EQ(ctx.issues[i].opd[0].handle, ctx.issues[last_select].opd[5].handle);
|
||||
CHECK_EQ(ctx.issues[i].opd[0].offset, ctx.issues[last_select].opd[5].offset);
|
||||
CHECK_EQ(ctx.issues[i].opd[1].kind, (int)RAD_OPK_BUF); /* nsel, its row count */
|
||||
/* One resolve per attention launch: the launch that follows reads exactly its rows. */
|
||||
size_t j = i + 1;
|
||||
for (; j < ctx.issues.size(); ++j) {
|
||||
const RecOp* n = b.op_of(ctx.issues[j].op);
|
||||
if (!n) continue;
|
||||
if (n->op == "attn_paged" || n->op == "attn_paged_gate_quant") break;
|
||||
}
|
||||
REQUIRE(j < ctx.issues.size());
|
||||
CHECK_EQ(ctx.issues[j].opd[2].handle, ctx.issues[i].opd[0].handle);
|
||||
CHECK_EQ(ctx.issues[j].opd[2].offset, ctx.issues[i].opd[0].offset);
|
||||
CHECK_EQ(ctx.issues[j].opd[2].rows, ctx.issues[i].opd[0].rows);
|
||||
} else if (o->op == "attn_paged" || o->op == "attn_paged_gate_quant") {
|
||||
++attn;
|
||||
CHECK(ctx.issues[i].opd[2].handle == ctx.issues[last_select].opd[5].handle);
|
||||
CHECK_EQ(ctx.issues[i].opd[2].offset, ctx.issues[last_select].opd[5].offset);
|
||||
}
|
||||
}
|
||||
CHECK(selects > 0);
|
||||
CHECK_EQ(stores, 2); /* one store a full-attention layer, before its attention */
|
||||
CHECK_EQ(attn, stores);
|
||||
CHECK_EQ(resolves, attn); /* exactly one resolve per attention launch, never two */
|
||||
}
|
||||
|
||||
/* The parity configuration's stream: a ring as wide as the group's context and an identity map, so
|
||||
* a resolve may rewrite a selection but can never copy. Only the rewrite is under test here, so
|
||||
* the maps alias one allocation and no kernel runs. */
|
||||
struct StreamRig {
|
||||
std::vector<int32_t> res, slot_block, slot_state, ctl;
|
||||
RadKVStream st{};
|
||||
};
|
||||
|
||||
/* `blocks` and `block_bytes` come from the declaration's own `kv_resolve`, so the rig is exactly
|
||||
* what the plugin planned for. The group is reached by name because `kv_attn` is the only full
|
||||
* group a stream flows through (the indexer's own block keys stay resident). */
|
||||
void attach_stream(const RadBuilder& b, Kit& k, StreamRig& r, int64_t blocks, int64_t bbytes) {
|
||||
r.res.assign((size_t)blocks, -1);
|
||||
r.slot_block.assign((size_t)blocks, -1);
|
||||
r.slot_state.assign((size_t)blocks * 2, 0);
|
||||
r.ctl.assign(16, 0);
|
||||
r.st.host = (uint8_t*)r.res.data();
|
||||
r.st.res = r.res.data();
|
||||
r.st.slot_block = r.slot_block.data();
|
||||
r.st.slot_state = r.slot_state.data();
|
||||
r.st.ctl = r.ctl.data();
|
||||
r.st.block_bytes = bbytes;
|
||||
r.st.host_layer_stride = blocks * bbytes;
|
||||
r.st.slots = blocks;
|
||||
r.st.host_bytes = blocks * bbytes * (int64_t)b.kv_groups.size();
|
||||
int streamed = 0;
|
||||
for (size_t gi = 0; gi < b.kv_groups.size(); ++gi)
|
||||
if (b.kv_groups[gi].name.find("kv_attn") != std::string::npos) {
|
||||
k.kv[gi].stream = &r.st;
|
||||
++streamed;
|
||||
}
|
||||
CHECK_EQ(streamed, 1);
|
||||
}
|
||||
|
||||
/* The `blocks`, `block_bytes` and `sel_pitch` the plugin declared for `kv_resolve`. */
|
||||
void resolve_geom(const RadBuilder& b, int64_t* blocks, int64_t* bbytes, int64_t* pitch) {
|
||||
for (const RecOp& o : b.ops)
|
||||
if (o.op == "kv_resolve")
|
||||
for (const RecParam& p : o.p) {
|
||||
if (p.key == "blocks") *blocks = p.ival;
|
||||
if (p.key == "block_bytes") *bbytes = p.ival;
|
||||
if (p.key == "sel_pitch") *pitch = p.ival;
|
||||
}
|
||||
}
|
||||
|
||||
/* The `slots` the plugin declared for `kv_resolve`: the ceiling a ring may not pass. */
|
||||
int64_t resolve_slots(const RadBuilder& b) {
|
||||
int64_t slots = 0;
|
||||
for (const RecOp& o : b.ops)
|
||||
if (o.op == "kv_resolve")
|
||||
for (const RecParam& p : o.p)
|
||||
if (p.key == "slots") slots = p.ival;
|
||||
return slots;
|
||||
}
|
||||
|
||||
/* The declared slot domain covers a ring the engine raised, not just the flag's own number. The
|
||||
* engine raises --kv-resident to the smallest ring one decode step can name when the operator asks
|
||||
* for less (kvstream_ring, kvstream/plan.h), and the resolve refuses a pool deeper than its declared
|
||||
* `slots` (libref/ref_kvstream.cpp: the parameter is the ceiling the operand may not pass). So the
|
||||
* declaration states that step bound: max_seqs x (1 + n_spec) x sel_pitch, the same product
|
||||
* kvstream_ceiling_blocks states on the engine's side. With nothing streaming it stays the context,
|
||||
* which is what an undeclared ring has always meant. */
|
||||
TEST(the_declared_slot_ceiling_covers_a_ring_raised_to_one_decode_step) {
|
||||
RadModelMeta meta = qwen4exp_meta();
|
||||
RadBuildCtx c = build_ctx(0, 1);
|
||||
c.max_tok = 64; c.max_seqs = 4; c.max_spec = 3; c.max_ctx = 200000;
|
||||
c.kv_resident_blocks = 8192; /* 2048 blocks of 4: under one step's selection */
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
|
||||
int64_t blocks = 0, bbytes = 0, pitch = 0;
|
||||
resolve_geom(b, &blocks, &bbytes, &pitch);
|
||||
REQUIRE(blocks > 0);
|
||||
REQUIRE(pitch > 0);
|
||||
const int64_t slots = resolve_slots(b);
|
||||
CHECK_EQ(slots, c.max_seqs * (1 + c.max_spec) * pitch);
|
||||
CHECK(slots > c.kv_resident_blocks / 4); /* the raised depth, not the flag's */
|
||||
CHECK(slots < blocks);
|
||||
|
||||
/* Nothing streaming: the ring covers the context, so the declaration is the context. */
|
||||
c.kv_resident_blocks = 0;
|
||||
RadBuilder b2;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b2, &meta, &c), RAD_OK);
|
||||
CHECK_EQ(resolve_slots(b2), blocks);
|
||||
}
|
||||
|
||||
/* A streamed group's prefill stage: every layer's mirror row is copied into the one-layer stage
|
||||
* view after that layer's store and before its attention, in layer order. The stage is the whole
|
||||
* context at identity addressing, so the attention reads block ids straight out of it and issues
|
||||
* no resolve. Both the sparse path (above qsa_exact_to) and the dense path (below it, which has no
|
||||
* resolve at all) must stage; a skipped stage would read the previous layer's bytes.
|
||||
*
|
||||
* A staged store is two writers: `kv_store_streamed` (the mirror, and the ring only where the
|
||||
* residency map says the block is resident) and then `kv_store` at the same slots (the identity
|
||||
* copy the stage view reads). Without the second, a prefill before the group's first decode
|
||||
* advances the mirror and not the pool, and the attention reads the previous layer's bytes
|
||||
* (record §60). This test counts both and pins their order. */
|
||||
TEST(a_staged_prefill_copies_each_layer_before_its_attention) {
|
||||
for (int dense = 0; dense < 2; ++dense) {
|
||||
RadModelMeta meta = qwen4exp_meta();
|
||||
RadBuildCtx c = build_ctx(0, 1);
|
||||
c.max_tok = 64; c.max_seqs = 4; c.max_spec = 3; c.max_ctx = 200000;
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
|
||||
int64_t blocks = 0, bbytes = 0, pitch = 0;
|
||||
resolve_geom(b, &blocks, &bbytes, &pitch);
|
||||
REQUIRE(blocks > 0);
|
||||
REQUIRE(bbytes > 0);
|
||||
|
||||
/* 100 tokens of history is below qsa_exact_to (dense); 5000 is above it (sparse). */
|
||||
auto k = kit(b, {}, { 32 }, dense ? 100 : 5000);
|
||||
StreamRig rig;
|
||||
attach_stream(b, *k, rig, blocks, bbytes);
|
||||
|
||||
RadCtx ctx;
|
||||
ctx.b = &b;
|
||||
ctx.batch = &k->b;
|
||||
ctx.stage_on = true;
|
||||
qwen4exp_fp8::step(&ctx, &k->b);
|
||||
|
||||
int stores = 0, identity = 0, attn = 0;
|
||||
size_t last_store = 0;
|
||||
int last_slot_opd = -1;
|
||||
for (size_t i = 0; i < ctx.issues.size(); ++i) {
|
||||
const RecOp* o = b.op_of(ctx.issues[i].op);
|
||||
if (!o) continue;
|
||||
if (o->op == "kv_store_streamed" ||
|
||||
o->op == "qk_norm_rope_gate_kv_store_streamed") {
|
||||
++stores;
|
||||
last_store = i;
|
||||
/* The streamed store's slot operand: 2 for the separate store, 10 for the folded
|
||||
* prologue (qg, k, cos/sin, pos, two norm weights, q, k, the v gap, v, slot,
|
||||
* cache, res, host). */
|
||||
last_slot_opd = o->op == "kv_store_streamed" ? 2 : 10;
|
||||
} else if (o->op == "kv_store") {
|
||||
/* A staged pass writes the pool at block-id identity, and kv_store_streamed writes
|
||||
* its device copy only where the residency map says the block is resident. Before
|
||||
* a decode the map is empty, so the stage would hold the previous layer's bytes
|
||||
* unless the identity store follows the mirror writer with the same slots
|
||||
* (record §60: PPL 5.85 against 3.99). */
|
||||
++identity;
|
||||
CHECK(last_slot_opd >= 0); /* a streamed store came first */
|
||||
CHECK(i > last_store);
|
||||
CHECK_EQ(ctx.issues[i].opd[2].kind,
|
||||
ctx.issues[last_store].opd[last_slot_opd].kind);
|
||||
CHECK_EQ(ctx.issues[i].opd[2].handle,
|
||||
ctx.issues[last_store].opd[last_slot_opd].handle);
|
||||
CHECK_EQ(ctx.issues[i].opd[2].offset,
|
||||
ctx.issues[last_store].opd[last_slot_opd].offset);
|
||||
} else if (o->op == "attn_paged" || o->op == "attn_paged_gate_quant") {
|
||||
++attn;
|
||||
}
|
||||
}
|
||||
CHECK(stores > 0);
|
||||
CHECK_EQ(identity, stores); /* one identity store a staged store */
|
||||
CHECK_EQ(attn, stores);
|
||||
CHECK_EQ(ctx.stages.size(), (size_t)stores); /* one stage a layer, no repeats */
|
||||
|
||||
/* The stage copy's wait is immediately before the layer's store, not after it: the copy
|
||||
* reads the mirror, which the store has not written yet (core/kvstream/stage.h). */
|
||||
int prev_layer = -1;
|
||||
for (size_t s = 0; s < ctx.stages.size(); ++s) {
|
||||
const StageRec& r = ctx.stages[s];
|
||||
CHECK(r.layer > prev_layer); /* layers in order */
|
||||
prev_layer = r.layer;
|
||||
REQUIRE(r.issue < ctx.issues.size());
|
||||
const RecOp* next = b.op_of(ctx.issues[r.issue].op);
|
||||
REQUIRE(next != nullptr);
|
||||
/* The wait sits immediately before the layer's store, which is either the separate
|
||||
* streamed store or the prologue that has it folded in (a step narrow enough for the
|
||||
* fused prologue). */
|
||||
CHECK(next->op == "kv_store_streamed" ||
|
||||
next->op == "qk_norm_rope_gate_kv_store_streamed");
|
||||
if (s + 1 < ctx.stages.size())
|
||||
CHECK(ctx.stages[s + 1].issue > r.issue);
|
||||
}
|
||||
/* And the next layer's copy is handed over after every attention launch of this one, so the
|
||||
* ride it waits on is behind all of them. */
|
||||
CHECK_EQ(ctx.stages_next.size(), (size_t)stores);
|
||||
for (size_t s = 0; s < ctx.stages_next.size(); ++s) {
|
||||
const StageRec& r = ctx.stages_next[s];
|
||||
CHECK_EQ(r.layer, ctx.stages[s].layer);
|
||||
int seen = 0;
|
||||
for (size_t i = 0; i < r.issue && i < ctx.issues.size(); ++i) {
|
||||
const RecOp* o = b.op_of(ctx.issues[i].op);
|
||||
if (o && (o->op == "attn_paged" || o->op == "attn_paged_gate_quant")) ++seen;
|
||||
}
|
||||
/* This layer's own attention (at least) is behind the hand-over; a layer wider than a
|
||||
* launch has more than one. */
|
||||
CHECK(seen >= (int)s + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Below `qsa_exact_to` a prefill takes the dense attention, which reads the block table and never
|
||||
* `sel`. The selection is still produced, but a resolve there would be read by nothing: work on the
|
||||
* step path whose only other effect is on the residency map's epoch, in a configuration the design
|
||||
* says never sees the op. So the dense step must issue none. */
|
||||
TEST(qsa_below_the_exact_bound_takes_the_dense_path_without_a_resolve) {
|
||||
RadModelMeta meta = qwen4exp_meta();
|
||||
RadBuildCtx c = build_ctx(0, 1);
|
||||
c.max_tok = 64; c.max_seqs = 4; c.max_spec = 3; c.max_ctx = 200000;
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
|
||||
int64_t blocks = 0, bbytes = 0, pitch = 0;
|
||||
resolve_geom(b, &blocks, &bbytes, &pitch);
|
||||
REQUIRE(blocks > 0);
|
||||
REQUIRE(bbytes > 0);
|
||||
REQUIRE(pitch > 0);
|
||||
|
||||
/* reach = max_ctx_len + max_q_len = (100 + 32) + 32: far below the 2051 bound, so dense. */
|
||||
auto k = kit(b, {}, { 32 }, 100);
|
||||
StreamRig rig;
|
||||
attach_stream(b, *k, rig, blocks, bbytes);
|
||||
|
||||
RadCtx ctx;
|
||||
ctx.b = &b;
|
||||
ctx.batch = &k->b;
|
||||
qwen4exp_fp8::step(&ctx, &k->b);
|
||||
|
||||
int selects = 0, resolves = 0, dense = 0;
|
||||
rad_buf sel = 0;
|
||||
for (const RecIssue& i : ctx.issues) {
|
||||
const RecOp* o = b.op_of(i.op);
|
||||
if (!o) continue;
|
||||
if (o->op == "qsa_select") { ++selects; sel = i.opd[5].handle; }
|
||||
else if (o->op == "kv_resolve") ++resolves;
|
||||
else if (o->op == "attn_paged" || o->op == "attn_paged_gate_quant") {
|
||||
if (i.opd[2].handle != sel) ++dense; /* the block table, not the selection */
|
||||
}
|
||||
}
|
||||
CHECK(selects > 0); /* the score/select half still runs */
|
||||
CHECK_EQ(resolves, 0);
|
||||
CHECK(dense > 0); /* and the launch that ran is the dense one, not a sparse one */
|
||||
}
|
||||
|
||||
/* Above `qsa_exact_to` the prefill is sparse: the attention takes the step in one launch, and the
|
||||
* resolve right before it rewrites exactly its rows, in the same `sel` buffer the selection
|
||||
* produced. */
|
||||
TEST(qsa_above_the_exact_bound_resolves_once_per_sparse_launch) {
|
||||
RadModelMeta meta = qwen4exp_meta();
|
||||
RadBuildCtx c = build_ctx(0, 1);
|
||||
c.max_tok = 64; c.max_seqs = 4; c.max_spec = 3; c.max_ctx = 200000;
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
|
||||
int64_t blocks = 0, bbytes = 0, pitch = 0;
|
||||
resolve_geom(b, &blocks, &bbytes, &pitch);
|
||||
REQUIRE(blocks > 0);
|
||||
REQUIRE(bbytes > 0);
|
||||
REQUIRE(pitch > 0);
|
||||
|
||||
/* reach = (3000 + 32) + 32: above the bound, so every layer's launch is sparse. */
|
||||
auto k = kit(b, {}, { 32 }, 3000);
|
||||
StreamRig rig;
|
||||
attach_stream(b, *k, rig, blocks, bbytes);
|
||||
|
||||
RadCtx ctx;
|
||||
ctx.b = &b;
|
||||
ctx.batch = &k->b;
|
||||
qwen4exp_fp8::step(&ctx, &k->b);
|
||||
|
||||
int selects = 0, resolves = 0, attn = 0;
|
||||
size_t last_select = 0;
|
||||
for (size_t i = 0; i < ctx.issues.size(); ++i) {
|
||||
const RecOp* o = b.op_of(ctx.issues[i].op);
|
||||
if (!o) continue;
|
||||
if (o->op == "qsa_select") { ++selects; last_select = i; }
|
||||
else if (o->op == "kv_resolve") {
|
||||
++resolves;
|
||||
const RadOperand& rsel = ctx.issues[i].opd[0];
|
||||
const RadOperand& rnsel = ctx.issues[i].opd[1];
|
||||
const RadOperand& ssel = ctx.issues[last_select].opd[5];
|
||||
const RadOperand& snsel = ctx.issues[last_select].opd[6];
|
||||
CHECK(i > last_select); /* after the selection it rewrites */
|
||||
CHECK_EQ(rsel.handle, ssel.handle);
|
||||
CHECK_EQ(rsel.offset, ssel.offset);
|
||||
CHECK_EQ(rsel.rows, ssel.rows); /* the launch's rows, not the step's */
|
||||
CHECK_EQ(rnsel.offset, snsel.offset);
|
||||
CHECK_EQ(rnsel.rows, snsel.rows);
|
||||
CHECK_EQ(rsel.rows, (int64_t)32); /* one select launch of the whole step */
|
||||
} else if (o->op == "attn_paged" || o->op == "attn_paged_gate_quant") {
|
||||
++attn;
|
||||
CHECK_EQ(ctx.issues[i].opd[2].handle,
|
||||
ctx.issues[last_select].opd[5].handle);
|
||||
CHECK_EQ(ctx.issues[i].opd[2].offset,
|
||||
ctx.issues[last_select].opd[5].offset);
|
||||
CHECK_EQ(ctx.issues[i].opd[2].rows, ctx.issues[last_select].opd[5].rows);
|
||||
}
|
||||
}
|
||||
CHECK_EQ(selects, 2); /* one indexer per full-attention layer of the eight */
|
||||
CHECK_EQ(resolves, selects);
|
||||
CHECK_EQ(attn, selects);
|
||||
}
|
||||
|
||||
/* A step wider than the score plane selects in windows, and the gated sparse attention takes the
|
||||
* whole step in one launch. The resolve is the attention's, not the indexer's: exactly one per
|
||||
* launch, over exactly that launch's rows, after the layer's store. So a launch may span several
|
||||
* select windows, and the resolve that precedes it rewrites all of their rows. */
|
||||
TEST(qsa_a_widened_step_resolves_the_launch_that_reads_each_window) {
|
||||
RadModelMeta meta = qwen4exp_meta();
|
||||
RadBuildCtx c = build_ctx(0, 1);
|
||||
c.max_tok = 4096; c.max_mixed_tok = 2048;
|
||||
c.max_seqs = 4; c.max_spec = 3; c.max_ctx = 200000;
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
|
||||
const qwen4exp_fp8::Model& m = qwen4exp_fp8::g_model[0];
|
||||
const int64_t PR = m.qsacfg.plane_rows;
|
||||
REQUIRE(PR > 0);
|
||||
int64_t blocks = 0, bbytes = 0, pitch = 0;
|
||||
resolve_geom(b, &blocks, &bbytes, &pitch);
|
||||
REQUIRE(blocks > 0);
|
||||
REQUIRE(bbytes > 0);
|
||||
REQUIRE(pitch > 0);
|
||||
|
||||
auto k = kit(b, {}, { 4096 }, 4096); /* T = 2 windows, one sequence: reach is above */
|
||||
StreamRig rig;
|
||||
attach_stream(b, *k, rig, blocks, bbytes);
|
||||
|
||||
RadCtx ctx;
|
||||
ctx.b = &b;
|
||||
ctx.batch = &k->b;
|
||||
qwen4exp_fp8::step(&ctx, &k->b);
|
||||
|
||||
struct Span { int64_t off, rows; };
|
||||
std::vector<Span> resolved;
|
||||
int selects = 0, stores = 0, resolves = 0, attn = 0;
|
||||
size_t last_select = 0, last_store = 0;
|
||||
for (size_t i = 0; i < ctx.issues.size(); ++i) {
|
||||
const RecOp* o = b.op_of(ctx.issues[i].op);
|
||||
if (!o) continue;
|
||||
if (o->op == "qsa_select") {
|
||||
++selects;
|
||||
last_select = i;
|
||||
} else if (o->op == "kv_store_streamed" ||
|
||||
o->op == "qk_norm_rope_gate_kv_store_streamed") {
|
||||
++stores;
|
||||
last_store = i;
|
||||
} else if (o->op == "kv_resolve") {
|
||||
++resolves;
|
||||
const RadOperand& rsel = ctx.issues[i].opd[0];
|
||||
CHECK(i > last_store); /* the mirror write precedes the copy */
|
||||
CHECK(i > last_select);
|
||||
CHECK_EQ(rsel.handle, ctx.issues[last_select].opd[5].handle);
|
||||
/* `sel` offsets are element offsets into a `pitch`-wide row buffer. */
|
||||
CHECK_EQ(rsel.offset % pitch, 0);
|
||||
resolved.push_back({ rsel.offset / pitch, rsel.rows });
|
||||
} else if (o->op == "attn_paged" || o->op == "attn_paged_gate_quant") {
|
||||
++attn;
|
||||
const RadOperand& asel = ctx.issues[i].opd[2];
|
||||
/* The resolve that reads this launch's rows is the issue right before it. */
|
||||
REQUIRE(i > 0);
|
||||
const RecOp* p = b.op_of(ctx.issues[i - 1].op);
|
||||
REQUIRE(p != nullptr);
|
||||
CHECK_EQ(std::string(p->op), std::string("kv_resolve"));
|
||||
CHECK_EQ(ctx.issues[i - 1].opd[0].handle, asel.handle);
|
||||
CHECK_EQ(ctx.issues[i - 1].opd[0].offset, asel.offset);
|
||||
CHECK_EQ(ctx.issues[i - 1].opd[0].rows, asel.rows);
|
||||
CHECK_EQ(asel.offset % pitch, 0);
|
||||
}
|
||||
}
|
||||
CHECK_EQ(selects, 4); /* two full layers, two windows each */
|
||||
CHECK_EQ(stores, 2);
|
||||
CHECK_EQ(resolves, attn); /* one resolve per attention launch, never two */
|
||||
CHECK(attn > 0);
|
||||
/* Every row of the step is resolved once a layer, by the one launch that reads it. */
|
||||
int64_t covered_to = 0;
|
||||
for (const Span& s : resolved) {
|
||||
CHECK_EQ(s.off, covered_to % 4096);
|
||||
covered_to += s.rows;
|
||||
}
|
||||
CHECK_EQ(covered_to, (int64_t)4096 * stores); /* one launch, the whole step, a layer */
|
||||
}
|
||||
|
||||
/* With no stream on the attention group's batch there is nothing to resolve: the selection is a
|
||||
* block table the pool already addresses. Issuing the op anyway would make a non-streaming
|
||||
* configuration pay for it. */
|
||||
TEST(qsa_without_a_stream_the_selection_is_never_resolved) {
|
||||
RadModelMeta meta = qwen4exp_meta();
|
||||
RadBuildCtx c = build_ctx(0, 1);
|
||||
c.max_tok = 64; c.max_seqs = 4; c.max_spec = 3; c.max_ctx = 200000;
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
|
||||
|
||||
auto k = kit(b, {}, { 32 }, 3000); /* the sparse path, but no stream to make resident */
|
||||
RadCtx ctx;
|
||||
ctx.b = &b;
|
||||
ctx.batch = &k->b;
|
||||
qwen4exp_fp8::step(&ctx, &k->b);
|
||||
|
||||
int selects = 0, resolves = 0, attn = 0;
|
||||
rad_buf sel = 0;
|
||||
for (const RecIssue& i : ctx.issues) {
|
||||
const RecOp* o = b.op_of(i.op);
|
||||
if (!o) continue;
|
||||
if (o->op == "qsa_select") { ++selects; sel = i.opd[5].handle; }
|
||||
else if (o->op == "kv_resolve") ++resolves;
|
||||
else if (o->op == "attn_paged" || o->op == "attn_paged_gate_quant") {
|
||||
++attn;
|
||||
CHECK_EQ(i.opd[2].handle, sel); /* the sparse path still read the selection */
|
||||
}
|
||||
}
|
||||
CHECK_EQ(selects, 2); /* the two full-attention layers of the eight */
|
||||
CHECK_EQ(resolves, 0);
|
||||
CHECK_EQ(attn, selects);
|
||||
}
|
||||
|
||||
/* The plugin declares by each weight's stored encoding. Flash-Next's experts, gated-residual
|
||||
* mixing matrices, MTP fc and 2-bit draft head each have a recipe-chosen format the plugin reads
|
||||
* from rad_weight_encoding, at convert and at load alike. With int4-rotated, E4M3-row and u2
|
||||
@@ -6877,7 +7392,12 @@ TEST(qwen35_fp8_fp8_v4_cache_is_400_bytes_a_token_and_head) {
|
||||
const int v4 = count(b, "fp8_v4"), e4 = count(b, "fp8_e4m3");
|
||||
CHECK(v4 > 0);
|
||||
CHECK(e4 > 0); /* the drafter's attention */
|
||||
CHECK_EQ(v4 + e4, n8);
|
||||
/* The packed build declares no streamed store or fold (libr4d has no row for the layout), so it
|
||||
* has exactly those two ops fewer than the plain build. */
|
||||
const int streamed = count_op(f8, "kv_store_streamed") +
|
||||
count_op(f8, "qk_norm_rope_gate_kv_store_streamed");
|
||||
CHECK(streamed > 0);
|
||||
CHECK_EQ(v4 + e4, n8 - streamed);
|
||||
CHECK(noted(b, "kv cache fp8_v4"));
|
||||
|
||||
RadArchProbe p{};
|
||||
@@ -6911,6 +7431,49 @@ TEST(qwen35_fp8_k6_v4_cache_is_352_bytes_a_token_and_head) {
|
||||
CHECK(noted(b, "kv cache k6_v4"));
|
||||
}
|
||||
|
||||
/* A packed KV cache declares no streamed store and no fold. libr4d's streamed rows are the E4M3 and
|
||||
* bf16 ones only (libr4d/r4d_rows.cpp), and both streamed ops are weightless, so a declaration with
|
||||
* no row for the layout is a device band that resolved to nothing -- a hole, which refuses startup
|
||||
* with or without --kv-resident and never names the flag. The group's core dtype is plain E4M3 at
|
||||
* head_dim 25/32 or 22/32 of the model's, so nothing downstream of declare can tell a packed cache
|
||||
* from fp8: the gate has to be here. Everything else the build declares is unchanged -- the
|
||||
* layout-blind resolve included, where the model has an indexer -- because a packed group is
|
||||
* unstreamable for want of a writer, not because its attention cannot address a ring. */
|
||||
TEST(a_packed_kv_cache_declares_no_streamed_store_and_no_fold) {
|
||||
using DeclareFn = int (*)(RadBuilder*, const RadModelMeta*, const RadBuildCtx*);
|
||||
RadModelMeta m4 = qwen4exp_meta();
|
||||
MetaKV m35 = q35fp8_meta();
|
||||
struct Plugin { const char* name; DeclareFn declare; const RadModelMeta* meta; };
|
||||
const Plugin plugins[] = { { "qwen4exp", qwen4exp_fp8::declare, &m4 },
|
||||
{ "qwen35", qwen35_fp8::declare, m35.meta() } };
|
||||
for (const Plugin& pl : plugins) {
|
||||
/* The same build at plain fp8: the two streamed ops are declared, so the zeros below are
|
||||
* the layout gate and not an architecture that never streams. */
|
||||
RadBuildCtx plain = build_ctx();
|
||||
plain.kv_dtype = RAD_F8E4M3;
|
||||
plain.max_ctx = 200000;
|
||||
RadBuilder p;
|
||||
REQUIRE_EQ(pl.declare(&p, pl.meta, &plain), RAD_OK);
|
||||
CHECK(count_op(p, "kv_store_streamed") > 0);
|
||||
REQUIRE(count_op(p, "qk_norm_rope_gate_kv_store_streamed") > 0);
|
||||
|
||||
for (int q : { (int)RAD_KVQ_V4, (int)RAD_KVQ_K6V4 }) {
|
||||
RadBuildCtx ctx = plain;
|
||||
ctx.kv_quant = q;
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(pl.declare(&b, pl.meta, &ctx), RAD_OK);
|
||||
const RadKVGroupDecl& f = b.kv_groups[0].d;
|
||||
CHECK_EQ(f.kind, RAD_KV_FULL);
|
||||
CHECK_EQ(f.dtype, (uint32_t)RAD_F8E4M3); /* the core sees E4M3, not the layout */
|
||||
CHECK_EQ(f.head_dim, q == RAD_KVQ_V4 ? (int64_t)200 : (int64_t)176);
|
||||
CHECK(count_op(b, "kv_store") > 0); /* the base store still serves the group */
|
||||
CHECK_EQ(count_op(b, "kv_resolve"), count_op(p, "kv_resolve"));
|
||||
CHECK_EQ(count_op(b, "kv_store_streamed"), 0);
|
||||
CHECK_EQ(count_op(b, "qk_norm_rope_gate_kv_store_streamed"), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* --attn-prefill-fp8: the trunk's attn_paged ops state `prefill_fp8` with the context's bits and
|
||||
* the DFlash2 drafter's do not. At 0 no op names the parameter, so the exact declare is the one
|
||||
* without the field: the same ops with the same parameters. */
|
||||
@@ -8626,32 +9189,44 @@ struct ArchCases {
|
||||
qwen35_bf16::step, 0, {}, 0xf82117051023e608ull);
|
||||
add("qwen35_fp8 + MTP", [this, sc](RadBuilder* b) {
|
||||
auto c = sc(1, 3); return qwen35_fp8::declare(b, q35.meta(), &c); },
|
||||
qwen35_fp8::step, 3, {}, 0x74fd4495028c1525ull);
|
||||
qwen35_fp8::step, 3, {}, 0x7c3df775e4091829ull);
|
||||
add("qwen35_fp8 + MTP, two ranks", [this, sc](RadBuilder* b) {
|
||||
auto c = sc(2, 3); return qwen35_fp8::declare(b, q35.meta(), &c); },
|
||||
nullptr, 3, { "ar_rmsnorm_quant_fp8" }, 0x1c28909125c12f52ull);
|
||||
nullptr, 3, { "ar_rmsnorm_quant_fp8" }, 0x8960f76b0bf8e50aull);
|
||||
add("qwen35_fp8 + DFlash2", [this, sc](RadBuilder* b) {
|
||||
auto c = sc(1, 7); return qwen35_fp8::declare(b, q35df.meta(), &c); },
|
||||
qwen35_fp8::step, 7, {}, 0x5eaef1ee37575caeull);
|
||||
qwen35_fp8::step, 7, {}, 0xa980fd70dfca7f04ull);
|
||||
add("qwen35_w8a8 + DFlash2", [this, sc](RadBuilder* b) {
|
||||
auto c = sc(1, 7); return w8a8_declare(b, q35w8df.meta(), &c); },
|
||||
qwen35_w8a8::step, 7, {}, 0x5a4f256c6fda0e87ull);
|
||||
qwen35_w8a8::step, 7, {}, 0xb01f0bb2b9898c1dull);
|
||||
add("qwen35_w8a8 + DFlash2, two ranks", [this, sc](RadBuilder* b) {
|
||||
auto c = sc(2, 7); return w8a8_declare(b, q35w8df.meta(), &c); },
|
||||
nullptr, 7, { "ar_rmsnorm_quant_fp8" }, 0xb825b9d264795b8eull);
|
||||
nullptr, 7, { "ar_rmsnorm_quant_fp8" }, 0xf60656437356f664ull);
|
||||
add("qwen35moe_fp8 + MTP", [this, sc](RadBuilder* b) {
|
||||
auto c = sc(1, 3); return qwen35moe_fp8::declare(b, q35moe.meta(), &c); },
|
||||
qwen35moe_fp8::step, 3, {}, 0x1bd3b53543cd06eeull);
|
||||
qwen35moe_fp8::step, 3, {}, 0xb5aaea840d6bf6c2ull);
|
||||
add("qwen4exp_fp8", [this](RadBuilder* b) {
|
||||
auto c = q4_ctx(0, 1); return qwen4exp_fp8::declare(b, &q4, &c); },
|
||||
qwen4exp_fp8::step, 3, {}, 0xbe74d476c7fd04b8ull);
|
||||
qwen4exp_fp8::step, 3, {}, 0xb00af22d0c1db143ull);
|
||||
add("qwen4exp_fp8, two ranks", [this](RadBuilder* b) {
|
||||
auto c = q4_ctx(0, 2); return qwen4exp_fp8::declare(b, &q4, &c); },
|
||||
nullptr, 3, { "ar_hc_write" }, 0x9d7df7914590d6d8ull);
|
||||
nullptr, 3, { "ar_hc_write" }, 0x2d685c5f5a068473ull);
|
||||
add("qwen4exp_fp8, four-bit experts", [this](RadBuilder* b) {
|
||||
b->encs = { { "ffn_gate_up_exps", w4 }, { "ffn_down_exps", w4 } };
|
||||
auto c = q4_ctx(0, 1); return qwen4exp_fp8::declare(b, &q4, &c); },
|
||||
qwen4exp_fp8::step, 3, {}, 0xb308d06b168a61f1ull);
|
||||
qwen4exp_fp8::step, 3, {}, 0xa00760f0eb3da078ull);
|
||||
/* The cache dtype stated rather than left to the plugin's default. The streamed store and
|
||||
* the prologue that folds it are declared only where the vocabulary has them, and both are
|
||||
* written at the KV group's dtype -- bf16 being the copy-only one, and it is what
|
||||
* every case above already gets from the default, the "fp8" in this architecture's name
|
||||
* being its trunk's quantisation rather than its cache. Naming it here makes that coverage
|
||||
* visible; the fingerprint below is therefore `qwen4exp_fp8`'s exact value, since the two
|
||||
* declare one graph, and the test after this table says by name what that graph holds. */
|
||||
add("qwen4exp_fp8, bf16 KV", [this](RadBuilder* b) {
|
||||
auto c = q4_ctx(0, 1);
|
||||
c.kv_dtype = RAD_BF16;
|
||||
return qwen4exp_fp8::declare(b, &q4, &c); },
|
||||
qwen4exp_fp8::step, 3, {}, 0xb00af22d0c1db143ull);
|
||||
add("llama_fp8 + DSpark", [this](RadBuilder* b) {
|
||||
auto c = dspark_ctx(0, 1); return llama_fp8::declare(b, lla.meta(), &c); },
|
||||
llama_fp8::step, 7, {}, 0xdcf36a1d31aba73cull);
|
||||
@@ -8788,6 +9363,40 @@ TEST(with_every_op_declared_each_architecture_declares_its_unchanged_graph) {
|
||||
}
|
||||
}
|
||||
|
||||
/* What the bf16 KV pin above is worth: a fingerprint that moves says nothing about which ops it
|
||||
* covered. The full-attention group must be bf16, and every layer that declares its cache store
|
||||
* must have both forms at that dtype -- the streamed store and the prologue that folds it in, which
|
||||
* is the one bf16 issues. With the store's row gone from the vocabulary the bf16
|
||||
* declare loses both and the hash above moves; this names them, so a bf16 streamed path that
|
||||
* silently disappears fails as itself rather than as a number. */
|
||||
TEST(the_bf16_kv_qwen4exp_graph_declares_the_streamed_store_and_its_fold) {
|
||||
const ArchCase* ac = nullptr;
|
||||
for (const ArchCase& c : arch_cases().list)
|
||||
if (c.name == "qwen4exp_fp8, bf16 KV") ac = &c;
|
||||
REQUIRE(ac != nullptr);
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(ac->declare(&b), RAD_OK);
|
||||
int group = 0, store = 0, folded = 0, hosted = 0;
|
||||
for (const auto& g : b.kv_groups)
|
||||
if (g.name.find("kv_attn") != std::string::npos) {
|
||||
CHECK_EQ((int)g.d.dtype, (int)RAD_BF16);
|
||||
++group;
|
||||
}
|
||||
for (const auto& o : b.ops) {
|
||||
const bool stream = o.op == "kv_store_streamed";
|
||||
const bool fold = o.op == "qk_norm_rope_gate_kv_store_streamed";
|
||||
if (o.op == "kv_store") ++hosted;
|
||||
if (!stream && !fold) continue;
|
||||
for (const auto& p : o.p)
|
||||
if (p.key == "kv_dtype") CHECK_EQ(p.sval, std::string("bf16"));
|
||||
++(stream ? store : folded);
|
||||
}
|
||||
CHECK_EQ(group, 1);
|
||||
CHECK_EQ(hosted, 3); /* two full-attention layers and the MTP head's */
|
||||
CHECK_EQ(store, hosted); /* each one's store has a streamed form ... */
|
||||
CHECK_EQ(folded, store); /* ... and a folded form beside it */
|
||||
}
|
||||
|
||||
/* Where gate_quant_fp8 is not declared, or declared and not resolved, the fp8 gated attention runs
|
||||
* the chain it fuses: the per-head sigmoid into a gate buffer of the layer's own, the multiply
|
||||
* into the attention output, and quant_act_fp8 into the codes the output projection reads, the
|
||||
@@ -9333,15 +9942,15 @@ TEST(a_4096_budget_without_a_span_is_the_pinned_graph_and_steps) {
|
||||
uint64_t graph, steps;
|
||||
};
|
||||
const Case cases[] = {
|
||||
{ "qwen4exp_fp8", 1, 0, 0x14b9cab0bea2591full, 0x26a9e2c23cf85696ull },
|
||||
{ "qwen4exp_fp8", 2, 0, 0xc9bcbf1b538b597full, 0x889c6b36c014609full },
|
||||
{ "qwen4exp_fp8", 2, 1, 0x8b2f1786a4ac12b8ull, 0x959ef6b7c6f57d8bull },
|
||||
{ "qwen4exp_fp8", 4, 0, 0x1e97a26b759d29dfull, 0xfbe74f2b9d7020bfull },
|
||||
{ "qwen4exp_fp8", 4, 3, 0x6669aa0573931943ull, 0x36df57cac613c6cfull },
|
||||
{ "qwen35_fp8", 1, 0, 0xcfc82ca915ec549dull, 0x4d6fca823d1e97efull },
|
||||
{ "qwen35_fp8", 2, 1, 0x5085b8c443c59f7cull, 0xb297654cef04bf08ull },
|
||||
{ "qwen35moe_fp8", 1, 0, 0x7fa7b015e1bc8873ull, 0x3fcf71676e558d79ull },
|
||||
{ "qwen35moe_fp8", 2, 0, 0xf60c9508dd197370ull, 0x05e0778848bf416cull },
|
||||
{ "qwen4exp_fp8", 1, 0, 0x43db8c3ddf237950ull, 0x26a9e2c23cf85696ull },
|
||||
{ "qwen4exp_fp8", 2, 0, 0x46b814bcc44bdaaaull, 0x889c6b36c014609full },
|
||||
{ "qwen4exp_fp8", 2, 1, 0xfaef43f1dff09459ull, 0x959ef6b7c6f57d8bull },
|
||||
{ "qwen4exp_fp8", 4, 0, 0xa6fe7e5732760f4cull, 0xfbe74f2b9d7020bfull },
|
||||
{ "qwen4exp_fp8", 4, 3, 0x9281597b6738d9a4ull, 0x36df57cac613c6cfull },
|
||||
{ "qwen35_fp8", 1, 0, 0xf5ea9e1795b08134ull, 0x4d6fca823d1e97efull },
|
||||
{ "qwen35_fp8", 2, 1, 0x803113b1ae0276e3ull, 0xb297654cef04bf08ull },
|
||||
{ "qwen35moe_fp8", 1, 0, 0x3979c928377bd5fcull, 0x3fcf71676e558d79ull },
|
||||
{ "qwen35moe_fp8", 2, 0, 0x5ef9a8b68cb03cf1ull, 0x05e0778848bf416cull },
|
||||
};
|
||||
for (const Case& cs : cases) {
|
||||
RadBuilder b;
|
||||
|
||||
+195
-3
@@ -1611,9 +1611,14 @@ TEST(glm53_is_the_pinned_graph_and_steps) {
|
||||
uint64_t graph, steps;
|
||||
bool heads_only = false; /* the default: the graph before the row form */
|
||||
};
|
||||
const Case cases[] = { { 1, 0x906897570034f5f5ull, 0x68ffc03d2b640d51ull },
|
||||
{ 8, 0x1d378152c40f8834ull, 0x82ed1094dbf4b637ull },
|
||||
{ 8, 0x9f39f6f7a04dc4d3ull, 0x3818eedb4ac10d1bull, true } };
|
||||
/* Both sides' rows, each from this test's own `pin:` printout after the 1.12.0 merge. Upstream
|
||||
* 1.12.0 adds the row form (heads_only false, the default), whose pins it published; the head
|
||||
* form's hashes move because our streamed ops (kv_store_streamed, kv_resolve) are declared in
|
||||
* it. The row form's graph also carries those ops -- the MLP, indexer and store rows are shared
|
||||
* -- so its hash is the merge's, not upstream's. */
|
||||
const Case cases[] = { { 1, 0x8e12725b3ed3548dull, 0x68ffc03d2b640d51ull },
|
||||
{ 8, 0xe4ba21765f690f70ull, 0x82ed1094dbf4b637ull },
|
||||
{ 8, 0xf404ab12440b389full, 0x3818eedb4ac10d1bull, true } };
|
||||
for (const Case& cs : cases) {
|
||||
RadBuilder b;
|
||||
b.kv_block = 4;
|
||||
@@ -2020,6 +2025,193 @@ TEST(glm53_sizing_declares_with_the_vision_tower_match_the_real_graph) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(glm53_declaring_without_streamed_ops_ignores_the_flag) {
|
||||
const char* keys[] = { "num_nextn_predict_layers", "hc_mult", "n_routed_experts",
|
||||
"index_topk" };
|
||||
const char* vals[] = { "1", "4", "288", "2048" };
|
||||
RadModelMeta meta{};
|
||||
meta.n_layers = 45;
|
||||
meta.n_embd = 4096;
|
||||
meta.n_vocab = 154880;
|
||||
meta.rms_eps = 1e-5f;
|
||||
meta.n_kv = 4;
|
||||
meta.kv_key = keys;
|
||||
meta.kv_val = vals;
|
||||
const auto mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
|
||||
const auto fp8 = rad_enc_affine(RAD_F8E4M3, RAD_F32, 128, 128);
|
||||
RadBuilder b;
|
||||
b.kv_block = 4;
|
||||
b.encs = { { "mtp.blk.45.ffn_gate_up_exps", fp8 }, { "mtp.blk.45.ffn_down_exps", fp8 },
|
||||
{ "ffn_gate_up_exps", mx }, { "ffn_down_exps", mx },
|
||||
{ "ffn_gate_up_shexp", mx }, { "ffn_down_shexp", mx },
|
||||
{ "ffn_gate_up.weight", fp8 }, { "ffn_down.weight", fp8 } };
|
||||
b.undeclared = { "kv_resolve", "kv_store_streamed" };
|
||||
RadBuildCtx build{};
|
||||
build.rank = 0;
|
||||
build.world_size = 1;
|
||||
build.max_tok = 512;
|
||||
build.max_seqs = 2;
|
||||
build.max_ctx = 4096;
|
||||
build.max_spec = 0;
|
||||
build.kv_resident_blocks = 32768; /* the ring floor: 8 192 blocks. This fixture toggles the ops,
|
||||
* not the ring; the undeclared list is the gate it tests. */
|
||||
REQUIRE_EQ(glm5next::declare(&b, &meta, &build), RAD_OK);
|
||||
CHECK_EQ(count_op(b, "kv_resolve"), 0);
|
||||
CHECK_EQ(count_op(b, "kv_store_streamed"), 0);
|
||||
/* The base store is still there: the zeros are the gate, not a layer that never stores. */
|
||||
CHECK(count_op(b, "kv_store") > 0);
|
||||
}
|
||||
|
||||
TEST(glm53_streamed_decode_resolves_after_select_and_before_attention_with_the_entry_count) {
|
||||
const char* keys[] = { "num_nextn_predict_layers", "hc_mult", "n_routed_experts",
|
||||
"index_topk" };
|
||||
const char* vals[] = { "1", "4", "288", "2048" };
|
||||
RadModelMeta meta{};
|
||||
meta.n_layers = 45;
|
||||
meta.n_embd = 4096;
|
||||
meta.n_vocab = 154880;
|
||||
meta.rms_eps = 1e-5f;
|
||||
meta.n_kv = 4;
|
||||
meta.kv_key = keys;
|
||||
meta.kv_val = vals;
|
||||
const auto mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
|
||||
const auto fp8 = rad_enc_affine(RAD_F8E4M3, RAD_F32, 128, 128);
|
||||
RadBuilder b;
|
||||
b.kv_block = 4;
|
||||
b.encs = { { "mtp.blk.45.ffn_gate_up_exps", fp8 }, { "mtp.blk.45.ffn_down_exps", fp8 },
|
||||
{ "ffn_gate_up_exps", mx }, { "ffn_down_exps", mx },
|
||||
{ "ffn_gate_up_shexp", mx }, { "ffn_down_shexp", mx },
|
||||
{ "ffn_gate_up.weight", fp8 }, { "ffn_down.weight", fp8 } };
|
||||
RadBuildCtx build{};
|
||||
build.rank = 0;
|
||||
build.world_size = 1;
|
||||
build.max_tok = 512;
|
||||
build.max_seqs = 2;
|
||||
build.max_ctx = 4096;
|
||||
build.max_spec = 3; /* so the drafter's layer 45 is the twelfth full layer */
|
||||
build.kv_resident_blocks = 32768; /* the ring floor, 8 192 blocks: above this fixture's spec-3
|
||||
* raise of 2 x 4 x 513 = 4 104 blocks, so the engine raises
|
||||
* nothing. The declared slots are the context's 2048 blocks
|
||||
* either way, which is what the fake stream mirrors. */
|
||||
REQUIRE_EQ(glm5next::declare(&b, &meta, &build), RAD_OK);
|
||||
const auto& m = *glm5next::models[0];
|
||||
REQUIRE_EQ(m.latent, (rad_kvgroup)1);
|
||||
REQUIRE_EQ(count_op(b, "kv_resolve"), 12);
|
||||
|
||||
/* A zeroed stream with the figures bring-up fixes: 4096 B a block (12 layers of 2 x 4 x
|
||||
* 256 x bf16), a 2048-block mirror, and the ring the declaration states -- the context's
|
||||
* 2048 blocks, because a 32 768-token ring is clamped to `blocks` for this 4096-token
|
||||
* context. Only the latent group streams. */
|
||||
const int64_t blocks = 2048, slots = 2048;
|
||||
std::vector<uint8_t> host((size_t)blocks * 4096);
|
||||
std::vector<int32_t> res(blocks), slot_block(slots), slot_state(slots * 2), ctl(16);
|
||||
RadKVStream st{};
|
||||
st.host = host.data();
|
||||
st.res = res.data();
|
||||
st.slot_block = slot_block.data();
|
||||
st.slot_state = slot_state.data();
|
||||
st.ctl = ctl.data();
|
||||
st.block_bytes = 4096;
|
||||
st.host_layer_stride = 4096 * blocks;
|
||||
st.slots = slots;
|
||||
st.host_bytes = (int64_t)host.size() * 12;
|
||||
|
||||
int32_t cu[] = { 0, 1, 2 }, positions[] = { 0, 1 }, ctxlens[] = { 1, 1 },
|
||||
ids[] = { 7, 9 }, slots_map[] = { 0, 1 }, out[] = { 1 };
|
||||
int32_t sidx[] = { 0, 0, 0, 1, 1, 1 }, ci[] = { 0 }, tables[64]{};
|
||||
std::vector<RadKVGroupBatch> kv(b.kv_groups.size());
|
||||
for (size_t i = 0; i < kv.size(); ++i) {
|
||||
kv[i].group = (rad_kvgroup)i + 1;
|
||||
kv[i].slot_mapping = slots_map;
|
||||
kv[i].block_table = tables;
|
||||
kv[i].block_table_pitch = 32;
|
||||
kv[i].state_index = b.kv_groups[i].d.kind == RAD_KV_CONV ? ci : sidx;
|
||||
kv[i].state_index_pitch = b.kv_groups[i].d.kind == RAD_KV_CONV ? 1 : 6;
|
||||
}
|
||||
kv[m.latent - 1].stream = &st;
|
||||
|
||||
RadBatch batch{};
|
||||
batch.phase = RAD_PHASE_DECODE;
|
||||
batch.n_tok = 2;
|
||||
batch.n_seq = 2;
|
||||
batch.token_ids = ids;
|
||||
batch.positions = positions;
|
||||
batch.cu_seqlens = cu;
|
||||
batch.ctx_lens = ctxlens;
|
||||
batch.kv = kv.data();
|
||||
batch.n_kv_groups = (int)kv.size();
|
||||
batch.n_out = 1;
|
||||
batch.out_ids = out;
|
||||
batch.max_ctx_len = 1;
|
||||
batch.max_q_len = 1;
|
||||
RadCtx ctx;
|
||||
ctx.b = &b;
|
||||
ctx.rank = 0;
|
||||
ctx.world = 1;
|
||||
ctx.batch = &batch;
|
||||
|
||||
auto resolve_count = [&] {
|
||||
int n = 0;
|
||||
for (const auto& issue : ctx.issues)
|
||||
if (issue.op >= 1 && (size_t)issue.op <= b.ops.size() &&
|
||||
b.ops[issue.op - 1].op == "kv_resolve")
|
||||
++n;
|
||||
return n;
|
||||
};
|
||||
auto find_issue = [&](rad_op op) -> size_t {
|
||||
for (size_t i = 0; i < ctx.issues.size(); ++i)
|
||||
if (ctx.issues[i].op == op) return i;
|
||||
return SIZE_MAX;
|
||||
};
|
||||
auto check_layer_order = [&](const glm5next::Layer& l) {
|
||||
const size_t store = find_issue(l.mla.op_store_stream);
|
||||
const size_t sel = find_issue(l.index.cache.op_select);
|
||||
const size_t res_i = find_issue(l.mla.op_resolve);
|
||||
const size_t attn = find_issue(l.mla.core.op_attn);
|
||||
REQUIRE(store != SIZE_MAX);
|
||||
REQUIRE(sel != SIZE_MAX);
|
||||
REQUIRE(res_i != SIZE_MAX);
|
||||
REQUIRE(attn != SIZE_MAX);
|
||||
CHECK(store < sel);
|
||||
CHECK(sel < res_i);
|
||||
CHECK(res_i < attn);
|
||||
/* §2: the count is `selected_count`, never the token count. */
|
||||
CHECK_EQ(ctx.issues[res_i].opd[1].handle, m.aw.core.nsel);
|
||||
CHECK(ctx.issues[res_i].opd[1].handle != m.aw.core.length);
|
||||
CHECK_EQ(ctx.issues[res_i].n, (int64_t)2);
|
||||
};
|
||||
|
||||
/* A decode pass: 11 trunk full layers resolve, and the draft layer does too. */
|
||||
glm5next::step(&ctx, &batch);
|
||||
CHECK(ctx.step_fail.empty());
|
||||
CHECK_EQ(resolve_count(), 11);
|
||||
for (const auto& l : m.layers)
|
||||
if (l.full) check_layer_order(l);
|
||||
|
||||
ctx.issues.clear();
|
||||
batch.draft_pass = 2;
|
||||
glm5next::step(&ctx, &batch);
|
||||
CHECK(ctx.step_fail.empty());
|
||||
CHECK_EQ(resolve_count(), 1);
|
||||
check_layer_order(m.draft);
|
||||
|
||||
/* A staged pass (prefill, or the stage view chosen for it) must not resolve: the selection
|
||||
* keeps the block ids the identity stage addresses directly. */
|
||||
ctx.issues.clear();
|
||||
ctx.stages.clear();
|
||||
ctx.stage_on = true;
|
||||
batch.draft_pass = 0;
|
||||
batch.phase = RAD_PHASE_PREFILL;
|
||||
batch.n_tok = 4;
|
||||
int32_t cu4[] = { 0, 2, 4 };
|
||||
batch.cu_seqlens = cu4;
|
||||
batch.max_q_len = 2;
|
||||
glm5next::step(&ctx, &batch);
|
||||
CHECK(ctx.step_fail.empty());
|
||||
CHECK_EQ(resolve_count(), 0);
|
||||
CHECK(!ctx.stages.empty());
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
|
||||
/* ==================================================================== the row rule */
|
||||
|
||||
Reference in new issue
Block a user