Archived
arch, data/recipes, libr4d: the 31B's prefill overlap, q|k|v as one linear, RedHatAI's DFlash2, its fc split
The 31B's prefill steps of 1024 rows or more run as two slices with each slice's all-reduces beside the other's compute; q, k and v as one linear where the container holds them so; the 31B serves RedHatAI's DFlash2 speculator; DFlash2's fc projection is split over the ranks, bit for bit. Squashed from: - arch/gemma4_bf16: a prefill step in two slices, each slice's reductions on the second lane - arch/gemma4_bf16: q, k and v as one linear where the container holds them so - data/recipes: the 31B -df recipes make q, k and v one linear (blk.*.attn_qkv) - arch/gemma4: the 31B serves RedHatAI's DFlash2 speculator (gemma-4-31B-it-speculator.dflash2) - libr4d: gemm_nt_q takes split_n, and all_gather lands rows at a wider output pitch - arch/common: DFlash2's fc split over the ranks as windows of block-aligned rows, bit for bit Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bdb37d5d16
commit
04e5480387
21 files changed
+1805
-199
No files matched your search
+24
-11
@@ -1262,17 +1262,30 @@ inline const RadKVGroupBatch* kv_batch(const RadBatch* b, rad_kvgroup group) {
|
||||
* layer, head, feed-forward, norm and rotary geometry of the container's `draft.` config, split
|
||||
* over the ranks, and a plain RMS norm. A drafter is a second model, so nothing is defaulted: a
|
||||
* config that does not describe one is refused, naming `what` (spec §17). So is a vocabulary
|
||||
* other than the target's, since a drafter reads the target's embedding and lm_head. */
|
||||
* other than the target's, since a drafter reads the target's embedding and lm_head.
|
||||
*
|
||||
* The layer config sits at the top of `draft.` in z-lab's and SpecForge's checkpoints, and under
|
||||
* `draft.transformer_layer_config.` in vLLM's speculators format (RedHatAI's DFlash2 drafters),
|
||||
* whose top level holds the speculator's own keys instead (drafter_layer_prefix). */
|
||||
inline const char* drafter_layer_prefix(const RadModelMeta* meta) {
|
||||
if (rad_meta_geti(meta, "draft.num_hidden_layers", 0) > 0) return "draft.";
|
||||
if (rad_meta_geti(meta, "draft.transformer_layer_config.num_hidden_layers", 0) > 0)
|
||||
return "draft.transformer_layer_config.";
|
||||
return "draft.";
|
||||
}
|
||||
inline int drafter_geom(Geom& dg, const Geom& target, const RadModelMeta* meta, const char* what) {
|
||||
dg = target;
|
||||
dg.n_layer = rad_meta_geti(meta, "draft.num_hidden_layers", 0);
|
||||
dg.head_dim = rad_meta_geti(meta, "draft.head_dim", 0);
|
||||
dg.n_head_all = rad_meta_geti(meta, "draft.num_attention_heads", 0);
|
||||
dg.n_head_kv_all = rad_meta_geti(meta, "draft.num_key_value_heads", 0);
|
||||
dg.n_ff_all = rad_meta_geti(meta, "draft.intermediate_size", 0);
|
||||
dg.eps = (float)rad_meta_getf(meta, "draft.rms_norm_eps", 0.0);
|
||||
dg.theta = (float)rad_meta_getf(meta, "draft.rope_parameters.rope_theta",
|
||||
rad_meta_getf(meta, "draft.rope_theta", 0.0));
|
||||
const char* pre = drafter_layer_prefix(meta);
|
||||
char key[160];
|
||||
auto k = [&](const char* name) { std::snprintf(key, sizeof key, "%s%s", pre, name); return key; };
|
||||
dg.n_layer = rad_meta_geti(meta, k("num_hidden_layers"), 0);
|
||||
dg.head_dim = rad_meta_geti(meta, k("head_dim"), 0);
|
||||
dg.n_head_all = rad_meta_geti(meta, k("num_attention_heads"), 0);
|
||||
dg.n_head_kv_all = rad_meta_geti(meta, k("num_key_value_heads"), 0);
|
||||
dg.n_ff_all = rad_meta_geti(meta, k("intermediate_size"), 0);
|
||||
dg.eps = (float)rad_meta_getf(meta, k("rms_norm_eps"), 0.0);
|
||||
const double th = rad_meta_getf(meta, k("rope_theta"), 0.0);
|
||||
dg.theta = (float)rad_meta_getf(meta, k("rope_parameters.rope_theta"), th);
|
||||
dg.wadd = dg.wadd_qk = 0.0f;
|
||||
if (dg.n_layer <= 0 || dg.head_dim <= 0 || dg.n_head_all <= 0 || dg.n_head_kv_all <= 0 ||
|
||||
dg.n_ff_all <= 0 || !(dg.eps > 0.0f) || !(dg.theta > 0.0f)) {
|
||||
@@ -1283,11 +1296,11 @@ inline int drafter_geom(Geom& dg, const Geom& target, const RadModelMeta* meta,
|
||||
(long long)dg.head_dim, (long long)dg.n_ff_all, (double)dg.eps, (double)dg.theta);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
RAD_ARCH_TRY(yarn_from(dg, meta, "draft.", dg.head_dim));
|
||||
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));
|
||||
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, "draft.vocab_size", dg.n_vocab_all);
|
||||
const long long dvocab = rad_meta_geti(meta, k("vocab_size"), dg.n_vocab_all);
|
||||
if (dvocab != dg.n_vocab_all) {
|
||||
fprintf(stderr, "radiance: the %s drafter claims a %lld-token vocabulary and the target "
|
||||
"has %lld; it reads the target's embedding and lm_head\n",
|
||||
|
||||
+119
-21
@@ -69,10 +69,21 @@
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
* kept bit for bit. Each rank hands over an equal share of c's columns
|
||||
* (n_embd / world) and computes the fewest whole 128-row scale blocks that cover
|
||||
* it (the 31B at tp 4: 1344 columns a rank inside 11 blocks, 1408 rows, against
|
||||
* 42 blocks whole), a row span of the one stored weight
|
||||
* (DenseFP8::declare_slice), so no reconvert. The GEMM takes its K split from
|
||||
* the whole width (OPS.md's `split_n`), so each output is summed as the unsplit
|
||||
* fc sums it, and the all-gather reads each rank's window out of its wider rows
|
||||
* (an input pitch) and writes c whole. A GEMM cannot write a column slice (the
|
||||
* narrow fp8 kernel's store is tight), which is why the windows are the
|
||||
* gather's to cut. One rank, or an fc off block fp8, keeps it whole.
|
||||
*
|
||||
* Replicated:
|
||||
*
|
||||
* fc [n_embd, n_taps*n_embd], 262 MiB. Column-sharding would halve it but add an
|
||||
* all-reduce to the prefill path.
|
||||
* conv weights projection and base kernel, 26 MiB a layer. The convolution spans the whole
|
||||
* replicated hidden row, so every rank needs every coefficient.
|
||||
* codebooks [n_vocab, 256], 127 MiB each, gathered by global token id. Only 128 rows are
|
||||
@@ -127,6 +138,10 @@ struct Dflash2 {
|
||||
/* the context pass */
|
||||
rad_buf src = 0; /* [max_tok, n_taps * n_embd] the trunk's taps, read only */
|
||||
rad_buf c = 0; /* [max_tok, n_embd] fc's output, then normed in place */
|
||||
/* [max_tok, fc_rows] the block-aligned rows of a split fc this rank computes, whose
|
||||
* fc_share-wide window at fc_off is gathered into c. The block declares it (only when it
|
||||
* splits fc); leave it 0. */
|
||||
rad_buf cpart = 0;
|
||||
/* K and V are separate buffers because of the per-head k norm: `rmsnorm` takes one row
|
||||
* pitch, and K heads inside a fused [K|V] row are head_dim apart within a token but
|
||||
* 2*kv_dim apart between tokens. libr4d refuses such an operand (libr4d/r4d_model.cpp,
|
||||
@@ -191,10 +206,17 @@ struct Dflash2 {
|
||||
* MXFP4 accepts ~1% fewer tokens per step than fp8 and saves ~1.5 ms of a ~34 ms step.
|
||||
* Quantisation can only change acceptance, since every draft is verified. */
|
||||
DenseFP8 w_fc{};
|
||||
/* fc split by rows (see SHARDING): w_fc is this rank's fc_rows block-aligned rows, holding its
|
||||
* fc_share output columns at row fc_off. fc_rows 0: w_fc is the whole fc, replicated. */
|
||||
int64_t fc_share = 0, fc_rows = 0, fc_off = 0;
|
||||
rad_weight w_hnorm = 0, w_norm = 0;
|
||||
rad_weight w_selp = 0, w_pred = 0, w_succ = 0;
|
||||
rad_weight w_tok = 0; /* the target's embedding table */
|
||||
rad_weight w_dh = 0, w_dhs = 0; /* the 2-bit copy of the target's lm_head */
|
||||
rad_weight w_dh = 0, w_dhs = 0; /* the 2-bit (or 8-bit) copy of the target's lm_head */
|
||||
/* The copy is int8 with an f16 scale per (row, 128 of K) where the recipe made it so (the
|
||||
* 31B's: the head read at w8a8, so the selector sees the candidates an 8-bit head ranks), and
|
||||
* the 2-bit grid otherwise. Set by declare from the copy's encoding. */
|
||||
bool head_i8 = false;
|
||||
|
||||
/* per-layer weights */
|
||||
rad_weight w_anorm[MAX_LAYERS] = {}, w_fnorm[MAX_LAYERS] = {};
|
||||
@@ -209,6 +231,7 @@ struct Dflash2 {
|
||||
|
||||
/* context-pass ops */
|
||||
rad_op op_cfc = 0, op_cnorm = 0, op_crope = 0, op_cstore = 0;
|
||||
rad_op op_cgath = 0; /* the split fc's all-gather */
|
||||
rad_op op_ck[MAX_LAYERS] = {}, op_cv[MAX_LAYERS] = {}, op_cknorm[MAX_LAYERS] = {};
|
||||
|
||||
/* Activation quantisers, one per activation that feeds a linear. Weightless, so one declaration
|
||||
@@ -284,9 +307,38 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
|
||||
if (qrows <= 0 || srows <= 0) return RAD_E_INVAL;
|
||||
|
||||
/* ---------------------------------------------------------------- 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) }));
|
||||
{
|
||||
const int64_t K = cfg.n_taps * g.n_embd;
|
||||
const char* fwn = nm.f("dflash.fc.weight");
|
||||
RAD_ARCH_TRY(map_linear(b, fwn, { nm.ckpt("%s.fc.weight", ck) }));
|
||||
/* Split only block fp8 (an MXFP4 GEMM's K split is its own), into equal shares of whole
|
||||
* 16-byte words (the gather's unit). */
|
||||
RadEncoding fe{};
|
||||
const RadEncPlane* fsc = nullptr;
|
||||
const bool fp8 = weight_enc(b, fwn, &fe) && rad_enc_is(&fe, "affine") && fe.n_planes == 2 &&
|
||||
!fe.transform[0] && fe.plane[0].dtype == RAD_F8E4M3 &&
|
||||
(fsc = rad_enc_plane(&fe, "scale")) && fsc->block[0] == RAD_FP8_BLOCK &&
|
||||
fsc->block[1] == RAD_FP8_BLOCK;
|
||||
const int64_t share = (g.world > 1 && g.n_embd % RAD_FP8_BLOCK == 0 &&
|
||||
g.n_embd % g.world == 0 && (g.n_embd / g.world) % 8 == 0)
|
||||
? g.n_embd / g.world : 0;
|
||||
if (fp8 && share > 0) {
|
||||
const int64_t B = RAD_FP8_BLOCK;
|
||||
const int64_t lo = share * g.rank / B * B;
|
||||
const int64_t hi = (share * (g.rank + 1) + B - 1) / B * B;
|
||||
fc_share = share;
|
||||
fc_rows = hi - lo;
|
||||
fc_off = share * g.rank - lo;
|
||||
RAD_ARCH_TRY(w_fc.declare_slice(b, nm, nm.f("dflash.fc"), fwn, lo, hi, K,
|
||||
RAD_ACCESS_PER_TOKEN, grp_model()));
|
||||
if (!w.cpart)
|
||||
w.cpart = decl_b(b, nm.f("df_cpart"), g.act_dtype, { g.max_tok, fc_rows });
|
||||
} else {
|
||||
RAD_ARCH_TRY(w_fc.declare(b, nm, nm.f("dflash.fc"), g.n_embd, K, RAD_ACCESS_PER_TOKEN,
|
||||
RAD_SHARD_NONE, grp_model(), { nm.ckpt("%s.fc.weight", ck) },
|
||||
{}, true));
|
||||
}
|
||||
}
|
||||
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"),
|
||||
@@ -321,10 +373,28 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
|
||||
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));
|
||||
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());
|
||||
/* 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;
|
||||
|
||||
/* ---------------------------------------------------------------- per-layer weights */
|
||||
@@ -422,7 +492,21 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
|
||||
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});
|
||||
if (fc_rows) {
|
||||
/* The split (SHARDING above): this rank's block-aligned rows into `cpart`, its window of
|
||||
* them gathered into `c`. The GEMM takes its K split from the whole width. The gather
|
||||
* rides the trunk's all-reduce instance (the same numel bound, so the same instance). */
|
||||
op_cfc = w_fc.gemm(b, crows, fc_rows, cfg.n_taps * g.n_embd, {w.srcq, w.srcs}, {w.cpart},
|
||||
g.n_embd);
|
||||
op_cgath = rw(b, RAD_OP(b, "all_gather",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
RAD_INT("numel", crows * g.n_embd),
|
||||
RAD_STR("dtype", g.dtype), RAD_INT("row", fc_share)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.cpart}, {w.c});
|
||||
} else {
|
||||
op_cfc = w_fc.gemm(b, crows, g.n_embd, cfg.n_taps * g.n_embd, {w.srcq, w.srcs}, {w.c});
|
||||
}
|
||||
/* In place, like the per-head q/k norms below: a row's scale is computed before the row is
|
||||
* written. */
|
||||
op_cnorm = rw(b, RAD_OP(b, "rmsnorm",
|
||||
@@ -643,12 +727,19 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
|
||||
RAD_INT("group", DF_W2_GROUP), RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.sel_h}, {w.dq, w.ds, w.dsum});
|
||||
op_dlogits = rw(b, RAD_OP(b, "gemm_nt_q",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("N", head_rows(g)),
|
||||
RAD_INT("K", g.n_embd), RAD_INT("group", DF_W2_GROUP),
|
||||
RAD_STR("dtype", "w2a8")),
|
||||
RAD_WEIGHTS(w_dh, w_dhs)),
|
||||
{w.dq, w.ds, w.dsum}, {w.dlog});
|
||||
op_dlogits = head_i8
|
||||
? rw(b, RAD_OP(b, "gemm_nt_q",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("N", head_rows(g)),
|
||||
RAD_INT("K", g.n_embd), RAD_INT("group", DF_W2_GROUP),
|
||||
RAD_STR("dtype", "w8a8")),
|
||||
RAD_WEIGHTS(w_dh, w_dhs)),
|
||||
{w.dq, w.ds}, {w.dlog})
|
||||
: rw(b, RAD_OP(b, "gemm_nt_q",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("N", head_rows(g)),
|
||||
RAD_INT("K", g.n_embd), RAD_INT("group", DF_W2_GROUP),
|
||||
RAD_STR("dtype", "w2a8")),
|
||||
RAD_WEIGHTS(w_dh, w_dhs)),
|
||||
{w.dq, w.ds, w.dsum}, {w.dlog});
|
||||
/* Sharded, the top-R runs over this rank's plane, reports global ids and also writes the pair
|
||||
* plane the gather moves. Replicated, `vocab_off` is omitted: an optional parameter present
|
||||
* with value 0 is a different declaration from an absent one. */
|
||||
@@ -731,8 +822,14 @@ inline void Dflash2::context(RadCtx* c, const RadBatch* batch) const {
|
||||
|
||||
/* As wide as the prefill chunk, one launch per op (the fp8a8 pair covers every M). */
|
||||
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());
|
||||
if (fc_rows) {
|
||||
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.cpart, T), RAD_NONE, w_fc.bref());
|
||||
RAD_ISSUE_N(c, op_cgath, T, bcol(w.cpart, fc_off, fc_share, T), brows(w.c, T));
|
||||
} else {
|
||||
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));
|
||||
|
||||
@@ -843,9 +940,10 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
|
||||
* whole. */
|
||||
RAD_ISSUE_N(c, op_dquant, S, brows(w.sel_h, S), brows(w.dq, S), brows(w.ds, S), RAD_B(w.dsum));
|
||||
/* The trailing operand is mxfp4's optional reference, absent here. Operands are positional,
|
||||
* so an absent optional one is RAD_NONE, not a shorter list. */
|
||||
* so an absent optional one is RAD_NONE, not a shorter list. The 8-bit head is symmetric and
|
||||
* takes no asum. */
|
||||
RAD_ISSUE_N(c, op_dlogits, S, brows(w.dq, S), brows(w.ds, S), RAD_W(w_dh), RAD_W(w_dhs),
|
||||
brows(w.dlog, S), RAD_B(w.dsum), RAD_NONE);
|
||||
brows(w.dlog, S), head_i8 ? RAD_NONE : RAD_B(w.dsum), RAD_NONE);
|
||||
/* RAD_NONE fills the unused `pairs` slot. Sharded, this rank's top-R are only candidates and
|
||||
* the merge overwrites `cand`/`unary`. */
|
||||
if (op_dmerge) {
|
||||
|
||||
+34
-2
@@ -1521,15 +1521,47 @@ struct DenseFP8 {
|
||||
int access, int shard, RadWeightGroup grp,
|
||||
std::initializer_list<const char*> src,
|
||||
std::initializer_list<int64_t> row_parts = {}, bool mapped = false);
|
||||
/* `gemm_nt_q` over this weight, `fp8a8` or `mxfp4a8` as it was declared, ranged M 1..hi. */
|
||||
/* Rows [lo, hi) of the stored block-fp8 weight `src_wn` (codes and scale plane; mapped by
|
||||
* whoever declared it), read as an [hi - lo, K] weight named `<base>`: a row slice that need
|
||||
* not be a rank's even share, and may be the same rows on every rank. Whole 128-row blocks,
|
||||
* so each slice's codes and scales are the stored ones. fp8 only: an MXFP4 linear's GEMM
|
||||
* splits K by its own rule. A slice issued with `gemm(..., split_n)` set to the stored width
|
||||
* computes the stored linear's outputs to the bit. */
|
||||
int declare_slice(RadBuilder* b, Names& nm, const char* base, const char* src_wn, int64_t lo,
|
||||
int64_t hi, int64_t K, int access, RadWeightGroup grp) {
|
||||
if (lo < 0 || hi <= lo || lo % RAD_FP8_BLOCK || hi % RAD_FP8_BLOCK || K % RAD_FP8_BLOCK)
|
||||
return RAD_E_INVAL;
|
||||
mx = false;
|
||||
ref = 0;
|
||||
const char* wn = nm.f("%s.weight", base);
|
||||
const char* sn = nm.f("%s.scale", base);
|
||||
const bool own = !std::strcmp(wn, src_wn); /* the stored weight's own name */
|
||||
w = decl_view(b, wn, own ? nullptr : src_wn, "codes", RAD_F8E4M3, { hi - lo, K }, access,
|
||||
RAD_SHARD_ROW, grp);
|
||||
s = decl_view(b, sn, src_wn, "scale", RAD_BF16, { fp8_blocks(hi - lo), fp8_blocks(K) },
|
||||
access, RAD_SHARD_ROW, grp);
|
||||
if (!w || !s) return RAD_E_INVAL;
|
||||
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);
|
||||
}
|
||||
/* `gemm_nt_q` over this weight, `fp8a8` or `mxfp4a8` 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). */
|
||||
rad_op gemm(RadBuilder* b, int64_t hi, int64_t N, int64_t K,
|
||||
std::initializer_list<rad_buf> reads, std::initializer_list<rad_buf> writes) const {
|
||||
std::initializer_list<rad_buf> reads, std::initializer_list<rad_buf> writes,
|
||||
int64_t split_n = 0) const {
|
||||
if (mx)
|
||||
return rw(b, RAD_OP(b, "gemm_nt_q",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
|
||||
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8")),
|
||||
RAD_WEIGHTS(w, s, ref)),
|
||||
reads, writes);
|
||||
if (split_n > 0)
|
||||
return rw(b, RAD_OP(b, "gemm_nt_q",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
|
||||
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "fp8a8"),
|
||||
RAD_INT("split_n", split_n)),
|
||||
RAD_WEIGHTS(w, s)),
|
||||
reads, writes);
|
||||
return rw(b, RAD_OP(b, "gemm_nt_q",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
|
||||
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "fp8a8")),
|
||||
|
||||
@@ -159,6 +159,54 @@ inline bool dflash_probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The same for a DFlash2 drafter, for a plugin that serves rad_block_dflash2.h (the 31B's): its
|
||||
* anchor predicts nothing either, so the depth is the trained block less one, 7 for RedHatAI's
|
||||
* gemma-4-31B-it-speculator.dflash2 (block_size 8). */
|
||||
inline bool dflash2_probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
if (dflash_kind(meta) != 2) return false;
|
||||
const long long bs = dflash_block(meta);
|
||||
if (bs <= 1) return false;
|
||||
out->draft_depth = (int)(bs - 1);
|
||||
out->draft_depth_fixed = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* A DFlash2 drafter's trunk taps, as the layers whose output the trunk copies into `dflash_src`.
|
||||
*
|
||||
* vLLM's speculators format names them `aux_hidden_state_layer_ids`, counted as vLLM captures
|
||||
* them: id i is the residual stream entering layer i, so the output of layer i - 1 (id n_layer,
|
||||
* which `--include-last-layer` emits, is the last layer's output). z-lab's `target_layer_ids` count
|
||||
* the layer whose output is taken. RedHatAI's 1 17 29 47 59 are this trunk's layers 0 16 28 46
|
||||
* 58. Id 0 would be the embedding, which no layer outputs, and is refused. */
|
||||
inline int dflash2_taps(const RadModelMeta* meta, const RadBuildCtx* ctx, int64_t n_layer, int* out,
|
||||
int max) {
|
||||
if (ctx->max_spec <= 0 || dflash_kind(meta) != 2) return 0;
|
||||
const char* aux = rad_meta_gets(meta, "draft.aux_hidden_state_layer_ids", nullptr);
|
||||
int n = 0;
|
||||
if (aux) {
|
||||
int raw[16];
|
||||
const int m = parse_layer_ids(aux, n_layer + 1, raw, max < 16 ? max : 16);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
if (raw[i] < 1) {
|
||||
fprintf(stderr, "radiance: gemma4: the DFlash2 drafter taps aux hidden state 0, the "
|
||||
"embedding, which no trunk layer outputs\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
out[n++] = raw[i] - 1;
|
||||
}
|
||||
} else {
|
||||
const char* ids = rad_meta_gets(meta, "draft.dflash_config.target_layer_ids", nullptr);
|
||||
if (!ids) ids = rad_meta_gets(meta, "draft.target_layer_ids", nullptr);
|
||||
n = parse_layer_ids(ids, n_layer, out, max);
|
||||
}
|
||||
if (n <= 0) {
|
||||
fprintf(stderr, "radiance: gemma4: the DFlash2 drafter names no trunk layer this model has; "
|
||||
"it is paired with a different target\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* ============================================================== the block */
|
||||
|
||||
struct Dflash1 {
|
||||
|
||||
@@ -18,8 +18,10 @@
|
||||
#include <arch/rad_block_gemma4_vision.h>
|
||||
#include <arch/rad_gemma4_chat.h>
|
||||
#include <arch/rad_gemma4_dflash.h>
|
||||
#include <arch/rad_block_dflash2.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
@@ -117,8 +119,24 @@ struct G4Linear {
|
||||
void step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const;
|
||||
/* the GEMM alone, for a caller that wrote the int8 pair itself (a fused norm + quantiser) */
|
||||
void gemm(RadCtx* c, rad_buf y, int64_t T) const;
|
||||
/* The same two over rows [r0, r0 + n) of the step: a slice of the prefill overlap. The
|
||||
* quantiser and the GEMM are a row at a time above their bands, so a slice's rows are the
|
||||
* whole step's (see "the prefill overlap" below). */
|
||||
void step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int64_t n) const;
|
||||
void gemm_rows(RadCtx* c, rad_buf y, int64_t r0, int64_t n) const;
|
||||
};
|
||||
|
||||
/* A weight on the w4a8 / w8a8 grid: i4 or i8 codes with an f16 scale per [1 x 128], the encoding
|
||||
* gemm_nt_q reads against quant_act_i8's pair. */
|
||||
static bool g4_grid_w4(const RadEncoding& enc) {
|
||||
const RadEncPlane* sc = rad_enc_plane(&enc, "scale");
|
||||
const uint32_t cdt = enc.n_planes > 0 ? enc.plane[0].dtype : 0;
|
||||
return rad_enc_is(&enc, "affine") && enc.n_planes == 2 && !enc.transform[0] &&
|
||||
(cdt == RAD_I4 || cdt == RAD_I8) && enc.plane[0].block[1] == 1 &&
|
||||
sc && sc->dtype == RAD_F16 && sc->block[0] == 1 && sc->block[1] == 128;
|
||||
}
|
||||
|
||||
/* An empty `src` means the caller made the weight's name map (a probe asked it first). */
|
||||
inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char* base,
|
||||
std::initializer_list<const char*> src,
|
||||
int64_t n_out, int64_t n_in, int shard, RadWeightGroup grp,
|
||||
@@ -126,10 +144,10 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char
|
||||
std::initializer_list<int64_t> row_parts,
|
||||
const G4Linear* in_from) {
|
||||
N = n_out; K = n_in;
|
||||
if (N <= 0 || K <= 0 || src.size() == 0) return RAD_E_INVAL;
|
||||
if (N <= 0 || K <= 0) return RAD_E_INVAL;
|
||||
const char* wn = nm.f("%s.weight", base);
|
||||
if (src.size() == 1) RAD_ARCH_TRY(map_copy(b, wn, *src.begin()));
|
||||
else RAD_ARCH_TRY(map_concat(b, wn, src, 0));
|
||||
if (src.size() == 1) RAD_ARCH_TRY(map_copy(b, wn, *src.begin()));
|
||||
else if (src.size() > 1) RAD_ARCH_TRY(map_concat(b, wn, src, 0));
|
||||
RadEncoding enc{};
|
||||
const bool known = weight_enc(b, wn, &enc);
|
||||
/* A builder with no model behind it (a test, a synthetic tool) answers RAD_E_NOTFOUND, and
|
||||
@@ -153,16 +171,11 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char
|
||||
/* A w4 weight: i4 codes and an f16 scale per [1 x 128]. gemm_nt_q at w4a8 reads an int8
|
||||
* activation and an f32 per-row scale, so a quantiser over the bf16 plane `a` writes the int8
|
||||
* pair (q8, s8). The codes + scale layout is the same as w4a16's, so containers serve
|
||||
* unchanged. */
|
||||
const RadEncPlane* sc = rad_enc_plane(&enc, "scale");
|
||||
/* An 8-bit weight on the same grid (i8 codes, f16 scale per [1 x 128]): gemm_nt_q at w8a8
|
||||
* reads the same int8 activation and per-row scale as w4a8, so the quantiser is shared. A
|
||||
* recipe uses it for linears int4 hurts (attention, down_proj). */
|
||||
* unchanged. An 8-bit weight on the same grid (i8 codes, f16 scale per [1 x 128]): gemm_nt_q
|
||||
* at w8a8 reads the same int8 activation and per-row scale as w4a8, so the quantiser is
|
||||
* shared. A recipe uses it for linears int4 hurts (attention, down_proj). */
|
||||
const uint32_t cdt = known && enc.n_planes > 0 ? enc.plane[0].dtype : 0;
|
||||
const bool w4 = known && rad_enc_is(&enc, "affine") && enc.n_planes == 2 && !enc.transform[0] &&
|
||||
(cdt == RAD_I4 || cdt == RAD_I8) && enc.plane[0].block[1] == 1 &&
|
||||
sc && sc->dtype == RAD_F16 && sc->block[0] == 1 && sc->block[1] == 128;
|
||||
if (w4) {
|
||||
if (known && g4_grid_w4(enc)) {
|
||||
if (N % 16 || K % 128) {
|
||||
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; a w4 weight takes rows in "
|
||||
"16s and K in whole 128-column scale groups.\n", base,
|
||||
@@ -225,9 +238,27 @@ inline void G4Linear::gemm(RadCtx* c, rad_buf y, int64_t T) const {
|
||||
brows(y, T), RAD_NONE, RAD_NONE);
|
||||
}
|
||||
|
||||
inline void G4Linear::step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int64_t n) const {
|
||||
if (!quant) {
|
||||
RAD_ISSUE_N(c, op, n, brow_slice(a, r0, n, K), RAD_W(w), brow_slice(y, r0, n, N));
|
||||
return;
|
||||
}
|
||||
if (!shared_in)
|
||||
RAD_ISSUE_N(c, op_q, n, brow_slice(a, r0, n, K), brow_slice(q8, r0, n, K),
|
||||
brow_slice(s8, r0, n, 1), RAD_NONE);
|
||||
gemm_rows(c, y, r0, n);
|
||||
}
|
||||
|
||||
inline void G4Linear::gemm_rows(RadCtx* c, rad_buf y, int64_t r0, int64_t n) const {
|
||||
RAD_ISSUE_N(c, op, n, brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, 1), RAD_W(w), RAD_W(ws),
|
||||
brow_slice(y, r0, n, N), RAD_NONE, RAD_NONE);
|
||||
}
|
||||
|
||||
/* ---- one attention block, parameterised by class. */
|
||||
struct Gemma4Attn {
|
||||
struct Wire { rad_buf x = 0, h = 0, q = 0, k = 0, v = 0, attn = 0; };
|
||||
/* `qkv` is the fused projection's row, [max_tok, q | k | v], declared by the first layer of its
|
||||
* class that has one (the model's slot passed to declare). */
|
||||
struct Wire { rad_buf x = 0, h = 0, q = 0, k = 0, v = 0, attn = 0, qkv = 0; };
|
||||
|
||||
Wire w{};
|
||||
Geom g{};
|
||||
@@ -237,23 +268,46 @@ struct Gemma4Attn {
|
||||
|
||||
rad_weight w_norm = 0, w_qn = 0, w_kn = 0, w_post = 0; /* the norms (always bf16) */
|
||||
G4Linear ln_q{}, ln_k{}, ln_v{}, ln_o{}; /* the GEMM weights (bf16 or w4a8) */
|
||||
/* q, k and v as one linear, where the container holds them so (blk.N.attn_qkv): one GEMM a
|
||||
* layer instead of three. q, k and v are then column slices of `w.qkv`. */
|
||||
G4Linear ln_qkv{};
|
||||
bool fused = false;
|
||||
rad_op op_norm = 0, op_vn = 0, op_qn = 0, op_kn = 0,
|
||||
op_rq = 0, op_rk = 0, op_attn = 0, op_kv = 0, op_post = 0, op_ar = 0, op_add = 0;
|
||||
/* the five prologue ops (v norm, q norm, k norm, two rotaries) as one, where it resolves */
|
||||
rad_op op_qnr = 0;
|
||||
/* Fused only: attention reading q where it lies in the row (attn_paged_qrow), where it resolves
|
||||
* to the kernel attn_paged would take anyway; else the copy of q into its own rows (`cast`) that
|
||||
* a step of more than one row issues ahead of attn_paged. Without qkv_norm_rope the copies of
|
||||
* k and v too, ahead of the unfused prologue, which reads them dense. */
|
||||
rad_op op_attn_row = 0, op_lift_q = 0, op_lift_k = 0, op_lift_v = 0;
|
||||
|
||||
/* The linear whose int8 pair a junction writes, and whether every projection reads it. */
|
||||
const G4Linear& ln_in() const { return fused ? ln_qkv : ln_q; }
|
||||
bool reads_pair() const {
|
||||
return fused ? ln_qkv.quant
|
||||
: ln_q.quant && ln_k.shared_in && (!cls.has_v || ln_v.shared_in);
|
||||
}
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const ClassGeom& cgeo,
|
||||
rad_kvgroup group, const Wire& wire, const char* pre);
|
||||
rad_kvgroup group, const Wire& wire, const char* pre, rad_buf* qkv_slot);
|
||||
/* `entry_done`: a junction op already wrote this block's quantised input (ln_q's pair).
|
||||
* `tail_fused`: the caller issues the post-norm and the add as a junction op. */
|
||||
void step(RadCtx* c, const RadBatch* batch, bool entry_done = false, bool tail_fused = false,
|
||||
bool ar_fused = false) const;
|
||||
/* step()'s parts, for the prefill overlap: the projections over rows [r0, r0 + n) from the
|
||||
* pair a junction wrote, everything between the projections and o_proj over the whole step
|
||||
* (it reads every row's keys), and o_proj over rows [r0, r0 + n). */
|
||||
void project(RadCtx* c, int64_t r0, int64_t n) const;
|
||||
void attend(RadCtx* c, const RadBatch* batch) const;
|
||||
void out_proj(RadCtx* c, int64_t r0, int64_t n) const;
|
||||
};
|
||||
|
||||
int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const ClassGeom& cgeo,
|
||||
rad_kvgroup group, const Wire& wire, const char* pre) {
|
||||
rad_kvgroup group, const Wire& wire, const char* pre, rad_buf* qkv_slot) {
|
||||
g = geom; cls = cgeo; layer = l; kv = group; w = wire;
|
||||
const int64_t qd = cls.q_dim(), kvd = cls.kv_dim();
|
||||
const int64_t qkvd = qd + kvd + (cls.has_v ? kvd : 0);
|
||||
|
||||
/* The norms: always bf16 (no rule matches them), name map first. */
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.attn_norm.weight", l), nm.ckpt("%s.%lld.input_layernorm.weight", pre, (long long)l)));
|
||||
@@ -271,18 +325,50 @@ int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
|
||||
w_post = decl_w(b, nm.f("blk.%d.attn_post_norm.weight", l), RAD_BF16, {g.n_embd},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
|
||||
/* The GEMM weights: bf16 or w4a8, probed by G4Linear. */
|
||||
RAD_ARCH_TRY(ln_q.declare(b, nm, g, nm.f("blk.%d.attn_q", l),
|
||||
{ nm.ckpt("%s.%lld.self_attn.q_proj.weight", pre, (long long)l) },
|
||||
qd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.q));
|
||||
const G4Linear* qin = &ln_q; /* k and v read q's int8 pair */
|
||||
RAD_ARCH_TRY(ln_k.declare(b, nm, g, nm.f("blk.%d.attn_k", l),
|
||||
{ nm.ckpt("%s.%lld.self_attn.k_proj.weight", pre, (long long)l) },
|
||||
kvd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.k, {}, qin));
|
||||
if (cls.has_v)
|
||||
RAD_ARCH_TRY(ln_v.declare(b, nm, g, nm.f("blk.%d.attn_v", l),
|
||||
{ nm.ckpt("%s.%lld.self_attn.v_proj.weight", pre, (long long)l) },
|
||||
kvd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.v, {}, qin));
|
||||
/* The GEMM weights: bf16 or w4a8, probed by G4Linear.
|
||||
*
|
||||
* q, k and v are one linear, `blk.N.attn_qkv` (q | k | v, or q | k on a k_eq_v class), where
|
||||
* the weight is on the w4a8 / w8a8 grid: a recipe that names it (rad-convert answers the
|
||||
* probe from the recipe) or a container converted with one. Each rank takes its slice of each
|
||||
* part, so the row holds this rank's q, k and v heads. The rows are q's, k's and v's rows, and
|
||||
* the grid's groups never cross a row, so the codes are the three linears' codes (libr4d's
|
||||
* skinny GEMM picks its K split from N, so on a card k and v may round differently in the last
|
||||
* bit). One GEMM a layer reads the three weights in one pass where three paid the skinny GEMM's
|
||||
* fixed cost each: on the R9700 at decode (--profile-ops, tp 4) q, k and v take 17 + 13.5 +
|
||||
* 13.5 us and a 4096-row GEMM of the same K 25 us. A container that holds the three apart, or
|
||||
* bf16 weights, keeps three linears reading q's int8 pair. */
|
||||
const char* q_src = nm.ckpt("%s.%lld.self_attn.q_proj.weight", pre, (long long)l);
|
||||
const char* k_src = nm.ckpt("%s.%lld.self_attn.k_proj.weight", pre, (long long)l);
|
||||
const char* v_src = nm.ckpt("%s.%lld.self_attn.v_proj.weight", pre, (long long)l);
|
||||
const char* qkv_wn = nm.f("blk.%d.attn_qkv.weight", l);
|
||||
if (cls.has_v) RAD_ARCH_TRY(map_concat(b, qkv_wn, { q_src, k_src, v_src }));
|
||||
else RAD_ARCH_TRY(map_concat(b, qkv_wn, { q_src, k_src }));
|
||||
{
|
||||
RadEncoding e{};
|
||||
fused = weight_enc(b, qkv_wn, &e) && g4_grid_w4(e);
|
||||
}
|
||||
if (fused) {
|
||||
if (!qkv_slot) return RAD_E_INVAL;
|
||||
if (!*qkv_slot)
|
||||
*qkv_slot = decl_b(b, nm.f(cls.has_v ? "attn_qkvs" : "attn_qkvg"), g.act_dtype,
|
||||
{g.max_tok, qkvd});
|
||||
w.qkv = *qkv_slot;
|
||||
if (cls.has_v)
|
||||
RAD_ARCH_TRY(ln_qkv.declare(b, nm, g, nm.f("blk.%d.attn_qkv", l), {}, qkvd, g.n_embd,
|
||||
RAD_SHARD_ROW, grp_layer(l), w.h, w.qkv, {qd, kvd, kvd}));
|
||||
else
|
||||
RAD_ARCH_TRY(ln_qkv.declare(b, nm, g, nm.f("blk.%d.attn_qkv", l), {}, qkvd, g.n_embd,
|
||||
RAD_SHARD_ROW, grp_layer(l), w.h, w.qkv, {qd, kvd}));
|
||||
} else {
|
||||
RAD_ARCH_TRY(ln_q.declare(b, nm, g, nm.f("blk.%d.attn_q", l), { q_src },
|
||||
qd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.q));
|
||||
const G4Linear* qin = &ln_q; /* k and v read q's int8 pair */
|
||||
RAD_ARCH_TRY(ln_k.declare(b, nm, g, nm.f("blk.%d.attn_k", l), { k_src },
|
||||
kvd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.k, {}, qin));
|
||||
if (cls.has_v)
|
||||
RAD_ARCH_TRY(ln_v.declare(b, nm, g, nm.f("blk.%d.attn_v", l), { v_src },
|
||||
kvd, g.n_embd, RAD_SHARD_ROW, grp_layer(l), w.h, w.v, {}, qin));
|
||||
}
|
||||
RAD_ARCH_TRY(ln_o.declare(b, nm, g, nm.f("blk.%d.attn_o", l),
|
||||
{ nm.ckpt("%s.%lld.self_attn.o_proj.weight", pre, (long long)l) },
|
||||
g.n_embd, qd, RAD_SHARD_COL, grp_layer(l), w.attn, w.h));
|
||||
@@ -340,22 +426,27 @@ int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
|
||||
/* Gemma 4 scores Q.K without the 1/sqrt(head_dim) factor (HF Gemma4TextAttention and vLLM
|
||||
* use scaling = 1.0; the q/k norm gains absorb it). attn_paged defaults to 1/sqrt(head_dim),
|
||||
* so 1.0 is passed explicitly. */
|
||||
op_attn = rw(b, RAD_OP(b, "attn_paged",
|
||||
RAD_PARAMS(RAD_RANGE("q_len", 1, g.max_tok), RAD_INT("head_dim", cls.head_dim),
|
||||
RAD_INT("gqa", cls.gqa()), RAD_INT("causal", 1), RAD_INT("window", cls.window),
|
||||
RAD_INT("n_head", cls.n_head), RAD_INT("block_size", RAD_KV_BLOCK),
|
||||
RAD_INT("max_seqs", g.max_seqs), RAD_INT("max_ctx", g.max_ctx),
|
||||
RAD_F64("scale", 1.0),
|
||||
RAD_STR("q_dtype", g.dtype), RAD_STR("kv_dtype", g.kv_dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.q}, {w.attn});
|
||||
auto attn = [&](const char* op, std::initializer_list<rad_buf> rd) {
|
||||
return rw(b, RAD_OP(b, op,
|
||||
RAD_PARAMS(RAD_RANGE("q_len", 1, g.max_tok), RAD_INT("head_dim", cls.head_dim),
|
||||
RAD_INT("gqa", cls.gqa()), RAD_INT("causal", 1),
|
||||
RAD_INT("window", cls.window), RAD_INT("n_head", cls.n_head),
|
||||
RAD_INT("block_size", RAD_KV_BLOCK),
|
||||
RAD_INT("max_seqs", g.max_seqs), RAD_INT("max_ctx", g.max_ctx),
|
||||
RAD_F64("scale", 1.0),
|
||||
RAD_STR("q_dtype", g.dtype), RAD_STR("kv_dtype", g.kv_dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
rd, {w.attn});
|
||||
};
|
||||
|
||||
/* The prologue as one op: libr4d's `qkv_norm_rope` is rmsnorm_p + rmsnorm + rmsnorm + rope +
|
||||
* rope over the three projections, byte-identical, one workgroup per (token, head). Declared
|
||||
* beside the unfused five and used only where it resolves; a multi-component or YaRN rotary
|
||||
* keeps the five, as does an engine whose libraries do not declare it (CPU-only), where asking
|
||||
* for it would stop the declare. */
|
||||
if (!g.rope_mc && g.yarn.factor == 0.0f && rad_op_declared(b, "qkv_norm_rope")) {
|
||||
* for it would stop the declare. It reads q, k and v at their own row pitches, so on a fused
|
||||
* layer it works in place on the row's column slices. */
|
||||
auto decl_qnr = [&]() {
|
||||
if (g.rope_mc || g.yarn.factor != 0.0f || !rad_op_declared(b, "qkv_norm_rope")) return;
|
||||
op_qnr = RAD_OP(b, "qkv_norm_rope",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("head_dim", cls.head_dim),
|
||||
RAD_INT("n_head", cls.n_head), RAD_INT("n_head_kv", cls.n_head_kv),
|
||||
@@ -367,17 +458,60 @@ int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_qn, w_kn));
|
||||
/* A k_eq_v class derives v from the raw k: v is written, never read. */
|
||||
op_qnr = cls.has_v ? rw(b, op_qnr, {w.q, w.k, w.v}, {w.q, w.k, w.v})
|
||||
: rw(b, op_qnr, {w.q, w.k}, {w.q, w.k, w.v});
|
||||
if (fused)
|
||||
op_qnr = cls.has_v ? rw(b, op_qnr, {w.qkv}, {w.qkv})
|
||||
: rw(b, op_qnr, {w.qkv}, {w.qkv, w.v});
|
||||
else
|
||||
op_qnr = cls.has_v ? rw(b, op_qnr, {w.q, w.k, w.v}, {w.q, w.k, w.v})
|
||||
: rw(b, op_qnr, {w.q, w.k}, {w.q, w.k, w.v});
|
||||
};
|
||||
|
||||
/* A column slice of the fused row copied into its own rows (`cast`, bf16 to bf16, which every
|
||||
* library takes with pitched operands). */
|
||||
auto lift = [&](int64_t n, rad_buf dst) {
|
||||
return rw(b, RAD_OP(b, "cast",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", n),
|
||||
RAD_STR("from", g.dtype), RAD_STR("to", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.qkv}, {dst});
|
||||
};
|
||||
|
||||
if (!fused) {
|
||||
op_attn = attn("attn_paged", {w.q});
|
||||
decl_qnr();
|
||||
} else {
|
||||
/* The prologue first: what attention reads depends on it. Without it the unfused five read
|
||||
* q, k and v dense, so all three are copied out of the row ahead of them. */
|
||||
decl_qnr();
|
||||
if (!op_qnr) {
|
||||
op_lift_k = lift(kvd, w.k);
|
||||
if (cls.has_v) op_lift_v = lift(kvd, w.v);
|
||||
}
|
||||
/* With it attention reads q in the row where that costs nothing. libr4d's dedicated paged
|
||||
* kernels stop at head_dim 256; at 512 (the global class) attn_paged takes the windowed
|
||||
* kernel, which is the kernel attn_paged_qrow takes, so the row is read in place. At 256 the
|
||||
* dedicated h256 kernels serve attn_paged in 0.26-0.71 of the windowed kernel's time
|
||||
* (libr4d/r4d_plugin.h, R4D_PRIO_ATTN_WINDOW), so the sliding class keeps attn_paged: a step
|
||||
* of one row reads q in place (one row of a slice is dense), a taller one copies q out. */
|
||||
if (op_qnr && cls.head_dim > 256 && rad_op_declared(b, "attn_paged_qrow"))
|
||||
op_attn_row = attn("attn_paged_qrow", {w.qkv});
|
||||
if (!op_attn_row) {
|
||||
op_lift_q = lift(qd, w.q);
|
||||
op_attn = attn("attn_paged", {w.q, w.qkv});
|
||||
}
|
||||
}
|
||||
|
||||
op_kv = rw(b, RAD_OP(b, "kv_store",
|
||||
/* On a fused layer with the one-op prologue, k and v are stored from the row (kv_store takes
|
||||
* them at their own pitches); a k_eq_v class's v is its own buffer either way. */
|
||||
op_kv = RAD_OP(b, "kv_store",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("head_dim", cls.head_dim),
|
||||
RAD_INT("n_head_kv", cls.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}, {});
|
||||
RAD_NOWEIGHTS);
|
||||
if (!(fused && op_qnr)) op_kv = rw(b, op_kv, {w.k, w.v}, {});
|
||||
else if (cls.has_v) op_kv = rw(b, op_kv, {w.qkv}, {});
|
||||
else op_kv = rw(b, op_kv, {w.qkv, w.v}, {});
|
||||
|
||||
/* o_proj is column-sharded: the rank's partial sum is reduced first, then normed. */
|
||||
if (g.world > 1)
|
||||
@@ -404,13 +538,13 @@ int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
|
||||
|
||||
void Gemma4Attn::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool tail_fused,
|
||||
bool ar_fused) const {
|
||||
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;
|
||||
const int64_t T = batch->n_tok;
|
||||
|
||||
if (entry_done) {
|
||||
if (fused) {
|
||||
if (!entry_done) RAD_ISSUE(c, op_norm, brows(w.x, T), RAD_W(w_norm), brows(w.h, T));
|
||||
if (entry_done) ln_qkv.gemm(c, w.qkv, T);
|
||||
else ln_qkv.step(c, w.h, w.qkv, T);
|
||||
} else if (entry_done) {
|
||||
ln_q.gemm(c, w.q, T);
|
||||
ln_k.gemm(c, w.k, T);
|
||||
if (cls.has_v) ln_v.gemm(c, w.v, T);
|
||||
@@ -420,11 +554,53 @@ void Gemma4Attn::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool ta
|
||||
ln_k.step(c, w.h, w.k, T);
|
||||
if (cls.has_v) ln_v.step(c, w.h, w.v, T);
|
||||
}
|
||||
attend(c, batch);
|
||||
|
||||
ln_o.step(c, w.attn, w.h, T);
|
||||
/* The all-reduce comes before the post-norm: o_proj is column-sharded, so each rank holds a
|
||||
* partial sum, and the norm is of the whole output (sum_r rmsnorm(p_r) != rmsnorm(sum_r p_r)). */
|
||||
if (op_ar && !(tail_fused && ar_fused)) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE);
|
||||
if (tail_fused) return;
|
||||
RAD_ISSUE_N(c, op_post, T, brows(w.h, T), RAD_W(w_post), brows(w.h, T));
|
||||
RAD_ISSUE(c, op_add, brows(w.x, T), brows(w.h, T), brows(w.x, T));
|
||||
}
|
||||
|
||||
void Gemma4Attn::project(RadCtx* c, int64_t r0, int64_t n) const {
|
||||
if (fused) {
|
||||
ln_qkv.gemm_rows(c, w.qkv, r0, n);
|
||||
return;
|
||||
}
|
||||
ln_q.gemm_rows(c, w.q, r0, n);
|
||||
ln_k.gemm_rows(c, w.k, r0, n);
|
||||
if (cls.has_v) ln_v.gemm_rows(c, w.v, r0, n);
|
||||
}
|
||||
|
||||
void Gemma4Attn::out_proj(RadCtx* c, int64_t r0, int64_t n) const {
|
||||
ln_o.step_rows(c, w.attn, w.h, r0, n);
|
||||
}
|
||||
|
||||
void Gemma4Attn::attend(RadCtx* c, const RadBatch* batch) const {
|
||||
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;
|
||||
const int64_t T = batch->n_tok;
|
||||
const int64_t qd = cls.q_dim(), kvd = cls.kv_dim();
|
||||
/* q, k and v where the prologue works on them: their own buffers, or on a fused layer with the
|
||||
* one-op prologue the row's column slices (a k_eq_v class's v is its own buffer either way). */
|
||||
const bool row = fused && op_qnr;
|
||||
const RadOperand Q = row ? bcol(w.qkv, 0, qd, T) : brows(w.q, T);
|
||||
const RadOperand K = row ? bcol(w.qkv, qd, kvd, T) : brows(w.k, T);
|
||||
const RadOperand V = row && cls.has_v ? bcol(w.qkv, qd + kvd, kvd, T) : brows(w.v, T);
|
||||
|
||||
if (op_qnr) {
|
||||
RAD_ISSUE(c, op_qnr, brows(w.q, T), brows(w.k, T), brows(w.v, T), rope_posmc(g, batch, T),
|
||||
RAD_W(w_qn), RAD_W(w_kn));
|
||||
RAD_ISSUE(c, op_qnr, Q, K, V, rope_posmc(g, batch, T), RAD_W(w_qn), RAD_W(w_kn));
|
||||
} else {
|
||||
if (fused) { /* the unfused five read q, k and v dense */
|
||||
RAD_ISSUE_N(c, op_lift_q, T, bcol(w.qkv, 0, qd, T), brows(w.q, T));
|
||||
RAD_ISSUE_N(c, op_lift_k, T, bcol(w.qkv, qd, kvd, T), brows(w.k, T));
|
||||
if (cls.has_v) RAD_ISSUE_N(c, op_lift_v, T, bcol(w.qkv, qd + kvd, kvd, T), brows(w.v, T));
|
||||
}
|
||||
/* Normalise v first: on a k_eq_v class v derives from the raw k, before k_norm rewrites it. */
|
||||
RAD_ISSUE_N(c, op_vn, T * cls.n_head_kv, brows(cls.has_v ? w.v : w.k, T), brows(w.v, T));
|
||||
RAD_ISSUE_N(c, op_qn, T * cls.n_head, brows(w.q, T), RAD_W(w_qn), brows(w.q, T));
|
||||
@@ -434,23 +610,25 @@ void Gemma4Attn::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool ta
|
||||
RAD_ISSUE(c, op_rk, brows(w.k, T), rope_posmc(g, batch, T));
|
||||
}
|
||||
|
||||
RAD_ISSUE(c, op_kv, brows(w.k, T), brows(w.v, T), praw(slot, RAD_I32, T), kv_cache(kv, layer));
|
||||
RAD_ISSUE(c, op_kv, K, V, praw(slot, RAD_I32, T), kv_cache(kv, layer));
|
||||
|
||||
RAD_ISSUE_N(c, op_attn, batch->max_q_len,
|
||||
brows(w.q, T), kv_cache(kv, layer),
|
||||
/* attn_paged reads q dense: one row of the row's slice is, a taller step copies q out. */
|
||||
rad_op a_op = op_attn;
|
||||
RadOperand a_q = brows(w.q, T);
|
||||
if (op_attn_row) {
|
||||
a_op = op_attn_row;
|
||||
a_q = Q;
|
||||
} else if (row) {
|
||||
if (T == 1) a_q = Q;
|
||||
else RAD_ISSUE_N(c, op_lift_q, T, Q, brows(w.q, T));
|
||||
}
|
||||
RAD_ISSUE_N(c, a_op, batch->max_q_len,
|
||||
a_q, kv_cache(kv, layer),
|
||||
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));
|
||||
|
||||
ln_o.step(c, w.attn, w.h, T);
|
||||
/* The all-reduce comes before the post-norm: o_proj is column-sharded, so each rank holds a
|
||||
* partial sum, and the norm is of the whole output (sum_r rmsnorm(p_r) != rmsnorm(sum_r p_r)). */
|
||||
if (op_ar && !(tail_fused && ar_fused)) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE);
|
||||
if (tail_fused) return;
|
||||
RAD_ISSUE_N(c, op_post, T, brows(w.h, T), RAD_W(w_post), brows(w.h, T));
|
||||
RAD_ISSUE(c, op_add, brows(w.x, T), brows(w.h, T), brows(w.x, T));
|
||||
}
|
||||
|
||||
/* ---- the feed-forward: GELU (not SwiGLU), with a second norm on the down-projection output. */
|
||||
@@ -469,8 +647,32 @@ struct Gemma4Mlp {
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire, const char* pre);
|
||||
void step(RadCtx* c, const RadBatch* batch, bool entry_done = false, bool tail_fused = false,
|
||||
bool ar_fused = false) const;
|
||||
/* For the prefill overlap, over rows [r0, r0 + n): the arm from the pair a junction wrote to
|
||||
* down_proj's partial in `h`, and the post-norm and add the last layer takes unfused. */
|
||||
void arm(RadCtx* c, int64_t r0, int64_t n) const;
|
||||
void tail(RadCtx* c, int64_t r0, int64_t n) const;
|
||||
};
|
||||
|
||||
void Gemma4Mlp::arm(RadCtx* c, int64_t r0, int64_t n) const {
|
||||
const int64_t F = g.n_ff;
|
||||
ln_gu.gemm_rows(c, w.gu, r0, n);
|
||||
if (op_gq) {
|
||||
RAD_ISSUE_N(c, op_gq, n, brow_slice(w.gu, r0, n, 2 * F), brow_slice(ln_down.q8, r0, n, F),
|
||||
brow_slice(ln_down.s8, r0, n, 1), RAD_NONE);
|
||||
ln_down.gemm_rows(c, w.h, r0, n);
|
||||
} else {
|
||||
RAD_ISSUE_N(c, op_act, n, brow_slice(w.gu, r0, n, 2 * F), brow_slice(w.ffn, r0, n, F));
|
||||
ln_down.step_rows(c, w.ffn, w.h, r0, n);
|
||||
}
|
||||
}
|
||||
|
||||
void Gemma4Mlp::tail(RadCtx* c, int64_t r0, int64_t n) const {
|
||||
const int64_t E = g.n_embd;
|
||||
RAD_ISSUE_N(c, op_post, n, brow_slice(w.h, r0, n, E), RAD_W(w_post), brow_slice(w.h, r0, n, E));
|
||||
RAD_ISSUE_N(c, op_add, n, brow_slice(w.x, r0, n, E), brow_slice(w.h, r0, n, E),
|
||||
brow_slice(w.x, r0, n, E));
|
||||
}
|
||||
|
||||
int Gemma4Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire, const char* pre) {
|
||||
g = geom; layer = l; w = wire;
|
||||
|
||||
@@ -627,6 +829,13 @@ struct G4Junction {
|
||||
brows(q8, T), brows(s8, T), RAD_NONE,
|
||||
wp ? RAD_W(wp) : RAD_NONE, sc ? RAD_W(sc) : RAD_NONE);
|
||||
}
|
||||
/* Rows [r0, r0 + n), without the all-reduce: a slice of the prefill overlap, whose reduction
|
||||
* rode the second lane. */
|
||||
void step_rows(RadCtx* c, int64_t E, int64_t r0, int64_t n) const {
|
||||
RAD_ISSUE_N(c, op, n, brow_slice(x, r0, n, E), res ? brow_slice(res, r0, n, E) : RAD_NONE,
|
||||
RAD_W(w), brow_slice(q8, r0, n, E), brow_slice(s8, r0, n, 1), RAD_NONE,
|
||||
wp ? RAD_W(wp) : RAD_NONE, sc ? RAD_W(sc) : RAD_NONE);
|
||||
}
|
||||
};
|
||||
|
||||
/* ==================================================================== the assistant head
|
||||
@@ -1088,6 +1297,13 @@ void Gemma4Head::step(RadCtx* c, const RadBatch* batch) const {
|
||||
ln_post.step(c, b_hd, b_hn, T);
|
||||
}
|
||||
|
||||
/* The prefill overlap's smallest slice (trunk_overlap). It is above every row band the sliced
|
||||
* kernels have, with room to spare: the int8 GEMMs' skinny band (64) and r4d_act_threads' last
|
||||
* width change at these widths (120 rows), which fixes the norms' and the quantisers'
|
||||
* sum-of-squares partition. And it is large enough that a slice's reduction is worth a second
|
||||
* lane. */
|
||||
static constexpr int64_t kOverlapRows = 512;
|
||||
|
||||
struct Layer {
|
||||
bool global = false;
|
||||
Gemma4Attn attn{};
|
||||
@@ -1115,6 +1331,7 @@ struct Model {
|
||||
rad_buf b_x = 0, b_h = 0, b_hout = 0, b_logits = 0;
|
||||
rad_buf b_qs = 0, b_ks = 0, b_vs = 0, b_attns = 0; /* sliding rows */
|
||||
rad_buf b_qg = 0, b_kg = 0, b_vg = 0, b_atting = 0; /* global rows */
|
||||
rad_buf b_qkvs = 0, b_qkvg = 0; /* the fused projections' rows, where a layer has one */
|
||||
rad_buf b_gu = 0, b_ffn = 0;
|
||||
|
||||
rad_weight w_tok = 0, w_out = 0, w_lm = 0;
|
||||
@@ -1136,6 +1353,9 @@ struct Model {
|
||||
bool fuse = false;
|
||||
bool fuse_ar = false; /* every junction also carries the all-reduce in front of it */
|
||||
int64_t ar_rows = 0; /* ...in a step of at most this many rows (G4Junction::ar_rows) */
|
||||
/* A prefill step of 2 * kOverlapRows rows or more runs as two slices, each slice's
|
||||
* all-reduces on the second lane beside the other's compute (trunk_overlap). Set at declare. */
|
||||
bool overlap = false;
|
||||
|
||||
/* z-lab's DFlash drafter (arch/common/rad_gemma4_dflash.h), declared only when the container
|
||||
* carries one and the deployment speculates: the trunk layers it taps, the [max_tok, taps x
|
||||
@@ -1147,10 +1367,214 @@ struct Model {
|
||||
gemma4::Dflash1 df{};
|
||||
rad_kvgroup kv_draft = 0;
|
||||
bool have_df = false;
|
||||
/* Or a DFlash2 drafter (arch/common/rad_block_dflash2.h): RedHatAI's speculators-format one
|
||||
* for the 31B. Same taps and plane; its own block, buffers and KV group. */
|
||||
Dflash2 df2{};
|
||||
Dflash2::Wire df2w{};
|
||||
bool have_df2 = false;
|
||||
};
|
||||
|
||||
static Model g_model[MAX_RANKS];
|
||||
|
||||
/* A DFlash2 drafter's config key, from vLLM's speculators layout (the speculator's keys at the
|
||||
* top of `draft.`) or z-lab's (under `draft.dflash_config.`), whichever the checkpoint has. */
|
||||
static long long df2_geti(const RadModelMeta* meta, const char* key, long long dflt) {
|
||||
char k[160];
|
||||
std::snprintf(k, sizeof k, "draft.%s", key);
|
||||
const long long v = rad_meta_geti(meta, k, LLONG_MIN);
|
||||
if (v != LLONG_MIN) return v;
|
||||
std::snprintf(k, sizeof k, "draft.dflash_config.%s", key);
|
||||
return rad_meta_geti(meta, k, dflt);
|
||||
}
|
||||
|
||||
/* The DFlash2 drafter: RedHatAI/gemma-4-31B-it-speculator.dflash2, a speculators-format DFlash2
|
||||
* (z-lab's architecture, trained by Red Hat on gemma-4-31B-it's outputs) in place of z-lab's
|
||||
* DFlash. rad_block_dflash2.h is the block; this reads its config and wires it to the trunk.
|
||||
*
|
||||
* From the checkpoint and speculators' code (models/dflash2, models/dflash/core.py):
|
||||
*
|
||||
* - five Qwen3-shaped layers at the trunk's width, every one sliding (2048) and non-causal;
|
||||
* - the target's table as a plain nn.Embedding: no Gemma sqrt(hidden) scale (z-lab's DFlash
|
||||
* goes through the target's scaled module; this one does not), so the block's unscaled
|
||||
* lookup is right;
|
||||
* - the target's head as a plain linear, no soft-cap, which the block's head is;
|
||||
* - taps counted as vLLM captures them, the residual entering a layer (gemma4::dflash2_taps);
|
||||
* - block 8: the anchor predicts nothing, seven drafts (sample_from_anchor false).
|
||||
*
|
||||
* The head reads the 8-bit copy the -df2 recipe makes (rad_block_dflash2.h, head_i8), so the
|
||||
* selector ranks what an 8-bit head proposes. Nothing here is defaulted quietly: a config this
|
||||
* block does not implement is refused by name. */
|
||||
static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx,
|
||||
Model& m) {
|
||||
Names& nm = m.nm;
|
||||
Geom dg{};
|
||||
RAD_ARCH_TRY(drafter_geom(dg, m.g, meta, "gemma4 DFlash2"));
|
||||
const char* lp = drafter_layer_prefix(meta);
|
||||
char key[160];
|
||||
std::snprintf(key, sizeof key, "%shidden_size", lp);
|
||||
if (rad_meta_geti(meta, key, dg.n_embd) != dg.n_embd) {
|
||||
fprintf(stderr, "radiance: gemma4: the DFlash2 drafter is %lld wide and the trunk %lld; its "
|
||||
"fc and residual stream are the trunk's width\n",
|
||||
rad_meta_geti(meta, key, 0), (long long)dg.n_embd);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
std::snprintf(key, sizeof key, "%shidden_act", lp);
|
||||
const char* act = rad_meta_gets(meta, key, "silu");
|
||||
if (std::strcmp(act, "silu")) {
|
||||
fprintf(stderr, "radiance: gemma4: the DFlash2 drafter's MLP uses %s; the block's is silu\n",
|
||||
act);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (df2_geti(meta, "sample_from_anchor", 0)) {
|
||||
fprintf(stderr, "radiance: gemma4: this DFlash2 drafter samples from its anchor row; the "
|
||||
"block's anchor predicts nothing\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
Dflash2::Config dc{};
|
||||
dc.n_taps = m.n_taps;
|
||||
dc.block = ctx->max_spec + 1;
|
||||
dc.taps = df2_geti(meta, "conv_kernel_size", 2);
|
||||
dc.group = df2_geti(meta, "conv_group_size", 16);
|
||||
dc.sel_rank = df2_geti(meta, "selector_rank", 0);
|
||||
dc.sel_topk = df2_geti(meta, "selector_top_k", 0);
|
||||
if (dc.sel_rank <= 0 || dc.sel_topk <= 0) {
|
||||
fprintf(stderr, "radiance: gemma4: the DFlash2 drafter states no selector geometry (rank "
|
||||
"%lld, top_k %lld)\n", (long long)dc.sel_rank, (long long)dc.sel_topk);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
/* Attention: one window for every layer, since the block issues one attention op. A sliding
|
||||
* window non-causal within the block is speculators' sliding_window_non_causal; z-lab's
|
||||
* layout says is_causal. */
|
||||
{
|
||||
std::snprintf(key, sizeof key, "%slayer_types", lp);
|
||||
std::vector<int> sliding;
|
||||
if (const char* lt = rad_meta_gets(meta, key, nullptr))
|
||||
RAD_ARCH_TRY(parse_layer_kinds("gemma4 DFlash2", lt, {"sliding_attention"},
|
||||
"full_attention", &sliding));
|
||||
const bool any_full = std::find(sliding.begin(), sliding.end(), 0) != sliding.end();
|
||||
const bool any_slide = std::find(sliding.begin(), sliding.end(), 1) != sliding.end();
|
||||
if (any_full && any_slide) {
|
||||
fprintf(stderr, "radiance: gemma4: the DFlash2 drafter mixes sliding and full layers; "
|
||||
"the block takes one attention width for all of them\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
std::snprintf(key, sizeof key, "%ssliding_window", lp);
|
||||
const long long win = rad_meta_geti(meta, key, 0);
|
||||
std::snprintf(key, sizeof key, "%suse_sliding_window", lp);
|
||||
if (any_slide || rad_meta_geti(meta, key, 0))
|
||||
dc.window = df2_geti(meta, "swa_window_size", win);
|
||||
const long long nc = rad_meta_geti(meta, "draft.sliding_window_non_causal", -1);
|
||||
dc.causal = nc >= 0 ? (nc ? 0 : 1) : (rad_meta_geti(meta, "draft.is_causal", 0) ? 1 : 0);
|
||||
}
|
||||
|
||||
/* The query block must be a power of two (the convolution masks a row's position with
|
||||
* block_size - 1), and the depth the probe named is the trained one. */
|
||||
const long long trained = gemma4::dflash_block(meta);
|
||||
if (dc.block < 2 || (dc.block & (dc.block - 1))) {
|
||||
fprintf(stderr, "radiance: gemma4: DFlash2's convolution masks the block position with "
|
||||
"block_size - 1, so the query block must be a power of two; %d draft "
|
||||
"tokens make it %lld (trained at %lld)\n",
|
||||
ctx->max_spec, (long long)dc.block, trained);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (trained > 0 && trained != dc.block)
|
||||
rad_note(b, "DFlash2 was trained at block_size %lld (%lld draft tokens) and runs at %lld; "
|
||||
"it accepts less away from its trained block", trained, trained - 1,
|
||||
(long long)dc.block);
|
||||
/* The three scalars rad_block_dflash2.h does not apply (z-lab's names; speculators has none). */
|
||||
if (rad_meta_getf(meta, "draft.dflash_config.input_embedding_scale", 1.0) != 1.0 ||
|
||||
rad_meta_getf(meta, "draft.dflash_config.output_multiplier", 1.0) != 1.0 ||
|
||||
rad_meta_getf(meta, "draft.dflash_config.final_logit_softcapping", 0.0) != 0.0) {
|
||||
fprintf(stderr, "radiance: gemma4: this DFlash2 checkpoint scales its embedding or its "
|
||||
"logits, or soft-caps them; the block applies none of the three\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
RAD_ARCH_TRY(drafter_kv_group(b, nm, ctx, dg, dc.window, (int)m.n_main, &m.kv_draft));
|
||||
|
||||
const int64_t qd = dg.n_head * dg.head_dim;
|
||||
const int64_t kvd = dg.n_head_kv * dg.head_dim;
|
||||
int64_t qr = dg.max_seqs * dc.block;
|
||||
if (qr > dg.max_tok) qr = dg.max_tok;
|
||||
const int64_t sr = dg.max_seqs * dc.steps();
|
||||
const int64_t spad = ((sr + 15) / 16) * 16;
|
||||
|
||||
Dflash2::Wire& dw = m.df2w;
|
||||
dw.src = m.b_dfsrc;
|
||||
dw.c = decl_b(b, nm.f("df_c"), dg.act_dtype, {dg.max_tok, dg.n_embd});
|
||||
dw.ck = decl_b(b, nm.f("df_ck"), dg.act_dtype, {dg.max_tok, kvd});
|
||||
dw.cv = decl_b(b, nm.f("df_cv"), dg.act_dtype, {dg.max_tok, kvd});
|
||||
dw.x = decl_b(b, nm.f("df_x"), dg.act_dtype, {qr, dg.n_embd});
|
||||
dw.h = decl_b(b, nm.f("df_h"), dg.act_dtype, {qr, dg.n_embd});
|
||||
dw.h2 = decl_b(b, nm.f("df_h2"), dg.act_dtype, {qr, dg.n_embd});
|
||||
dw.coef = decl_b(b, nm.f("df_coef"), dg.act_dtype, {qr, dc.coef_dim(dg.n_embd)});
|
||||
dw.q = decl_b(b, nm.f("df_q"), dg.act_dtype, {qr, qd});
|
||||
dw.k = decl_b(b, nm.f("df_k"), dg.act_dtype, {qr, kvd});
|
||||
dw.v = decl_b(b, nm.f("df_v"), dg.act_dtype, {qr, kvd});
|
||||
dw.attn = decl_b(b, nm.f("df_attn"), dg.act_dtype, {qr, qd});
|
||||
dw.gate_up = decl_b(b, nm.f("df_gate_up"), dg.act_dtype, {qr, 2 * dg.n_ff});
|
||||
dw.ffn = decl_b(b, nm.f("df_ffn"), dg.act_dtype, {qr, dg.n_ff});
|
||||
dw.srcq = decl_b(b, nm.f("df_srcq"), RAD_F8E4M3, {dg.max_tok, dc.n_taps * dg.n_embd});
|
||||
dw.srcs = decl_b(b, nm.f("df_srcs"), RAD_F32,
|
||||
{dg.max_tok, fp8_blocks(dc.n_taps * dg.n_embd)});
|
||||
dw.cq = decl_b(b, nm.f("df_cq"), RAD_F8E4M3, {dg.max_tok, dg.n_embd});
|
||||
dw.cs = decl_b(b, nm.f("df_cs"), RAD_F32, {dg.max_tok, fp8_blocks(dg.n_embd)});
|
||||
dw.h2q = decl_b(b, nm.f("df_h2q"), RAD_F8E4M3, {qr, dg.n_embd});
|
||||
dw.h2s = decl_b(b, nm.f("df_h2s"), RAD_F32, {qr, fp8_blocks(dg.n_embd)});
|
||||
dw.atq = decl_b(b, nm.f("df_atq"), RAD_F8E4M3, {qr, qd});
|
||||
dw.ats = decl_b(b, nm.f("df_ats"), RAD_F32, {qr, fp8_blocks(qd)});
|
||||
dw.ffq = decl_b(b, nm.f("df_ffq"), RAD_F8E4M3, {qr, dg.n_ff});
|
||||
dw.ffs = decl_b(b, nm.f("df_ffs"), RAD_F32, {qr, fp8_blocks(dg.n_ff)});
|
||||
dw.sel_h = decl_b(b, nm.f("df_selh"), dg.act_dtype, {sr, dg.n_embd});
|
||||
dw.hp = decl_b(b, nm.f("df_hp"), dg.act_dtype, {sr, dc.sel_rank});
|
||||
dw.dq = decl_b(b, nm.f("df_dq"), RAD_I8, {sr, dg.n_embd});
|
||||
dw.ds = decl_b(b, nm.f("df_ds"), RAD_F32, {sr, 1});
|
||||
dw.dsum = decl_b(b, nm.f("df_dsum"), RAD_F32, {dg.n_embd / 128, spad});
|
||||
dw.dlog = decl_b(b, nm.f("df_dlog"), dg.act_dtype, {sr, Dflash2::head_rows(dg)});
|
||||
/* No rescoring against the trunk's head: the -df2 recipe's head is already 8-bit, so the
|
||||
* candidates are the ones an 8-bit head ranks (rad_block_dflash2.h, head_i8). */
|
||||
const int64_t tw = dc.sel_topk;
|
||||
dw.cand = decl_b(b, nm.f("df_cand"), RAD_I32, {sr, tw});
|
||||
dw.unary = decl_b(b, nm.f("df_unary"), RAD_F32, {sr, tw});
|
||||
if (Dflash2::head_sharded(dg)) {
|
||||
dw.dpair = decl_b(b, nm.f("df_dpair"), RAD_F32, {sr, tw, 2});
|
||||
dw.dgath = decl_b(b, nm.f("df_dgath"), RAD_F32, {sr, (int64_t)dg.world * tw, 2});
|
||||
}
|
||||
dw.tokens = decl_b(b, nm.f("dflash_tokens"), RAD_I32, {dg.max_seqs, dc.steps()});
|
||||
|
||||
/* The head copy comes from the tied table, under the block's own name; the recipe makes it
|
||||
* 8-bit. */
|
||||
RAD_ARCH_TRY(m.df2.declare(b, nm, dg, dc, (int)m.n_main, m.kv_draft, m.w_tok, "draft",
|
||||
nm.ckpt("model.language_model.embed_tokens.weight"), dw));
|
||||
m.have_df2 = true;
|
||||
|
||||
RadDrafterDecl drf{};
|
||||
drf.name = "dflash2";
|
||||
drf.kind = RAD_DRAFT_BLOCK;
|
||||
drf.depth = ctx->max_spec;
|
||||
drf.block = dc.block;
|
||||
drf.proposal = dw.tokens;
|
||||
drf.proposal_pitch = dc.steps();
|
||||
drf.window = dc.window;
|
||||
drf.mask_token = (int32_t)df2_geti(meta, "mask_token_id", -1);
|
||||
if (drf.mask_token < 0) {
|
||||
fprintf(stderr, "radiance: gemma4: this DFlash2 checkpoint declares no mask_token_id, and "
|
||||
"every drafted position embeds it\n");
|
||||
return RAD_E_FORMAT;
|
||||
}
|
||||
RAD_ARCH_TRY(rad_declare_drafter(b, &drf));
|
||||
|
||||
rad_note(b, "DFlash2 drafter: %lld layers at absolute %lld.., %lld heads / %lld kv x %lld, ffn "
|
||||
"%lld, %s attention with a %lld-token window, %d trunk taps; one forward proposes "
|
||||
"%lld tokens through the candidate selector, its head %s",
|
||||
(long long)dg.n_layer, (long long)m.n_main, (long long)dg.n_head_all,
|
||||
(long long)dg.n_head_kv_all, (long long)dg.head_dim, (long long)dg.n_ff_all,
|
||||
dc.causal ? "causal" : "non-causal", (long long)dc.window, m.n_taps,
|
||||
(long long)dc.steps(), m.df2.head_i8 ? "8-bit" : "2-bit");
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx) {
|
||||
if (!b || !meta || !ctx) return RAD_E_INVAL;
|
||||
if (ctx->rank < 0 || ctx->rank >= MAX_RANKS) return RAD_E_INVAL;
|
||||
@@ -1273,7 +1697,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
* serve speculates. The plane is declared here and written by an op declared inside each layer
|
||||
* it taps, because the buffer planner's liveness is declaration order (the tap below). */
|
||||
{
|
||||
const int n = gemma4::Dflash1::taps(meta, ctx, g.n_layer, m.tap_layer, Model::MAX_TAPS);
|
||||
const int n = gemma4::dflash_kind(meta) == 2
|
||||
? gemma4::dflash2_taps(meta, ctx, g.n_layer, m.tap_layer, Model::MAX_TAPS)
|
||||
: gemma4::Dflash1::taps(meta, ctx, g.n_layer, m.tap_layer, Model::MAX_TAPS);
|
||||
if (n < 0) return n;
|
||||
m.n_taps = n;
|
||||
if (n > 0)
|
||||
@@ -1342,7 +1768,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
const rad_kvgroup kg = lay.global ? m.kv_g : m.kv_s;
|
||||
const Gemma4Attn::Wire& aw = lay.global ? gw : sw;
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, kg));
|
||||
RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, cgeo, kg, aw, pre));
|
||||
RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, cgeo, kg, aw, pre,
|
||||
lay.global ? &m.b_qkvg : &m.b_qkvs));
|
||||
RAD_ARCH_TRY(lay.mlp.declare(b, nm, g, (int)l, mw, pre));
|
||||
|
||||
/* The per-layer scalar: `cur = cur * scalar`, a [1] bf16 over the residual stream. */
|
||||
@@ -1378,10 +1805,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
if (m.n_main > 0) {
|
||||
bool ok = true;
|
||||
for (const Layer& lay : m.layers)
|
||||
ok = ok && lay.attn.ln_q.quant && lay.attn.ln_k.shared_in &&
|
||||
(!lay.attn.cls.has_v || lay.attn.ln_v.shared_in) && lay.mlp.ln_gu.quant;
|
||||
ok = ok && lay.attn.reads_pair() && lay.mlp.ln_gu.quant;
|
||||
if (ok) {
|
||||
RAD_ARCH_TRY(m.j0.declare(b, g, m.b_x, 0, m.layers[0].attn.w_norm, 0, 0, m.layers[0].attn.ln_q));
|
||||
RAD_ARCH_TRY(m.j0.declare(b, g, m.b_x, 0, m.layers[0].attn.w_norm, 0, 0,
|
||||
m.layers[0].attn.ln_in()));
|
||||
ok = m.j0.op != 0;
|
||||
for (int64_t l = 0; ok && l < m.n_main; ++l) {
|
||||
Layer& lay = m.layers[(size_t)l];
|
||||
@@ -1391,7 +1818,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
if (ok && l + 1 < m.n_main) {
|
||||
const Layer& nx = m.layers[(size_t)l + 1];
|
||||
RAD_ARCH_TRY(lay.j_ma.declare(b, g, m.b_h, m.b_x, nx.attn.w_norm, lay.mlp.w_post,
|
||||
lay.w_scalar, nx.attn.ln_q));
|
||||
lay.w_scalar, nx.attn.ln_in()));
|
||||
ok = lay.j_ma.op != 0;
|
||||
}
|
||||
}
|
||||
@@ -1409,6 +1836,18 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
m.ar_rows = ar ? rows : 0;
|
||||
}
|
||||
|
||||
/* ---- the prefill overlap (trunk_overlap). It needs a reduction to hide, every junction fused
|
||||
* (the slices issue only the fused forms), and the exact wire, the one shown to give a slice
|
||||
* the bits it gives the whole. The buffer the second lane reduces in place is taken out of the
|
||||
* arena's sharing, which the runtime asks of every buffer a second lane touches; it lives
|
||||
* across the whole trunk already. */
|
||||
const char* ov = std::getenv("RADIANCE_GEMMA4_OVERLAP"); /* 0: every step whole */
|
||||
const bool ov_off = ov && !std::strcmp(ov, "0");
|
||||
m.overlap = !ov_off && g.world > 1 && g.wire_exact && m.fuse && g.max_tok >= 2 * kOverlapRows;
|
||||
for (const Layer& lay : m.layers)
|
||||
m.overlap = m.overlap && lay.attn.op_ar && lay.mlp.op_ar;
|
||||
if (m.overlap) RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_h));
|
||||
|
||||
/* ---- the tail: final norm, the sampled rows, the lm_head, and the soft-cap. */
|
||||
m.op_final = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
|
||||
@@ -1457,7 +1896,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
* declaration order is the buffer planner's liveness. It reads the taps the trunk copied into
|
||||
* `dflash_src` and the trunk's tied table, as its embedding and, where the container holds no
|
||||
* 8-bit copy, as its head. */
|
||||
if (m.n_taps > 0) {
|
||||
if (m.n_taps > 0 && gemma4::dflash_kind(meta) == 2) {
|
||||
RAD_ARCH_TRY(declare_dflash2(b, meta, ctx, m));
|
||||
} else if (m.n_taps > 0) {
|
||||
gemma4::Dflash1::Trunk t{};
|
||||
t.n_taps = m.n_taps;
|
||||
t.escale = gemma4::embed_scale(g.n_embd);
|
||||
@@ -1478,6 +1919,11 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
head_groups = meta_text_geti(meta, "num_hidden_layers", 0, "draft.") + 1;
|
||||
else if (gemma4::dflash_kind(meta) == 1)
|
||||
head_groups = rad_meta_geti(meta, "draft.num_hidden_layers", 0);
|
||||
else if (gemma4::dflash_kind(meta) == 2) {
|
||||
char k[160];
|
||||
std::snprintf(k, sizeof k, "%snum_hidden_layers", drafter_layer_prefix(meta));
|
||||
head_groups = rad_meta_geti(meta, k, 0);
|
||||
}
|
||||
RAD_ARCH_TRY(m.vis.declare(b, meta, ctx, nm, g.n_embd, (int)(m.n_main + head_groups),
|
||||
"model.", "gemma4_bf16"));
|
||||
}
|
||||
@@ -1487,6 +1933,32 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
g.rank, g.world, (long long)g.n_layer, (long long)n_global,
|
||||
(long long)(g.n_layer - n_global), (long long)sliding_win,
|
||||
(long long)g.n_head, (long long)g.head_dim, (long long)g.n_head, (long long)head_g);
|
||||
{
|
||||
int64_t n_fused = 0, n_row = 0;
|
||||
for (const Layer& lay : m.layers) {
|
||||
n_fused += lay.attn.fused ? 1 : 0;
|
||||
n_row += lay.attn.op_attn_row ? 1 : 0;
|
||||
}
|
||||
if (n_fused)
|
||||
rad_note(b, "q, k and v as one linear (blk.*.attn_qkv) on %lld of %lld layers; attention "
|
||||
"reads q in the row on %lld, copies it out above one row on the rest",
|
||||
(long long)n_fused, (long long)m.layers.size(), (long long)n_row);
|
||||
else
|
||||
rad_note(b, "q, k and v as three linears (a container converted before attn_qkv, or "
|
||||
"bf16 weights): convert with a recipe naming blk.*.attn_qkv.weight for one");
|
||||
}
|
||||
if (g.world > 1) {
|
||||
if (m.overlap)
|
||||
rad_note(b, "prefill overlap: a step of %lld rows or more runs as two slices, each "
|
||||
"slice's all-reduces on a second stream beside the other's compute",
|
||||
(long long)(2 * kOverlapRows));
|
||||
else
|
||||
rad_note(b, "prefill overlap: off (%s)",
|
||||
ov_off ? "RADIANCE_GEMMA4_OVERLAP=0"
|
||||
: !g.wire_exact ? "the lossy all-reduce wire"
|
||||
: !m.fuse ? "the residual junctions are not fused"
|
||||
: "a step holds fewer than two slices");
|
||||
}
|
||||
if (m.fuse && (g.world == 2 || g.world == 4)) {
|
||||
if (!m.fuse_ar)
|
||||
rad_note(b, "residual junctions: the all-reduce in front of each issued apart");
|
||||
@@ -1500,6 +1972,109 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ==================================================================== the prefill overlap
|
||||
*
|
||||
* A prefill step in two slices, each slice's all-reduces on the runtime's second lane (rad_lane)
|
||||
* beside the other slice's compute: arch/gemma4's trunk_overlap, on this trunk. A layer reduces
|
||||
* T x n_embd twice, after o_proj and after down_proj, both column-sharded; run whole, the
|
||||
* reductions and the compute take turns. Cut in two, a slice's reduction runs while the other
|
||||
* slice computes:
|
||||
*
|
||||
* lane 0 o(A) o(B) . jam(A) arm(A) . jam(B) arm(B) . jma(A) qkv(A) . jma(B) qkv(B) attn o(A) ..
|
||||
* lane 1 ar(A) ar(B) ar(A) ar(B)
|
||||
*
|
||||
* Attention runs whole (the prologue, the cache write and attn_paged): it reads every row's keys,
|
||||
* and its batch bounds describe the whole step. The rest is a row at a time: the projections, the
|
||||
* junctions, the feed-forward arm and the reductions.
|
||||
*
|
||||
* The same kernels give the same bits (core/runtime/ctx.h, the second lane's rule four). Both slices
|
||||
* are at least kOverlapRows rows: above the int8 GEMMs' skinny band, whose prefill and tiled
|
||||
* kernels take each row's K in order whatever M is; above r4d_act_threads' last width change, so
|
||||
* every junction, quantiser and norm folds a row's sum of squares as the whole step's does; and the
|
||||
* exact all-reduce folds a word in fp32 in ascending rank order whatever message carries it. On
|
||||
* the exact wire only.
|
||||
*
|
||||
* One buffer carries both reductions: o_proj and down_proj both write `h`, and the junctions read
|
||||
* it. So the order below never lets lane 0 write rows of `h` lane 1 is reducing: slice A's arm
|
||||
* writes h[A] only after A's attention reduction was waited for, while lane 1 reduces h[B]. Every
|
||||
* reduction rides lane 1 behind a join from lane 0, which holds its producer; lane 0 waits for lane
|
||||
* 1 before the reduction's consumer; each wait goes in after the lane-0 work that does not need it.
|
||||
* The embedding's reduction stays on lane 0, ahead of everything lane 1 is first joined behind, so
|
||||
* the collectives keep one order on every rank. At two ranks the junctions here carry no
|
||||
* all-reduce (the reduction rode lane 1); a step that runs whole keeps the fused one. */
|
||||
static int64_t split_at(const Model& m, int64_t T) {
|
||||
if (!m.overlap || T < 2 * kOverlapRows) return 0;
|
||||
return (T / 2) / 128 * 128; /* slice A whole 128-row tiles; both slices >= kOverlapRows */
|
||||
}
|
||||
|
||||
static void trunk_overlap(RadCtx* c, const Model& m, const RadBatch* batch, int64_t cut) {
|
||||
const int64_t T = batch->n_tok, E = m.g.n_embd;
|
||||
const int64_t a0 = 0, an = cut, b0 = cut, bn = T - cut;
|
||||
bool lanes_ok = true;
|
||||
/* An all-reduce in place over rows [r0, r0 + n) of `h`, on lane 1 behind everything lane 0
|
||||
* has issued -- its producer among it. */
|
||||
auto reduce = [&](rad_op op, int64_t r0, int64_t n) {
|
||||
lanes_ok = lanes_ok && rad_lane_join(c, 0, 1) == RAD_OK && rad_lane(c, 1) == RAD_OK;
|
||||
RAD_ISSUE_N(c, op, n * E, brow_slice(m.b_h, r0, n, E), RAD_NONE);
|
||||
lanes_ok = rad_lane(c, 0) == RAD_OK && lanes_ok;
|
||||
};
|
||||
/* Lane 0 behind every reduction issued so far. */
|
||||
auto join_back = [&] { lanes_ok = rad_lane_join(c, 1, 0) == RAD_OK && lanes_ok; };
|
||||
/* Rows [r0, r0 + n) of layer li's end, once their feed-forward reduction is in: the junction
|
||||
* into the next layer -- or the last layer's post-norm, add and scalar -- and the drafter's
|
||||
* taps of those rows. */
|
||||
auto join_ff = [&](size_t li, int64_t r0, int64_t n) {
|
||||
const Layer& y = m.layers[li];
|
||||
if (li + 1 < m.layers.size()) {
|
||||
y.j_ma.step_rows(c, E, r0, n);
|
||||
} else {
|
||||
y.mlp.tail(c, r0, n);
|
||||
RAD_ISSUE_N(c, y.op_scale, n, brow_slice(m.b_x, r0, n, E), RAD_W(y.w_scalar),
|
||||
brow_slice(m.b_x, r0, n, E));
|
||||
}
|
||||
for (int t = 0; t < m.n_taps && y.op_tap; ++t)
|
||||
if (m.tap_layer[t] == (int)li)
|
||||
RAD_ISSUE_N(c, y.op_tap, n, brow_slice(m.b_x, r0, n, E),
|
||||
bcol_at(m.b_dfsrc, r0, (int64_t)m.n_taps * E, (int64_t)t * E, E, n));
|
||||
};
|
||||
|
||||
m.j0.step(c, T);
|
||||
const size_t L = m.layers.size();
|
||||
for (size_t li = 0; li < L; ++li) {
|
||||
const Layer& y = m.layers[li];
|
||||
if (li == 0) {
|
||||
y.attn.project(c, a0, an);
|
||||
y.attn.project(c, b0, bn);
|
||||
} else {
|
||||
join_ff(li - 1, a0, an); /* its reduction was waited for last layer */
|
||||
y.attn.project(c, a0, an);
|
||||
join_back(); /* the last layer's feed-forward, slice B */
|
||||
join_ff(li - 1, b0, bn);
|
||||
y.attn.project(c, b0, bn);
|
||||
}
|
||||
y.attn.attend(c, batch);
|
||||
y.attn.out_proj(c, a0, an);
|
||||
reduce(y.attn.op_ar, a0, an);
|
||||
y.attn.out_proj(c, b0, bn);
|
||||
join_back(); /* attention, slice A */
|
||||
reduce(y.attn.op_ar, b0, bn);
|
||||
y.j_am.step_rows(c, E, a0, an);
|
||||
y.mlp.arm(c, a0, an);
|
||||
join_back(); /* attention, slice B */
|
||||
reduce(y.mlp.op_ar, a0, an);
|
||||
y.j_am.step_rows(c, E, b0, bn);
|
||||
y.mlp.arm(c, b0, bn);
|
||||
join_back(); /* the feed-forward, slice A */
|
||||
reduce(y.mlp.op_ar, b0, bn);
|
||||
}
|
||||
join_ff(L - 1, a0, an);
|
||||
join_back(); /* the last feed-forward, slice B */
|
||||
join_ff(L - 1, b0, bn);
|
||||
if (!lanes_ok)
|
||||
rad_step_fail(c, "gemma4_bf16: the prefill overlap could not order its second lane against "
|
||||
"the first");
|
||||
}
|
||||
|
||||
static void step(RadCtx* c, const RadBatch* batch) {
|
||||
Model& m = g_model[rad_rank(c)];
|
||||
|
||||
@@ -1517,6 +2092,11 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
else m.df.context(c, batch);
|
||||
return;
|
||||
}
|
||||
if (m.have_df2) {
|
||||
if (batch->draft_pass > 0) m.df2.step(c, batch);
|
||||
else m.df2.context(c, batch);
|
||||
return;
|
||||
}
|
||||
if (!m.have_head) {
|
||||
rad_step_fail(c, "the batch asks for a drafter pass and no drafter is declared: the "
|
||||
"container carries no `draft.` assistant or DFlash drafter, or declare "
|
||||
@@ -1538,10 +2118,12 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
praw(batch->mm_rows, RAD_I32, batch->n_mm_rows), brows(m.b_x, batch->n_tok));
|
||||
|
||||
const int64_t T = batch->n_tok;
|
||||
if (m.fuse) m.j0.step(c, T);
|
||||
const int64_t cut = split_at(m, T);
|
||||
if (cut > 0) trunk_overlap(c, m, batch, cut);
|
||||
else if (m.fuse) m.j0.step(c, T);
|
||||
/* the junctions carry the reductions in front of them in a step their fold serves */
|
||||
const bool far = m.fuse_ar && T <= m.ar_rows;
|
||||
for (size_t li = 0; li < m.layers.size(); ++li) {
|
||||
for (size_t li = 0; cut == 0 && li < m.layers.size(); ++li) {
|
||||
const Layer& l = m.layers[li];
|
||||
const bool last = li + 1 == m.layers.size();
|
||||
l.attn.step(c, batch, m.fuse, m.fuse, far);
|
||||
@@ -1595,8 +2177,10 @@ static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
out->draft_depth = kHeadDepth;
|
||||
out->draft_depth_fixed = 0;
|
||||
}
|
||||
/* A DFlash drafter names the block it was trained at, and only that (rad_gemma4_dflash.h). */
|
||||
/* A DFlash drafter names the block it was trained at, and only that (rad_gemma4_dflash.h); a
|
||||
* DFlash2 one too (rad_block_dflash2.h). */
|
||||
(void)gemma4::dflash_probe(meta, out);
|
||||
(void)gemma4::dflash2_probe(meta, out);
|
||||
/* The tower's pooling window, so a pass size the core picks itself is whole windows. */
|
||||
if (Gemma4Vision::present(meta)) {
|
||||
const int64_t k = rad_meta_geti(meta, "vision_config.pooling_kernel_size", 0);
|
||||
|
||||
@@ -35,11 +35,10 @@
|
||||
# 32 GiB R9700.
|
||||
#
|
||||
# This starts from bf16 for the reason gemma4-31b-dense-bf16-w4a8-df.recipe gives: the drafter
|
||||
# reads the trunk's hidden states and was trained beside the bf16 trunk.
|
||||
# reads the trunk's hidden states and was trained beside the bf16 trunk. q, k and v are one linear
|
||||
# (blk.*.attn_qkv), for the reason that recipe gives.
|
||||
|
||||
blk.*.attn_q.weight rtn codes=i8 group=128 scale=f16 clamp=sym
|
||||
blk.*.attn_k.weight rtn codes=i8 group=128 scale=f16 clamp=sym
|
||||
blk.*.attn_v.weight rtn codes=i8 group=128 scale=f16 clamp=sym
|
||||
blk.*.attn_qkv.weight rtn codes=i8 group=128 scale=f16 clamp=sym
|
||||
blk.*.attn_o.weight rtn codes=i8 group=128 scale=f16 clamp=sym
|
||||
blk.*.ffn_down.weight rtn codes=i8 group=128 scale=f16 clamp=sym
|
||||
blk.*.ffn_gate_up.weight rtn codes=i8 group=128 scale=f16 clamp=sym
|
||||
|
||||
@@ -28,10 +28,14 @@
|
||||
#
|
||||
# ~20 GiB: the int4 trunk's linears (~14 GiB), the bf16 table and its int8 head (2 GiB), the
|
||||
# vision tower, the drafter at block fp8 (1.4 GiB) and its head's block-fp8 copy (0.7 GiB).
|
||||
#
|
||||
# q, k and v are one linear, blk.*.attn_qkv (q | k | v; q | k on the global layers, where v is
|
||||
# k): the plugin serves one GEMM a layer where three paid the skinny GEMM's fixed cost each. The
|
||||
# grid's groups never cross a row, so the codes are those of three separate rules (on the cards k
|
||||
# and v may round differently in the last bit: the GEMM's K split follows N). A container
|
||||
# converted before this, with the three apart, still serves.
|
||||
|
||||
blk.*.attn_q.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
blk.*.attn_k.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
blk.*.attn_v.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
blk.*.attn_qkv.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
blk.*.attn_o.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
blk.*.ffn_down.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
blk.*.ffn_gate_up.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
# gemma4-31b-dense-bf16-w4a8-df2.recipe -- Gemma 4 31B dense (text) from Google's bf16 checkpoint
|
||||
# at w4a8, as gemma4-31b-dense-bf16-w4a8-df.recipe makes it, with RedHatAI's DFlash2 speculator
|
||||
# merged in place of z-lab's DFlash drafter.
|
||||
#
|
||||
# rad-convert <gemma-4-31B-it> --draft-model <gemma-4-31B-it-speculator.dflash2> \
|
||||
# --recipe data/recipes/gemma4-31b-dense-bf16-w4a8-df2.recipe \
|
||||
# --set quantization_config.quant_method=compressed-tensors -o gemma4-31b-dense-w4a8-df2-qkv.rad
|
||||
#
|
||||
# radiance serve gemma4-31b-dense-w4a8-df2-qkv.rad --num-speculative-tokens auto ...
|
||||
#
|
||||
# The drafter (RedHatAI/gemma-4-31B-it-speculator.dflash2) is a speculators-format DFlash2: five
|
||||
# Qwen3-shaped layers at 5376 (32 heads / 16 kv x 256, MLP 21504), every one on a 2048-token
|
||||
# window, non-causal, an fc over the trunk's residual stream entering layers 1 17 29 47 59, a
|
||||
# grouped dynamic convolution around each sublayer and a candidate selector (rank 256, top-k 16):
|
||||
# 2.75 B parameters, 5.5 GB at bf16. It proposes 7 tokens a pass (block 8); the plugin's probe names
|
||||
# that depth, and rad_block_dflash2.h is the block.
|
||||
#
|
||||
# It ships no embedding or head: it reads the trunk's table, unscaled, and the trunk's head. The
|
||||
# trunk is the -df recipe's (int4 trunk, q|k|v as one linear, int8 lm_head), so the same rules.
|
||||
#
|
||||
# ~24 GiB: the -df recipe's trunk and table (~16 GiB), the vision tower, the drafter at block fp8
|
||||
# (2.6 GiB), its selector codebooks at bf16 (2 x 128 MiB) and its head's int8 copy (1.4 GiB).
|
||||
|
||||
blk.*.attn_qkv.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
blk.*.attn_o.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
blk.*.ffn_down.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
blk.*.ffn_gate_up.weight rtn codes=i4 group=128 scale=f16 rule=search
|
||||
|
||||
output.weight rtn codes=i8 group=128 scale=bf16 clamp=sym
|
||||
|
||||
# The drafter's seven linears: block fp8, read at fp8a8 against an E4M3 activation, as the block
|
||||
# declares them (rad_fp8.h's DenseFP8). The convolution projections, the selector and the norms
|
||||
# stay bf16, as the block reads them.
|
||||
dflash.fc.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16
|
||||
dflash.blk.*.attn_q.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16
|
||||
dflash.blk.*.attn_k.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16
|
||||
dflash.blk.*.attn_v.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16
|
||||
dflash.blk.*.attn_output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16
|
||||
dflash.blk.*.ffn_gate_up.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16
|
||||
dflash.blk.*.ffn_down.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16
|
||||
|
||||
# The draft head: the trunk's tied table under the block's own name, int8 with an f16 scale per
|
||||
# (row, 128 of K), which the block reads at w8a8 (rad_block_dflash2.h, head_i8). Not the 2-bit
|
||||
# grid the Qwen3.8 recipes use: the selector then ranks the candidates an 8-bit head proposes.
|
||||
# Drafts are verified, so this changes what is proposed, never what is accepted.
|
||||
dflash.draft_head.weight rtn codes=i8 group=128 scale=f16 clamp=sym
|
||||
+44
-1
@@ -17,7 +17,7 @@ plugin claims.
|
||||
| `arch/glm5next` | `glm5next` | *(empty)* | GLM-5.3-Flash from AMD's Quark MXFP4 export: four mHC residual streams over 34 KDA and 11 sparse latent-attention (MLA) layers with a DSA indexer, 288 MXFP4 experts at top-8 under sigmoid routing, block-FP8 attention and dense layers at f32 scales, each weight served as stored; its MTP layer is a serial drafter. Text only, bf16 KV |
|
||||
| `arch/lfm2_bf16` | `lfm2` | *(empty)* | LiquidAI's LFM2 and LFM2-VL, bf16, one rank |
|
||||
| `arch/embgemma2` | `embeddinggemma2` | *(empty)* | EmbeddingGemma 2: a bidirectional encoder with Gemma 4's vision tower and audio conformer, pooled to one vector |
|
||||
| `arch/gemma4_bf16` | `gemma4` | `compressed-tensors` | Gemma 4 31B dense with its vision tower: sliding-window and proportional-RoPE global (`attention_k_eq_v`) attention, GELU ffn, softcapped logits; int4 codes with an f16 scale per 128 from Google's CT-INT4 (QAT) release or from bf16, served w4a8, or int8 codes on the same grid from bf16 (w8a8, the Q8 container). Optional draft model (`rad-convert --draft-model`): Google's assistant, with no cache of its own (`rad_drafter_shared_kv`), or z-lab's DFlash drafter, the block `arch/gemma4` serves (`arch/common/rad_gemma4_dflash.h`) |
|
||||
| `arch/gemma4_bf16` | `gemma4` | `compressed-tensors` | Gemma 4 31B dense with its vision tower: sliding-window and proportional-RoPE global (`attention_k_eq_v`) attention, GELU ffn, softcapped logits; int4 codes with an f16 scale per 128 from Google's CT-INT4 (QAT) release or from bf16, served w4a8, or int8 codes on the same grid from bf16 (w8a8, the Q8 container). Optional draft model (`rad-convert --draft-model`): Google's assistant, with no cache of its own (`rad_drafter_shared_kv`), or z-lab's DFlash drafter, the block `arch/gemma4` serves (`arch/common/rad_gemma4_dflash.h`), or RedHatAI's DFlash2 speculator (`arch/common/rad_block_dflash2.h`) |
|
||||
| `arch/gemma3` | `gemma3` | *(empty)* | Gemma 3 dense text (TranslateGemma 27B) from the bf16 checkpoint: sliding-window and global attention at one head geometry, GemmaRMSNorm, GELU ffn; linears bf16, or int8, MXFP4 or asymmetric int4 over an activation scaled per (row, 128), as the recipe stores them. Optional DFlash2 drafter (`rad-convert --draft-model`) |
|
||||
| `arch/gemma4` | `gemma4` | *(empty)* | Gemma 4 text from Google's bf16 checkpoint (the 26B-A4B MoE and the dense sizes without per-layer embeddings) with z-lab's DFlash drafter; routed experts bf16 or block fp8 |
|
||||
| `arch/qwenimage21` | `qwenimage21` | *(empty)* | Qwen-Image 2.1 and 2.1-Turbo, an image generator, from the diffusers pipeline directory; DiT and text encoder bf16 or block fp8 |
|
||||
@@ -745,6 +745,49 @@ hidden states and was trained beside the bf16 trunk. Both plugins scale the embe
|
||||
sqrt(hidden) rounded to bf16, as the reference's embedding module does (73.5 at 5376, not 73.32),
|
||||
and the drafter reads it through the same scale.
|
||||
|
||||
**The 31B's prefill overlap.** `arch/gemma4_bf16` cuts a prefill step of 1024 rows or more in two
|
||||
(slice A whole 128-row tiles, both at least 512) and runs each slice's two all-reduces per layer on
|
||||
the second lane beside the other slice's compute, as the prefill overlap above does, with one
|
||||
difference: o_proj and down_proj both write `h`, so the issue order never lets lane 0 write rows of
|
||||
`h` that lane 1 is reducing, and `h` is the one buffer taken out of the arena's sharing. Attention
|
||||
runs whole. Both slices are above every row band the sliced kernels have (the int8 GEMMs' skinny
|
||||
band at 64, since the prefill kernel takes each row's K in order whatever M is, and
|
||||
r4d_act_threads' last width change at 120 rows for these widths), so on the exact wire the output
|
||||
equals the whole step's bit for bit. Every junction must be fused, as the w4a8 and w8a8
|
||||
containers' are; a bf16 trunk runs whole, and so does every step under `RADIANCE_GEMMA4_OVERLAP=0`.
|
||||
|
||||
**The 31B's q, k and v as one linear.** Converted with a recipe that names `blk.*.attn_qkv.weight`
|
||||
(both -df recipes do), each layer's q, k and v (q and k on a global layer, where v is k) are one
|
||||
weight, each rank holding its slice of every part, and one `gemm_nt_q` reads them where three did:
|
||||
at decode on the R9700 q, k and v take 17 + 13.5 + 13.5 us and a 4096-row GEMM of the same K 25.
|
||||
The grid's groups never cross a row, so the codes are those of three separate rules, and on libref
|
||||
the replies are too. On libr4d the skinny GEMM picks its in-block K split from N (k and v alone
|
||||
split K in two, the fused GEMM does not), so k and v can round differently in the last bit and a
|
||||
long greedy reply can part from the old container's late. `qkv_norm_rope` and `kv_store` work on
|
||||
the row's column slices in place. A global layer's
|
||||
attention reads q in the row (`attn_paged_qrow`, the windowed kernel that serves head_dim 512 under
|
||||
`attn_paged` too); a sliding layer's reads it there in a one-row step, and a taller step copies q
|
||||
out with a `cast` for libr4d's h256/gqa2 kernels, which take 0.26-0.71 of the windowed kernel's
|
||||
time. Without `qkv_norm_rope` (a CPU-only engine) q, k and v are copied out for the unfused
|
||||
prologue. A container that holds the three apart serves as before: the plugin asks the weight's
|
||||
encoding, which rad-convert answers from the recipe and a container from its entries.
|
||||
|
||||
**The 31B with a DFlash2 drafter.** RedHatAI's `gemma-4-31B-it-speculator.dflash2` is z-lab's
|
||||
DFlash2 architecture trained with vLLM's speculators library: five Qwen3-shaped layers at 5376 (32
|
||||
heads / 16 kv x 256, ffn 21504), every one on a 2048-token window and non-causal, the grouped
|
||||
dynamic convolution and the candidate selector (rank 256, top-k 16), block 8 (seven drafts). The
|
||||
31B serves it with the block qwen35_fp8 uses for Qwen3.8's (`arch/common/rad_block_dflash2.h`) in
|
||||
place of z-lab's DFlash, when the container's drafter is a `DFlash2DraftModel`. Its config is in
|
||||
the speculators layout: the speculator's keys at the top of `draft.` and the layers' under
|
||||
`draft.transformer_layer_config.` (`drafter_layer_prefix`). Its `aux_hidden_state_layer_ids`
|
||||
count as vLLM captures them, the residual entering a layer, so 1 17 29 47 59 tap the outputs of
|
||||
layers 0 16 28 46 58 (`gemma4::dflash2_taps`). It was trained against the target's table as a
|
||||
plain embedding and the target's head as a plain linear, so its lookup takes no Gemma scale and its
|
||||
head no soft-cap. The -df2 recipe stores its head copy int8 (`head_i8`, read at w8a8) rather than
|
||||
the 2-bit grid Qwen3.8's recipes use, so the selector ranks what an 8-bit head proposes. Its
|
||||
attention is the sliding layers' h256 gqa-2 split-KV decode, non-causal. `run-gemma4-31b.sh`
|
||||
takes it with `DRAFTER=df2`.
|
||||
|
||||
**The 31B's junctions at four ranks.** At two ranks every junction carries the all-reduce in front
|
||||
of it (`ar_rmsnorm_quant_i8`). At four it does too, at decode heights. libr4d's
|
||||
`ar_rmsnorm_quant_i8_4rank` is the exact four-rank one-shot with the junction as its epilogue,
|
||||
|
||||
@@ -3086,6 +3086,7 @@ reductions running on fewer blocks: Qwen3.8-27B's 41k-token prefill 9.5 s uncapp
|
||||
| `RADIANCE_G3_CALIB_LAYERS` | `lo:hi`: `arch/gemma3`'s calibration tap over layers lo to hi - 1 only, to take a large model's Grams in passes (docs/ARCHITECTURES.md) |
|
||||
| `RADIANCE_PROFILE_EVERY` | with `--profile-ops`, dump the per-op table every N steps (default 32) |
|
||||
| `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) |
|
||||
| `RADIANCE_GEMMA4_OVERLAP` | the Gemma 4 31B's prefill overlap (`arch/gemma4_bf16`, two or more ranks, fused junctions, the exact all-reduce wire): a prefill step of 1024 rows or more runs as two slices, each slice's all-reduces on a second stream beside the other's compute. On by default; `0` turns it off and runs every step whole (the startup notes say which) |
|
||||
| `RADIANCE_AR8_SHARD_WEIGHTS`, `RADIANCE_AR8_PIECES` | the eight-rank two-shot's shard weights (`auto`, the default, reads the PCIe links; `off`; or eight integers) and piece count, with `RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS` and `RADIANCE_AR8_PIECES_MIN_ELEMS` for the message size each starts at (§4.7) |
|
||||
|
||||
Engine development has its own instruments: `RADIANCE_DEBUG_*` (residual probes, routing and issue
|
||||
|
||||
+15
-2
@@ -691,7 +691,7 @@ may not have arrived.
|
||||
| `gemm_nt` | `M`(range) `N` `K` `dtype` | `a`, `b`w → `y` |
|
||||
| `gemm_nt_f32` | `M`(range) `N` `K` `dtype` | `a`, `b`w → `y` |
|
||||
| `gemm_nt_bias` | `M`(range) `N` `K` `dtype` `act`/opt | `a`, `b`w, `bias`w, `res`? → `y` |
|
||||
| `gemm_nt_q` | `M`(range) `N` `K` `group` `dtype` `pb1`/opt `pb2`/opt | `a`, `a_scale`?, `b`w, `b_scale`w → `y`; `a_sum`?, `b_ref`w? |
|
||||
| `gemm_nt_q` | `M`(range) `N` `K` `group` `dtype` `pb1`/opt `pb2`/opt `split_n`/opt | `a`, `a_scale`?, `b`w, `b_scale`w → `y`; `a_sum`?, `b_ref`w? |
|
||||
| `gemm_nt_q_bias` | `M`(range) `N` `K` `group` `dtype` | `a`, `a_scale`, `b`w, `b_scale`w, `bias`w → `y` |
|
||||
|
||||
`nt` is A row-major `[M,K]` times B transposed `[N,K]`, giving `[M,N]`: the layout every weight in
|
||||
@@ -726,7 +726,14 @@ weight element (n, k) is `(code - zero) * scale`, one f16 scale and one integer
|
||||
|
||||
`a_sum` is `quant_act_i8`'s `asum`, group-major `[K/group][Mpad]` with `Mpad = ceil(M/16)*16`. The
|
||||
ParoQuant grids (the four 4- and 5-bit ones) take parts (`pb1`, `pb2`); `w2a8` and `w4a8_asym` do
|
||||
not, and none has a bias form. libr4d's kernels compute the same products in another order (split-K
|
||||
not, and none has a bias form.
|
||||
|
||||
`split_n` is the output width whose K split the call takes, when it is a row slice of a wider
|
||||
linear (a rank's share of a weight whose rows do not divide among the ranks, issued as two slices).
|
||||
libr4d's narrow fp8a8 and i8a8 kernels pick their K split from N alone, and the split sets the order
|
||||
K partials are summed in, so a slice given the whole width sums each output as the whole linear
|
||||
does and its outputs are the linear's to the bit. Absent, N. The host libraries never split K and
|
||||
ignore it. libr4d's kernels compute the same products in another order (split-K
|
||||
partials, the activation scale applied at the store); libavx folds the scales into its decoded rows,
|
||||
each product rounding once where libref rounds each group's sub-sum. A `b_scale` that is not joined
|
||||
at these grids has no zero to read and is refused (`RAD_E_DTYPE`). libref describes none of them
|
||||
@@ -2094,6 +2101,12 @@ libr4d serves these at 2, 3, 4 and 8 ranks:
|
||||
- `all_gather` at those widths rides the same instance as the wide `all_reduce` (`ar_gather_3rank`,
|
||||
`_4rank`, `_8rank`).
|
||||
|
||||
With `row`, `all_gather`'s `y` may be the leading `world_size * row` columns of a wider buffer, and
|
||||
`x` a `row`-wide window of a wider one (column views whose row pitch is whole 16-byte words): rows
|
||||
are read and land at those pitches, and the rest of each row is left as it was. A linear split by
|
||||
rows whose rank shares cut its scale blocks has each rank compute the whole blocks covering its
|
||||
share and hand over the share's window, gathered into a tight output (arch/common's DFlash2 fc).
|
||||
|
||||
`min_bytes` is the byte floor under which a lossy row serves the message exactly. It is a parameter,
|
||||
not a library constant, because it is a property of the link: compression pays only once the
|
||||
transfer is bandwidth-bound, and for a decode step's few residual rows the rotate, pack and unpack
|
||||
|
||||
@@ -258,8 +258,10 @@ static const RadParamSpec pGemmBias[] = { P_INT("M"), P_INT("N"), P_INT("K"),
|
||||
static const RadOperandSpec oGemmBias[] = { OPD("a"), WGT("b"), WGT("bias"), OPD_O("res"),
|
||||
OUT("y") };
|
||||
|
||||
/* `split_n` is libr4d's (docs/OPS.md); accepted and ignored here. */
|
||||
static const RadParamSpec pGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("group"),
|
||||
P_STR("dtype"), P_INTO("pb1"), P_INTO("pb2") };
|
||||
P_STR("dtype"), P_INTO("pb1"), P_INTO("pb2"),
|
||||
P_INTO("split_n") };
|
||||
static const RadParamSpec pGemmQBias[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("group"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oGemmQ[] = { OPD("a"), OPD_O("a_scale"), WGT("b"), WGT("b_scale"),
|
||||
|
||||
+4
-2
@@ -309,7 +309,8 @@ void r4d_ar_wht6_dims(int* group, int* bits, int* chunk_elems); // rotated-6-bit
|
||||
void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, long my_flags,
|
||||
long seq_ctrs, long slot_stride16, long inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream, long nblocks,
|
||||
long nthreads, long drain, long acq);
|
||||
long nthreads, long drain, long acq, long dpitch,
|
||||
long spitch);
|
||||
|
||||
// ---- all-reduce: one-shot, push, 4 or 8 ranks over P2P -------------------------------------
|
||||
// The same algorithm at width (r4d_ar_oneshot_Nrank_exact.hip). Peer arguments are in ascending
|
||||
@@ -343,7 +344,8 @@ void r4d_ar_gather_3rank(long peer_scratch0, long peer_scratch1, long peer_flags
|
||||
long peer_flags1, long my_scratch, long my_flags, long seq_ctrs,
|
||||
long slot_stride16, long inp, long out, long n_elem, long dtype,
|
||||
long rank, long row_elems, long stream, long nblocks, long nthreads,
|
||||
long drain, long acq, long pub);
|
||||
long drain, long acq, long pub, long dpitch,
|
||||
long spitch);
|
||||
void r4d_ar_oneshot_8rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
long peer_scratch3, long peer_scratch4, long peer_scratch5,
|
||||
long peer_scratch6, long peer_flags0, long peer_flags1,
|
||||
|
||||
+44
-17
@@ -752,7 +752,7 @@ extern "C" void r4d_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long
|
||||
long slot_stride16, long inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub,
|
||||
long gate, long gate_ctr);
|
||||
long gate, long gate_ctr, long dpitch, long spitch);
|
||||
extern "C" void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
long peer_scratch3, long peer_scratch4, long peer_scratch5,
|
||||
long peer_scratch6, long peer_flags0, long peer_flags1,
|
||||
@@ -761,11 +761,14 @@ extern "C" void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp,
|
||||
long out, long n_elem, long dtype, long rank, long row_elems,
|
||||
long stream, long nblocks, long nthreads, long drain, long acq,
|
||||
long pub);
|
||||
long pub, long dpitch, long spitch);
|
||||
|
||||
/* Not ar_setup: an all-gather's output is world_size times its input. */
|
||||
/* Not ar_setup: an all-gather's output is world_size times its input. With `row`, `y` may also be
|
||||
* the leading world_size * row columns of a wider buffer and `x` a row-wide window of one
|
||||
* (docs/OPS.md); `*dpitch_out` and `*spitch_out` are then their row pitches in elements, else 0. */
|
||||
static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in, ArCall* c,
|
||||
long long* row_out) {
|
||||
long long* row_out, long long* dpitch_out,
|
||||
long long* spitch_out) {
|
||||
const ArInst* in = (const ArInst*)(a ? a->instance : nullptr);
|
||||
if (!in) return RAD_E_STATE; /* init did not run: a core sequencing bug */
|
||||
if (in->world != ws) return RAD_E_SHAPE; /* the peer bundle is compiled for this width */
|
||||
@@ -773,7 +776,6 @@ static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in,
|
||||
const RadTensor* x = r4d_opt(a, AR_X);
|
||||
const RadTensor* y = r4d_opt(a, AR_Y);
|
||||
if (!x || !y) return RAD_E_INVAL; /* the gather has no in-place form */
|
||||
if (!r4d_contig(x) || !r4d_contig(y)) return RAD_E_STRIDE;
|
||||
if (y->data == x->data) return RAD_E_INVAL;
|
||||
|
||||
const long n = (long)r4d_numel(x);
|
||||
@@ -783,6 +785,27 @@ static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in,
|
||||
|
||||
const long long row = r4d_pgeti_or(a->p, a->n_p, "row", 0);
|
||||
if (row < 0 || (row > 0 && (n % row || (row * in->esize) % 16))) return RAD_E_SHAPE;
|
||||
/* A row-interleaved gather's [rows][world * row] may sit in the leading columns of a wider
|
||||
* buffer, the pitch in whole 16-byte words: rows land at that pitch, the rest of each row is
|
||||
* left alone. */
|
||||
long long dpitch = 0;
|
||||
if (!r4d_contig(y)) {
|
||||
const long long p = r4d_row_pitch(y);
|
||||
if (row <= 0 || y->rank != 2 || p <= 0 || y->shape[1] != (int64_t)row * in->world ||
|
||||
(p * in->esize) % 16)
|
||||
return RAD_E_STRIDE;
|
||||
dpitch = p;
|
||||
}
|
||||
/* Likewise x may be [rows][row] read at a wider pitch: a rank's window of a row it computed
|
||||
* wider (a split linear's block-aligned superset, arch/common/rad_block_dflash2.h). */
|
||||
long long spitch = 0;
|
||||
if (!r4d_contig(x)) {
|
||||
const long long p = r4d_row_pitch(x);
|
||||
if (row <= 0 || x->rank != 2 || p <= 0 || x->shape[1] != (int64_t)row ||
|
||||
(p * in->esize) % 16)
|
||||
return RAD_E_STRIDE;
|
||||
spitch = p;
|
||||
}
|
||||
|
||||
if (((long long)n * in->esize) % 16) return RAD_E_ALIGN;
|
||||
if (!r4d_aligned(x->data, 16) || !r4d_aligned(y->data, 16)) return RAD_E_ALIGN;
|
||||
@@ -804,43 +827,46 @@ static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in,
|
||||
c->acq = cfgv.acq;
|
||||
c->pub = cfgv.pub;
|
||||
*row_out = row;
|
||||
*dpitch_out = dpitch;
|
||||
*spitch_out = spitch;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_2rank(const RadArgs* a, RadStream s) {
|
||||
const ArInst* in = nullptr;
|
||||
ArCall c;
|
||||
long long row = 0;
|
||||
int rc = ar_gather_wide_setup(a, 2, &in, &c, &row);
|
||||
long long row = 0, dpitch = 0, spitch = 0;
|
||||
int rc = ar_gather_wide_setup(a, 2, &in, &c, &row, &dpitch, &spitch);
|
||||
if (rc != RAD_OK) return rc;
|
||||
R4D_TRY("ar_gather_2rank",
|
||||
r4d_ar_gather_2rank(in->peer_scratch[0], in->my_scratch, in->peer_flags[0], in->my_flags,
|
||||
in->my_seq, in->slot16, c.inp, c.out, c.numel, in->dtype, in->rank,
|
||||
(long)row, (long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain,
|
||||
c.acq));
|
||||
c.acq, (long)dpitch, (long)spitch));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_3rank(const RadArgs* a, RadStream s) {
|
||||
const ArInst* in = nullptr;
|
||||
ArCall c;
|
||||
long long row = 0;
|
||||
int rc = ar_gather_wide_setup(a, 3, &in, &c, &row);
|
||||
long long row = 0, dpitch = 0, spitch = 0;
|
||||
int rc = ar_gather_wide_setup(a, 3, &in, &c, &row, &dpitch, &spitch);
|
||||
if (rc != RAD_OK) return rc;
|
||||
R4D_TRY("ar_gather_3rank",
|
||||
r4d_ar_gather_3rank(in->peer_scratch[0], in->peer_scratch[1],
|
||||
in->peer_flags[0], in->peer_flags[1],
|
||||
in->my_scratch, in->my_flags, in->my_seq, in->slot16,
|
||||
c.inp, c.out, c.numel, in->dtype, in->rank, (long)row,
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub));
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub,
|
||||
(long)dpitch, (long)spitch));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_4rank(const RadArgs* a, RadStream s) {
|
||||
const ArInst* in = nullptr;
|
||||
ArCall c;
|
||||
long long row = 0;
|
||||
int rc = ar_gather_wide_setup(a, 4, &in, &c, &row);
|
||||
long long row = 0, dpitch = 0, spitch = 0;
|
||||
int rc = ar_gather_wide_setup(a, 4, &in, &c, &row, &dpitch, &spitch);
|
||||
if (rc != RAD_OK) return rc;
|
||||
/* One handshake, placement after it local: the release is the one-shot's. */
|
||||
const long gate = link_gate(a), early = in->host ? 0L : gate;
|
||||
@@ -850,15 +876,15 @@ extern "C" int r4d_rad_ar_gather_4rank(const RadArgs* a, RadStream s) {
|
||||
in->my_scratch, in->my_flags, in->my_seq, in->slot16,
|
||||
c.inp, c.out, c.numel, in->dtype, in->rank, (long)row,
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub,
|
||||
early, early ? in->gate_ctr : 0L));
|
||||
early, early ? in->gate_ctr : 0L, (long)dpitch, (long)spitch));
|
||||
return early ? RAD_LINK_ENDED : RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ar_gather_8rank(const RadArgs* a, RadStream s) {
|
||||
const ArInst* in = nullptr;
|
||||
ArCall c;
|
||||
long long row = 0;
|
||||
int rc = ar_gather_wide_setup(a, 8, &in, &c, &row);
|
||||
long long row = 0, dpitch = 0, spitch = 0;
|
||||
int rc = ar_gather_wide_setup(a, 8, &in, &c, &row, &dpitch, &spitch);
|
||||
if (rc != RAD_OK) return rc;
|
||||
R4D_TRY("ar_gather_8rank",
|
||||
r4d_ar_gather_8rank(in->peer_scratch[0], in->peer_scratch[1], in->peer_scratch[2],
|
||||
@@ -869,7 +895,8 @@ extern "C" int r4d_rad_ar_gather_8rank(const RadArgs* a, RadStream s) {
|
||||
in->peer_flags[6],
|
||||
in->my_scratch, in->my_flags, in->my_seq, in->slot16,
|
||||
c.inp, c.out, c.numel, in->dtype, in->rank, (long)row,
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub));
|
||||
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub,
|
||||
(long)dpitch, (long)spitch));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,11 +38,20 @@ __device__ __forceinline__ unsigned int r4d_g_load_acq(const unsigned int* p) {
|
||||
//
|
||||
// The vocab-parallel sampler's merge reads one row of gathered candidates per position at a single
|
||||
// row stride, which the concatenated form cannot express. Interleaving costs one division per word
|
||||
// and saves a transpose launch.
|
||||
__device__ __forceinline__ int r4d_g_dst(int k, int row16, int n16, int who) {
|
||||
// and saves a transpose launch. `dp16` is the output's row pitch in words: 2 * row16 for a tight
|
||||
// [rows][2][row16], more when the gathered rows are the leading columns of a wider buffer.
|
||||
__device__ __forceinline__ int r4d_g_dst(int k, int row16, int n16, int who, int dp16) {
|
||||
if (row16 <= 0 || row16 >= n16) return who * n16 + k;
|
||||
const int m = k / row16, c = k - m * row16;
|
||||
return m * (2 * row16) + who * row16 + c;
|
||||
return m * dp16 + who * row16 + c;
|
||||
}
|
||||
|
||||
// Where word k of this rank's contribution is read: `sp16` is the input's row pitch in words,
|
||||
// row16 when its rows are tight, more when they are a window of a wider row.
|
||||
__device__ __forceinline__ int r4d_g_src(int k, int row16, int n16, int sp16) {
|
||||
if (row16 <= 0 || row16 >= n16 || sp16 == row16) return k;
|
||||
const int m = k / row16, c = k - m * row16;
|
||||
return m * sp16 + c;
|
||||
}
|
||||
|
||||
// Block b owns words [start,end) of the 16B-word space and drives its own flag handshake; no
|
||||
@@ -50,7 +59,8 @@ __device__ __forceinline__ int r4d_g_dst(int k, int row16, int n16, int who) {
|
||||
__global__ void r4d_ar_gather_2rank_kernel(
|
||||
const int4* __restrict__ in4, int4* peer_base, const int4* my_base, int4* __restrict__ out4,
|
||||
unsigned int* peer_flags, unsigned int* my_flags, unsigned int* seq_ctrs,
|
||||
int n16, int slot_stride16, int rank, int row16, int drain, int acq, unsigned int nbw) {
|
||||
int n16, int slot_stride16, int rank, int row16, int dp16, int sp16, int drain, int acq,
|
||||
unsigned int nbw) {
|
||||
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
|
||||
__shared__ unsigned int s_seq, s_slot;
|
||||
r4d_ar_seq_take(seq_ctrs, nbw, s_seq, s_slot); // device-resident, replay-safe
|
||||
@@ -68,14 +78,20 @@ __global__ void r4d_ar_gather_2rank_kernel(
|
||||
{
|
||||
int k = start + tid;
|
||||
for (; k + 3 * nt < end; k += 4 * nt) {
|
||||
int4 v0 = in4[k], v1 = in4[k + nt], v2 = in4[k + 2 * nt], v3 = in4[k + 3 * nt];
|
||||
int4 v0 = in4[r4d_g_src(k, row16, n16, sp16)], v1 = in4[r4d_g_src(k + nt, row16, n16, sp16)],
|
||||
v2 = in4[r4d_g_src(k + 2 * nt, row16, n16, sp16)],
|
||||
v3 = in4[r4d_g_src(k + 3 * nt, row16, n16, sp16)];
|
||||
ps[k] = v0; ps[k + nt] = v1; ps[k + 2 * nt] = v2; ps[k + 3 * nt] = v3;
|
||||
out4[r4d_g_dst(k, row16, n16, rank)] = v0;
|
||||
out4[r4d_g_dst(k + nt, row16, n16, rank)] = v1;
|
||||
out4[r4d_g_dst(k + 2 * nt, row16, n16, rank)] = v2;
|
||||
out4[r4d_g_dst(k + 3 * nt, row16, n16, rank)] = v3;
|
||||
out4[r4d_g_dst(k, row16, n16, rank, dp16)] = v0;
|
||||
out4[r4d_g_dst(k + nt, row16, n16, rank, dp16)] = v1;
|
||||
out4[r4d_g_dst(k + 2 * nt, row16, n16, rank, dp16)] = v2;
|
||||
out4[r4d_g_dst(k + 3 * nt, row16, n16, rank, dp16)] = v3;
|
||||
}
|
||||
for (; k < end; k += nt) {
|
||||
int4 v = in4[r4d_g_src(k, row16, n16, sp16)];
|
||||
ps[k] = v;
|
||||
out4[r4d_g_dst(k, row16, n16, rank, dp16)] = v;
|
||||
}
|
||||
for (; k < end; k += nt) { int4 v = in4[k]; ps[k] = v; out4[r4d_g_dst(k, row16, n16, rank)] = v; }
|
||||
}
|
||||
// Drain modes, as in the all-reduce: order the pushes before the release flag so the peer cannot
|
||||
// see the flag before the data.
|
||||
@@ -100,19 +116,19 @@ __global__ void r4d_ar_gather_2rank_kernel(
|
||||
const int who = 1 - rank;
|
||||
int k = start + tid;
|
||||
for (; k + 3 * nt < end; k += 4 * nt) {
|
||||
out4[r4d_g_dst(k, row16, n16, who)] = ms[k];
|
||||
out4[r4d_g_dst(k + nt, row16, n16, who)] = ms[k + nt];
|
||||
out4[r4d_g_dst(k + 2 * nt, row16, n16, who)] = ms[k + 2 * nt];
|
||||
out4[r4d_g_dst(k + 3 * nt, row16, n16, who)] = ms[k + 3 * nt];
|
||||
out4[r4d_g_dst(k, row16, n16, who, dp16)] = ms[k];
|
||||
out4[r4d_g_dst(k + nt, row16, n16, who, dp16)] = ms[k + nt];
|
||||
out4[r4d_g_dst(k + 2 * nt, row16, n16, who, dp16)] = ms[k + 2 * nt];
|
||||
out4[r4d_g_dst(k + 3 * nt, row16, n16, who, dp16)] = ms[k + 3 * nt];
|
||||
}
|
||||
for (; k < end; k += nt) out4[r4d_g_dst(k, row16, n16, who)] = ms[k];
|
||||
for (; k < end; k += nt) out4[r4d_g_dst(k, row16, n16, who, dp16)] = ms[k];
|
||||
}
|
||||
}
|
||||
|
||||
void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, long my_flags,
|
||||
long seq_ctrs, long slot_stride16, long inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream_i, long nblocks,
|
||||
long nthreads, long drain, long acq) {
|
||||
long nthreads, long drain, long acq, long dpitch, long spitch) {
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
const int esize = (dtype == 2) ? 4 : 2;
|
||||
const long nbytes = (long)n_elem * esize;
|
||||
@@ -126,6 +142,19 @@ void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, lo
|
||||
if (row_elems > 0 && (rowb % 16 != 0 || nbytes % rowb != 0))
|
||||
throw std::runtime_error("all_gather: row is not a whole number of 16B words");
|
||||
const int row16 = row_elems > 0 ? (int)(rowb / 16) : 0;
|
||||
// The output's row pitch in elements, 0 for a tight [rows][2][row]: whole words, at least the
|
||||
// two ranks' row.
|
||||
const long dpb = (long)dpitch * esize;
|
||||
if (dpitch > 0 && (row16 <= 0 || dpb % 16 != 0 || dpitch < 2 * row_elems))
|
||||
throw std::runtime_error("all_gather: the output pitch is not a whole number of 16B words "
|
||||
"at least two rows wide");
|
||||
const int dp16 = dpitch > 0 ? (int)(dpb / 16) : 2 * row16;
|
||||
// The input's row pitch in elements, 0 for tight rows: whole words, at least a row.
|
||||
const long spb = (long)spitch * esize;
|
||||
if (spitch > 0 && (row16 <= 0 || spb % 16 != 0 || spitch < row_elems))
|
||||
throw std::runtime_error("all_gather: the input pitch is not a whole number of 16B words "
|
||||
"at least a row wide");
|
||||
const int sp16 = spitch > 0 ? (int)(spb / 16) : row16;
|
||||
int nb = (int)nblocks;
|
||||
if (nb < 1) nb = 1;
|
||||
if (nb > n16) nb = n16;
|
||||
@@ -139,5 +168,5 @@ void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, lo
|
||||
r4d_ar_gather_2rank_kernel<<<nb, nt, 0, st>>>(
|
||||
(const int4*)inp, (int4*)peer_scratch, (const int4*)my_scratch, (int4*)out,
|
||||
(unsigned*)peer_flags, (unsigned*)my_flags, (unsigned*)seq_ctrs, n16,
|
||||
(int)slot_stride16, (int)rank, row16, (int)drain, (int)acq, nbw);
|
||||
(int)slot_stride16, (int)rank, row16, dp16, sp16, (int)drain, (int)acq, nbw);
|
||||
}
|
||||
@@ -32,14 +32,15 @@ extern "C" void r4d_ar_gather_3rank(long peer_scratch0, long peer_scratch1,
|
||||
long my_scratch, long my_flags, long seq_ctrs,
|
||||
long slot_stride16, long inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub);
|
||||
long nblocks, long nthreads, long drain, long acq, long pub,
|
||||
long dpitch, long spitch);
|
||||
extern "C" void r4d_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
long peer_flags0, long peer_flags1, long peer_flags2,
|
||||
long my_scratch, long my_flags, long seq_ctrs,
|
||||
long slot_stride16, long inp, long out, long n_elem,
|
||||
long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub,
|
||||
long gate, long gate_ctr);
|
||||
long gate, long gate_ctr, long dpitch, long spitch);
|
||||
extern "C" void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
long peer_scratch3, long peer_scratch4, long peer_scratch5,
|
||||
long peer_scratch6, long peer_flags0, long peer_flags1,
|
||||
@@ -48,14 +49,23 @@ extern "C" void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp,
|
||||
long out, long n_elem, long dtype, long rank, long row_elems,
|
||||
long stream, long nblocks, long nthreads, long drain, long acq,
|
||||
long pub);
|
||||
long pub, long dpitch, long spitch);
|
||||
|
||||
// Where a rank's words land: the 2-rank r4d_g_dst generalised to WS. Named differently because two
|
||||
// __device__ definitions of one name in the two units would violate the ODR.
|
||||
__device__ __forceinline__ int r4d_g_dst_ws(int k, int row16, int n16, int WS, int who) {
|
||||
// `dp16` is the output's row pitch in words, WS * row16 when the gathered rows are tight.
|
||||
__device__ __forceinline__ int r4d_g_dst_ws(int k, int row16, int n16, int who, int dp16) {
|
||||
if (row16 <= 0 || row16 >= n16) return who * n16 + k;
|
||||
const int m = k / row16, c = k - m * row16;
|
||||
return m * (WS * row16) + who * row16 + c;
|
||||
return m * dp16 + who * row16 + c;
|
||||
}
|
||||
|
||||
// Where word k of this rank's contribution is read: `sp16` is the input's row pitch in words,
|
||||
// row16 when its rows are tight, more when they are a window of a wider row.
|
||||
__device__ __forceinline__ int r4d_g_src_ws(int k, int row16, int n16, int sp16) {
|
||||
if (row16 <= 0 || row16 >= n16 || sp16 == row16) return k;
|
||||
const int m = k / row16, c = k - m * row16;
|
||||
return m * sp16 + c;
|
||||
}
|
||||
|
||||
// Block b owns words [start,end) of the 16B-word space and drives its own handshake; no cross-block
|
||||
@@ -65,8 +75,8 @@ __global__ void r4d_ar_gather_wide_kernel(
|
||||
const int4* __restrict__ in4, R4DArWidePeers<int4, WS - 1> peers,
|
||||
const int4* __restrict__ my_base, int4* __restrict__ out4,
|
||||
unsigned int* __restrict__ my_flags, unsigned int* __restrict__ seq_ctrs,
|
||||
int n16, int slot_stride16, int rank, int row16, int drain, int acq, int pub,
|
||||
long long* gate, unsigned int* gate_ctr) {
|
||||
int n16, int slot_stride16, int rank, int row16, int dp16, int sp16, int drain, int acq,
|
||||
int pub, long long* gate, unsigned int* gate_ctr) {
|
||||
constexpr int NPEERS = WS - 1;
|
||||
// Placement holds WS source words live per output word, so the 2x unroll spills at ws=8 (same
|
||||
// limit as the one-shot all-reduce).
|
||||
@@ -95,14 +105,17 @@ __global__ void r4d_ar_gather_wide_kernel(
|
||||
{
|
||||
int k = start + tid;
|
||||
for (; k + 3 * nt < end; k += 4 * nt) {
|
||||
const int4 v0 = in4[k], v1 = in4[k + nt], v2 = in4[k + 2 * nt], v3 = in4[k + 3 * nt];
|
||||
const int4 v0 = in4[r4d_g_src_ws(k, row16, n16, sp16)];
|
||||
const int4 v1 = in4[r4d_g_src_ws(k + nt, row16, n16, sp16)];
|
||||
const int4 v2 = in4[r4d_g_src_ws(k + 2 * nt, row16, n16, sp16)];
|
||||
const int4 v3 = in4[r4d_g_src_ws(k + 3 * nt, row16, n16, sp16)];
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) {
|
||||
ps[p][k] = v0; ps[p][k + nt] = v1; ps[p][k + 2 * nt] = v2; ps[p][k + 3 * nt] = v3;
|
||||
}
|
||||
}
|
||||
for (; k < end; k += nt) {
|
||||
const int4 v = in4[k];
|
||||
const int4 v = in4[r4d_g_src_ws(k, row16, n16, sp16)];
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) ps[p][k] = v;
|
||||
}
|
||||
@@ -117,7 +130,7 @@ __global__ void r4d_ar_gather_wide_kernel(
|
||||
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
|
||||
|
||||
// Place every rank's slice. Slot [parity][our rank] of our own scratch is never written, so, as
|
||||
// in the all-reduce, our rank's source is the input itself and the loop is branch-free.
|
||||
// in the all-reduce, our rank's source is the input itself, read at its own pitch.
|
||||
const int4* src[WS];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r)
|
||||
@@ -127,17 +140,19 @@ __global__ void r4d_ar_gather_wide_kernel(
|
||||
int k = start + tid;
|
||||
if (RU == 2) {
|
||||
for (; k + nt < end; k += 2 * nt) {
|
||||
const int k0 = r4d_g_src_ws(k, row16, n16, sp16), k1 = r4d_g_src_ws(k + nt, row16, n16, sp16);
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r) {
|
||||
out4[r4d_g_dst_ws(k, row16, n16, WS, r)] = src[r][k];
|
||||
out4[r4d_g_dst_ws(k + nt, row16, n16, WS, r)] = src[r][k + nt];
|
||||
out4[r4d_g_dst_ws(k, row16, n16, r, dp16)] = src[r][r == rank ? k0 : k];
|
||||
out4[r4d_g_dst_ws(k + nt, row16, n16, r, dp16)] = src[r][r == rank ? k1 : k + nt];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (; k < end; k += nt) {
|
||||
const int k0 = r4d_g_src_ws(k, row16, n16, sp16);
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r)
|
||||
out4[r4d_g_dst_ws(k, row16, n16, WS, r)] = src[r][k];
|
||||
out4[r4d_g_dst_ws(k, row16, n16, r, dp16)] = src[r][r == rank ? k0 : k];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,7 +165,7 @@ static void ar_gather_wide_launch(const long* peer_scratch, const long* peer_fla
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp,
|
||||
long out, long n_elem, long dtype, long rank, long row_elems,
|
||||
long stream_i, long nblocks, long nthreads, long drain, long acq,
|
||||
long pub, long gate, long gate_ctr) {
|
||||
long pub, long gate, long gate_ctr, long dpitch, long spitch) {
|
||||
constexpr int NPEERS = WS - 1;
|
||||
static_assert(NPEERS <= R4D_AR_WIDE_MAX_PEERS, "npeers exceeds the peer bundle bound");
|
||||
|
||||
@@ -173,6 +188,20 @@ static void ar_gather_wide_launch(const long* peer_scratch, const long* peer_fla
|
||||
if (row_elems > 0 && (rowb % 16 != 0 || nbytes % rowb != 0))
|
||||
throw std::runtime_error("all_gather: row is not a whole number of 16B words");
|
||||
const int row16 = row_elems > 0 ? (int)(rowb / 16) : 0;
|
||||
// The output's row pitch in elements, 0 for a tight [rows][WS][row]: whole words, at least the
|
||||
// WS ranks' row (the gathered rows as the leading columns of a wider buffer).
|
||||
const long dpb = (long)dpitch * esize;
|
||||
if (dpitch > 0 && (row16 <= 0 || dpb % 16 != 0 || dpitch < (long)WS * row_elems))
|
||||
throw std::runtime_error("all_gather: the output pitch is not a whole number of 16B words "
|
||||
"at least " + std::to_string(WS) + " rows wide");
|
||||
const int dp16 = dpitch > 0 ? (int)(dpb / 16) : WS * row16;
|
||||
// The input's row pitch in elements, 0 for tight rows: whole words, at least a row (a rank's
|
||||
// window of a wider row).
|
||||
const long spb = (long)spitch * esize;
|
||||
if (spitch > 0 && (row16 <= 0 || spb % 16 != 0 || spitch < row_elems))
|
||||
throw std::runtime_error("all_gather: the input pitch is not a whole number of 16B words "
|
||||
"at least a row wide");
|
||||
const int sp16 = spitch > 0 ? (int)(spb / 16) : row16;
|
||||
|
||||
int nb = (int)nblocks;
|
||||
if (nb < 1) nb = R4D_AR_NB_DESIGN;
|
||||
@@ -209,8 +238,8 @@ static void ar_gather_wide_launch(const long* peer_scratch, const long* peer_fla
|
||||
hipStream_t st = (hipStream_t)stream_i;
|
||||
r4d_ar_gather_wide_kernel<WS><<<nb, nt, 0, st>>>(
|
||||
(const int4*)inp, peers, (const int4*)my_scratch, (int4*)out, (unsigned int*)my_flags,
|
||||
(unsigned int*)seq_ctrs, n16, (int)slot_stride16, (int)rank, row16, (int)drain, (int)acq,
|
||||
(int)pub, (long long*)gate, (unsigned int*)gate_ctr);
|
||||
(unsigned int*)seq_ctrs, n16, (int)slot_stride16, (int)rank, row16, dp16, sp16, (int)drain,
|
||||
(int)acq, (int)pub, (long long*)gate, (unsigned int*)gate_ctr);
|
||||
}
|
||||
|
||||
/* Three ranks, e.g. Qwen3.8-Flash-Next padded to 36 heads / 3 KV heads (docs/TP3.md). */
|
||||
@@ -218,13 +247,14 @@ void r4d_ar_gather_3rank(long peer_scratch0, long peer_scratch1,
|
||||
long peer_flags0, long peer_flags1, long my_scratch,
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp, long out,
|
||||
long n_elem, long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub) {
|
||||
long nblocks, long nthreads, long drain, long acq, long pub, long dpitch,
|
||||
long spitch) {
|
||||
const long ps[2] = {peer_scratch0, peer_scratch1};
|
||||
const long pf[2] = {peer_flags0, peer_flags1};
|
||||
/* No link gate at three ranks: the row answers 1, the engine releases by barrier. */
|
||||
ar_gather_wide_launch<3>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
|
||||
dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub,
|
||||
0L, 0L);
|
||||
0L, 0L, dpitch, spitch);
|
||||
}
|
||||
|
||||
void r4d_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
@@ -232,12 +262,12 @@ void r4d_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long peer_scrat
|
||||
long my_flags, long seq_ctrs, long slot_stride16, long inp, long out,
|
||||
long n_elem, long dtype, long rank, long row_elems, long stream,
|
||||
long nblocks, long nthreads, long drain, long acq, long pub, long gate,
|
||||
long gate_ctr) {
|
||||
long gate_ctr, long dpitch, long spitch) {
|
||||
const long ps[3] = {peer_scratch0, peer_scratch1, peer_scratch2};
|
||||
const long pf[3] = {peer_flags0, peer_flags1, peer_flags2};
|
||||
ar_gather_wide_launch<4>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
|
||||
dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub,
|
||||
gate, gate_ctr);
|
||||
gate, gate_ctr, dpitch, spitch);
|
||||
}
|
||||
|
||||
void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
@@ -247,7 +277,7 @@ void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scrat
|
||||
long my_scratch, long my_flags, long seq_ctrs, long slot_stride16,
|
||||
long inp, long out, long n_elem, long dtype, long rank, long row_elems,
|
||||
long stream, long nblocks, long nthreads, long drain, long acq,
|
||||
long pub) {
|
||||
long pub, long dpitch, long spitch) {
|
||||
const long ps[7] = {peer_scratch0, peer_scratch1, peer_scratch2, peer_scratch3,
|
||||
peer_scratch4, peer_scratch5, peer_scratch6};
|
||||
const long pf[7] = {peer_flags0, peer_flags1, peer_flags2, peer_flags3,
|
||||
@@ -255,5 +285,5 @@ void r4d_ar_gather_8rank(long peer_scratch0, long peer_scratch1, long peer_scrat
|
||||
/* No link gate at eight ranks: the row answers 1, the engine releases by barrier. */
|
||||
ar_gather_wide_launch<8>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
|
||||
dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub,
|
||||
0L, 0L);
|
||||
0L, 0L, dpitch, spitch);
|
||||
}
|
||||
+24
-10
@@ -931,7 +931,7 @@ extern "C" int r4d_gemm_fp8a8_block(void) { return (int)kBlock; }
|
||||
static int gemm_fp8a8_nt_m16_emit(long a, long ascale, long wq, long ws, long c,
|
||||
int M, int K, int N, int a_ld, int sa_ld, int w_ld, int sw_ld,
|
||||
long scratch, long scratch_bytes, RadStream stream,
|
||||
bool i8 = false);
|
||||
bool i8 = false, int split_n = 0);
|
||||
|
||||
extern "C" int r4d_gemm_fp8a8_nt_m16(long a, long ascale, long wq, long ws, long c,
|
||||
int M, int K, int N, int a_ld, int sa_ld, int w_ld, int sw_ld,
|
||||
@@ -961,12 +961,21 @@ extern "C" void r4d_gemm_fp8a8_pin_nt(int nt) {
|
||||
// Bytes of f32 scratch one r4d_gemm_fp8a8_nt_m16 call needs, or 0 when it will not split. The
|
||||
// caller may pass more, or none (the launch then runs unsplit: correct but slow, and unreported).
|
||||
// The bound is max_m() so the query and the band cannot drift apart.
|
||||
extern "C" long r4d_gemm_fp8a8_nt_m16_scratch(int M, int K, int N) {
|
||||
//
|
||||
// `split_n` (0: N) is the width the split is chosen for. The split depends on N alone, and it
|
||||
// fixes the order K partials are summed in, so a row slice of a wider linear (N rows of a
|
||||
// split_n-row weight) passes the whole width to sum each output as the whole linear does: the
|
||||
// slice's outputs are then the linear's to the bit.
|
||||
static long m16_scratch(int M, int K, int N, int split_n) {
|
||||
if (M <= 0 || K <= 0 || N <= 0 || M > r4d_gemm_fp8a8_nt_m16_max_m()) return 0;
|
||||
uint32_t ck = 0;
|
||||
const uint32_t k2 = narrow_ksplit((uint32_t)N, (uint32_t)K, (uint32_t)M, &ck);
|
||||
const uint32_t k2 = narrow_ksplit((uint32_t)(split_n > 0 ? split_n : N), (uint32_t)K,
|
||||
(uint32_t)M, &ck);
|
||||
return k2 > 1 ? (long)k2 * M * N * (long)sizeof(float) : 0;
|
||||
}
|
||||
extern "C" long r4d_gemm_fp8a8_nt_m16_scratch(int M, int K, int N) {
|
||||
return m16_scratch(M, K, N, 0);
|
||||
}
|
||||
|
||||
// C[M,N] = A[M,K] @ dequant(Wq)[N,K]^T, M <= 64.
|
||||
//
|
||||
@@ -976,10 +985,12 @@ extern "C" long r4d_gemm_fp8a8_nt_m16_scratch(int M, int K, int N) {
|
||||
// ws [ceil(N/128)][ceil(K/128)] bf16, `sw_ld` elements a row (0 = ceil(K/128))
|
||||
// c [M][N] bf16
|
||||
//
|
||||
// Returns 0, or a negative code for a shape it does not serve. `scratch` may be 0.
|
||||
// Returns 0, or a negative code for a shape it does not serve. `scratch` may be 0. `split_n` is
|
||||
// r4d_gemm_fp8a8_nt_m16_scratch's.
|
||||
static int gemm_fp8a8_nt_m16_emit(long a, long ascale, long wq, long ws, long c,
|
||||
int M, int K, int N, int a_ld, int sa_ld, int w_ld, int sw_ld,
|
||||
long scratch, long scratch_bytes, RadStream stream, bool i8) {
|
||||
long scratch, long scratch_bytes, RadStream stream, bool i8,
|
||||
int split_n) {
|
||||
int emit_rc = RAD_OK;
|
||||
if (!a || !ascale || !wq || !ws || !c) return -1;
|
||||
if (M <= 0 || K <= 0 || N <= 0) return -2;
|
||||
@@ -1000,7 +1011,8 @@ static int gemm_fp8a8_nt_m16_emit(long a, long ascale, long wq, long ws, long c,
|
||||
const uint32_t gx = ((uint32_t)N + fm - 1) / fm;
|
||||
|
||||
uint32_t ck = 0;
|
||||
uint32_t ksplit = narrow_ksplit((uint32_t)N, (uint32_t)K, (uint32_t)M, &ck);
|
||||
uint32_t ksplit = narrow_ksplit((uint32_t)(split_n > 0 ? split_n : N), (uint32_t)K,
|
||||
(uint32_t)M, &ck);
|
||||
const long need = ksplit > 1 ? (long)ksplit * M * N * (long)sizeof(float) : 0;
|
||||
if (need && (!scratch || scratch_bytes < need)) { ksplit = 1; ck = 0; }
|
||||
|
||||
@@ -1260,10 +1272,10 @@ static int gemm_8a8_tiled(long a, long ascale, long wq, long ws, long c, int M,
|
||||
// The shared operand reader is r4d_fp8_args.h; see libr4d/r4d_args.h.
|
||||
extern "C" int64_t r4d_rad_gemm_fp8a8_m16_scratch(const RadArgs* a) {
|
||||
const long long M = r4d_geti_or(a, "M", 0), N = r4d_geti_or(a, "N", 0),
|
||||
K = r4d_geti_or(a, "K", 0);
|
||||
K = r4d_geti_or(a, "K", 0), sn = r4d_geti_or(a, "split_n", 0);
|
||||
if (M < 1 || N < 1 || K < 1) return 0;
|
||||
if (M > r4d_gemm_fp8a8_nt_m16_max_m()) return 0;
|
||||
return (int64_t)r4d_gemm_fp8a8_nt_m16_scratch((int)M, (int)K, (int)N);
|
||||
return (int64_t)m16_scratch((int)M, (int)K, (int)N, (int)sn);
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_gemm_fp8a8_m16(const RadArgs* a, RadStream stream) {
|
||||
@@ -1276,7 +1288,8 @@ extern "C" int r4d_rad_gemm_fp8a8_m16(const RadArgs* a, RadStream stream) {
|
||||
const int e = gemm_fp8a8_nt_m16_emit(
|
||||
r4d_addr(g.a), r4d_addr(g.ascale), r4d_addr(g.b), r4d_addr(g.bscale), r4d_addr(g.y),
|
||||
(int)g.M, (int)g.K, (int)g.N, (int)g.a_ld, (int)g.sa_ld, (int)g.w_ld, (int)g.sw_ld,
|
||||
(long)(uintptr_t)a->scratch, (long)a->scratch_bytes, stream);
|
||||
(long)(uintptr_t)a->scratch, (long)a->scratch_bytes, stream, false,
|
||||
(int)r4d_geti_or(a, "split_n", 0));
|
||||
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
||||
}
|
||||
|
||||
@@ -1390,7 +1403,8 @@ extern "C" int r4d_rad_gemm_i8a8_m16(const RadArgs* a, RadStream stream) {
|
||||
const int e = gemm_fp8a8_nt_m16_emit(
|
||||
r4d_addr(g.a), r4d_addr(g.ascale), r4d_addr(g.b), r4d_addr(g.bscale), r4d_addr(g.y),
|
||||
(int)g.M, (int)g.K, (int)g.N, (int)g.a_ld, (int)g.sa_ld, (int)g.w_ld, (int)g.sw_ld,
|
||||
(long)(uintptr_t)a->scratch, (long)a->scratch_bytes, stream, true);
|
||||
(long)(uintptr_t)a->scratch, (long)a->scratch_bytes, stream, true,
|
||||
(int)r4d_geti_or(a, "split_n", 0));
|
||||
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
||||
}
|
||||
|
||||
|
||||
@@ -736,6 +736,9 @@ static const RadParamSpec pGemmNtQ[] = {
|
||||
{ "dtype", RAD_P_STR, RAD_REQUIRED },
|
||||
{ "pb1", RAD_P_INT, RAD_OPTIONAL },
|
||||
{ "pb2", RAD_P_INT, RAD_OPTIONAL },
|
||||
/* The width whose K split a row slice of a wider linear takes (docs/OPS.md); only the narrow
|
||||
* fp8a8 / i8a8 rows split K by N, and they read it. */
|
||||
{ "split_n", RAD_P_INT, RAD_OPTIONAL },
|
||||
};
|
||||
/* docs/OPS.md's `gemm_nt_q` is `a`, `a_scale`, `b`w, `b_scale`w -> `y`. Two of libr4d's six
|
||||
* quantised GEMMs need a seventh operand. RadLayout can declare aux streams beside a weight, but
|
||||
|
||||
+167
-4
@@ -11647,6 +11647,62 @@ static void fp8_numeric(const Plugin& pl, int64_t N, int64_t K) {
|
||||
why.empty(), why);
|
||||
}
|
||||
|
||||
/* A row slice of this linear issued with split_n = N (DenseFP8::declare_slice: a split
|
||||
* fc's share and its tail) must give each of its rows exactly as the whole GEMM does,
|
||||
* whatever K split its own width would pick: half the rows, and the last 128 (which
|
||||
* alone would split K the most). The slice's planes are quantised from its own rows,
|
||||
* which on whole 128-row blocks are the whole weight's codes and scales. */
|
||||
if (N >= 256) {
|
||||
const int64_t cut = (N / 2) / 128 * 128;
|
||||
std::string why;
|
||||
const std::pair<int64_t, int64_t> slices[] = { { 0, cut }, { N - 128, N } };
|
||||
for (const auto& sl : slices) {
|
||||
const int64_t lo = sl.first, hi = sl.second, Ns = hi - lo;
|
||||
std::vector<float> wsl(wf.begin() + lo * K, wf.begin() + hi * K);
|
||||
const Planes psl = fp8_block_planes(wsl, Ns, K);
|
||||
std::vector<RadParam> lps = { PI("N", Ns), PI("K", K), PI("group", 128) };
|
||||
std::vector<uint8_t> qsl;
|
||||
if (stored(nr, lps, 2, psl, { "codes" }, &qsl) != RAD_OK) {
|
||||
why = "the slice's layout hook refused";
|
||||
break;
|
||||
}
|
||||
void* d_wsl = dalloc((size_t)Ns * K);
|
||||
void* d_ysl = dalloc((size_t)M * Ns * 2);
|
||||
if (!d_wsl || !d_ysl) { why = "hipMalloc failed"; break; }
|
||||
st_memcpy(d_wsl, qsl.data(), qsl.size(), hipMemcpyHostToDevice);
|
||||
std::vector<RadTensor> tsl = {
|
||||
T(d_q, RAD_F8E4M3, { M, K }), T(d_qs, RAD_F32, { M, sk }),
|
||||
T(d_wsl, RAD_F8E4M3, { Ns, K }),
|
||||
T((char*)d_s + (size_t)(lo / 128) * (size_t)sk * 2, RAD_BF16,
|
||||
{ Ns / 128, sk }),
|
||||
T(d_ysl, RAD_BF16, { M, Ns }), T(nullptr, RAD_F32, { 1 }),
|
||||
T(nullptr, RAD_U8, { 1 }),
|
||||
};
|
||||
absent(tsl[5]); absent(tsl[6]);
|
||||
std::vector<RadParam> pss = {
|
||||
PI("M", M), PI("N", Ns), PI("K", K), PI("group", 128),
|
||||
PS("dtype", "fp8a8"), PI("split_n", N),
|
||||
};
|
||||
st_memset(d_ysl, 0, (size_t)M * Ns * 2);
|
||||
run_case(nr, tsl, pss);
|
||||
std::vector<uint16_t> ys((size_t)M * Ns);
|
||||
st_memcpy(ys.data(), d_ysl, ys.size() * 2, hipMemcpyDeviceToHost);
|
||||
for (int64_t m = 0; m < M && why.empty(); ++m)
|
||||
for (int64_t r = 0; r < Ns; ++r)
|
||||
if (ys[(size_t)(m * Ns + r)] != got[(size_t)(m * N + lo + r)]) {
|
||||
why = "rows [" + std::to_string((long long)lo) + ", " +
|
||||
std::to_string((long long)hi) + ") differ at m " +
|
||||
std::to_string((long long)m) + " row " +
|
||||
std::to_string((long long)(lo + r));
|
||||
break;
|
||||
}
|
||||
if (!why.empty()) break;
|
||||
}
|
||||
ok((std::string("gemm_fp8a8_nt_m16's row slices given split_n are the whole GEMM's "
|
||||
"rows, M ") + std::to_string((long long)M)).c_str(),
|
||||
why.empty(), why);
|
||||
}
|
||||
|
||||
/* ---- ...and with the gate and the quantiser folded into its epilogue -------------
|
||||
*
|
||||
* Byte equality: the engine substitutes the fold for the pair (this GEMM's bf16
|
||||
@@ -21025,6 +21081,12 @@ struct ArThread {
|
||||
/* Ranks in the collective: 2, or 3 for the wide family at a width that is not a power of
|
||||
* two. */
|
||||
int world = 2;
|
||||
/* The all-gather's output row pitch in elements, 0 for a tight [rows][world][row]; more lands
|
||||
* the gathered rows in the leading columns of a wider buffer. */
|
||||
int64_t pitch = 0;
|
||||
/* Its input row pitch, 0 for tight rows; more reads each row's first `row_elems` out of a wider
|
||||
* row whose other columns hold junk. */
|
||||
int64_t spitch = 0;
|
||||
};
|
||||
|
||||
static void ar_worker(ArThread* t) {
|
||||
@@ -21207,8 +21269,10 @@ static void gather_worker(ArThread* t) {
|
||||
|
||||
void* in = nullptr;
|
||||
void* out = nullptr;
|
||||
const int64_t rows = t->pitch > 0 ? t->numel / t->row_elems : 0;
|
||||
const int64_t out_n = t->pitch > 0 ? rows * t->pitch : t->numel * t->world;
|
||||
if (hipMalloc(&in, (size_t)t->numel * 4) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||
if (hipMalloc(&out, (size_t)t->numel * 4 * t->world) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||
if (hipMalloc(&out, (size_t)out_n * 4) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||
|
||||
std::vector<uint32_t> h((size_t)t->numel);
|
||||
for (int64_t i = 0; i < t->numel; i += 2) {
|
||||
@@ -21216,10 +21280,29 @@ static void gather_worker(ArThread* t) {
|
||||
const float v = (float)(t->rank + 1) * 0.5f + (float)i; /* its logit */
|
||||
std::memcpy(&h[(size_t)i + 1], &v, 4);
|
||||
}
|
||||
st_memcpy(in, h.data(), h.size() * 4, hipMemcpyHostToDevice);
|
||||
st_memset(out, 0xAB, (size_t)t->numel * 4 * t->world); /* marks untouched slices */
|
||||
if (t->spitch > 0) { /* each row's contribution at the front of a wider row of junk */
|
||||
const int64_t srows = t->numel / t->row_elems;
|
||||
std::vector<uint32_t> hp((size_t)(srows * t->spitch), 0xCDCDCDCDu);
|
||||
for (int64_t m = 0; m < srows; ++m)
|
||||
std::memcpy(&hp[(size_t)(m * t->spitch)], &h[(size_t)(m * t->row_elems)],
|
||||
(size_t)t->row_elems * 4);
|
||||
st_free(in);
|
||||
if (hipMalloc(&in, hp.size() * 4) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||
st_memcpy(in, hp.data(), hp.size() * 4, hipMemcpyHostToDevice);
|
||||
} else {
|
||||
st_memcpy(in, h.data(), h.size() * 4, hipMemcpyHostToDevice);
|
||||
}
|
||||
st_memset(out, 0xAB, (size_t)out_n * 4); /* marks untouched slices */
|
||||
|
||||
RadTensor ts[2] = { T(in, RAD_F32, { t->numel }), T(out, RAD_F32, { t->numel * t->world }) };
|
||||
if (t->pitch > 0) { /* [rows][world * row], the leading columns of rows `pitch` apart */
|
||||
ts[1] = T(out, RAD_F32, { rows, t->world * t->row_elems });
|
||||
ts[1].stride[0] = t->pitch;
|
||||
}
|
||||
if (t->spitch > 0) { /* [rows][row], read `spitch` apart */
|
||||
ts[0] = T(in, RAD_F32, { t->numel / t->row_elems, t->row_elems });
|
||||
ts[0].stride[0] = t->spitch;
|
||||
}
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
a.t = ts; a.n_t = 2;
|
||||
@@ -21230,7 +21313,7 @@ static void gather_worker(ArThread* t) {
|
||||
t->rc_launch = t->row->launch(&a, nullptr);
|
||||
st_stream_sync(nullptr);
|
||||
|
||||
t->out.assign((size_t)t->numel * t->world, 0.0f);
|
||||
t->out.assign((size_t)out_n, 0.0f);
|
||||
st_memcpy(t->out.data(), out, t->out.size() * 4, hipMemcpyDeviceToHost);
|
||||
st_free(in);
|
||||
st_free(out);
|
||||
@@ -22310,6 +22393,86 @@ static void gather_wide_rank(const Plugin& pl, const std::vector<int>& devs, con
|
||||
bad == 0, std::to_string(bad) + " words wrong, " + std::to_string(bad_id) +
|
||||
" of them token ids");
|
||||
if (row->fini) for (ArThread& a : t) if (a.inst) row->fini(a.inst);
|
||||
|
||||
/* The same rows landed in the leading columns of a wider buffer, as a row-sharded linear whose
|
||||
* width does not divide among the ranks gathers into the front of its full output row: 64
|
||||
* columns (four words) past world * row, which must keep their marker. */
|
||||
{
|
||||
const int64_t pitch = W * rowe + 64;
|
||||
std::vector<ArThread> tp((size_t)W);
|
||||
std::vector<std::thread> thp;
|
||||
for (int r = 0; r < W; ++r) {
|
||||
tp[(size_t)r] = ArThread{ row, r, devs[(size_t)r], numel };
|
||||
tp[(size_t)r].world = W;
|
||||
tp[(size_t)r].row_elems = rowe;
|
||||
tp[(size_t)r].pitch = pitch;
|
||||
thp.emplace_back(gather_worker, &tp[(size_t)r]);
|
||||
}
|
||||
for (std::thread& x : thp) x.join();
|
||||
bool upp = true;
|
||||
std::string rcp;
|
||||
for (const ArThread& a : tp) {
|
||||
upp = upp && a.rc_init == RAD_OK && a.rc_launch == RAD_OK;
|
||||
rcp += std::to_string(a.rc_init) + "/" + std::to_string(a.rc_launch) + " ";
|
||||
}
|
||||
int64_t badp = 0, clobbered = 0;
|
||||
if (upp)
|
||||
for (const ArThread& a : tp)
|
||||
for (int64_t m = 0; m < nrows; ++m) {
|
||||
for (int r = 0; r < W; ++r)
|
||||
for (int64_t j = 0; j < rowe; ++j) {
|
||||
uint32_t got;
|
||||
std::memcpy(&got, &a.out[(size_t)(m * pitch + r * rowe + j)], 4);
|
||||
if (got != sent(r, m * rowe + j)) ++badp;
|
||||
}
|
||||
for (int64_t j = W * rowe; j < pitch; ++j) {
|
||||
uint32_t got;
|
||||
std::memcpy(&got, &a.out[(size_t)(m * pitch + j)], 4);
|
||||
if (got != 0xABABABABu) ++clobbered;
|
||||
}
|
||||
}
|
||||
ok((w + "-rank all-gather lands its rows at a wider output pitch, the rest untouched").c_str(),
|
||||
upp && badp == 0 && clobbered == 0,
|
||||
upp ? std::to_string(badp) + " words wrong, " + std::to_string(clobbered) +
|
||||
" words past the rows overwritten"
|
||||
: "init/launch rc " + rcp);
|
||||
if (row->fini) for (ArThread& a : tp) if (a.inst) row->fini(a.inst);
|
||||
}
|
||||
|
||||
/* And each rank's rows read out of a wider input row, as a split linear's rank hands over its
|
||||
* window of the block-aligned rows it computed: the junk around the window must not travel. */
|
||||
{
|
||||
const int64_t spitch = rowe + 64;
|
||||
std::vector<ArThread> ts2((size_t)W);
|
||||
std::vector<std::thread> ths;
|
||||
for (int r = 0; r < W; ++r) {
|
||||
ts2[(size_t)r] = ArThread{ row, r, devs[(size_t)r], numel };
|
||||
ts2[(size_t)r].world = W;
|
||||
ts2[(size_t)r].row_elems = rowe;
|
||||
ts2[(size_t)r].spitch = spitch;
|
||||
ths.emplace_back(gather_worker, &ts2[(size_t)r]);
|
||||
}
|
||||
for (std::thread& x : ths) x.join();
|
||||
bool ups = true;
|
||||
std::string rcs2;
|
||||
for (const ArThread& a : ts2) {
|
||||
ups = ups && a.rc_init == RAD_OK && a.rc_launch == RAD_OK;
|
||||
rcs2 += std::to_string(a.rc_init) + "/" + std::to_string(a.rc_launch) + " ";
|
||||
}
|
||||
int64_t bads = 0;
|
||||
if (ups)
|
||||
for (const ArThread& a : ts2)
|
||||
for (int64_t m = 0; m < nrows; ++m)
|
||||
for (int r = 0; r < W; ++r)
|
||||
for (int64_t j = 0; j < rowe; ++j) {
|
||||
uint32_t got;
|
||||
std::memcpy(&got, &a.out[(size_t)(m * W * rowe + r * rowe + j)], 4);
|
||||
if (got != sent(r, m * rowe + j)) ++bads;
|
||||
}
|
||||
ok((w + "-rank all-gather reads each rank's rows at a wider input pitch").c_str(),
|
||||
ups && bads == 0, ups ? std::to_string(bads) + " words wrong" : "init/launch rc " + rcs2);
|
||||
if (row->fini) for (ArThread& a : ts2) if (a.inst) row->fini(a.inst);
|
||||
}
|
||||
ok((w + "-rank all-gather fini ran").c_str(), true);
|
||||
}
|
||||
|
||||
|
||||
@@ -317,8 +317,10 @@ static const RadParamSpec pGemmBias[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_
|
||||
static const RadOperandSpec oGemmBias[] = { OPD("a"), WGT("b"), WGT("bias"), OPD_O("res"),
|
||||
OUT("y") };
|
||||
|
||||
/* `split_n` is libr4d's (docs/OPS.md): ref never splits K, so it accepts and ignores it. */
|
||||
static const RadParamSpec pGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("group"),
|
||||
P_STR("dtype"), P_INTO("pb1"), P_INTO("pb2") };
|
||||
P_STR("dtype"), P_INTO("pb1"), P_INTO("pb2"),
|
||||
P_INTO("split_n") };
|
||||
/* The bias form has no parts (`pb1`/`pb2`); only the plain product supports them. */
|
||||
static const RadParamSpec pGemmQBias[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("group"),
|
||||
P_STR("dtype") };
|
||||
|
||||
+463
-10
@@ -4722,15 +4722,19 @@ struct G431Df {
|
||||
/* The trunk at w4a8 as the -df recipe makes it, the drafter's linears on one 8-bit grid -- the
|
||||
* E4M3 one (block fp8) or the int8 one (w8a8) -- and, with `head`, the table's 8-bit copy in the
|
||||
* same grid. The drafter's keys come FIRST: the fake answers the first key a name contains, and
|
||||
* `attn_q.` is in `dflash.blk.0.attn_q.weight` too. */
|
||||
void g431df_encs(RadBuilder& b, bool i8, bool head) {
|
||||
* `attn_q.` is in `dflash.blk.0.attn_q.weight` too. With `fused`, the trunk's q, k and v are one
|
||||
* linear, as a recipe naming `blk.*.attn_qkv.weight` converts them. */
|
||||
void g431df_encs(RadBuilder& b, bool i8, bool head, bool fused = false) {
|
||||
const RadEncoding w4 = rad_enc_affine(RAD_I4, RAD_F16, 1, 128);
|
||||
const RadEncoding fp8 = rad_enc_affine(RAD_F8E4M3, RAD_BF16, 128, 128);
|
||||
const RadEncoding q8 = rad_enc_affine(RAD_I8, RAD_F16, 1, 128);
|
||||
const RadEncoding& d = i8 ? q8 : fp8;
|
||||
b.encs = { { "dflash.fc.", d }, { "dflash.blk.", d } };
|
||||
if (head) b.encs.push_back({ "dflash.draft_head.", d });
|
||||
for (const char* s : { "attn_q.", "attn_k.", "attn_v.", "attn_o.", "ffn_gate_up.", "ffn_down." })
|
||||
if (fused) b.encs.push_back({ "attn_qkv.", w4 });
|
||||
else
|
||||
for (const char* s : { "attn_q.", "attn_k.", "attn_v." }) b.encs.push_back({ s, w4 });
|
||||
for (const char* s : { "attn_o.", "ffn_gate_up.", "ffn_down." })
|
||||
b.encs.push_back({ s, w4 });
|
||||
}
|
||||
|
||||
@@ -5080,6 +5084,140 @@ TEST(gemma4_31b_dflash_at_four_ranks) {
|
||||
}
|
||||
}
|
||||
|
||||
/* The 31B's prefill in two slices (gemma4_bf16, trunk_overlap). The order and the work: every
|
||||
* trunk reduction rides lane 1 behind its producer and ahead of its consumer and nothing else does;
|
||||
* no two issues on different lanes reach the same rows unordered (`h` carries both reductions); the
|
||||
* cut leaves both slices at least 512 rows; and the slices issue exactly the rows the whole step
|
||||
* issues, the drafter's taps a slice each. Only `h` is taken out of the arena's sharing. A step
|
||||
* under two slices, and a decode step, run whole with their reductions on lane 0. At two ranks the
|
||||
* slices take the plain junctions, the whole step the ones with the all-reduce folded in. */
|
||||
TEST(a_gemma4_31b_prefill_step_runs_as_two_slices_with_its_reductions_on_the_second_lane) {
|
||||
G431Df f;
|
||||
const RadModelMeta meta = f.meta();
|
||||
const int64_t E = 5376, L = 12;
|
||||
for (int world : { 4, 2 }) {
|
||||
auto model = [&](RadBuilder& b, bool lossy) {
|
||||
g431df_encs(b, false, true);
|
||||
RadBuildCtx ctx = build_ctx(0, world);
|
||||
ctx.max_tok = 4096;
|
||||
ctx.max_spec = 15;
|
||||
if (lossy) ctx.tp_wire_lossy = 1; /* nothing shows a slice its bits on that wire */
|
||||
return gemma4_bf16::declare(&b, &meta, &ctx);
|
||||
};
|
||||
auto run = [&](RadBuilder& b, RadCtx& c, RadBatch& batch) {
|
||||
c.b = &b; c.rank = 0; c.world = world; c.batch = &batch;
|
||||
gemma4_bf16::step(&c, &batch);
|
||||
};
|
||||
|
||||
struct Case { int64_t T, cut; };
|
||||
for (const Case k : { Case{4096, 2048}, Case{1100, 512}, Case{1024, 512}, Case{1023, 0} }) {
|
||||
const int32_t out[1] = { (int32_t)(k.T - 1) };
|
||||
RadBuilder b;
|
||||
CHECK_OK(model(b, false));
|
||||
CHECK(noted(b, "prefill overlap: a step of 1024 rows or more runs as two slices"));
|
||||
REQUIRE(b.concurrent.size() == 1);
|
||||
CHECK_EQ(b.concurrent[0], (rad_buf)buf_named(b, "h"));
|
||||
RadBatch batch = one_prompt(k.T, out);
|
||||
RadCtx c;
|
||||
run(b, c, batch);
|
||||
CHECK(c.step_fail.empty());
|
||||
CHECK_EQ(c.lane, 0);
|
||||
CHECK_EQ(races(b, c), 0);
|
||||
|
||||
std::vector<int64_t> lane1;
|
||||
int other1 = 0;
|
||||
for (const auto& i : c.issues) {
|
||||
if (i.lane != 1) continue;
|
||||
const RecOp* o = b.op_of(i.op);
|
||||
REQUIRE(o != nullptr);
|
||||
if (o->op == "all_reduce") lane1.push_back(i.n);
|
||||
else ++other1;
|
||||
}
|
||||
CHECK_EQ(other1, 0);
|
||||
if (k.cut == 0) {
|
||||
CHECK(lane1.empty());
|
||||
} else {
|
||||
REQUIRE(lane1.size() == (size_t)(4 * L));
|
||||
for (int64_t l = 0; l < L; ++l) {
|
||||
CHECK_EQ(lane1[4 * l + 0], k.cut * E); /* attention, slice A */
|
||||
CHECK_EQ(lane1[4 * l + 1], (k.T - k.cut) * E); /* attention, slice B */
|
||||
CHECK_EQ(lane1[4 * l + 2], k.cut * E); /* feed-forward, slice A */
|
||||
CHECK_EQ(lane1[4 * l + 3], (k.T - k.cut) * E); /* feed-forward, slice B */
|
||||
}
|
||||
CHECK_EQ(count_issued(issued_ops(b, c), "ar_rmsnorm_quant_i8"), 0);
|
||||
}
|
||||
|
||||
/* The whole step, on the lossy wire: the same rows by op -- every junction, GEMM and
|
||||
* quantiser over the same rows, the reductions over the same elements. */
|
||||
RadBuilder w;
|
||||
CHECK_OK(model(w, true));
|
||||
CHECK(noted(w, "prefill overlap: off (the lossy all-reduce wire)"));
|
||||
CHECK(w.concurrent.empty());
|
||||
RadBatch wbatch = one_prompt(k.T, out);
|
||||
RadCtx wc;
|
||||
run(w, wc, wbatch);
|
||||
CHECK(wc.step_fail.empty());
|
||||
for (const auto& i : wc.issues) CHECK_EQ(i.lane, 0);
|
||||
std::map<std::string, int64_t> sliced = rows_by_op(b, c, k.T), whole = rows_by_op(w, wc, k.T);
|
||||
if (world == 2 && k.cut == 0) {
|
||||
/* At two ranks on the exact wire a step that runs whole folds all but two of its
|
||||
* reductions -- the embedding's and the last layer's feed-forward -- into its
|
||||
* 2L - 1 junctions; the lossy wire folds none. */
|
||||
CHECK_EQ(sliced["ar_rmsnorm_quant_i8"], (2 * L - 1) * k.T);
|
||||
sliced.erase("ar_rmsnorm_quant_i8");
|
||||
sliced["all_reduce"] += (2 * L - 1) * k.T * E;
|
||||
sliced["rmsnorm_quant_i8"] += (2 * L - 1) * k.T;
|
||||
}
|
||||
CHECK(sliced == whole);
|
||||
CHECK_EQ((int)ns_of(b, c, "cast").size(), k.cut ? 6 : 3); /* three taps, a slice each */
|
||||
}
|
||||
|
||||
/* a decode step runs whole: every reduction on lane 0, nothing on lane 1 */
|
||||
RadBuilder b;
|
||||
CHECK_OK(model(b, false));
|
||||
RadBatch d = decode_batch();
|
||||
RadCtx c;
|
||||
run(b, c, d);
|
||||
CHECK(c.step_fail.empty());
|
||||
CHECK(c.joins.empty());
|
||||
for (const auto& i : c.issues) CHECK_EQ(i.lane, 0);
|
||||
/* folded where the plugin declares the fold at this rank count (two ranks; four with the
|
||||
* four-rank junction), every junction but the last layer's feed-forward one */
|
||||
bool fold = false;
|
||||
for (const auto& o : b.ops) fold = fold || o.op == "ar_rmsnorm_quant_i8";
|
||||
CHECK(fold || world == 4);
|
||||
CHECK_EQ(count_issued(issued_ops(b, c), "ar_rmsnorm_quant_i8"), fold ? 2 * (int)L - 1 : 0);
|
||||
}
|
||||
|
||||
/* the overlap needs the fused junctions: a bf16 trunk keeps its chain and runs whole */
|
||||
RadBuilder b;
|
||||
RadModelMeta plain = gemma4_meta();
|
||||
RadBuildCtx ctx = build_ctx(0, 4);
|
||||
ctx.max_tok = 4096;
|
||||
CHECK_OK(gemma4_bf16::declare(&b, &plain, &ctx));
|
||||
CHECK(noted(b, "prefill overlap: off (the residual junctions are not fused)"));
|
||||
CHECK(b.concurrent.empty());
|
||||
|
||||
/* and the switch: every step whole, nothing on the second lane */
|
||||
setenv("RADIANCE_GEMMA4_OVERLAP", "0", 1);
|
||||
RadBuilder sw;
|
||||
g431df_encs(sw, false, true);
|
||||
RadBuildCtx sctx = build_ctx(0, 4);
|
||||
sctx.max_tok = 4096;
|
||||
sctx.max_spec = 15;
|
||||
const int rc = gemma4_bf16::declare(&sw, &meta, &sctx);
|
||||
unsetenv("RADIANCE_GEMMA4_OVERLAP");
|
||||
CHECK_OK(rc);
|
||||
CHECK(noted(sw, "prefill overlap: off (RADIANCE_GEMMA4_OVERLAP=0)"));
|
||||
CHECK(sw.concurrent.empty());
|
||||
const int32_t out[1] = { 4095 };
|
||||
RadBatch big = one_prompt(4096, out);
|
||||
RadCtx sc; sc.b = &sw; sc.rank = 0; sc.world = 4; sc.batch = &big;
|
||||
gemma4_bf16::step(&sc, &big);
|
||||
CHECK(sc.step_fail.empty());
|
||||
for (const auto& i : sc.issues) CHECK_EQ(i.lane, 0);
|
||||
}
|
||||
|
||||
/* Where no loaded library declares libr4d's fused ops (a CPU-only build, where rad-convert and the
|
||||
* host backend run), asking for one is an error, so the 31B with its DFlash drafter asks first and
|
||||
* declares the chains: the junctions, the attention prologue, the gated quantiser, the folded
|
||||
@@ -5121,23 +5259,26 @@ TEST(gemma4_31b_without_libr4d_declares_the_chains_and_asks_for_no_undeclared_op
|
||||
* passes behind it. The lint holds the buffers each pass reads to what an earlier op of the step
|
||||
* wrote and the planner kept alive, across the passes, so a tap whose plane the planner let another
|
||||
* buffer share, or a drafter buffer read after its arena slot was reused, fails here. Eight
|
||||
* sequences at a block of 16 is 128 query rows, past every fused band the drafter has. */
|
||||
* sequences at a block of 16 is 128 query rows, past every fused band the drafter has. Both trunk
|
||||
* layouts: q, k and v apart, and as one linear, where a one-row decode reads q in the row and a
|
||||
* taller step copies it out. */
|
||||
TEST(gemma4_31b_steps_lint_clean_with_a_dflash_drafter) {
|
||||
G431Df f;
|
||||
const RadModelMeta meta = f.meta();
|
||||
for (int fused = 0; fused < 2; ++fused)
|
||||
for (int i8 = 0; i8 < 2; ++i8)
|
||||
for (int world : { 1, 2, 4 })
|
||||
for (int rank : { 0, world - 1 }) {
|
||||
if (world == 1 && rank > 0) continue;
|
||||
RadBuilder b;
|
||||
g431df_encs(b, i8 == 1, true);
|
||||
g431df_encs(b, i8 == 1, true, fused == 1);
|
||||
RadBuildCtx ctx = build_ctx(rank, world);
|
||||
ctx.max_tok = 2048;
|
||||
ctx.max_spec = 15;
|
||||
REQUIRE_EQ(gemma4_bf16::declare(&b, &meta, &ctx), RAD_OK);
|
||||
REQUIRE_EQ((int)b.drafter.kind, (int)RAD_DRAFT_BLOCK);
|
||||
for (const auto& k : { kit(b, { 1, 1, 1 }, {}), kit(b, { 8, 1 }, { 5 }),
|
||||
kit(b, {}, { 1500 }) }) {
|
||||
for (const auto& k : { kit(b, { 1 }, {}), kit(b, { 1, 1, 1 }, {}),
|
||||
kit(b, { 8, 1 }, { 5 }), kit(b, {}, { 1500 }) }) {
|
||||
const auto v = run_passes(b, gemma4_bf16::step, { k.get() }, rank, world);
|
||||
CHECK_PASSES(b, v);
|
||||
}
|
||||
@@ -5152,6 +5293,304 @@ TEST(gemma4_31b_steps_lint_clean_with_a_dflash_drafter) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ============ gemma4 31B + DFlash2: RedHatAI's speculators-format drafter ============ */
|
||||
namespace {
|
||||
/* The 31B fixture with a DFlash2 drafter in vLLM's speculators layout, as rad-convert merges
|
||||
* RedHatAI/gemma-4-31B-it-speculator.dflash2's config.json: the speculator's own keys at the top of
|
||||
* `draft.`, the decoder layers' config under `draft.transformer_layer_config.`, booleans as 1/0.
|
||||
* Taps at aux ids 1 5 9 (the residual entering those layers) for the fixture's twelve layers. */
|
||||
struct G431Df2 {
|
||||
std::vector<const char*> keys, vals;
|
||||
G431Df2() {
|
||||
keys.assign(std::begin(k_gem4_keys), std::end(k_gem4_keys));
|
||||
vals.assign(std::begin(k_gem4_vals), std::end(k_gem4_vals));
|
||||
const char* kv[][2] = {
|
||||
{ "draft.architectures", "[\"DFlash2DraftModel\"]" },
|
||||
{ "draft.speculators_model_type", "dflash2" },
|
||||
{ "draft.block_size", "8" },
|
||||
{ "draft.mask_token_id", "4" },
|
||||
{ "draft.aux_hidden_state_layer_ids", "[1, 5, 9]" },
|
||||
{ "draft.selector_rank", "256" },
|
||||
{ "draft.selector_top_k", "16" },
|
||||
{ "draft.conv_kernel_size", "2" },
|
||||
{ "draft.conv_group_size", "16" },
|
||||
{ "draft.sample_from_anchor", "0" },
|
||||
{ "draft.sliding_window_non_causal", "1" },
|
||||
{ "draft.draft_vocab_size", "262144" },
|
||||
{ "draft.transformer_layer_config.num_hidden_layers", "5" },
|
||||
{ "draft.transformer_layer_config.head_dim", "256" },
|
||||
{ "draft.transformer_layer_config.num_attention_heads", "32" },
|
||||
{ "draft.transformer_layer_config.num_key_value_heads", "16" },
|
||||
{ "draft.transformer_layer_config.intermediate_size", "21504" },
|
||||
{ "draft.transformer_layer_config.rms_norm_eps", "1e-06" },
|
||||
{ "draft.transformer_layer_config.rope_parameters.rope_theta", "10000.0" },
|
||||
{ "draft.transformer_layer_config.hidden_size", "5376" },
|
||||
{ "draft.transformer_layer_config.vocab_size", "262144" },
|
||||
{ "draft.transformer_layer_config.hidden_act", "silu" },
|
||||
{ "draft.transformer_layer_config.sliding_window", "2048" },
|
||||
{ "draft.transformer_layer_config.use_sliding_window", "1" },
|
||||
{ "draft.transformer_layer_config.layer_types",
|
||||
"[\"sliding_attention\", \"sliding_attention\", \"sliding_attention\", "
|
||||
"\"sliding_attention\", \"sliding_attention\"]" },
|
||||
};
|
||||
for (const auto& p : kv) { keys.push_back(p[0]); vals.push_back(p[1]); }
|
||||
}
|
||||
RadModelMeta meta() const {
|
||||
RadModelMeta m = gemma4_meta();
|
||||
m.n_kv = (int)keys.size();
|
||||
m.kv_key = keys.data();
|
||||
m.kv_val = vals.data();
|
||||
return m;
|
||||
}
|
||||
};
|
||||
|
||||
/* The -df2 recipe's grids: the drafter's linears at block fp8, its head copy int8 (or, without
|
||||
* `head8`, no 8-bit encoding, which leaves the 2-bit grid), and the trunk at w4a8 with q|k|v fused. */
|
||||
void g431df2_encs(RadBuilder& b, bool head8) {
|
||||
g431df_encs(b, false, false, true);
|
||||
if (head8) b.encs.insert(b.encs.begin(), { "dflash.draft_head.", rad_enc_affine(RAD_I8, RAD_F16, 1, 128) });
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
/* The 31B declares RedHatAI's DFlash2 drafter as a block drafter past its trunk, read from the
|
||||
* speculators layout: depth 7 (block 8, the anchor predicting nothing), mask 4, every layer on a
|
||||
* 2048-token window, non-causal, its own windowed KV group of 16 kv heads x 256, the taps on the
|
||||
* layers BEFORE the aux ids (1 5 9 -> 0 4 8), the trunk's table read unscaled, and its head on the
|
||||
* 8-bit copy, the candidates merged across ranks where the head is vocab-sharded. */
|
||||
TEST(gemma4_31b_declares_redhats_dflash2_from_the_speculators_layout) {
|
||||
G431Df2 f;
|
||||
const RadModelMeta meta = f.meta();
|
||||
RadBuildCtx pc = build_ctx();
|
||||
pc.max_spec = 7;
|
||||
int taps[8] = {};
|
||||
CHECK_EQ(gemma4::dflash2_taps(&meta, &pc, 12, taps, 8), 3);
|
||||
CHECK_EQ(taps[0], 0);
|
||||
CHECK_EQ(taps[1], 4);
|
||||
CHECK_EQ(taps[2], 8);
|
||||
RadArchProbe p{};
|
||||
CHECK_OK(gemma4_bf16::probe(&meta, &p));
|
||||
CHECK_EQ(p.draft_depth, 7);
|
||||
CHECK_EQ(p.draft_depth_fixed, 1);
|
||||
|
||||
for (int world : { 1, 4 })
|
||||
for (int head8 = 0; head8 < 2; ++head8) {
|
||||
RadBuilder b;
|
||||
g431df2_encs(b, head8 == 1);
|
||||
RadBuildCtx ctx = build_ctx(0, world);
|
||||
ctx.max_spec = 7;
|
||||
REQUIRE_EQ(gemma4_bf16::declare(&b, &meta, &ctx), RAD_OK);
|
||||
|
||||
CHECK_EQ(b.drafter_name, std::string("dflash2"));
|
||||
CHECK_EQ((int)b.drafter.kind, (int)RAD_DRAFT_BLOCK);
|
||||
CHECK_EQ(b.drafter.depth, 7);
|
||||
CHECK_EQ((int)b.drafter.block, 8);
|
||||
CHECK_EQ(b.drafter.mask_token, 4);
|
||||
CHECK_EQ(b.drafter.window, 2048);
|
||||
CHECK_EQ(b.drafter.proposal, (rad_buf)buf_named(b, "dflash_tokens"));
|
||||
|
||||
const rad_kvgroup kvd = kv_named(b, "kv_draft");
|
||||
REQUIRE(kvd != 0);
|
||||
CHECK_EQ((int)b.kv_groups[kvd - 1].d.kind, (int)RAD_KV_WINDOW);
|
||||
CHECK_EQ(b.kv_groups[kvd - 1].d.head_dim, 256);
|
||||
CHECK_EQ(b.kv_groups[kvd - 1].d.n_head_kv, 16 / world);
|
||||
|
||||
const int src = buf_named(b, "dflash_src");
|
||||
REQUIRE(src > 0);
|
||||
CHECK_EQ(b.bufs[src - 1].d.shape[1], 3 * 5376);
|
||||
|
||||
/* attention: head 256, gqa 2, non-causal, the window */
|
||||
int n_attn = 0;
|
||||
for (const auto& o : b.ops)
|
||||
if (o.op == "attn_paged" && pint(&o, "head_dim") == 256 && pint(&o, "window") == 2048 &&
|
||||
pint(&o, "causal") == 0) {
|
||||
++n_attn;
|
||||
CHECK_EQ(pint(&o, "gqa"), 2);
|
||||
}
|
||||
CHECK_EQ(n_attn, 1);
|
||||
|
||||
/* the drafter's lookup reads the trunk's table without Gemma's scale */
|
||||
const RecOp* te = find_op(b, "embed_lookup", 0);
|
||||
const RecOp* de = find_op(b, "embed_lookup", 1);
|
||||
REQUIRE(te != nullptr && de != nullptr);
|
||||
CHECK(te->w == de->w);
|
||||
CHECK_NEAR(pflt(te, "mult"), 73.5, 1e-9);
|
||||
CHECK(param(de, "mult") == nullptr);
|
||||
|
||||
/* the head: 8-bit where the copy is int8, else the 2-bit grid; merged across ranks */
|
||||
bool head_seen = false;
|
||||
for (const auto& o : b.ops)
|
||||
if (o.op == "gemm_nt_q" && pint(&o, "K") == 5376 && pint(&o, "N") == 262144 / world) {
|
||||
head_seen = true;
|
||||
CHECK_EQ(pstr(&o, "dtype"), std::string(head8 ? "w8a8" : "w2a8"));
|
||||
}
|
||||
CHECK(head_seen);
|
||||
CHECK_EQ(count_op(b, "row_topk_merge"), world > 1 ? 1 : 0);
|
||||
CHECK_EQ(count_op(b, "dflash_select"), 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Every engine step with RedHatAI's DFlash2 behind the trunk lints clean at one, two and four ranks:
|
||||
* decode, a mixed step, a prefill chunk, and a verify step with the drafter's context and query
|
||||
* passes behind it (block 8). */
|
||||
TEST(gemma4_31b_steps_lint_clean_with_a_dflash2_drafter) {
|
||||
G431Df2 f;
|
||||
const RadModelMeta meta = f.meta();
|
||||
for (int world : { 1, 2, 4 })
|
||||
for (int rank : { 0, world - 1 }) {
|
||||
if (world == 1 && rank > 0) continue;
|
||||
RadBuilder b;
|
||||
g431df2_encs(b, true);
|
||||
RadBuildCtx ctx = build_ctx(rank, world);
|
||||
ctx.max_tok = 2048;
|
||||
ctx.max_spec = 7;
|
||||
REQUIRE_EQ(gemma4_bf16::declare(&b, &meta, &ctx), RAD_OK);
|
||||
for (const auto& k : { kit(b, { 1 }, {}), kit(b, { 1, 1, 1 }, {}),
|
||||
kit(b, { 8, 1 }, { 5 }), kit(b, {}, { 1500 }) }) {
|
||||
const auto v = run_passes(b, gemma4_bf16::step, { k.get() }, rank, world);
|
||||
CHECK_PASSES(b, v);
|
||||
}
|
||||
for (int64_t n_seq : { 2, 8 }) {
|
||||
const auto ver = kit(b, std::vector<int>((size_t)n_seq, 8), {});
|
||||
const auto ctxp = context_pass(b, *ver);
|
||||
const auto qry = query_pass(b, n_seq, 8, 7);
|
||||
const auto v = run_passes(b, gemma4_bf16::step,
|
||||
{ ver.get(), ctxp.get(), qry.get() }, rank, world);
|
||||
CHECK_PASSES(b, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* DFlash2's fc, split by rows over the ranks and kept bit for bit. At four ranks each rank hands
|
||||
* over 1344 of c's 5376 columns and computes the 11 whole scale blocks covering them (1408 rows: a
|
||||
* span of the one stored weight, so a container converted before the split serves it), its window
|
||||
* starting 0 or 64 rows in; the GEMM takes its K split from the whole 5376 rows (split_n), and the
|
||||
* all_gather reads each window at the GEMM output's pitch and writes c whole. At two ranks a share
|
||||
* is 21 whole blocks; at one rank the fc is whole, and so is an MXFP4 fc at four (its GEMM picks
|
||||
* its own K split, so only block fp8 splits). */
|
||||
TEST(gemma4_31b_dflash2_fc_splits_by_rows_without_changing_a_bit) {
|
||||
G431Df2 f;
|
||||
const RadModelMeta meta = f.meta();
|
||||
const int64_t E = 5376, K = 3 * E; /* the fixture taps three trunk layers */
|
||||
struct Case { int world; int rank; int64_t lo, hi, share; };
|
||||
for (const Case cs : { Case{ 4, 0, 0, 1408, 1344 }, Case{ 4, 1, 1280, 2688, 1344 },
|
||||
Case{ 4, 2, 2688, 4096, 1344 }, Case{ 4, 3, 3968, 5376, 1344 },
|
||||
Case{ 2, 0, 0, 2688, 2688 }, Case{ 2, 1, 2688, 5376, 2688 },
|
||||
Case{ 1, 0, 0, 0, 0 } }) {
|
||||
RadBuilder b;
|
||||
g431df2_encs(b, true);
|
||||
RadBuildCtx ctx = build_ctx(cs.rank, cs.world);
|
||||
ctx.max_tok = 2048;
|
||||
ctx.max_spec = 7;
|
||||
REQUIRE_EQ(gemma4_bf16::declare(&b, &meta, &ctx), RAD_OK);
|
||||
const RecWeight* fw = find_w(b, "dflash.fc.weight");
|
||||
const RecWeight* fs = find_w(b, "dflash.fc.scale");
|
||||
REQUIRE(fw != nullptr);
|
||||
REQUIRE(fs != nullptr);
|
||||
CHECK_EQ(fw->d.shape[1], K);
|
||||
CHECK(find_w(b, "dflash.fc_tail.weight") == nullptr);
|
||||
int split_gemms = 0, fc_gathers = 0;
|
||||
for (const RecOp& o : b.ops) {
|
||||
if (o.op == "gemm_nt_q" && pint(&o, "split_n") == E) {
|
||||
++split_gemms;
|
||||
CHECK_EQ(pint(&o, "N"), (long long)(cs.hi - cs.lo));
|
||||
}
|
||||
if (o.op == "all_gather" && pint(&o, "row") == cs.share) {
|
||||
++fc_gathers;
|
||||
CHECK_EQ(pint(&o, "numel"), (long long)(2048 * E)); /* the trunk's instance */
|
||||
CHECK_EQ(pint(&o, "world_size"), (long long)cs.world);
|
||||
}
|
||||
}
|
||||
if (!cs.share) {
|
||||
CHECK_EQ(fw->d.shape[0], E);
|
||||
CHECK_EQ(fw->d.shard, RAD_SHARD_NONE);
|
||||
CHECK(!fw->span_set);
|
||||
CHECK_EQ(split_gemms, 0);
|
||||
continue;
|
||||
}
|
||||
CHECK_EQ(fw->d.shape[0], cs.hi - cs.lo);
|
||||
CHECK_EQ(fw->d.shard, RAD_SHARD_ROW);
|
||||
CHECK(fw->span_set);
|
||||
CHECK_EQ(fw->span_lo, cs.lo);
|
||||
CHECK_EQ(fw->span_hi, cs.hi);
|
||||
CHECK(fs->span_set); /* the scale plane's span is in 128-row blocks */
|
||||
CHECK_EQ(fs->span_lo, cs.lo / 128);
|
||||
CHECK_EQ(fs->span_hi, cs.hi / 128);
|
||||
/* the window covers exactly this rank's share */
|
||||
CHECK(cs.lo <= cs.rank * cs.share && (cs.rank + 1) * cs.share <= cs.hi);
|
||||
CHECK_EQ(split_gemms, 1);
|
||||
CHECK_EQ(fc_gathers, 1);
|
||||
}
|
||||
|
||||
/* An MXFP4 drafter: fc whole on every rank. */
|
||||
{
|
||||
RadBuilder b;
|
||||
g431df2_encs(b, true);
|
||||
for (auto& e : b.encs)
|
||||
if (e.first == "dflash.fc." || e.first == "dflash.blk.")
|
||||
e.second = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
|
||||
RadBuildCtx ctx = build_ctx(1, 4);
|
||||
ctx.max_tok = 2048;
|
||||
ctx.max_spec = 7;
|
||||
REQUIRE_EQ(gemma4_bf16::declare(&b, &meta, &ctx), RAD_OK);
|
||||
const RecWeight* fw = find_w(b, "dflash.fc.weight");
|
||||
REQUIRE(fw != nullptr);
|
||||
CHECK_EQ(fw->d.shape[0], E);
|
||||
CHECK_EQ(fw->d.shard, RAD_SHARD_NONE);
|
||||
CHECK(find_w(b, "dflash.fc_tail.weight") == nullptr);
|
||||
for (const RecOp& o : b.ops) CHECK(pint(&o, "split_n") != E);
|
||||
}
|
||||
}
|
||||
|
||||
/* Where the container holds q, k and v as one linear (a recipe naming blk.*.attn_qkv.weight), the
|
||||
* 31B declares one projection a layer -- q | k | v on a sliding layer, q | k on a global one, each
|
||||
* rank its slice of every part -- and none of the three apart, and the junction writes that
|
||||
* linear's int8 pair. The step: four GEMMs a layer instead of six (five on a global layer). A
|
||||
* global layer's attention reads q in the row (attn_paged_qrow); a sliding layer's reads it there
|
||||
* at one row and from attn_qs, copied out, at sixteen. */
|
||||
TEST(gemma4_31b_takes_q_k_and_v_as_one_linear_where_the_container_holds_them_so) {
|
||||
G431Df f;
|
||||
const RadModelMeta meta = f.meta();
|
||||
const int64_t E = 5376, L = 12;
|
||||
for (int world : { 1, 4 }) {
|
||||
RadBuilder b;
|
||||
g431df_encs(b, false, true, true);
|
||||
RadBuildCtx ctx = build_ctx(0, world);
|
||||
ctx.max_tok = 2048;
|
||||
ctx.max_spec = 15;
|
||||
REQUIRE_EQ(gemma4_bf16::declare(&b, &meta, &ctx), RAD_OK);
|
||||
|
||||
for (int64_t l = 0; l < L; ++l) {
|
||||
const bool global = (l + 1) % 6 == 0;
|
||||
const std::string pre = "blk." + std::to_string(l) + ".";
|
||||
const RecWeight* qkv = nullptr;
|
||||
for (const RecWeight& w : b.weights) {
|
||||
if (w.name == pre + "attn_qkv.weight") qkv = &w;
|
||||
for (const char* sep : { "attn_q.weight", "attn_k.weight", "attn_v.weight" })
|
||||
CHECK(w.name != pre + sep);
|
||||
}
|
||||
REQUIRE(qkv);
|
||||
CHECK_EQ(qkv->d.shape[0], (global ? 32 * 512 + 4 * 512 : 32 * 256 + 2 * 16 * 256) / world);
|
||||
CHECK_EQ(qkv->d.shape[1], E);
|
||||
CHECK_EQ((int)qkv->d.n_row_parts, global ? 2 : 3);
|
||||
}
|
||||
|
||||
for (const int q : { 1, 16 }) {
|
||||
const auto k = kit(b, { q }, {});
|
||||
const auto v = run_passes(b, gemma4_bf16::step, { k.get() }, 0, world);
|
||||
CHECK_PASSES(b, v);
|
||||
const std::vector<std::string> ops = issued_ops(b, *v[0]);
|
||||
CHECK_EQ(count_issued(ops, "gemm_nt_q"), (int)(4 * L));
|
||||
CHECK_EQ(count_issued(ops, "attn_paged_qrow"), 2);
|
||||
CHECK_EQ(count_issued(ops, "attn_paged"), 10);
|
||||
CHECK_EQ(count_issued(ops, "quant_act_i8"), (int)L); /* o_proj's input only */
|
||||
CHECK_EQ(count_writes_to(b, *v[0], buf_named(b, "attn_qs")), q == 1 ? 0 : 10);
|
||||
/* the GEMM, then qkv_norm_rope in place */
|
||||
CHECK_EQ(count_writes_to(b, *v[0], buf_named(b, "attn_qkvs")), 2 * 10);
|
||||
CHECK_EQ(count_writes_to(b, *v[0], buf_named(b, "attn_qkvg")), 2 * 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ==================================================================== the step lint, checked */
|
||||
|
||||
namespace {
|
||||
@@ -7321,6 +7760,11 @@ struct ArchCases {
|
||||
b->encs = { { "attn_q.", w4g }, { "attn_k.", w4g }, { "attn_v.", w4g },
|
||||
{ "attn_o.", w4g }, { "ffn_gate_up.", w4g }, { "ffn_down.", w4g } };
|
||||
};
|
||||
/* The same with q, k and v as one linear, as a recipe naming attn_qkv converts it. */
|
||||
auto g4qkv = [this](RadBuilder* b) {
|
||||
b->encs = { { "attn_qkv.", w4g }, { "attn_o.", w4g }, { "ffn_gate_up.", w4g },
|
||||
{ "ffn_down.", w4g } };
|
||||
};
|
||||
/* The ops libref lacks that each still asks for, both two-rank fusions with no chain:
|
||||
* qwen35_fp8's all-reduce norm (layer_fold assumes it at two ranks) and Flash-Next's
|
||||
* all-reduce into the wide stream. */
|
||||
@@ -7365,15 +7809,24 @@ struct ArchCases {
|
||||
add("lfm2_bf16", [this, sc](RadBuilder* b) {
|
||||
auto c = sc(1, 0); return lfm2_bf16::declare(b, &lfm, &c); },
|
||||
lfm2_bf16::step, 0, {}, 0x1775b188dc4a5120ull);
|
||||
/* The first three moved only by the attn_qkv name map each layer declares to ask whether
|
||||
* the container holds q, k and v as one linear: with those maps left out of the hash they
|
||||
* are the earlier pins, 0xafc23087c634a537, 0x719fa07788f8b98a and 0x93b174ab52cc0d31. */
|
||||
add("gemma4_bf16", [this, sc](RadBuilder* b) {
|
||||
auto c = sc(1, 0); return gemma4_bf16::declare(b, &g4, &c); },
|
||||
gemma4_bf16::step, 0, {}, 0xafc23087c634a537ull);
|
||||
gemma4_bf16::step, 0, {}, 0x174e157911e121a9ull);
|
||||
add("gemma4_bf16, w4a8", [this, sc, g4w4](RadBuilder* b) {
|
||||
g4w4(b); auto c = sc(1, 0); return gemma4_bf16::declare(b, &g4, &c); },
|
||||
gemma4_bf16::step, 0, {}, 0x719fa07788f8b98aull);
|
||||
gemma4_bf16::step, 0, {}, 0x4bf536b78c6b11eeull);
|
||||
add("gemma4_bf16, w4a8, two ranks", [this, sc, g4w4](RadBuilder* b) {
|
||||
g4w4(b); auto c = sc(2, 0); return gemma4_bf16::declare(b, &g4, &c); },
|
||||
nullptr, 0, {}, 0x93b174ab52cc0d31ull);
|
||||
nullptr, 0, {}, 0xed7ccbb0c6aecfebull);
|
||||
add("gemma4_bf16, w4a8, fused qkv", [this, sc, g4qkv](RadBuilder* b) {
|
||||
g4qkv(b); auto c = sc(1, 0); return gemma4_bf16::declare(b, &g4, &c); },
|
||||
gemma4_bf16::step, 0, {}, 0x0ae60e16d6b6424cull);
|
||||
add("gemma4_bf16, w4a8, fused qkv, two ranks", [this, sc, g4qkv](RadBuilder* b) {
|
||||
g4qkv(b); auto c = sc(2, 0); return gemma4_bf16::declare(b, &g4, &c); },
|
||||
nullptr, 0, {}, 0xcc58bb53457bca11ull);
|
||||
add("gemma4", [this, sc](RadBuilder* b) {
|
||||
auto c = sc(1, 0); return gemma4::declare(b, &g4m, &c); },
|
||||
gemma4::step, 0, {}, 0x0f227f5853b6488cull);
|
||||
|
||||
Reference in new issue
Block a user