diff --git a/arch/common/rad_arch.h b/arch/common/rad_arch.h index 5073ea70..cd862bd8 100644 --- a/arch/common/rad_arch.h +++ b/arch/common/rad_arch.h @@ -1515,7 +1515,12 @@ inline const char* drafter_layer_prefix(const RadModelMeta* meta) { return "draft.transformer_layer_config."; return "draft."; } -inline int drafter_geom(Geom& dg, const Geom& target, const RadModelMeta* meta, const char* what) { +/* `kv_repl_ok`: the drafter block reads its k and v at Geom::kv_off (rad_block_dflash2.h does), so + * fewer KV heads than ranks are replicated as geom_from replicates the target's: world a multiple + * of the head count, one KV head a rank, `kv_off` its first row in the unsharded k/v projection. + * Otherwise such a world is refused, as before. */ +inline int drafter_geom(Geom& dg, const Geom& target, const RadModelMeta* meta, const char* what, + bool kv_repl_ok = false) { dg = target; const char* pre = drafter_layer_prefix(meta); char key[160]; @@ -1540,7 +1545,15 @@ inline int drafter_geom(Geom& dg, const Geom& target, const RadModelMeta* meta, } RAD_ARCH_TRY(yarn_from(dg, meta, pre, dg.head_dim)); RAD_ARCH_TRY(divide_or_fail(dg.n_head_all, dg.world, "draft num_attention_heads", &dg.n_head)); - RAD_ARCH_TRY(divide_or_fail(dg.n_head_kv_all, dg.world, "draft num_key_value_heads", &dg.n_head_kv)); + /* The target's kv_off is the target's; the drafter's is set here or zero. */ + dg.kv_off = 0; + if (kv_repl_ok && dg.world > dg.n_head_kv_all && dg.world % dg.n_head_kv_all == 0) { + dg.n_head_kv = 1; /* world / n_head_kv_all ranks share each KV head */ + dg.kv_off = (dg.rank / (dg.world / dg.n_head_kv_all)) * dg.head_dim; + } else { + RAD_ARCH_TRY(divide_or_fail(dg.n_head_kv_all, dg.world, "draft num_key_value_heads", + &dg.n_head_kv)); + } RAD_ARCH_TRY(divide_or_fail(dg.n_ff_all, dg.world, "draft intermediate_size", &dg.n_ff)); const long long dvocab = rad_meta_geti(meta, k("vocab_size"), dg.n_vocab_all); if (dvocab != dg.n_vocab_all) { diff --git a/arch/common/rad_block_dflash2.h b/arch/common/rad_block_dflash2.h index bb9f3b1f..ee63d53d 100644 --- a/arch/common/rad_block_dflash2.h +++ b/arch/common/rad_block_dflash2.h @@ -47,6 +47,13 @@ * hp = hidden_projection(x) the selector's query, rank 256 * tokens = dflash_select(cand, unary, hp, anchor, predecessor, successor) * + * ============================== LILICORR ============================== + * + * Config::lili is NVIDIA's LiLiCorr backbone (Nemotron-3.5-Bolt's drafter): the same layers with a + * learned sink logit a query head in the attention (attn_paged_sink, `attention_sink_bias`), and no + * selector or draft head here. The query pass ends with the gather into `sel_h`; the plugin then + * runs rad_block_lilicorr.h's correlator over those rows, which reads the target's own head. + * * ============================== WHAT IS SHARED WITH THE TARGET ============================== * * The embedding table and lm_head. The checkpoint (81 tensors) ships neither, and @@ -68,6 +75,8 @@ * * q/kv and gate_up shard by row, o and down by column with an all-reduce: ten collectives per * draft pass, each 5120 * block * 2 bytes (80 KiB at block 8), below the lossy wire's floor. + * Fewer KV heads than ranks are replicated (drafter_geom's `kv_repl_ok`): each rank reads its + * head's k/v rows at `kv_off` and keeps that head in its cache. * * fc [n_embd, n_taps*n_embd], 262 MiB in fp8 at Qwen3.8's width, read whole by the * context pass every step. Split by rows over the ranks and all-gathered, and @@ -152,6 +161,9 @@ struct Dflash2 { * acceptance, sum min(P, Q). */ double q_temp = 1.0; int64_t short_rows = 0; + /* LiLiCorr's backbone (the LILICORR section above): sinks, and the head left to the + * plugin. sel_rank and sel_topk are then unused. */ + int lili = 0; int64_t steps() const { return block - 1; } int64_t nodes() const { return tree && tree_nodes > 0 ? tree_nodes : steps(); } int64_t ng(int64_t n_embd) const { return group ? n_embd / group : 0; } @@ -294,6 +306,9 @@ struct Dflash2 { int64_t block_lo = 0; int64_t sel_lo_nodes = 0; /* a tree's nodes at the short block (op_sel_lo) */ rad_op op_qrope = 0, op_krope = 0, op_store = 0, op_attn = 0; + /* LiLiCorr: the attention with each layer's sinks, a weight, so one op a layer. */ + rad_weight w_sink[MAX_LAYERS] = {}; + rad_op op_attn_sink[MAX_LAYERS] = {}; rad_op op_act = 0, op_ar = 0, op_add = 0, op_final = 0; /* The residual add folded into the norm after it (add_rmsnorm), issued in place of op_add and * that norm where it resolves: op_afn[i] after layer i's attention (its feed-forward norm), @@ -353,7 +368,7 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co (long long)g.n_layer, MAX_LAYERS); return RAD_E_UNSUPPORTED; } - if (cfg.block < 2 || cfg.n_taps <= 0 || cfg.sel_rank <= 0 || cfg.sel_topk <= 0) + if (cfg.block < 2 || cfg.n_taps <= 0 || (!cfg.lili && (cfg.sel_rank <= 0 || cfg.sel_topk <= 0))) return RAD_E_INVAL; const int64_t NG = cfg.ng(g.n_embd); @@ -401,23 +416,10 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co } RAD_ARCH_TRY(map_copy(b, nm.f("dflash.hidden_norm.weight"), nm.ckpt("%s.hidden_norm.weight", ck))); RAD_ARCH_TRY(map_copy(b, nm.f("dflash.norm.weight"), nm.ckpt("%s.norm.weight", ck))); - RAD_ARCH_TRY(map_copy(b, nm.f("dflash.sel_proj.weight"), - nm.ckpt("%s.candidate_selector.hidden_projection.weight", ck))); - RAD_ARCH_TRY(map_copy(b, nm.f("dflash.sel_pred"), - nm.ckpt("%s.candidate_selector.predecessor_codebook", ck))); - RAD_ARCH_TRY(map_copy(b, nm.f("dflash.sel_succ"), - nm.ckpt("%s.candidate_selector.successor_codebook", ck))); - w_hnorm = decl_w(b, nm.f("dflash.hidden_norm.weight"), RAD_F32, {g.n_embd}, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model()); w_norm = decl_w(b, nm.f("dflash.norm.weight"), RAD_F32, {g.n_embd}, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model()); - w_selp = decl_w(b, nm.f("dflash.sel_proj.weight"), RAD_BF16, {cfg.sel_rank, g.n_embd}, - RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model()); - w_pred = decl_w(b, nm.f("dflash.sel_pred"), RAD_BF16, {g.n_vocab_all, cfg.sel_rank}, - RAD_ACCESS_VOCAB, RAD_SHARD_NONE, grp_model()); - w_succ = decl_w(b, nm.f("dflash.sel_succ"), RAD_BF16, {g.n_vocab_all, cfg.sel_rank}, - RAD_ACCESS_VOCAB, RAD_SHARD_NONE, grp_model()); /* The 2-bit draft head, declared as the MTP head's is (rad_block_mtp_fp8.h): * `dflash.draft_head.weight` is the target's lm_head under its own name, quantised by the @@ -427,35 +429,53 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co * When sharded, each rank's `row_topk` reports global ids, an all_gather moves R pairs a row, * and `row_topk_merge` reduces them to exactly the unsharded global top-R, ties included * (docs/OPS.md). This runs before the selector, so every rank holds the same candidates for - * the replicated codebooks. The all_gather is `world * R` pairs a row (2 KiB at R = 16). */ + * the replicated codebooks. The all_gather is `world * R` pairs a row (2 KiB at R = 16). + * + * LiLiCorr has neither the selector nor this head (its correlator reads the target's). */ enum { DF_W2_GROUP = 128 }; const bool dh_shard = head_sharded(g); const int dh_split = dh_shard ? RAD_SHARD_ROW : RAD_SHARD_NONE; - const char* dhn = nm.f("dflash.draft_head.weight"); - RAD_ARCH_TRY(map_copy(b, dhn, ck_lm)); - /* The 8-bit form: int8 codes, one f16 scale per (row, 128 of K), symmetric, which gemm_nt_q - * reads at w8a8 against the same quant_act_i8 activation (without its asum). */ - { - RadEncoding e{}; - const RadEncPlane* sc = nullptr; - head_i8 = weight_enc(b, dhn, &e) && rad_enc_is(&e, "affine") && e.n_planes == 2 && - !e.transform[0] && e.plane[0].dtype == RAD_I8 && e.plane[0].block[1] == 1 && - (sc = rad_enc_plane(&e, "scale")) && sc->dtype == RAD_F16 && - sc->block[0] == 1 && sc->block[1] == DF_W2_GROUP; + if (!cfg.lili) { + RAD_ARCH_TRY(map_copy(b, nm.f("dflash.sel_proj.weight"), + nm.ckpt("%s.candidate_selector.hidden_projection.weight", ck))); + RAD_ARCH_TRY(map_copy(b, nm.f("dflash.sel_pred"), + nm.ckpt("%s.candidate_selector.predecessor_codebook", ck))); + RAD_ARCH_TRY(map_copy(b, nm.f("dflash.sel_succ"), + nm.ckpt("%s.candidate_selector.successor_codebook", ck))); + w_selp = decl_w(b, nm.f("dflash.sel_proj.weight"), RAD_BF16, {cfg.sel_rank, g.n_embd}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model()); + w_pred = decl_w(b, nm.f("dflash.sel_pred"), RAD_BF16, {g.n_vocab_all, cfg.sel_rank}, + RAD_ACCESS_VOCAB, RAD_SHARD_NONE, grp_model()); + w_succ = decl_w(b, nm.f("dflash.sel_succ"), RAD_BF16, {g.n_vocab_all, cfg.sel_rank}, + RAD_ACCESS_VOCAB, RAD_SHARD_NONE, grp_model()); + + const char* dhn = nm.f("dflash.draft_head.weight"); + RAD_ARCH_TRY(map_copy(b, dhn, ck_lm)); + /* The 8-bit form: int8 codes, one f16 scale per (row, 128 of K), symmetric, which + * gemm_nt_q reads at w8a8 against the same quant_act_i8 activation (without its asum). */ + { + RadEncoding e{}; + const RadEncPlane* sc = nullptr; + head_i8 = weight_enc(b, dhn, &e) && rad_enc_is(&e, "affine") && e.n_planes == 2 && + !e.transform[0] && e.plane[0].dtype == RAD_I8 && e.plane[0].block[1] == 1 && + (sc = rad_enc_plane(&e, "scale")) && sc->dtype == RAD_F16 && + sc->block[0] == 1 && sc->block[1] == DF_W2_GROUP; + } + if (head_i8) { + w_dh = decl_view(b, dhn, nullptr, "codes", RAD_I8, {head_rows(g), g.n_embd}, + RAD_ACCESS_PER_TOKEN, dh_split, grp_model()); + w_dhs = decl_view(b, nm.f("dflash.draft_head.scale"), dhn, "scale", RAD_F16, + {head_rows(g), g.n_embd / DF_W2_GROUP}, RAD_ACCESS_PER_TOKEN, + dh_split, grp_model()); + } else { + w_dh = decl_view(b, dhn, nullptr, "codes", RAD_U2, {head_rows(g), g.n_embd}, + RAD_ACCESS_PER_TOKEN, dh_split, grp_model()); + w_dhs = decl_view(b, nm.f("dflash.draft_head.scale"), dhn, "scale,zero", RAD_F16, + {head_rows(g), g.n_embd}, RAD_ACCESS_PER_TOKEN, dh_split, + grp_model()); + } + if (!w_dh || !w_dhs) return RAD_E_INVAL; } - if (head_i8) { - w_dh = decl_view(b, dhn, nullptr, "codes", RAD_I8, {head_rows(g), g.n_embd}, - RAD_ACCESS_PER_TOKEN, dh_split, grp_model()); - w_dhs = decl_view(b, nm.f("dflash.draft_head.scale"), dhn, "scale", RAD_F16, - {head_rows(g), g.n_embd / DF_W2_GROUP}, RAD_ACCESS_PER_TOKEN, dh_split, - grp_model()); - } else { - w_dh = decl_view(b, dhn, nullptr, "codes", RAD_U2, {head_rows(g), g.n_embd}, - RAD_ACCESS_PER_TOKEN, dh_split, grp_model()); - w_dhs = decl_view(b, nm.f("dflash.draft_head.scale"), dhn, "scale,zero", RAD_F16, - {head_rows(g), g.n_embd}, RAD_ACCESS_PER_TOKEN, dh_split, grp_model()); - } - if (!w_dh || !w_dhs) return RAD_E_INVAL; /* ---------------------------------------------------------------- per-layer weights */ for (int64_t l = 0; l < g.n_layer; ++l) { @@ -492,6 +512,13 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co RAD_ARCH_TRY(w_v[i].declare(b, nm, nm.f("dflash.blk.%d.attn_v", i), kvd, g.n_embd, RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(first_layer + i), { nm.ckpt("%s.layers.%d.self_attn.v_proj.weight", ck, i) })); + /* Fewer KV heads than ranks (drafter_geom's `kv_repl_ok`): k and v are this rank's + * replicated head, rows [kv_off, kv_off + kv_dim) of the unsharded projection, where the + * even shard would hand a rank part of a head. q stays an even share of the heads. */ + if (g.world > g.n_head_kv_all) { + RAD_ARCH_TRY(w_k[i].span(b, g.kv_off, g.kv_off + kvd)); + RAD_ARCH_TRY(w_v[i].span(b, g.kv_off, g.kv_off + kvd)); + } w_qn[i] = decl_w(b, nm.f("dflash.blk.%d.attn_q_norm.weight", i), RAD_F32, {g.head_dim}, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(first_layer + i)); w_kn[i] = decl_w(b, nm.f("dflash.blk.%d.attn_k_norm.weight", i), RAD_F32, {g.head_dim}, @@ -520,6 +547,14 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co w_fcb[i] = decl_w(b, nm.f("dflash.blk.%d.ffn_conv_base", i), RAD_BF16, {2, cfg.taps, g.n_embd}, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(first_layer + i)); + /* LiLiCorr's sink logit a query head, split with the heads. */ + if (cfg.lili) { + RAD_ARCH_TRY(map_copy(b, nm.f("dflash.blk.%d.attn_sinks", i), + nm.ckpt("%s.layers.%d.self_attn.attention_sink_bias", ck, i))); + w_sink[i] = decl_w(b, nm.f("dflash.blk.%d.attn_sinks", i), RAD_F32, {g.n_head}, + RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(first_layer + i)); + if (!w_sink[i]) return RAD_E_INVAL; + } } @@ -529,15 +564,35 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co * * Declared before both kv_stores, which read the resolved `block_size` back with * rad_kv_block_size(); before this declaration it returns zero. */ - op_attn = rw(b, RAD_OP(b, "attn_paged", - RAD_PARAMS(RAD_RANGE("q_len", 1, cfg.block), RAD_INT("head_dim", g.head_dim), - RAD_INT("gqa", g.gqa()), RAD_INT("causal", cfg.causal), - RAD_INT("window", cfg.window), RAD_INT("n_head", g.n_head), - RAD_INT("block_size", RAD_KV_BLOCK), - RAD_INT("max_seqs", g.max_seqs), RAD_INT("max_ctx", g.max_ctx), - RAD_STR("q_dtype", g.dtype), RAD_STR("kv_dtype", g.kv_dtype)), - RAD_NOWEIGHTS), - {w.q}, {w.attn}); + if (!cfg.lili) { + op_attn = rw(b, RAD_OP(b, "attn_paged", + RAD_PARAMS(RAD_RANGE("q_len", 1, cfg.block), RAD_INT("head_dim", g.head_dim), + RAD_INT("gqa", g.gqa()), RAD_INT("causal", cfg.causal), + RAD_INT("window", cfg.window), RAD_INT("n_head", g.n_head), + RAD_INT("block_size", RAD_KV_BLOCK), + RAD_INT("max_seqs", g.max_seqs), RAD_INT("max_ctx", g.max_ctx), + RAD_STR("q_dtype", g.dtype), RAD_STR("kv_dtype", g.kv_dtype)), + RAD_NOWEIGHTS), + {w.q}, {w.attn}); + } else { + for (int64_t l = 0; l < g.n_layer; ++l) { + const int i = (int)l; + op_attn_sink[i] = rw(b, RAD_OP(b, "attn_paged_sink", + RAD_PARAMS(RAD_RANGE("q_len", 1, cfg.block), + RAD_INT("head_dim", g.head_dim), + RAD_INT("gqa", g.gqa()), RAD_INT("causal", cfg.causal), + RAD_INT("window", cfg.window), + RAD_INT("n_head", g.n_head), + RAD_INT("block_size", RAD_KV_BLOCK), + RAD_INT("max_seqs", g.max_seqs), + RAD_INT("max_ctx", g.max_ctx), + RAD_STR("q_dtype", g.dtype), + RAD_STR("kv_dtype", g.kv_dtype)), + RAD_WEIGHTS(w_sink[i])), + {w.q}, {w.attn}); + if (!op_attn_sink[i]) return RAD_E_INVAL; + } + } /* ---------------------------------------------------------------- the context pass */ /* Every linear is `fp8a8` (or `mxfp4a8` for a 4-bit recipe; DenseFP8 reads which from the @@ -887,6 +942,8 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), {w.h}, {w.sel_h}); + /* LiLiCorr's head is the plugin's (rad_block_lilicorr.h), over sel_h. */ + if (cfg.lili) return RAD_OK; op_dquant = rw(b, RAD_OP(b, "quant_act_i8", RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("n", g.n_embd), RAD_INT("group", DF_W2_GROUP), RAD_STR("dtype", g.dtype)), @@ -1192,13 +1249,22 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { RAD_ISSUE_N(c, op_store, T, brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, first_layer + i)); } - RAD_ISSUE_N(c, op_attn, batch->max_q_len, - brows(w.q, T), kv_cache(kv, first_layer + i), - praw2(table, RAD_I32, batch->n_seq, kvb ? kvb->block_table_pitch : 0), - praw(used, RAD_I32, batch->n_seq), - RAD_NONE, RAD_NONE, - praw(batch->cu_seqlens, RAD_I32, batch->n_seq + 1), - brows(w.attn, T)); + if (cfg.lili) + RAD_ISSUE_N(c, op_attn_sink[i], batch->max_q_len, + brows(w.q, T), kv_cache(kv, first_layer + i), + praw2(table, RAD_I32, batch->n_seq, kvb ? kvb->block_table_pitch : 0), + praw(used, RAD_I32, batch->n_seq), + RAD_NONE, RAD_NONE, + praw(batch->cu_seqlens, RAD_I32, batch->n_seq + 1), + RAD_W(w_sink[i]), brows(w.attn, T)); + else + RAD_ISSUE_N(c, op_attn, batch->max_q_len, + brows(w.q, T), kv_cache(kv, first_layer + i), + praw2(table, RAD_I32, batch->n_seq, kvb ? kvb->block_table_pitch : 0), + praw(used, RAD_I32, batch->n_seq), + RAD_NONE, RAD_NONE, + praw(batch->cu_seqlens, RAD_I32, batch->n_seq + 1), + brows(w.attn, T)); RAD_ISSUE_N(c, op_qat, T, brows(w.attn, T), brows(w.atq, T), brows(w.ats, T)); RAD_ISSUE_N(c, op_op[i], T, brows(w.atq, T), brows(w.ats, T), RAD_W(w_o[i].w), RAD_W(w_o[i].s), brows(w.h, T), RAD_NONE, w_o[i].bref()); @@ -1242,6 +1308,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { const int64_t S = batch->n_out; if (S <= 0) return; RAD_ISSUE_N(c, op_gather, S, RAD_B(w.h), praw(batch->out_ids, RAD_I32, S), brows(w.sel_h, S)); + if (cfg.lili) return; /* the plugin's correlator takes sel_h from here */ /* `dsum` is group-major, [n_embd/group][ceil(M/16)*16], so it has no row slice; pass it * whole. */ diff --git a/arch/common/rad_block_lilicorr.h b/arch/common/rad_block_lilicorr.h new file mode 100644 index 00000000..1219673a --- /dev/null +++ b/arch/common/rad_block_lilicorr.h @@ -0,0 +1,395 @@ +/* rad_block_lilicorr.h -- LiLiCorr's head: the candidate correlator after a DFlash2 backbone. + * + * NVIDIA's drafter for Nemotron-3.5-Bolt (vLLM's model_executor/models/lilicorr.py). The backbone is + * rad_block_dflash2.h's, with attention sinks (Dflash2::Config::lili). Its final hidden states at + * the block's draft rows go here instead of to DFlash2's selector: + * + * candidates each draft position's top-k from the target's own lm_head (log-softmax values, + * vocab-parallel: each rank's shard, all-gathered, merged) + * lattice steps x k rows a sequence, row s * k + j: + * token_proj(embed(cand)) + pass_hidden_proj(h_s) + * + feature_mlp(layernorm(five rank features)) + slot[s] + rank[j] + * 2 layers rmsnorm, attention over the lattice with a learned slot bias (lili_attn), the + * in/out projections; rmsnorm, silu MLP with biases; residuals + * factor x = output_norm(lattice), a = anchor_norm(context_proj(the anchor's context row)) + * f = silu(W [x | a | x * a] + b), [out | in] = edge heads of f + * walk normalised dot products, start from the anchor's own outgoing head, greedy + * (lili_walk) + * + * The anchor's context row is hidden_norm(fc(taps)) at the last committed position, which the + * context pass left in Dflash2's `c` (RadBatch::anchor_rows names the row). The head reads the + * target's embedding and lm_head, and its own 25 linears are bf16: ModelOpt ships them NVFP4, + * which no kernel serves, and the recipe decodes them (`cast dtype=bf16`). + * + * Every rank computes the whole lattice (vLLM: "candidate IDs and embeddings are replicated across + * TP ranks"); only the head's logits and the embedding lookup are per rank, with the merge in + * lili_cand and an exact all-reduce after the lookup. + */ +#ifndef RAD_BLOCK_LILICORR_H +#define RAD_BLOCK_LILICORR_H + +#include "rad_arch.h" + +namespace rad { +namespace arch { + +struct LiliCorr { + enum { MAX_LAYERS = 4 }; + + struct Config { + int64_t steps = 0; /* draft positions a sequence: the block less the anchor */ + int64_t k = 8; /* candidates a position (lilicorr_candidate_topk) */ + int64_t hs = 1024; /* the lattice's width (lilicorr_hidden_size) */ + int64_t n_head = 8; + int64_t mlp = 2048; /* hs * lilicorr_mlp_ratio */ + int64_t fdim = 1024; /* lilicorr_factor_dim */ + int64_t n_layer = 2; + double eps = 1e-6; /* rms_norm_eps, the lattice's RMS norms */ + double vec_eps = 1e-4; /* lilicorr_vector_eps, F.normalize's floor */ + double scale = 8.0; /* lilicorr_logit_scale */ + }; + + /* row_k: the pairs a rank's logprob row carries, k rounded up so 2 + 2 row_k floats are whole + * 16-byte words (the all-gather's unit): 9 for k = 8. */ + static int64_t row_k(int64_t k) { return (k % 2) ? k : k + 1; } + + struct Wire { + /* Dflash2's */ + rad_buf sel_h = 0; /* [srows, n_embd] the drafter's final hidden states, draft rows */ + rad_buf c = 0; /* [max_tok, n_embd] hidden_norm(fc(taps)), the context pass's rows */ + rad_buf tokens = 0; /* [max_seqs, steps] i32, the proposal */ + /* the head's own */ + rad_buf logits = 0; /* [srows, n_vocab] f32, this rank's shard */ + rad_buf stats = 0; /* [srows, 2 + 2 row_k] f32 */ + rad_buf sgath = 0; /* [srows, world * (2 + 2 row_k)] f32, world > 1 */ + rad_buf pas = 0; /* [srows, hs] */ + rad_buf cand = 0; /* [srows * k] i32 */ + rad_buf feat = 0; /* [lrows, hs] */ + rad_buf hh = 0; /* [lrows, hs] the lattice's residual stream; starts as lili_cand's base */ + rad_buf emb = 0; /* [lrows, n_embd] the candidates' embeddings */ + rad_buf y = 0; /* [lrows, hs] */ + rad_buf qkv = 0; /* [lrows, 3 hs] */ + rad_buf att = 0; /* [lrows, hs] */ + rad_buf up = 0; /* [lrows, mlp] */ + rad_buf ups = 0; /* [lrows, mlp] */ + rad_buf cat = 0; /* [lrows, 3 hs] */ + rad_buf fh = 0; /* [lrows, hs] */ + rad_buf fhs = 0; /* [lrows, hs] */ + rad_buf edges = 0; /* [lrows, 2 fdim] */ + rad_buf anc = 0; /* [max_seqs, n_embd] */ + rad_buf ast = 0; /* [max_seqs, hs] */ + rad_buf an = 0; /* [max_seqs, hs] */ + rad_buf aout = 0; /* [max_seqs, fdim] */ + }; + + Geom g{}; + Config cfg{}; + Wire w{}; + int64_t srows = 0, lrows = 0, rk = 0; + + rad_weight w_lm = 0, w_tok = 0; /* the target's */ + rad_weight w_tokp = 0, b_tokp = 0, w_pass = 0, b_pass = 0, w_ctx = 0, b_ctx = 0; + rad_weight w_fdn = 0, b_fdn = 0, ln_w = 0, ln_b = 0, up_w = 0, up_b = 0, w_slot = 0, w_rank = 0; + rad_weight w_anorm[MAX_LAYERS] = {}, w_in[MAX_LAYERS] = {}, b_in[MAX_LAYERS] = {}; + rad_weight w_out[MAX_LAYERS] = {}, b_out[MAX_LAYERS] = {}, w_mnorm[MAX_LAYERS] = {}; + rad_weight w_up[MAX_LAYERS] = {}, b_up[MAX_LAYERS] = {}, w_dn[MAX_LAYERS] = {}; + rad_weight b_dn[MAX_LAYERS] = {}; + rad_weight w_rel = 0, w_same = 0, w_onorm = 0, w_annorm = 0; + rad_weight w_fac = 0, b_fac = 0, w_edge = 0, b_edge = 0, w_aout = 0, b_aout = 0; + + rad_op op_logits = 0, op_stats = 0, op_sgath = 0, op_pas = 0, op_cand = 0; + rad_op op_emb = 0, op_emb_ar = 0, op_tokp = 0, op_fdn = 0; + rad_op op_anorm[MAX_LAYERS] = {}, op_in[MAX_LAYERS] = {}, op_attn = 0; + rad_op op_out[MAX_LAYERS] = {}, op_mnorm[MAX_LAYERS] = {}, op_up[MAX_LAYERS] = {}; + rad_op op_dn[MAX_LAYERS] = {}, op_silu_up = 0; + rad_op op_onorm = 0, op_anc = 0, op_ctx = 0, op_annorm = 0, op_factor = 0, op_fac = 0; + rad_op op_silu_f = 0, op_edge = 0, op_aout = 0, op_walk = 0; + + int declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& c, rad_weight lm_head, + rad_weight tok_embd, const char* ck, const Wire& wire); + /* After Dflash2::step, on the same query batch: `sel_h` holds the draft rows. */ + void step(RadCtx* c, const RadBatch* batch) const; +}; + +inline int LiliCorr::declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& c, + rad_weight lm_head, rad_weight tok_embd, const char* ck, + const Wire& wire) { + g = geom; cfg = c; w = wire; w_lm = lm_head; w_tok = tok_embd; + if (!w_lm || !w_tok || !ck || cfg.steps < 1 || cfg.k < 2 || cfg.n_layer < 1 || + cfg.n_layer > MAX_LAYERS || cfg.hs % cfg.n_head) + return RAD_E_INVAL; + const int64_t K = cfg.k, S = cfg.steps, hs = cfg.hs, fd = cfg.fdim, E = g.n_embd; + srows = g.max_seqs * S; + lrows = srows * K; + rk = row_k(K); + const int ACC = RAD_ACCESS_PER_TOKEN; + const RadWeightGroup grp = grp_model(); + + /* The head's weights, all replicated. Linears and tables bf16, biases, norms and the + * feature layer f32. */ + auto wt = [&](const char* dn, const char* src, uint32_t dt, + std::initializer_list shp) -> rad_weight { + if (map_copy(b, dn, src) < 0) return 0; + return decl_w(b, dn, dt, shp, ACC, RAD_SHARD_NONE, grp); + }; + const char* L = nm.ckpt("%s.lilicorr", ck); + auto src = [&](const char* tail) { return nm.ckpt("%s.%s", L, tail); }; + w_tokp = wt(nm.f("dflash.lili.token_proj.weight"), src("token_proj.weight"), RAD_BF16, { hs, E }); + b_tokp = wt(nm.f("dflash.lili.token_proj.bias"), src("token_proj.bias"), RAD_F32, { hs }); + w_pass = wt(nm.f("dflash.lili.pass_proj.weight"), src("pass_hidden_proj.weight"), RAD_BF16, + { hs, E }); + b_pass = wt(nm.f("dflash.lili.pass_proj.bias"), src("pass_hidden_proj.bias"), RAD_F32, { hs }); + w_ctx = wt(nm.f("dflash.lili.context_proj.weight"), src("context_proj.weight"), RAD_BF16, + { hs, E }); + b_ctx = wt(nm.f("dflash.lili.context_proj.bias"), src("context_proj.bias"), RAD_F32, { hs }); + ln_w = wt(nm.f("dflash.lili.feature_norm.weight"), src("feature_norm.weight"), RAD_F32, { 5 }); + ln_b = wt(nm.f("dflash.lili.feature_norm.bias"), src("feature_norm.bias"), RAD_F32, { 5 }); + up_w = wt(nm.f("dflash.lili.feature_up.weight"), src("feature_mlp.up_proj.weight"), RAD_F32, + { hs, 5 }); + up_b = wt(nm.f("dflash.lili.feature_up.bias"), src("feature_mlp.up_proj.bias"), RAD_F32, + { hs }); + w_fdn = wt(nm.f("dflash.lili.feature_down.weight"), src("feature_mlp.down_proj.weight"), + RAD_BF16, { hs, hs }); + b_fdn = wt(nm.f("dflash.lili.feature_down.bias"), src("feature_mlp.down_proj.bias"), RAD_F32, + { hs }); + /* [1, 1, steps, 1, hs] and [1, 1, 1, k, hs], read as tables (a reshape of one whole plane). */ + w_slot = wt(nm.f("dflash.lili.slot_embedding"), src("slot_embedding"), RAD_BF16, { S, hs }); + w_rank = wt(nm.f("dflash.lili.rank_embedding"), src("rank_embedding"), RAD_BF16, { K, hs }); + w_rel = wt(nm.f("dflash.lili.relative_slot_bias"), src("relative_slot_bias"), RAD_F32, + { cfg.n_head, 2 * (S + 1) - 1 }); + w_same = wt(nm.f("dflash.lili.same_slot_bias"), src("same_slot_bias"), RAD_F32, { cfg.n_head }); + for (int64_t l = 0; l < cfg.n_layer; ++l) { + const int i = (int)l; + auto ls = [&](const char* tail) { return nm.ckpt("%s.layers.%d.%s", L, i, tail); }; + w_anorm[i] = wt(nm.f("dflash.lili.blk.%d.attn_norm.weight", i), ls("attn_norm.weight"), + RAD_F32, { hs }); + w_in[i] = wt(nm.f("dflash.lili.blk.%d.attn_in.weight", i), ls("attn.in_proj_weight"), + RAD_BF16, { 3 * hs, hs }); + b_in[i] = wt(nm.f("dflash.lili.blk.%d.attn_in.bias", i), ls("attn.in_proj_bias"), RAD_F32, + { 3 * hs }); + w_out[i] = wt(nm.f("dflash.lili.blk.%d.attn_out.weight", i), ls("attn.out_proj.weight"), + RAD_BF16, { hs, hs }); + b_out[i] = wt(nm.f("dflash.lili.blk.%d.attn_out.bias", i), ls("attn.out_proj.bias"), + RAD_F32, { hs }); + w_mnorm[i] = wt(nm.f("dflash.lili.blk.%d.mlp_norm.weight", i), ls("mlp_norm.weight"), + RAD_F32, { hs }); + w_up[i] = wt(nm.f("dflash.lili.blk.%d.mlp_up.weight", i), ls("mlp.up_proj.weight"), + RAD_BF16, { cfg.mlp, hs }); + b_up[i] = wt(nm.f("dflash.lili.blk.%d.mlp_up.bias", i), ls("mlp.up_proj.bias"), RAD_F32, + { cfg.mlp }); + w_dn[i] = wt(nm.f("dflash.lili.blk.%d.mlp_down.weight", i), ls("mlp.down_proj.weight"), + RAD_BF16, { hs, cfg.mlp }); + b_dn[i] = wt(nm.f("dflash.lili.blk.%d.mlp_down.bias", i), ls("mlp.down_proj.bias"), + RAD_F32, { hs }); + if (!w_anorm[i] || !w_in[i] || !b_in[i] || !w_out[i] || !b_out[i] || !w_mnorm[i] || + !w_up[i] || !b_up[i] || !w_dn[i] || !b_dn[i]) + return RAD_E_INVAL; + } + w_onorm = wt(nm.f("dflash.lili.output_norm.weight"), src("output_norm.weight"), RAD_F32, { hs }); + w_annorm = wt(nm.f("dflash.lili.anchor_norm.weight"), src("anchor_norm.weight"), RAD_F32, { hs }); + w_fac = wt(nm.f("dflash.lili.factor_proj.weight"), src("factor_input_proj.weight"), RAD_BF16, + { hs, 3 * hs }); + b_fac = wt(nm.f("dflash.lili.factor_proj.bias"), src("factor_input_proj.bias"), RAD_F32, + { hs }); + /* [out_head | in_head] as one linear, as vLLM fuses them (`_fused_edge_weight`). */ + RAD_ARCH_TRY(map_concat(b, nm.f("dflash.lili.edge.weight"), + { src("out_head.weight"), src("in_head.weight") }, 0)); + RAD_ARCH_TRY(map_concat(b, nm.f("dflash.lili.edge.bias"), + { src("out_head.bias"), src("in_head.bias") }, 0)); + w_edge = decl_w(b, nm.f("dflash.lili.edge.weight"), RAD_BF16, { 2 * fd, hs }, ACC, + RAD_SHARD_NONE, grp); + b_edge = decl_w(b, nm.f("dflash.lili.edge.bias"), RAD_F32, { 2 * fd }, ACC, RAD_SHARD_NONE, + grp); + w_aout = wt(nm.f("dflash.lili.anchor_out.weight"), src("anchor_out_head.weight"), RAD_BF16, + { fd, hs }); + b_aout = wt(nm.f("dflash.lili.anchor_out.bias"), src("anchor_out_head.bias"), RAD_F32, { fd }); + if (!w_tokp || !b_tokp || !w_pass || !b_pass || !w_ctx || !b_ctx || !ln_w || !ln_b || !up_w || + !up_b || !w_fdn || !b_fdn || !w_slot || !w_rank || !w_rel || !w_same || !w_onorm || + !w_annorm || !w_fac || !b_fac || !w_edge || !b_edge || !w_aout || !b_aout) + return RAD_E_INVAL; + + /* ---------------------------------------------------------------- ops */ + auto gemm_b = [&](int64_t rows, int64_t N, int64_t Kd, rad_weight wgt, rad_weight bias) + -> rad_op { + return RAD_OP(b, "gemm_nt_bias", + RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("N", N), RAD_INT("K", Kd), + RAD_STR("dtype", "bf16"), RAD_STR("act", "none")), + RAD_WEIGHTS(wgt, bias)); + }; + auto rms = [&](int64_t rows, int64_t n, rad_weight gain) -> rad_op { + return RAD_OP(b, "rmsnorm", + RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n), RAD_F64("eps", cfg.eps), + RAD_STR("dtype", "bf16")), + RAD_WEIGHTS(gain)); + }; + auto silu = [&](int64_t rows, int64_t n) -> rad_op { + return RAD_OP(b, "silu", + RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n), RAD_STR("dtype", "bf16")), + RAD_NOWEIGHTS); + }; + const int64_t sw = 2 + 2 * rk; + + /* The candidates: this rank's shard of the target's logits, its logprob row, the exchange. */ + op_logits = rw(b, RAD_OP(b, "logits_gemm", + RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("n_vocab", g.n_vocab), + RAD_INT("n_embd", E), RAD_STR("dtype", "bf16")), + RAD_WEIGHTS(w_lm)), + { w.sel_h }, { w.logits }); + /* Replicated, `vocab_off` is omitted: an optional parameter present at 0 is a different + * declaration from an absent one. */ + op_stats = g.world > 1 + ? rw(b, RAD_OP(b, "sample_logprobs", + RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("n_vocab", g.n_vocab), + RAD_INT("k", rk), RAD_INT("vocab_off", g.vocab_off)), + RAD_NOWEIGHTS), + { w.logits }, { w.stats }) + : rw(b, RAD_OP(b, "sample_logprobs", + RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("n_vocab", g.n_vocab), + RAD_INT("k", rk)), + RAD_NOWEIGHTS), + { w.logits }, { w.stats }); + if (g.world > 1) + op_sgath = rw(b, RAD_OP(b, "all_gather", + RAD_PARAMS(RAD_INT("world_size", g.world), + RAD_INT("numel", srows * sw), RAD_STR("dtype", "f32"), + RAD_INT("row", sw)), + RAD_NOWEIGHTS), + { w.stats }, { w.sgath }); + op_pas = rw(b, gemm_b(srows, hs, E, w_pass, b_pass), { w.sel_h }, { w.pas }); + op_cand = rw(b, RAD_OP(b, "lili_cand", + RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("k", K), + RAD_INT("world", g.world), RAD_INT("steps", S), + RAD_INT("n", hs), RAD_STR("dtype", "bf16"), + RAD_F64("eps", 1e-5), RAD_INT("row_k", rk)), + RAD_WEIGHTS(ln_w, ln_b, up_w, up_b, w_slot, w_rank)), + { g.world > 1 ? w.sgath : w.stats, w.pas }, { w.cand, w.feat, w.hh }); + /* The lattice: candidates' embeddings (the trunk's vocab-sharded table, exact reduce), the + * token and feature projections added onto lili_cand's base in their GEMMs' residual. */ + op_emb = rw(b, RAD_OP(b, "embed_lookup", + RAD_PARAMS(RAD_RANGE("M", 1, lrows), RAD_INT("n_embd", E), + RAD_INT("n_vocab", g.n_vocab), RAD_STR("dtype", "bf16"), + RAD_INT("vocab_offset", g.vocab_off)), + RAD_WEIGHTS(w_tok)), + { w.cand }, { w.emb }); + if (g.world > 1) + op_emb_ar = rw(b, RAD_OP(b, "all_reduce", + RAD_PARAMS(RAD_INT("world_size", g.world), + RAD_RANGE("numel", E, lrows * E), + RAD_STR("dtype", "bf16"), RAD_INT("exact", 1)), + RAD_NOWEIGHTS), + { w.emb }, { w.emb }); + op_tokp = rw(b, gemm_b(lrows, hs, E, w_tokp, b_tokp), { w.emb, w.hh }, { w.hh }); + op_fdn = rw(b, gemm_b(lrows, hs, hs, w_fdn, b_fdn), { w.feat, w.hh }, { w.hh }); + for (int64_t l = 0; l < cfg.n_layer; ++l) { + const int i = (int)l; + op_anorm[i] = rw(b, rms(lrows, hs, w_anorm[i]), { w.hh }, { w.y }); + op_in[i] = rw(b, gemm_b(lrows, 3 * hs, hs, w_in[i], b_in[i]), { w.y }, { w.qkv }); + op_out[i] = rw(b, gemm_b(lrows, hs, hs, w_out[i], b_out[i]), { w.att, w.hh }, { w.hh }); + op_mnorm[i] = rw(b, rms(lrows, hs, w_mnorm[i]), { w.hh }, { w.y }); + op_up[i] = rw(b, gemm_b(lrows, cfg.mlp, hs, w_up[i], b_up[i]), { w.y }, { w.up }); + op_dn[i] = rw(b, gemm_b(lrows, hs, cfg.mlp, w_dn[i], b_dn[i]), { w.ups, w.hh }, { w.hh }); + } + op_attn = rw(b, RAD_OP(b, "lili_attn", + RAD_PARAMS(RAD_RANGE("M", 1, lrows), RAD_INT("steps", S), RAD_INT("k", K), + RAD_INT("n_head", cfg.n_head), + RAD_INT("head_dim", hs / cfg.n_head), + RAD_STR("dtype", "bf16")), + RAD_WEIGHTS(w_rel, w_same)), + { w.qkv }, { w.att }); + op_silu_up = rw(b, silu(lrows, cfg.mlp), { w.up }, { w.ups }); + op_onorm = rw(b, rms(lrows, hs, w_onorm), { w.hh }, { w.y }); + /* The anchor: the context pass's row at each drafting sequence's last committed position. */ + op_anc = rw(b, RAD_OP(b, "gather_rows", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("n", E), + RAD_STR("dtype", "bf16")), + RAD_NOWEIGHTS), + { w.c }, { w.anc }); + op_ctx = rw(b, gemm_b(g.max_seqs, hs, E, w_ctx, b_ctx), { w.anc }, { w.ast }); + op_annorm = rw(b, rms(g.max_seqs, hs, w_annorm), { w.ast }, { w.an }); + op_factor = rw(b, RAD_OP(b, "lili_factor", + RAD_PARAMS(RAD_RANGE("M", 1, lrows), RAD_INT("L", S * K), + RAD_INT("n", hs), RAD_STR("dtype", "bf16")), + RAD_NOWEIGHTS), + { w.y, w.an }, { w.cat }); + op_fac = rw(b, gemm_b(lrows, hs, 3 * hs, w_fac, b_fac), { w.cat }, { w.fh }); + op_silu_f = rw(b, silu(lrows, hs), { w.fh }, { w.fhs }); + op_edge = rw(b, gemm_b(lrows, 2 * fd, hs, w_edge, b_edge), { w.fhs }, { w.edges }); + op_aout = rw(b, gemm_b(g.max_seqs, fd, hs, w_aout, b_aout), { w.an }, { w.aout }); + op_walk = rw(b, RAD_OP(b, "lili_walk", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("steps", S), + RAD_INT("k", K), RAD_INT("n", fd), RAD_STR("dtype", "bf16"), + RAD_F64("eps", cfg.vec_eps), RAD_F64("scale", cfg.scale)), + RAD_NOWEIGHTS), + { w.edges, w.aout, w.cand }, { w.tokens }); + if (!op_logits || !op_stats || (g.world > 1 && (!op_sgath || !op_emb_ar)) || !op_pas || + !op_cand || !op_emb || !op_tokp || !op_fdn || !op_attn || !op_silu_up || !op_onorm || + !op_anc || !op_ctx || !op_annorm || !op_factor || !op_fac || !op_silu_f || !op_edge || + !op_aout || !op_walk) + return RAD_E_INVAL; + return RAD_OK; +} + +inline void LiliCorr::step(RadCtx* c, const RadBatch* batch) const { + const int64_t n = batch->n_seq, S = n * cfg.steps, Lr = S * cfg.k; + if (n <= 0) return; + if (!batch->anchor_rows) { + rad_step_fail(c, "lilicorr: the query pass carries no anchor rows (RadBatch::anchor_rows): " + "this engine does not position a block drafter's anchor"); + return; + } + const int64_t E = g.n_embd, hs = cfg.hs, fd = cfg.fdim; + const int64_t sw = 2 + 2 * rk; + + RAD_ISSUE_N(c, op_logits, S, brows(w.sel_h, S), RAD_W(w_lm), brows(w.logits, S)); + RAD_ISSUE_N(c, op_stats, S, brows(w.logits, S), RAD_NONE, brows(w.stats, S)); + if (op_sgath) RAD_ISSUE(c, op_sgath, brows(w.stats, S), brows(w.sgath, S)); + RAD_ISSUE_N(c, op_pas, S, brows(w.sel_h, S), RAD_W(w_pass), RAD_W(b_pass), RAD_NONE, + brows(w.pas, S)); + RAD_ISSUE_N(c, op_cand, S, op_sgath ? brows(w.sgath, S) : brows(w.stats, S), brows(w.pas, S), + RAD_W(ln_w), RAD_W(ln_b), RAD_W(up_w), RAD_W(up_b), RAD_W(w_slot), RAD_W(w_rank), + brow_slice(w.cand, 0, Lr, 1), brows(w.feat, Lr), brows(w.hh, Lr)); + (void)sw; + + RAD_ISSUE_N(c, op_emb, Lr, brow_slice(w.cand, 0, Lr, 1), RAD_W(w_tok), brows(w.emb, Lr)); + if (op_emb_ar) RAD_ISSUE_N(c, op_emb_ar, Lr * E, brows(w.emb, Lr), RAD_NONE); + RAD_ISSUE_N(c, op_tokp, Lr, brows(w.emb, Lr), RAD_W(w_tokp), RAD_W(b_tokp), brows(w.hh, Lr), + brows(w.hh, Lr)); + RAD_ISSUE_N(c, op_fdn, Lr, brows(w.feat, Lr), RAD_W(w_fdn), RAD_W(b_fdn), brows(w.hh, Lr), + brows(w.hh, Lr)); + for (int64_t l = 0; l < cfg.n_layer; ++l) { + const int i = (int)l; + RAD_ISSUE_N(c, op_anorm[i], Lr, brows(w.hh, Lr), RAD_W(w_anorm[i]), brows(w.y, Lr)); + RAD_ISSUE_N(c, op_in[i], Lr, brows(w.y, Lr), RAD_W(w_in[i]), RAD_W(b_in[i]), RAD_NONE, + brows(w.qkv, Lr)); + RAD_ISSUE_N(c, op_attn, Lr, brows(w.qkv, Lr), RAD_W(w_rel), RAD_W(w_same), brows(w.att, Lr)); + RAD_ISSUE_N(c, op_out[i], Lr, brows(w.att, Lr), RAD_W(w_out[i]), RAD_W(b_out[i]), + brows(w.hh, Lr), brows(w.hh, Lr)); + RAD_ISSUE_N(c, op_mnorm[i], Lr, brows(w.hh, Lr), RAD_W(w_mnorm[i]), brows(w.y, Lr)); + RAD_ISSUE_N(c, op_up[i], Lr, brows(w.y, Lr), RAD_W(w_up[i]), RAD_W(b_up[i]), RAD_NONE, + brows(w.up, Lr)); + RAD_ISSUE_N(c, op_silu_up, Lr, brows(w.up, Lr), brows(w.ups, Lr)); + RAD_ISSUE_N(c, op_dn[i], Lr, brows(w.ups, Lr), RAD_W(w_dn[i]), RAD_W(b_dn[i]), + brows(w.hh, Lr), brows(w.hh, Lr)); + } + RAD_ISSUE_N(c, op_onorm, Lr, brows(w.hh, Lr), RAD_W(w_onorm), brows(w.y, Lr)); + + RAD_ISSUE_N(c, op_anc, n, RAD_B(w.c), praw(batch->anchor_rows, RAD_I32, n), brows(w.anc, n)); + RAD_ISSUE_N(c, op_ctx, n, brows(w.anc, n), RAD_W(w_ctx), RAD_W(b_ctx), RAD_NONE, brows(w.ast, n)); + RAD_ISSUE_N(c, op_annorm, n, brows(w.ast, n), RAD_W(w_annorm), brows(w.an, n)); + RAD_ISSUE_N(c, op_factor, Lr, brows(w.y, Lr), brows(w.an, n), brows(w.cat, Lr)); + RAD_ISSUE_N(c, op_fac, Lr, brows(w.cat, Lr), RAD_W(w_fac), RAD_W(b_fac), RAD_NONE, + brows(w.fh, Lr)); + RAD_ISSUE_N(c, op_silu_f, Lr, brows(w.fh, Lr), brows(w.fhs, Lr)); + RAD_ISSUE_N(c, op_edge, Lr, brows(w.fhs, Lr), RAD_W(w_edge), RAD_W(b_edge), RAD_NONE, + brows(w.edges, Lr)); + RAD_ISSUE_N(c, op_aout, n, brows(w.an, n), RAD_W(w_aout), RAD_W(b_aout), RAD_NONE, + brows(w.aout, n)); + RAD_ISSUE_N(c, op_walk, n, brows(w.edges, Lr), brows(w.aout, n), + brow_slice(w.cand, 0, Lr, 1), brows(w.tokens, n), RAD_NONE); + (void)E; (void)hs; (void)fd; +} + +} /* namespace arch */ +} /* namespace rad */ + +#endif /* RAD_BLOCK_LILICORR_H */ diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index 05a0a93a..f22e1452 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -1819,6 +1819,23 @@ struct DenseFP8 { RAD_ARCH_TRY(rad_weight_shard_span(b, w, lo, hi)); return rad_weight_shard_span(b, s, lo / RAD_FP8_BLOCK, hi / RAD_FP8_BLOCK); } + /* This rank's rows [lo, hi) of a RAD_SHARD_ROW weight where the even shard is the wrong cut: a + * KV head replicated over ranks (Geom::kv_off), the same rows on every rank that shares it. + * Call after declare(), whose N is already hi - lo. The fp8 scale plane takes the span in its + * 128-row blocks; the MXFP4 and M2XFP4 planes are one row a weight row, as in LinearFP8. */ + int span(RadBuilder* b, int64_t lo, int64_t hi) const { + RAD_ARCH_TRY(rad_weight_shard_span(b, w, lo, hi)); + if (mx || m2x) { + RAD_ARCH_TRY(rad_weight_shard_span(b, s, lo, hi)); + return rad_weight_shard_span(b, ref, lo, hi); + } + if (lo % RAD_FP8_BLOCK || hi % RAD_FP8_BLOCK) { + fprintf(stderr, "radiance: a row span [%lld, %lld) is not whole %d-row scale blocks\n", + (long long)lo, (long long)hi, RAD_FP8_BLOCK); + return RAD_E_INVAL; + } + return rad_weight_shard_span(b, s, lo / RAD_FP8_BLOCK, hi / RAD_FP8_BLOCK); + } /* `gemm_nt_q` over this weight, `fp8a8`, `mxfp4a8` or `m2xfp4` as it was declared, ranged * M 1..hi. `split_n`, when set, is the stored width a row slice takes its K split from * (docs/OPS.md). */