Archived
arch/gemma4: Gemma 4 text with z-lab's DFlash drafter -- the 26B-A4B MoE and the dense sizes
Claims ("gemma4", ""): a container converted from Google's bf16 checkpoint, the recipe choosing
what is fp8. gemma4_bf16 keeps ("gemma4", "compressed-tensors"), the CT-INT4 dense release; the
two do not overlap. Serves gemma-4-26B-A4B-it (128 experts of 704, top 8) and the dense sizes
without per-layer embeddings, with a DFlash block-diffusion drafter merged at convert time.
The readings that stay plausible when wrong, each explained at its site:
- Proportional rope is not a partial rotary: the full layers rotate the first 64 frequency pairs
of the 512-wide head at theta^(-2j/512), pair j = channels (j, j + 256), declared as full-width
NeoX with the frequencies past pair 63 divided by 1e30 through the YaRN ramp `rope` carries.
- K is V on the full layers: V is the un-normed, un-rotated k projection, so its norm is issued
before the key norm overwrites `k`.
- The router reads the residual through a gainless rmsnorm times its `scale` times hidden^-0.5,
and per_expert_scale multiplies after the renormalised top-k.
- Two post-norms a layer's feed-forward, one an arm, each arm's partial reduced before its norm;
both partials share one [2T, n_embd] buffer so one all-reduce carries them.
- The embedding scale is sqrt(hidden) rounded to bf16 (53.0, not 53.066).
- The sliding layers' KV group is RAD_KV_FULL with the window on the attention op: a WINDOW
group is not prefix-cached, and on this tree a hit on one re-prefills; FULL keeps the hit.
- fp8 experts are two tables by parity, so a rank holds an even count; down at K = 704 cannot be
fp8 (whole 128-blocks), hence the 768-padded recipe.
- The sharded draft head's merge is pair_argmax at R = 2 on the wire.
- The drafter's tap copy is declared at the end of the layer it taps, where it is issued. The
buffer planner takes liveness from declaration order; declared once after the trunk, it let the
plan share `dflash_src` with `moe_edn`, which every MoE layer writes, whenever the sizes
allowed. At --max-num-seqs 16 they did, and on the cards DFlash accepted 2% of its drafts
(97 tok/s against 135 with speculation off and 186 with the taps intact), the text fluent
throughout.
The sandwich joins are asked for as norm_add_norm / join_add_scale (gemma4k) and declared as the
rmsnorm / add chain where nothing serves them; RADIANCE_GEMMA4_FUSE=0 asks for the chain. The
reply format is the shared one (arch/rad_gemma4_chat.h), so tool calls are parsed in the engine;
the plugin as measured declared reasoning only and left calls to an out-of-tree sidecar, so the
call path is untested on this plugin's containers.
arch_test: the MoE trunk declares both arms and six of each join; both KV groups FULL with the
window on the op; the full layers' rope over the whole head with the ramp at 63.5..64; with the
joins refused, the chain is declared and issued instead; both Gemma 4 plugins export one format;
with a DFlash drafter, `dflash_src` is live from the first tap layer (its range overlaps
`moe_edn`'s, so no plan can share them) and a step writes each tap once.
Measured on 4x R9700 at TP4 (gemma-4-26B-A4B-it + DFlash, gate_up experts block fp8), with
gemma4k and --draft-window adaptive: a story at T 1 177 tok/s, greedy thinking 572 tok/s, code at
T 0 319-334 tok/s.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
368d0363eb
commit
c2bd41fe95
4 files changed
+2494
No files matched your search
@@ -0,0 +1,14 @@
|
||||
# gemma4 -- the architecture plugin for Gemma 4's text model (Gemma4ForConditionalGeneration /
|
||||
# Gemma4ForCausalLM; the 26B-A4B MoE and the dense sizes without per-layer embeddings), with a
|
||||
# z-lab DFlash drafter merged at convert time.
|
||||
#
|
||||
# Built inside radiance's tree (arch/ discovers it) or on its own against an installed radiance:
|
||||
# cmake -S arch/gemma4 -B build-gemma4 -DCMAKE_PREFIX_PATH=/opt/radiance && cmake --build build-gemma4
|
||||
if(NOT COMMAND rad_add_plugin)
|
||||
cmake_minimum_required(VERSION 3.21)
|
||||
project(gemma4 LANGUAGES CXX)
|
||||
find_package(radiance REQUIRED)
|
||||
rad_plugin_defaults()
|
||||
endif()
|
||||
|
||||
rad_add_plugin(gemma4 architectures SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gemma4.cpp)
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,592 @@
|
||||
/* gemma4_dflash.h -- the DFlash (v1) block-diffusion drafter, as z-lab trains it for Gemma 4.
|
||||
*
|
||||
* ============================== WHAT IT IS ==============================
|
||||
*
|
||||
* rad_block_dflash2.h WITHOUT THE TWO THINGS DFLASH2 ADDED: the per-layer dynamic convolutions
|
||||
* around attention and the feed-forward, and the candidate selector that walks a path through the
|
||||
* block. What is left is the original DFlash: a small Qwen3-shaped transformer that denoises one
|
||||
* anchor token plus `block - 1` mask tokens in ONE forward and reads every position's argmax off
|
||||
* the TARGET's lm_head.
|
||||
*
|
||||
* context pass, one row a committed token:
|
||||
* c = hidden_norm(fc(concat of the trunk's residual stream at target_layer_ids)))
|
||||
* for each layer: k = k_norm(k_proj(c)) then rope; v = v_proj(c); kv_store(k, v)
|
||||
*
|
||||
* query pass, `block` rows a sequence:
|
||||
* x = embed(anchor, mask, ...) * sqrt(hidden) <- Gemma's embedding scale, see below
|
||||
* for each layer:
|
||||
* h = input_layernorm(x)
|
||||
* q, k, v = q_proj(h), k_proj(h), v_proj(h); q_norm, k_norm; rope; kv_store
|
||||
* x += o_proj(attention(q, cache)) sliding layers CAUSAL over a 2048 window,
|
||||
* the full layer NON-causal over everything
|
||||
* x += down(silu(gate(h')) * up(h')), h' = post_attention_layernorm(x)
|
||||
* x = norm(x)
|
||||
* over the mask rows: logits = lm_head(x) [the TARGET's]; draft = argmax
|
||||
*
|
||||
* ============================== THE THREE GEMMA-SPECIFIC FACTS ==============================
|
||||
*
|
||||
* They are vLLM PR #41703's, and each one costs acceptance and nothing else when it is wrong --
|
||||
* every draft is verified against the trunk, so the model's output never depends on any of them.
|
||||
*
|
||||
* THE EMBEDDING IS SCALED. Gemma multiplies its token embedding by sqrt(hidden_size), stored as a
|
||||
* bf16 (53.0 at 2816), and the drafter was trained reading the target's embedding module, which
|
||||
* applies it. `embed_lookup`'s `mult` carries it.
|
||||
*
|
||||
* THE SLIDING LAYERS ARE CAUSAL AND THE FULL LAYER IS NOT. The checkpoint's `layer_types` names
|
||||
* four sliding layers and one full one; the PR's own test pins `causal True` on the sliding
|
||||
* layers and `False` on the full one. So this block declares TWO attention ops and issues the
|
||||
* one each layer's type names.
|
||||
*
|
||||
* THE LOGITS ARE SOFT-CAPPED, and that is ignored on purpose: the draft is an argmax, and
|
||||
* cap * tanh(x / cap) is strictly increasing, so it cannot move one.
|
||||
*
|
||||
* ============================== THE HEAD ==============================
|
||||
*
|
||||
* The target's own head, read in bf16 by an ordinary `gemm_nt` over the gathered mask rows. Gemma
|
||||
* ties it to the embedding, so it is the same weight the trunk's `embed_lookup` and `logits_gemm`
|
||||
* read: no extra bytes are stored. It is vocab-sharded with the trunk, so the top-1 runs over this
|
||||
* rank's slice and the ranks' candidates are merged exactly (`row_topk` at R = 2 -> `all_gather` ->
|
||||
* `pair_argmax`; R = 2 because the gather moves 16-byte rows, see kTopR).
|
||||
*
|
||||
* Reading the bf16 head every draft pass is the expensive way to do it -- 262144 x 2816 x 2 bytes
|
||||
* over the ranks. The cheaper way is the 2-bit copy plus an exact rerank that DFlash2 uses, and
|
||||
* that needs the trunk's head in fp8 or int8, which a tied Gemma head is not. Stated rather than
|
||||
* hidden: it is the first thing to change once the model runs.
|
||||
*
|
||||
* ============================== THE LINEARS ARE FP8 ==============================
|
||||
*
|
||||
* The same as DFlash2 and for the same reason -- the drafter is read whole on every decode step --
|
||||
* so the container must be converted with a recipe that makes the six linears block fp8 (or
|
||||
* MXFP4). declare() refuses a bf16 one by name rather than letting the loader fail on a view.
|
||||
*/
|
||||
#ifndef GEMMA4_DFLASH_H
|
||||
#define GEMMA4_DFLASH_H
|
||||
|
||||
#include <arch/rad_arch.h>
|
||||
#include <arch/rad_fp8.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
namespace gemma4 {
|
||||
|
||||
using namespace rad::arch;
|
||||
|
||||
struct Dflash1 {
|
||||
enum { MAX_LAYERS = 8 };
|
||||
|
||||
struct Config {
|
||||
int64_t n_taps = 0; /* how many trunk layers `fc` concatenates */
|
||||
int64_t block = 0; /* query rows a sequence: the anchor plus block - 1 masks */
|
||||
int64_t window = 0; /* the sliding layers' window; 0 for none */
|
||||
int full[MAX_LAYERS] = {}; /* 1: a full-attention (non-causal, unwindowed) layer */
|
||||
double escale = 1.0; /* the target's embedding scale, rounded as the target holds it */
|
||||
int64_t steps() const { return block - 1; }
|
||||
};
|
||||
|
||||
struct Wire {
|
||||
/* the context pass */
|
||||
rad_buf src = 0, srcq = 0, srcs = 0; /* [max_tok, n_taps*n_embd] bf16 | e4m3 | f32 */
|
||||
rad_buf c = 0, cq = 0, cs = 0; /* [max_tok, n_embd] */
|
||||
rad_buf ck = 0, cv = 0; /* [max_tok, kv_dim] -- see rad_block_dflash2.h */
|
||||
/* the query pass */
|
||||
rad_buf x = 0, h = 0, hq = 0, hs = 0; /* [qrows, n_embd] */
|
||||
rad_buf q = 0, k = 0, v = 0; /* [qrows, q_dim | kv_dim | kv_dim] */
|
||||
rad_buf attn = 0, atq = 0, ats = 0; /* [qrows, q_dim] */
|
||||
rad_buf gate_up = 0; /* [qrows, 2 * n_ff] */
|
||||
rad_buf ffn = 0, ffq = 0, ffs = 0; /* [qrows, n_ff] */
|
||||
/* the head, one row a draft position */
|
||||
rad_buf sel_h = 0; /* [srows, n_embd] */
|
||||
rad_buf dlog = 0; /* [srows, n_vocab on this rank] bf16 */
|
||||
rad_buf cand = 0, unary = 0; /* [srows, kTopR] i32 | f32 -- this rank's best */
|
||||
rad_buf dpair = 0, dgath = 0; /* [srows, kTopR, 2] | [srows, world * kTopR, 2] f32 */
|
||||
rad_buf tokens = 0; /* [srows, 1] i32: THE DRAFT, pitch `steps` */
|
||||
};
|
||||
|
||||
Geom g{}; /* the drafter's geometry */
|
||||
Config cfg{};
|
||||
Wire w{};
|
||||
int first_layer = 0;
|
||||
int64_t qrows = 0, crows = 0, srows = 0, gated_rows = 0;
|
||||
rad_kvgroup kv = 0;
|
||||
bool sharded = false;
|
||||
|
||||
DenseFP8 w_fc{};
|
||||
rad_weight w_hnorm = 0, w_norm = 0;
|
||||
rad_weight w_tok = 0; /* the TARGET's table: embedding AND head */
|
||||
rad_weight w_anorm[MAX_LAYERS] = {}, w_fnorm[MAX_LAYERS] = {};
|
||||
DenseFP8 w_q[MAX_LAYERS]{}, w_k[MAX_LAYERS]{}, w_v[MAX_LAYERS]{}, w_o[MAX_LAYERS]{};
|
||||
rad_weight w_qn[MAX_LAYERS] = {}, w_kn[MAX_LAYERS] = {};
|
||||
DenseFP8 w_gu[MAX_LAYERS]{}, w_dn[MAX_LAYERS]{};
|
||||
|
||||
rad_op op_qsrc = 0, op_cfc = 0, op_cnorm = 0, op_qc = 0, op_crope = 0, op_cstore = 0;
|
||||
rad_op op_ck[MAX_LAYERS] = {}, op_cv[MAX_LAYERS] = {}, op_cknorm[MAX_LAYERS] = {};
|
||||
|
||||
rad_op op_embed = 0, op_embed_ar = 0, op_qh = 0, op_qat = 0, op_qff = 0;
|
||||
rad_op op_anorm[MAX_LAYERS] = {}, op_fnorm[MAX_LAYERS] = {};
|
||||
rad_op op_qp[MAX_LAYERS] = {}, op_kp[MAX_LAYERS] = {}, op_vp[MAX_LAYERS] = {};
|
||||
rad_op op_op[MAX_LAYERS] = {}, op_qn[MAX_LAYERS] = {}, op_kn[MAX_LAYERS] = {};
|
||||
rad_op op_gu[MAX_LAYERS] = {}, op_gug[MAX_LAYERS] = {}, op_dn[MAX_LAYERS] = {};
|
||||
rad_op op_qrope = 0, op_krope = 0, op_store = 0, op_attn_swa = 0, op_attn_full = 0;
|
||||
rad_op op_act = 0, op_ar = 0, op_add = 0, op_final = 0;
|
||||
rad_op op_gather = 0, op_head = 0, op_topk = 0, op_dgather = 0, op_pick = 0;
|
||||
|
||||
/* CANDIDATES A RANK CONTRIBUTES under a sharded head. The draft needs one, but the gather moves
|
||||
* whole 16-byte words a row and an (int32 id, f32 value) pair is 8 bytes, so R = 1 is refused at
|
||||
* launch ("shape this kernel does not serve") -- the bound rad_block_mtp_hc.h states. Two pairs
|
||||
* are one word; `pair_argmax` reduces the world * 2 of them to the global top-1. */
|
||||
static constexpr int64_t kTopR = 2;
|
||||
|
||||
static bool head_sharded(const Geom& g) { return vocab_head_sharded(g); }
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& c, int base_layer,
|
||||
rad_kvgroup group, rad_weight tok_embd, const char* ck, const Wire& wire);
|
||||
void context(RadCtx* c, const RadBatch* batch) const;
|
||||
void step(RadCtx* c, const RadBatch* batch) const;
|
||||
|
||||
private:
|
||||
int64_t q_dim() const { return g.n_head * g.head_dim; }
|
||||
int64_t kv_dim() const { return g.n_head_kv * g.head_dim; }
|
||||
/* The six linears must be what the recipe made them. DenseFP8 declares E4M3 (or MXFP4) views
|
||||
* whatever the container holds, and the loader's complaint about a bf16 weight read through an
|
||||
* fp8 view names the view, not the missing recipe. */
|
||||
static int need_quantised(RadBuilder* b, const char* wn) {
|
||||
RadEncoding e{};
|
||||
if (!weight_enc(b, wn, &e) || e.n_planes <= 0) return RAD_OK; /* nothing to ask yet */
|
||||
const uint32_t dt = e.plane[0].dtype;
|
||||
if (dt == RAD_F8E4M3 || dt == RAD_FP4E2M1) return RAD_OK;
|
||||
fprintf(stderr, "radiance: gemma4: the DFlash drafter's '%s' is stored %s. Its six linears "
|
||||
"are served block fp8 (or MXFP4): convert with a recipe that names them, "
|
||||
"e.g. `dflash.blk.*.attn_q.weight rtn codes=fp8_e4m3 block=128x128 "
|
||||
"scale=bf16` and the same for fc, attn_k/v/output, ffn_gate_up and "
|
||||
"ffn_down.\n", wn, rad_dtype_name(dt));
|
||||
return RAD_E_DTYPE;
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------ declare */
|
||||
|
||||
inline int Dflash1::declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& c,
|
||||
int base_layer, rad_kvgroup group, rad_weight tok_embd,
|
||||
const char* ck, const Wire& wire) {
|
||||
g = geom; cfg = c; w = wire; first_layer = base_layer; kv = group; w_tok = tok_embd;
|
||||
if (!ck || !w_tok) return RAD_E_INVAL;
|
||||
if (g.n_layer <= 0 || g.n_layer > MAX_LAYERS) {
|
||||
fprintf(stderr, "radiance: gemma4: the drafter has %lld layers; this block carries at most "
|
||||
"%d\n", (long long)g.n_layer, MAX_LAYERS);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (cfg.block < 2 || cfg.n_taps <= 0) return RAD_E_INVAL;
|
||||
|
||||
const int64_t qd = q_dim(), kvd = kv_dim();
|
||||
crows = g.max_tok;
|
||||
qrows = g.max_seqs * cfg.block;
|
||||
if (qrows > g.max_tok) qrows = g.max_tok;
|
||||
srows = g.max_seqs * cfg.steps();
|
||||
if (qrows <= 0 || srows <= 0) return RAD_E_INVAL;
|
||||
sharded = head_sharded(g);
|
||||
|
||||
/* ---------------------------------------------------------------- model-scope weights */
|
||||
RAD_ARCH_TRY(w_fc.declare(b, nm, nm.f("dflash.fc"), g.n_embd, cfg.n_taps * g.n_embd,
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model(),
|
||||
{ nm.ckpt("%s.fc.weight", ck) }));
|
||||
RAD_ARCH_TRY(need_quantised(b, nm.f("dflash.fc.weight")));
|
||||
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)));
|
||||
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());
|
||||
|
||||
/* ---------------------------------------------------------------- per-layer weights */
|
||||
for (int64_t l = 0; l < g.n_layer; ++l) {
|
||||
const int i = (int)l;
|
||||
const RadWeightGroup gl = grp_layer(first_layer + i);
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("dflash.blk.%d.attn_norm.weight", i),
|
||||
nm.ckpt("%s.layers.%d.input_layernorm.weight", ck, i)));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("dflash.blk.%d.ffn_norm.weight", i),
|
||||
nm.ckpt("%s.layers.%d.post_attention_layernorm.weight", ck, i)));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("dflash.blk.%d.attn_q_norm.weight", i),
|
||||
nm.ckpt("%s.layers.%d.self_attn.q_norm.weight", ck, i)));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("dflash.blk.%d.attn_k_norm.weight", i),
|
||||
nm.ckpt("%s.layers.%d.self_attn.k_norm.weight", ck, i)));
|
||||
w_anorm[i] = decl_w(b, nm.f("dflash.blk.%d.attn_norm.weight", i), RAD_F32, {g.n_embd},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, gl);
|
||||
w_fnorm[i] = decl_w(b, nm.f("dflash.blk.%d.ffn_norm.weight", i), RAD_F32, {g.n_embd},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, gl);
|
||||
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, gl);
|
||||
w_kn[i] = decl_w(b, nm.f("dflash.blk.%d.attn_k_norm.weight", i), RAD_F32, {g.head_dim},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, gl);
|
||||
/* The declared names are rad_block_dflash2.h's, deliberately: the recipe that quantises a
|
||||
* DFlash2 drafter's linears names exactly these, so one recipe serves both drafters. */
|
||||
RAD_ARCH_TRY(w_q[i].declare(b, nm, nm.f("dflash.blk.%d.attn_q", i), qd, g.n_embd,
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, gl,
|
||||
{ nm.ckpt("%s.layers.%d.self_attn.q_proj.weight", ck, i) }));
|
||||
RAD_ARCH_TRY(w_k[i].declare(b, nm, nm.f("dflash.blk.%d.attn_k", i), kvd, g.n_embd,
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, gl,
|
||||
{ nm.ckpt("%s.layers.%d.self_attn.k_proj.weight", ck, i) }));
|
||||
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, gl,
|
||||
{ nm.ckpt("%s.layers.%d.self_attn.v_proj.weight", ck, i) }));
|
||||
RAD_ARCH_TRY(w_o[i].declare(b, nm, nm.f("dflash.blk.%d.attn_output", i), g.n_embd, qd,
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_COL, gl,
|
||||
{ nm.ckpt("%s.layers.%d.self_attn.o_proj.weight", ck, i) }));
|
||||
RAD_ARCH_TRY(w_gu[i].declare(b, nm, nm.f("dflash.blk.%d.ffn_gate_up", i), 2 * g.n_ff,
|
||||
g.n_embd, RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, gl,
|
||||
{ nm.ckpt("%s.layers.%d.mlp.gate_proj.weight", ck, i),
|
||||
nm.ckpt("%s.layers.%d.mlp.up_proj.weight", ck, i) },
|
||||
{ g.n_ff, g.n_ff }));
|
||||
RAD_ARCH_TRY(w_dn[i].declare(b, nm, nm.f("dflash.blk.%d.ffn_down", i), g.n_embd, g.n_ff,
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_COL, gl,
|
||||
{ nm.ckpt("%s.layers.%d.mlp.down_proj.weight", ck, i) }));
|
||||
for (const char* s : { "attn_q", "attn_k", "attn_v", "attn_output", "ffn_gate_up",
|
||||
"ffn_down" })
|
||||
RAD_ARCH_TRY(need_quantised(b, nm.f("dflash.blk.%d.%s.weight", i, s)));
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- attention: two kinds
|
||||
* Declared before either kv_store: `block_size` is read back off the resolved kernel. Both
|
||||
* read the one KV group, so whichever resolves first answers for it. */
|
||||
auto attn = [&](int causal, int64_t window) {
|
||||
return 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", causal),
|
||||
RAD_INT("window", 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});
|
||||
};
|
||||
bool any_full = false, any_swa = false;
|
||||
for (int64_t l = 0; l < g.n_layer; ++l) (cfg.full[l] ? any_full : any_swa) = true;
|
||||
if (any_swa) op_attn_swa = attn(1, cfg.window);
|
||||
if (any_full) op_attn_full = attn(0, 0);
|
||||
|
||||
/* ---------------------------------------------------------------- the context pass */
|
||||
op_qsrc = rw(b, RAD_OP(b, "quant_act_fp8",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, crows), RAD_INT("n", cfg.n_taps * g.n_embd),
|
||||
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.src}, {w.srcq, w.srcs});
|
||||
op_cfc = w_fc.gemm(b, crows, g.n_embd, cfg.n_taps * g.n_embd, {w.srcq, w.srcs}, {w.c});
|
||||
op_cnorm = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, crows), RAD_INT("n", g.n_embd),
|
||||
RAD_F64("eps", g.eps), RAD_F64("wadd", 0.0),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_hnorm)),
|
||||
{w.c}, {w.c});
|
||||
op_qc = rw(b, RAD_OP(b, "quant_act_fp8",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, crows), RAD_INT("n", g.n_embd),
|
||||
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.c}, {w.cq, w.cs});
|
||||
for (int64_t l = 0; l < g.n_layer; ++l) {
|
||||
const int i = (int)l;
|
||||
op_ck[i] = w_k[i].gemm(b, crows, kvd, g.n_embd, {w.cq, w.cs}, {w.ck});
|
||||
op_cv[i] = w_v[i].gemm(b, crows, kvd, g.n_embd, {w.cq, w.cs}, {w.cv});
|
||||
op_cknorm[i] = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, crows * g.n_head_kv),
|
||||
RAD_INT("n", g.head_dim), RAD_F64("eps", g.eps),
|
||||
RAD_F64("wadd", 0.0), RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_kn[i])),
|
||||
{w.ck}, {w.ck});
|
||||
}
|
||||
op_crope = rw(b, decl_rotary(b, g, "rope",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, crows), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("n_head", g.n_head_kv), RAD_INT("n_head_kv", 0),
|
||||
RAD_F64("theta", g.theta), RAD_F64("scale", g.rope_scale),
|
||||
RAD_STR("mode", "neox")),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.ck}, {w.ck});
|
||||
op_cstore = rw(b, RAD_OP(b, "kv_store",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, crows), 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.ck, w.cv}, {});
|
||||
|
||||
/* ---------------------------------------------------------------- the query pass */
|
||||
op_embed = rw(b, RAD_OP(b, "embed_lookup",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n_embd", g.n_embd),
|
||||
RAD_INT("n_vocab", g.n_vocab), RAD_STR("dtype", g.dtype),
|
||||
RAD_INT("vocab_offset", g.vocab_off), RAD_F64("mult", cfg.escale)),
|
||||
RAD_WEIGHTS(w_tok)),
|
||||
{}, {w.x});
|
||||
if (g.world > 1)
|
||||
op_embed_ar = rw(b, RAD_OP(b, "all_reduce",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
RAD_RANGE("numel", g.n_embd, qrows * g.n_embd),
|
||||
RAD_STR("dtype", g.dtype), RAD_INT("exact", 1)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.x}, {w.x});
|
||||
op_qh = rw(b, RAD_OP(b, "quant_act_fp8",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n", g.n_embd),
|
||||
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.h}, {w.hq, w.hs});
|
||||
op_qat = rw(b, RAD_OP(b, "quant_act_fp8",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n", qd),
|
||||
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.attn}, {w.atq, w.ats});
|
||||
op_qff = rw(b, RAD_OP(b, "quant_act_fp8",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n", g.n_ff),
|
||||
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.ffn}, {w.ffq, w.ffs});
|
||||
|
||||
for (int64_t l = 0; l < g.n_layer; ++l) {
|
||||
const int i = (int)l;
|
||||
op_anorm[i] = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n", g.n_embd),
|
||||
RAD_F64("eps", g.eps), RAD_F64("wadd", 0.0),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_anorm[i])),
|
||||
{w.x}, {w.h});
|
||||
op_qp[i] = w_q[i].gemm(b, qrows, qd, g.n_embd, {w.hq, w.hs}, {w.q});
|
||||
op_kp[i] = w_k[i].gemm(b, qrows, kvd, g.n_embd, {w.hq, w.hs}, {w.k});
|
||||
op_vp[i] = w_v[i].gemm(b, qrows, kvd, g.n_embd, {w.hq, w.hs}, {w.v});
|
||||
op_qn[i] = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows * g.n_head), RAD_INT("n", g.head_dim),
|
||||
RAD_F64("eps", g.eps), RAD_F64("wadd", 0.0),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_qn[i])),
|
||||
{w.q}, {w.q});
|
||||
op_kn[i] = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows * g.n_head_kv),
|
||||
RAD_INT("n", g.head_dim), RAD_F64("eps", g.eps),
|
||||
RAD_F64("wadd", 0.0), RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_kn[i])),
|
||||
{w.k}, {w.k});
|
||||
op_op[i] = w_o[i].gemm(b, qrows, g.n_embd, qd, {w.atq, w.ats}, {w.h});
|
||||
op_fnorm[i] = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n", g.n_embd),
|
||||
RAD_F64("eps", g.eps), RAD_F64("wadd", 0.0),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_fnorm[i])),
|
||||
{w.x}, {w.h});
|
||||
op_gu[i] = w_gu[i].gemm(b, qrows, 2 * g.n_ff, g.n_embd, {w.hq, w.hs}, {w.gate_up});
|
||||
/* The gate_up|silu|quant fold where it resolves, the pair where it does not --
|
||||
* rad_block_dflash2.h's rule, band included. */
|
||||
{
|
||||
const int64_t hi = qrows < 64 ? qrows : 64;
|
||||
if (hi > 0 && !w_gu[i].mx) {
|
||||
op_gug[i] = rw(b, RAD_OP(b, "gemm_nt_q_gated",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", 2 * g.n_ff),
|
||||
RAD_INT("K", g.n_embd),
|
||||
RAD_INT("group", RAD_FP8_BLOCK),
|
||||
RAD_STR("dtype", "fp8a8"), RAD_STR("act", "silu")),
|
||||
RAD_WEIGHTS(w_gu[i].w, w_gu[i].s)),
|
||||
{w.hq, w.hs}, {w.ffq, w.ffs});
|
||||
if (op_gug[i]) gated_rows = hi;
|
||||
}
|
||||
}
|
||||
op_dn[i] = w_dn[i].gemm(b, qrows, g.n_embd, g.n_ff, {w.ffq, w.ffs}, {w.h});
|
||||
}
|
||||
op_qrope = rw(b, decl_rotary(b, g, "rope",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("n_head", g.n_head), RAD_INT("n_head_kv", 0),
|
||||
RAD_F64("theta", g.theta), RAD_F64("scale", g.rope_scale),
|
||||
RAD_STR("mode", "neox")),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.q}, {w.q});
|
||||
op_krope = rw(b, decl_rotary(b, g, "rope",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("n_head", g.n_head_kv), RAD_INT("n_head_kv", 0),
|
||||
RAD_F64("theta", g.theta), RAD_F64("scale", g.rope_scale),
|
||||
RAD_STR("mode", "neox")),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.k}, {w.k});
|
||||
op_store = rw(b, RAD_OP(b, "kv_store",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), 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}, {});
|
||||
op_act = rw(b, RAD_OP(b, "silu_mul",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n", g.n_ff),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.gate_up}, {w.ffn});
|
||||
if (g.world > 1)
|
||||
op_ar = rw(b, RAD_OP(b, "all_reduce",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
RAD_RANGE("numel", g.n_embd, qrows * g.n_embd),
|
||||
RAD_STR("dtype", g.dtype), RAD_INT("exact", g.wire_exact),
|
||||
RAD_INT("min_bytes", g.wire_min_bytes)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.h}, {w.h});
|
||||
op_add = rw(b, RAD_OP(b, "add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.x, w.h}, {w.x});
|
||||
op_final = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n", g.n_embd),
|
||||
RAD_F64("eps", g.eps), RAD_F64("wadd", 0.0),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_norm)),
|
||||
{w.x}, {w.h});
|
||||
|
||||
/* ---------------------------------------------------------------- the head
|
||||
* The MASK rows -- offsets 1..block-1 of each block; the core names them through out_ids --
|
||||
* gathered contiguous, projected onto THIS RANK's slice of the target's vocabulary, and the
|
||||
* argmax taken. Sharded, each rank's best kTopR are reported as global ids with their logits,
|
||||
* all-gathered, and reduced to the global top-1 by `pair_argmax` (gemma4k), ties to the lower
|
||||
* id -- exact, since the global winner is its own shard's first. */
|
||||
op_gather = rw(b, RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.h}, {w.sel_h});
|
||||
op_head = rw(b, RAD_OP(b, "gemm_nt",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("N", g.n_vocab),
|
||||
RAD_INT("K", g.n_embd), RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_tok)),
|
||||
{w.sel_h}, {w.dlog});
|
||||
if (sharded) {
|
||||
op_topk = rw(b, RAD_OP(b, "row_topk",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("N", g.n_vocab),
|
||||
RAD_INT("R", kTopR), RAD_INT("vocab_off", g.vocab_off),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.dlog}, {w.cand, w.unary, w.dpair});
|
||||
/* ROW-INTERLEAVED, so one draft position's pairs from every rank land side by side. */
|
||||
op_dgather = rw(b, RAD_OP(b, "all_gather",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
RAD_INT("numel", srows * kTopR * 2), RAD_STR("dtype", "f32"),
|
||||
RAD_INT("row", kTopR * 2)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.dpair}, {w.dgath});
|
||||
op_pick = rw(b, RAD_OP(b, "pair_argmax",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("n", g.world * kTopR),
|
||||
RAD_STR("dtype", "f32")),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.dgath}, {w.tokens});
|
||||
if (!op_pick) {
|
||||
fprintf(stderr, "radiance: gemma4: the sharded draft head needs `pair_argmax`, which the "
|
||||
"gemma4k kernel library serves -- load it (--kernels gemma4k,libr4d,...)\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
} else {
|
||||
op_topk = rw(b, RAD_OP(b, "row_topk",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("N", g.n_vocab),
|
||||
RAD_INT("R", 1), RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.dlog}, {w.tokens, w.unary});
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------ the context pass */
|
||||
|
||||
inline void Dflash1::context(RadCtx* c, const RadBatch* batch) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
if (T <= 0) return;
|
||||
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
|
||||
const int32_t* slot = kvb ? kvb->slot_mapping : nullptr;
|
||||
|
||||
RAD_ISSUE_N(c, op_qsrc, T, brows(w.src, T), brows(w.srcq, T), brows(w.srcs, T));
|
||||
RAD_ISSUE_N(c, op_cfc, T, brows(w.srcq, T), brows(w.srcs, T), RAD_W(w_fc.w), RAD_W(w_fc.s),
|
||||
brows(w.c, T), RAD_NONE, w_fc.bref());
|
||||
RAD_ISSUE_N(c, op_cnorm, T, brows(w.c, T), RAD_W(w_hnorm), brows(w.c, T));
|
||||
RAD_ISSUE_N(c, op_qc, T, brows(w.c, T), brows(w.cq, T), brows(w.cs, T));
|
||||
for (int64_t l = 0; l < g.n_layer; ++l) {
|
||||
const int i = (int)l;
|
||||
RAD_ISSUE_N(c, op_ck[i], T, brows(w.cq, T), brows(w.cs, T),
|
||||
RAD_W(w_k[i].w), RAD_W(w_k[i].s), brows(w.ck, T), RAD_NONE, w_k[i].bref());
|
||||
RAD_ISSUE_N(c, op_cv[i], T, brows(w.cq, T), brows(w.cs, T),
|
||||
RAD_W(w_v[i].w), RAD_W(w_v[i].s), brows(w.cv, T), RAD_NONE, w_v[i].bref());
|
||||
RAD_ISSUE_N(c, op_cknorm[i], T * g.n_head_kv, brows(w.ck, T), RAD_W(w_kn[i]),
|
||||
brows(w.ck, T));
|
||||
RAD_ISSUE_N(c, op_crope, T, brows(w.ck, T), praw(batch->positions, RAD_I32, T));
|
||||
RAD_ISSUE_N(c, op_cstore, T, brows(w.ck, T), brows(w.cv, T), praw(slot, RAD_I32, T),
|
||||
kv_cache(kv, first_layer + i));
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------ the query pass */
|
||||
|
||||
inline void Dflash1::step(RadCtx* c, const RadBatch* batch) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
if (T <= 0) return;
|
||||
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
|
||||
const int32_t* slot = kvb ? kvb->slot_mapping : nullptr;
|
||||
const int32_t* table = kvb ? kvb->block_table : nullptr;
|
||||
const int32_t* used = kvb ? kvb->seqused : nullptr;
|
||||
|
||||
RAD_ISSUE_N(c, op_embed, T, praw(batch->token_ids, RAD_I32, T), RAD_W(w_tok), brows(w.x, T));
|
||||
if (op_embed_ar)
|
||||
RAD_ISSUE_N(c, op_embed_ar, (int64_t)T * g.n_embd, brows(w.x, T), RAD_NONE);
|
||||
|
||||
for (int64_t l = 0; l < g.n_layer; ++l) {
|
||||
const int i = (int)l;
|
||||
const rad_op op_attn = cfg.full[i] ? op_attn_full : op_attn_swa;
|
||||
|
||||
RAD_ISSUE_N(c, op_anorm[i], T, brows(w.x, T), RAD_W(w_anorm[i]), brows(w.h, T));
|
||||
RAD_ISSUE_N(c, op_qh, T, brows(w.h, T), brows(w.hq, T), brows(w.hs, T));
|
||||
RAD_ISSUE_N(c, op_qp[i], T, brows(w.hq, T), brows(w.hs, T),
|
||||
RAD_W(w_q[i].w), RAD_W(w_q[i].s), brows(w.q, T), RAD_NONE, w_q[i].bref());
|
||||
RAD_ISSUE_N(c, op_kp[i], T, brows(w.hq, T), brows(w.hs, T),
|
||||
RAD_W(w_k[i].w), RAD_W(w_k[i].s), brows(w.k, T), RAD_NONE, w_k[i].bref());
|
||||
RAD_ISSUE_N(c, op_vp[i], T, brows(w.hq, T), brows(w.hs, T),
|
||||
RAD_W(w_v[i].w), RAD_W(w_v[i].s), brows(w.v, T), RAD_NONE, w_v[i].bref());
|
||||
RAD_ISSUE_N(c, op_qn[i], T * g.n_head, brows(w.q, T), RAD_W(w_qn[i]), brows(w.q, T));
|
||||
RAD_ISSUE_N(c, op_kn[i], T * g.n_head_kv, brows(w.k, T), RAD_W(w_kn[i]), brows(w.k, T));
|
||||
RAD_ISSUE_N(c, op_qrope, T, brows(w.q, T), praw(batch->positions, RAD_I32, T));
|
||||
RAD_ISSUE_N(c, op_krope, T, brows(w.k, T), praw(batch->positions, RAD_I32, T));
|
||||
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));
|
||||
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());
|
||||
if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE);
|
||||
RAD_ISSUE_N(c, op_add, T, brows(w.x, T), brows(w.h, T), brows(w.x, T));
|
||||
|
||||
RAD_ISSUE_N(c, op_fnorm[i], T, brows(w.x, T), RAD_W(w_fnorm[i]), brows(w.h, T));
|
||||
RAD_ISSUE_N(c, op_qh, T, brows(w.h, T), brows(w.hq, T), brows(w.hs, T));
|
||||
if (op_gug[i] && T <= gated_rows) {
|
||||
RAD_ISSUE_N(c, op_gug[i], T, brows(w.hq, T), brows(w.hs, T),
|
||||
RAD_W(w_gu[i].w), RAD_W(w_gu[i].s), brows(w.ffq, T), brows(w.ffs, T),
|
||||
RAD_NONE);
|
||||
} else {
|
||||
RAD_ISSUE_N(c, op_gu[i], T, brows(w.hq, T), brows(w.hs, T),
|
||||
RAD_W(w_gu[i].w), RAD_W(w_gu[i].s), brows(w.gate_up, T), RAD_NONE,
|
||||
w_gu[i].bref());
|
||||
RAD_ISSUE_N(c, op_act, T, brows(w.gate_up, T), brows(w.ffn, T));
|
||||
RAD_ISSUE_N(c, op_qff, T, brows(w.ffn, T), brows(w.ffq, T), brows(w.ffs, T));
|
||||
}
|
||||
RAD_ISSUE_N(c, op_dn[i], T, brows(w.ffq, T), brows(w.ffs, T),
|
||||
RAD_W(w_dn[i].w), RAD_W(w_dn[i].s), brows(w.h, T), RAD_NONE, w_dn[i].bref());
|
||||
if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE);
|
||||
RAD_ISSUE_N(c, op_add, T, brows(w.x, T), brows(w.h, T), brows(w.x, T));
|
||||
}
|
||||
RAD_ISSUE_N(c, op_final, T, brows(w.x, T), RAD_W(w_norm), brows(w.h, T));
|
||||
|
||||
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));
|
||||
RAD_ISSUE_N(c, op_head, S, brows(w.sel_h, S), RAD_W(w_tok), brows(w.dlog, S));
|
||||
if (sharded) {
|
||||
RAD_ISSUE_N(c, op_topk, S, brows(w.dlog, S), brows(w.cand, S), brows(w.unary, S),
|
||||
brows(w.dpair, S));
|
||||
RAD_ISSUE(c, op_dgather, brows(w.dpair, S), brows(w.dgath, S));
|
||||
RAD_ISSUE_N(c, op_pick, S, brows(w.dgath, S), brows(w.tokens, S), RAD_NONE);
|
||||
} else {
|
||||
RAD_ISSUE_N(c, op_topk, S, brows(w.dlog, S), brows(w.tokens, S), brows(w.unary, S),
|
||||
RAD_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace gemma4 */
|
||||
|
||||
#endif /* GEMMA4_DFLASH_H */
|
||||
@@ -320,6 +320,8 @@ extern "C" void* rad_buf_ptr (RadCtx* c, rad_buf b) { (void)c; return (void
|
||||
#include "../arch/llama_fp8/llama_fp8.cpp"
|
||||
/* Gemma 4 dense: two attention classes (sliding-window + proportional-RoPE global) on one stream. */
|
||||
#include "../arch/gemma4_bf16/gemma4_bf16.cpp"
|
||||
/* And the bf16 / fp8-expert Gemma 4 plugin: the 26B-A4B's dense and routed arms side by side. */
|
||||
#include "../arch/gemma4/gemma4.cpp"
|
||||
/* The PLE block on its own: no architecture in this file declares it, and what its step() hands
|
||||
* the two windowed ops is a contract of its own (rad_block_ple.h). */
|
||||
#include <arch/rad_block_ple.h>
|
||||
@@ -3374,3 +3376,251 @@ TEST(a_gemma4_decode_step_runs_both_classes_in_order) {
|
||||
CHECK_EQ(i.n, (int64_t)batch.n_out);
|
||||
}
|
||||
}
|
||||
|
||||
/* ===================== gemma4: the 26B-A4B, a dense and a routed arm side by side ===================== */
|
||||
|
||||
namespace {
|
||||
|
||||
/* gemma-4-26B-A4B-it's text geometry cut to SIX layers -- five sliding then one full, the model's
|
||||
* own 5:1 -- with the routed arm on. The keys are the checkpoint's (text_config flattened), which
|
||||
* is what arch/gemma4 reads, and quant is empty: the container this plugin claims. */
|
||||
const char* k_g4moe_keys[] = {
|
||||
"hidden_size", "num_attention_heads", "num_key_value_heads", "head_dim", "global_head_dim",
|
||||
"num_global_key_value_heads", "attention_k_eq_v", "vocab_size", "intermediate_size",
|
||||
"rms_norm_eps", "sliding_window", "final_logit_softcapping", "hidden_activation", "layer_types",
|
||||
"rope_parameters.sliding_attention.rope_theta", "rope_parameters.sliding_attention.rope_type",
|
||||
"rope_parameters.full_attention.rope_theta", "rope_parameters.full_attention.rope_type",
|
||||
"rope_parameters.full_attention.partial_rotary_factor", "enable_moe_block", "num_experts",
|
||||
"top_k_experts", "moe_intermediate_size",
|
||||
};
|
||||
const char* k_g4moe_vals[] = {
|
||||
"2816", "16", "8", "256", "512",
|
||||
"2", "true", "262144", "2112",
|
||||
"1e-06", "1024", "30.0", "gelu_pytorch_tanh",
|
||||
"[\"sliding_attention\", \"sliding_attention\", \"sliding_attention\", \"sliding_attention\", "
|
||||
"\"sliding_attention\", \"full_attention\"]",
|
||||
"10000.0", "default",
|
||||
"1000000.0", "proportional",
|
||||
"0.25", "true", "128",
|
||||
"8", "704",
|
||||
};
|
||||
static_assert(sizeof(k_g4moe_keys) == sizeof(k_g4moe_vals), "one value a key");
|
||||
|
||||
RadModelMeta gemma4_moe_meta() {
|
||||
RadModelMeta m{};
|
||||
m.arch_id = "gemma4";
|
||||
m.name = "test-gemma4-26b-a4b";
|
||||
m.quant = "";
|
||||
m.n_layers = 6;
|
||||
m.n_embd = 2816;
|
||||
m.n_head = 16;
|
||||
m.n_head_kv = 8;
|
||||
m.head_dim = 256;
|
||||
m.n_ff = 2112;
|
||||
m.n_vocab = 262144;
|
||||
m.n_ctx_train = 262144;
|
||||
m.rms_eps = 1e-6f;
|
||||
m.n_kv = (int)(sizeof(k_g4moe_keys) / sizeof(k_g4moe_keys[0]));
|
||||
m.kv_key = k_g4moe_keys;
|
||||
m.kv_val = k_g4moe_vals;
|
||||
return m;
|
||||
}
|
||||
|
||||
int count_resolved(const RadBuilder& b, const std::string& name) {
|
||||
int n = 0;
|
||||
for (const auto& o : b.ops) if (o.op == name && o.resolved) ++n;
|
||||
return n;
|
||||
}
|
||||
|
||||
int count_issued(const std::vector<std::string>& v, const std::string& name) {
|
||||
return (int)std::count(v.begin(), v.end(), name);
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
TEST(a_gemma4_moe_trunk_declares_both_arms_and_the_joins) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_moe_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4::declare(&b, &meta, &ctx));
|
||||
|
||||
CHECK_EQ(count_op(b, "attn_paged"), 6);
|
||||
CHECK_EQ(count_op(b, "kv_store"), 6);
|
||||
/* One join after attention and one after the two feed-forward arms, a layer. */
|
||||
CHECK_EQ(count_resolved(b, "norm_add_norm"), 6);
|
||||
CHECK_EQ(count_resolved(b, "join_add_scale"), 6);
|
||||
/* The routed arm: experts served bf16 where the container holds no encoding for them. */
|
||||
CHECK(count_op(b, "moe_gemm") > 0);
|
||||
CHECK_EQ(count_op(b, "moe_gemm_q"), 0);
|
||||
CHECK_EQ(count_op(b, "logit_softcap"), 1);
|
||||
CHECK_EQ(count_op(b, "all_reduce"), 0); /* world 1 */
|
||||
|
||||
for (const auto& o : b.ops)
|
||||
if (o.resolved) CHECK(o.uses_declared);
|
||||
for (const auto& w : b.weights)
|
||||
CHECK(find_map(b, w.name) != nullptr);
|
||||
}
|
||||
|
||||
/* THE SLIDING GROUP IS FULL, with the window on the attention op: a WINDOW group is not
|
||||
* prefix-cached, and FULL keeps a hit (arch/gemma4/gemma4.cpp, the KV groups). */
|
||||
TEST(gemma4_moe_kv_groups_are_both_full_and_the_window_rides_the_op) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_moe_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4::declare(&b, &meta, &ctx));
|
||||
|
||||
REQUIRE((int)b.kv_groups.size() == 2);
|
||||
for (const auto& g : b.kv_groups) {
|
||||
CHECK_EQ((int)g.d.kind, RAD_KV_FULL);
|
||||
CHECK_EQ(g.d.window, 0);
|
||||
}
|
||||
const RecOp* sl = find_op(b, "attn_paged", 0);
|
||||
REQUIRE(sl != nullptr);
|
||||
CHECK_EQ(pint(sl, "head_dim"), 256);
|
||||
CHECK_EQ(pint(sl, "window"), 1024);
|
||||
const RecOp* fu = find_op(b, "attn_paged", 5);
|
||||
REQUIRE(fu != nullptr);
|
||||
CHECK_EQ(pint(fu, "head_dim"), 512);
|
||||
CHECK_EQ(pint(fu, "gqa"), 8);
|
||||
CHECK_EQ(pint(fu, "window"), 0);
|
||||
}
|
||||
|
||||
/* PROPORTIONAL ROPE IS NOT A PARTIAL ROTARY: the full layers rotate the whole 512-wide head as a
|
||||
* NeoX pair (j, j + 256), with every frequency past pair 63 divided away by the YaRN ramp. A
|
||||
* rotary_dim of 128 would pair (j, j + 64) -- fluent, and wrong. */
|
||||
TEST(gemma4_full_rope_is_the_whole_head_with_the_frequencies_past_64_pairs_cut) {
|
||||
RadBuilder b;
|
||||
RadModelMeta meta = gemma4_moe_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4::declare(&b, &meta, &ctx));
|
||||
|
||||
int full = 0;
|
||||
for (const auto& o : b.ops) {
|
||||
if (o.op != "rope" || pint(&o, "head_dim") != 512) continue;
|
||||
++full;
|
||||
CHECK_EQ(pint(&o, "rotary_dim"), 512);
|
||||
CHECK_EQ(pstr(&o, "mode"), std::string("neox"));
|
||||
CHECK_NEAR(pflt(&o, "theta"), 1e6, 1.0);
|
||||
CHECK_NEAR(pflt(&o, "yarn_low"), 63.5, 1e-9);
|
||||
CHECK_NEAR(pflt(&o, "yarn_high"), 64.0, 1e-9);
|
||||
CHECK(pflt(&o, "yarn_factor") >= 1e30);
|
||||
}
|
||||
CHECK(full > 0);
|
||||
}
|
||||
|
||||
/* Where no kernel library serves a join, the plugin declares the rmsnorm / add chain instead and
|
||||
* issues that -- and libref has no row for the joins precisely so that this path stays reachable
|
||||
* (docs/OPS.md). */
|
||||
TEST(gemma4_falls_back_to_the_chain_where_no_join_resolves) {
|
||||
std::vector<std::string> fused, chain;
|
||||
for (int refuse = 0; refuse < 2; ++refuse) {
|
||||
RadBuilder b;
|
||||
if (refuse) { b.refuse.insert("norm_add_norm"); b.refuse.insert("join_add_scale"); }
|
||||
RadModelMeta meta = gemma4_moe_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
CHECK_OK(gemma4::declare(&b, &meta, &ctx));
|
||||
|
||||
RadCtx c; c.b = &b; c.rank = 0; c.world = 1;
|
||||
RadBatch batch = decode_batch();
|
||||
c.batch = &batch;
|
||||
gemma4::step(&c, &batch);
|
||||
CHECK(c.step_fail.empty());
|
||||
(refuse ? chain : fused) = issued_ops(b, c);
|
||||
}
|
||||
CHECK_EQ(count_issued(fused, "norm_add_norm"), 6);
|
||||
CHECK_EQ(count_issued(fused, "join_add_scale"), 6);
|
||||
CHECK_EQ(count_issued(chain, "norm_add_norm"), 0);
|
||||
CHECK_EQ(count_issued(chain, "join_add_scale"), 0);
|
||||
CHECK_EQ(count_issued(fused, "attn_paged"), 6);
|
||||
CHECK_EQ(count_issued(chain, "attn_paged"), 6);
|
||||
/* The chain is longer, by the norms and adds the joins fold in. */
|
||||
CHECK(count_issued(chain, "rmsnorm") > count_issued(fused, "rmsnorm"));
|
||||
CHECK(count_issued(chain, "add") > count_issued(fused, "add"));
|
||||
}
|
||||
|
||||
/* Both Gemma 4 plugins state the model's reply format, and it is one declaration. */
|
||||
TEST(both_gemma4_plugins_export_one_reply_format) {
|
||||
RadModelMeta moe = gemma4_moe_meta(), dense = gemma4_meta();
|
||||
CHECK(gemma4::chat_format(&moe) == rad::arch::gemma4_chat_format());
|
||||
CHECK(gemma4_bf16::chat_format(&dense) == rad::arch::gemma4_chat_format());
|
||||
}
|
||||
|
||||
/* ================== gemma4 + DFlash: the drafter's taps, live where they are written ================== */
|
||||
|
||||
namespace {
|
||||
|
||||
/* The MoE trunk above with z-lab's DFlash drafter merged: five layers, four sliding and one full,
|
||||
* reading the trunk at layers 1, 3 and 5. */
|
||||
std::vector<const char*> g4df_keys() {
|
||||
std::vector<const char*> k(std::begin(k_g4moe_keys), std::end(k_g4moe_keys));
|
||||
for (const char* s : { "draft.architectures", "draft.num_hidden_layers", "draft.head_dim",
|
||||
"draft.num_attention_heads", "draft.num_key_value_heads",
|
||||
"draft.intermediate_size", "draft.rms_norm_eps", "draft.rope_theta",
|
||||
"draft.hidden_size", "draft.vocab_size", "draft.block_size",
|
||||
"draft.dflash_config.target_layer_ids",
|
||||
"draft.dflash_config.mask_token_id", "draft.layer_types",
|
||||
"draft.sliding_window" })
|
||||
k.push_back(s);
|
||||
return k;
|
||||
}
|
||||
std::vector<const char*> g4df_vals() {
|
||||
std::vector<const char*> v(std::begin(k_g4moe_vals), std::end(k_g4moe_vals));
|
||||
for (const char* s : { "[\"DFlashDraftModel\"]", "5", "128", "32", "8", "5632", "1e-06",
|
||||
"1000000.0", "2816", "262144", "16", "[1, 3, 5]", "4",
|
||||
"[\"sliding_attention\", \"sliding_attention\", \"sliding_attention\", "
|
||||
"\"sliding_attention\", \"full_attention\"]",
|
||||
"2048" })
|
||||
v.push_back(s);
|
||||
return v;
|
||||
}
|
||||
|
||||
int buf_named(const RadBuilder& b, const char* name) {
|
||||
for (size_t i = 0; i < b.bufs.size(); ++i)
|
||||
if (b.bufs[i].name == name) return (int)i + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* THE TAP IS DECLARED WHERE IT IS ISSUED. The buffer planner takes liveness from declaration order,
|
||||
* so `dflash_src` is live from its first DECLARED write. A tap op declared once after the whole
|
||||
* trunk -- and issued inside it, at each tap layer -- made the buffer look born at the end, and the
|
||||
* plan was free to put it on top of `moe_edn`, which every MoE layer writes. At --max-num-seqs 16
|
||||
* it did: every layer after a tap overwrote what the drafter reads, and acceptance fell to 2% with
|
||||
* the reply still fluent. The two ranges must overlap, so no plan can share them. */
|
||||
TEST(gemma4_dflash_taps_are_live_from_the_layer_that_writes_them) {
|
||||
const std::vector<const char*> keys = g4df_keys(), vals = g4df_vals();
|
||||
RadModelMeta meta = gemma4_moe_meta();
|
||||
meta.n_kv = (int)keys.size();
|
||||
meta.kv_key = keys.data();
|
||||
meta.kv_val = vals.data();
|
||||
RadBuilder b;
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
ctx.max_spec = 15;
|
||||
CHECK_OK(gemma4::declare(&b, &meta, &ctx));
|
||||
|
||||
const int src = buf_named(b, "dflash_src"), edn = buf_named(b, "moe_edn");
|
||||
REQUIRE(src > 0 && edn > 0);
|
||||
int first_src = -1, last_edn = -1;
|
||||
for (size_t i = 0; i < b.ops.size(); ++i)
|
||||
for (rad_buf w : b.ops[i].writes) {
|
||||
if ((int)w == src && first_src < 0) first_src = (int)i;
|
||||
if ((int)w == edn) last_edn = (int)i;
|
||||
}
|
||||
REQUIRE(first_src >= 0 && last_edn >= 0);
|
||||
CHECK(first_src < last_edn);
|
||||
|
||||
/* And a trunk step writes each tap once: three taps, three copies into `dflash_src`. */
|
||||
RadCtx c; c.b = &b; c.rank = 0; c.world = 1;
|
||||
RadBatch batch = decode_batch();
|
||||
c.batch = &batch;
|
||||
gemma4::step(&c, &batch);
|
||||
CHECK(c.step_fail.empty());
|
||||
int taps = 0;
|
||||
for (const auto& i : c.issues) {
|
||||
const RecOp* o = b.op_of(i.op);
|
||||
if (o && std::find(o->writes.begin(), o->writes.end(), (rad_buf)src) != o->writes.end())
|
||||
++taps;
|
||||
}
|
||||
CHECK_EQ(taps, 3);
|
||||
}
|
||||
Reference in new issue
Block a user