Archived
gemma3: the gated gate_up fold up to 64 rows (RADIANCE_G3_FOLD_ROWS), and prefetch knobs
The gate_up GEMM + gelu_mul + quant_act_fp8 fold ran only up to 8 rows; at 2-8 concurrent translations with speculation a step is 16-64 rows and the three kernels ran apart. gemm_mxfp4a8_gated_gemv_wide (9 <= M <= 64, K >= 2688) is gemv_wide at two column tiles a block, the gate tile j and its up tile n/16 + j: each K-wave loads both tiles' weights and reads the activation once; the wave partials are summed in gemv_wide's order and the narrow fold's epilogue quantises, so codes and scales are byte-identical to the unfused path. No spill (96-196 VGPRs, RT 1-4). RADIANCE_G3_FOLD_ROWS (0 = today's 8) sets the fold band; RADIANCE_G3_PF_ROWS / _PF_MIB / _PF_EARLY expose the prefetch beside the all-reduces (measured: no gain, defaults unchanged). Unset knobs keep the pinned graph and steps. Measured on 2x Radeon AI PRO R9700, TranslateGemma 27B (MXFP4 GPTQ, DFlash2 drafter, speculation 7), greedy held-out translations, aggregate tok/s, 2 interleaved rounds: | fold ceiling | x2 | x4 | x8 | | 8 (today) | 289.9 | 521.2 | 686.3 | | 16 | 298.6 (+3.0 %) | 521.0 (-0.0 %) | 686.9 (+0.1 %) | | 32 | 296.9 (+2.4 %) | 541.6 (+3.9 %) | 682.7 (-0.5 %) | | 64 | 297.8 (+2.7 %) | 549.0 (+5.3 %) | 700.0 (+2.0 %) | Tests: r4d_selftest gated wide fold byte-for-byte against gemv_wide + gelu_mul + quant_act_fp8 (or gated_quant_fp8) for TranslateGemma's 21504x5376 at M 9-64 and other shapes (156 ok, 0 FAIL on gfx1201); arch_test gemma3 16/16 (defaults keep the pinned graph and steps, knobs move only what they name). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
96bc0465b3
commit
b6ad757bfa
10 files changed
+645
-42
No files matched your search
+101
-11
@@ -410,6 +410,10 @@ struct Gemma3Attn {
|
||||
void post(RadCtx* c, int64_t T) const {
|
||||
RAD_ISSUE_N(c, op_post, T, brows(w.h, T), RAD_W(w_post), brows(w.h, T));
|
||||
}
|
||||
/* The block's all-reduce alone, as Gemma3Mlp::ar. */
|
||||
void ar(RadCtx* c, int64_t T) const {
|
||||
if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE);
|
||||
}
|
||||
/* The pipelined prefill (step_pipelined): the block over one prefill sequence in two pieces,
|
||||
* its input pair already written; hands the first piece behind its out projection. */
|
||||
void step_pieces(RadCtx* c, const RadBatch* batch, ArPieces* pc, const int32_t* used0) const;
|
||||
@@ -660,6 +664,10 @@ struct Gemma3Mlp {
|
||||
* GEMM, gelu_mul and quant_act_fp8 it replaces. */
|
||||
rad_op op_fold = 0;
|
||||
int64_t fold_rows = 0;
|
||||
/* Set before declare: the band the fold is declared over (RADIANCE_G3_FOLD_ROWS). libr4d's
|
||||
* gemm_mxfp4a8_gated_gemv serves 1..8 rows and _gated_gemv_wide 9..64, each byte-identical to
|
||||
* the GEMM row of its band (gemv, gemv_wide) followed by gelu_mul and quant_act_fp8. */
|
||||
int64_t fold_max = 8;
|
||||
/* Calibration: gate|up's input and down's (this rank's columns). */
|
||||
G3Tap tap_gu{}, tap_dn{};
|
||||
const bool* tap_live = nullptr;
|
||||
@@ -674,6 +682,11 @@ struct Gemma3Mlp {
|
||||
void post(RadCtx* c, int64_t T) const {
|
||||
RAD_ISSUE_N(c, op_post, T, brows(w.h, T), RAD_W(w_post), brows(w.h, T));
|
||||
}
|
||||
/* The block's all-reduce alone, for a caller that issues it after something of its own (the
|
||||
* step's read beside it, RADIANCE_G3_PF_EARLY). */
|
||||
void ar(RadCtx* c, int64_t T) const {
|
||||
if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE);
|
||||
}
|
||||
/* The pipelined prefill: the block over rows [r0, r0 + n), its input pair already written, up
|
||||
* to the down projection; then the tail a piece at a time on the second lane. */
|
||||
void body_rows(RadCtx* c, int64_t r0, int64_t n) const {
|
||||
@@ -732,7 +745,7 @@ int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
|
||||
{w.gu}, {ln_down.q8, ln_down.s8});
|
||||
// Not under calibration either: the down tap reads the bf16 product, which the fold never writes.
|
||||
{
|
||||
const int64_t hi = g.max_tok < 8 ? g.max_tok : 8;
|
||||
const int64_t hi = g.max_tok < fold_max ? g.max_tok : fold_max;
|
||||
// G3Linear's mx is linear_is_mxfp4, never a rotated (ParoQuant) weight, so one part.
|
||||
if (ln_gu.mx && ln_down.mx && !ln_down.shared_in && !calib_dir() && hi > 0 &&
|
||||
ln_gu.N % 256 == 0 && ln_gu.K % RAD_FP8_BLOCK == 0 &&
|
||||
@@ -926,6 +939,60 @@ struct G3JunctionFP8 {
|
||||
constexpr int64_t kPrefetchBytes = (int64_t)24 << 20;
|
||||
constexpr int64_t kPrefetchStepRows = 32;
|
||||
|
||||
/* A/B knobs over the decode steps, read at declare; unset, each is today's behaviour and the graph
|
||||
* and steps are the ones pinned before they existed (arch_test,
|
||||
* gemma3_served_knobs_at_their_defaults_keep_the_pinned_graph_and_steps).
|
||||
*
|
||||
* RADIANCE_G3_PF_ROWS the tallest decode step that reads beside its reductions (default
|
||||
* kPrefetchStepRows, 32, qwen35_fp8's). Eight sequences at speculation 7
|
||||
* verify 64 rows, and every all-reduce of such a step runs with the card's
|
||||
* memory idle (~5.8 ms eager a step on 2x R9700).
|
||||
* RADIANCE_G3_PF_MIB the read's size on the steps above 32 rows (default kPrefetchBytes, whose
|
||||
* sweep was at 1..32-row steps): a 64-row step's GEMMs are partly compute-
|
||||
* bound, so the size that fits beside its longer reduction may differ.
|
||||
* RADIANCE_G3_PF_EARLY 1: issue the read between the GEMM and a bare (unfused) all-reduce
|
||||
* rather than after it. Above ar_wire_is_exact's rows the lossy wire's
|
||||
* all-reduce is issued by the block itself, so the read's join on lane 1
|
||||
* comes after the reduction and the read runs beside the post-norm, the
|
||||
* junction and the next GEMM, not beside the reduction. The fused steps
|
||||
* (exact wire) have it in front either way.
|
||||
* RADIANCE_G3_FOLD_ROWS the gated fold's band (Gemma3Mlp::fold_max): 0 is today's 8; up to 64. */
|
||||
struct G3Knobs {
|
||||
int64_t pf_rows = kPrefetchStepRows;
|
||||
int64_t pf_wide = kPrefetchBytes; /* bytes, steps of more than kPrefetchStepRows rows */
|
||||
bool pf_early = false;
|
||||
int64_t fold = 8;
|
||||
};
|
||||
|
||||
/* An integer knob in [lo, hi], or `dflt` when unset; anything else is refused by name, so a typo
|
||||
* in an A/B does not silently measure the default. */
|
||||
static int g3_knob(const char* name, long long lo, long long hi, long long dflt, long long* out) {
|
||||
*out = dflt;
|
||||
const char* s = std::getenv(name);
|
||||
if (!s || !*s) return RAD_OK;
|
||||
char* end = nullptr;
|
||||
const long long v = std::strtoll(s, &end, 10);
|
||||
if (!end || *end != '\0' || v < lo || v > hi) {
|
||||
fprintf(stderr, "radiance: gemma3: %s='%s' is not an integer in %lld..%lld\n", name, s, lo, hi);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
*out = v;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
static int g3_knobs(G3Knobs* k) {
|
||||
long long v = 0;
|
||||
RAD_ARCH_TRY(g3_knob("RADIANCE_G3_PF_ROWS", 1, 1 << 20, kPrefetchStepRows, &v));
|
||||
k->pf_rows = v;
|
||||
RAD_ARCH_TRY(g3_knob("RADIANCE_G3_PF_MIB", 1, 256, kPrefetchBytes >> 20, &v));
|
||||
k->pf_wide = (int64_t)v << 20;
|
||||
RAD_ARCH_TRY(g3_knob("RADIANCE_G3_PF_EARLY", 0, 1, 0, &v));
|
||||
k->pf_early = v != 0;
|
||||
RAD_ARCH_TRY(g3_knob("RADIANCE_G3_FOLD_ROWS", 0, 64, 0, &v));
|
||||
k->fold = v == 0 ? 8 : v;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
struct Layer {
|
||||
bool global = false;
|
||||
Gemma3Attn attn{};
|
||||
@@ -933,8 +1000,11 @@ struct Layer {
|
||||
G3Junction j_am{}, j_ma{};
|
||||
G3JunctionFP8 jf_am{}, jf_ma{};
|
||||
/* The reads beside the decode all-reduces: gate|up's leading bytes beside the attention block's
|
||||
* reduction, the next layer's q's beside the feed-forward's. 0 where none is declared. */
|
||||
* reduction, the next layer's q's beside the feed-forward's. 0 where none is declared. The
|
||||
* `_wide` pair serves the steps above kPrefetchStepRows rows; it is the narrow pair itself
|
||||
* unless RADIANCE_G3_PF_MIB gives those steps another size. */
|
||||
rad_op op_pf_gu = 0, op_pf_next = 0;
|
||||
rad_op op_pf_gu_wide = 0, op_pf_next_wide = 0;
|
||||
rad_weight pf_gu_w = 0, pf_next_w = 0;
|
||||
};
|
||||
|
||||
@@ -954,6 +1024,7 @@ struct Model {
|
||||
rad_op op_embed = 0, op_embed_ar = 0, op_final = 0, op_gather = 0, op_logits = 0;
|
||||
|
||||
std::vector<Layer> layers;
|
||||
G3Knobs knobs{};
|
||||
|
||||
G3Junction j0{};
|
||||
bool fuse = false;
|
||||
@@ -1149,6 +1220,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
if (m.pipe_tbl) rad_dev_free(m.pipe_tbl, RAD_MEM_HOST_PINNED);
|
||||
m = Model{};
|
||||
m.nm = Names(ctx->scope);
|
||||
RAD_ARCH_TRY(g3_knobs(&m.knobs));
|
||||
RAD_ARCH_TRY(geom_from(m.g, meta, ctx, "bf16", RAD_BF16));
|
||||
/* The rotary scaling (linear, factor 8 on the 27B) into g.rope_scale, for the global class. */
|
||||
RAD_ARCH_TRY(yarn_from(m.g, meta, "", m.g.head_dim));
|
||||
@@ -1305,6 +1377,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
lay.attn.ar_overlap = lay.mlp.ar_overlap = 1;
|
||||
lay.attn.pipe_in = lay.mlp.pipe_in = &m.pipe_in;
|
||||
}
|
||||
lay.mlp.fold_max = m.knobs.fold;
|
||||
RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, cgeo, kg, aw, attn_scale));
|
||||
RAD_ARCH_TRY(lay.mlp.declare(b, nm, g, (int)l, mw));
|
||||
}
|
||||
@@ -1383,6 +1456,14 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
lay.pf_next_w = m.layers[(size_t)l + 1].attn.ln_q.w;
|
||||
lay.op_pf_next = decl_prefetch(b, lay.pf_next_w, kPrefetchBytes);
|
||||
}
|
||||
/* A size of their own for the steps above kPrefetchStepRows, declared only when some
|
||||
* step can take it, so an unused RADIANCE_G3_PF_MIB leaves the graph as it was. */
|
||||
lay.op_pf_gu_wide = lay.op_pf_gu;
|
||||
lay.op_pf_next_wide = lay.op_pf_next;
|
||||
if (m.knobs.pf_rows > kPrefetchStepRows && m.knobs.pf_wide != kPrefetchBytes) {
|
||||
lay.op_pf_gu_wide = decl_prefetch(b, lay.pf_gu_w, m.knobs.pf_wide);
|
||||
if (lay.pf_next_w) lay.op_pf_next_wide = decl_prefetch(b, lay.pf_next_w, m.knobs.pf_wide);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* The pipeline needs the fused fp8 junctions and every block's overlap all-reduce. */
|
||||
@@ -1702,9 +1783,11 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
if (m.op_embed_ar)
|
||||
RAD_ISSUE_N(c, m.op_embed_ar, T * m.g.n_embd, brows(m.b_x, T), RAD_NONE);
|
||||
|
||||
/* The reads beside the reductions go on decode steps of up to kPrefetchStepRows rows, none on a
|
||||
* step that carries a prompt's rows (as qwen35_fp8's). */
|
||||
const int64_t pf_h = batch->phase == RAD_PHASE_DECODE && T <= kPrefetchStepRows ? 1 : INT64_MAX;
|
||||
/* The reads beside the reductions go on decode steps of up to RADIANCE_G3_PF_ROWS rows
|
||||
* (kPrefetchStepRows by default), none on a step that carries a prompt's rows (as
|
||||
* qwen35_fp8's); the steps above kPrefetchStepRows take the `_wide` reads. */
|
||||
const int64_t pf_h = batch->phase == RAD_PHASE_DECODE && T <= m.knobs.pf_rows ? 1 : INT64_MAX;
|
||||
const bool pf_wide = T > kPrefetchStepRows;
|
||||
if (m.fuse_mx) {
|
||||
/* MXFP4 trunk: [post-norm, then add + next norm + quantiser as one op] at every junction;
|
||||
* the last block's tail runs unfused into the final norm. */
|
||||
@@ -1714,13 +1797,20 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
const bool last = li + 1 == m.layers.size();
|
||||
/* Per step: the fused junction sends the exact wire only (G3JunctionFP8::op_ar). */
|
||||
const bool far = m.fuse_mx_ar && ar_wire_is_exact(m.g, T);
|
||||
l.attn.step(c, batch, true, true, far);
|
||||
prefetch_beside(c, l.op_pf_gu, l.pf_gu_w, pf_h);
|
||||
/* RADIANCE_G3_PF_EARLY: a bare all-reduce issued here, behind the read, rather than by
|
||||
* the block (the last feed-forward, with no read after it, keeps its own). */
|
||||
const bool early = m.knobs.pf_early && !far;
|
||||
const rad_op pf_gu = pf_wide ? l.op_pf_gu_wide : l.op_pf_gu;
|
||||
const rad_op pf_next = pf_wide ? l.op_pf_next_wide : l.op_pf_next;
|
||||
l.attn.step(c, batch, true, true, far || early);
|
||||
prefetch_beside(c, pf_gu, l.pf_gu_w, pf_h);
|
||||
if (early) l.attn.ar(c, T);
|
||||
if (!far) l.attn.post(c, T);
|
||||
l.jf_am.step(c, T, far);
|
||||
l.mlp.step(c, batch, true, !last, far);
|
||||
prefetch_beside(c, l.op_pf_next, l.pf_next_w, pf_h);
|
||||
l.mlp.step(c, batch, true, !last, far || early);
|
||||
prefetch_beside(c, pf_next, l.pf_next_w, pf_h);
|
||||
if (!last) {
|
||||
if (early) l.mlp.ar(c, T);
|
||||
if (!far) l.mlp.post(c, T);
|
||||
l.jf_ma.step(c, T, far);
|
||||
}
|
||||
@@ -1733,10 +1823,10 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
const bool last = li + 1 == m.layers.size();
|
||||
const bool ar = m.fuse_ar && ar_wire_is_exact(m.g, T); /* exact wire only, as above */
|
||||
l.attn.step(c, batch, m.fuse, m.fuse, ar);
|
||||
prefetch_beside(c, l.op_pf_gu, l.pf_gu_w, pf_h);
|
||||
prefetch_beside(c, pf_wide ? l.op_pf_gu_wide : l.op_pf_gu, l.pf_gu_w, pf_h);
|
||||
if (m.fuse) l.j_am.step(c, T, ar);
|
||||
l.mlp.step(c, batch, m.fuse, m.fuse && !last, ar);
|
||||
prefetch_beside(c, l.op_pf_next, l.pf_next_w, pf_h);
|
||||
prefetch_beside(c, pf_wide ? l.op_pf_next_wide : l.op_pf_next, l.pf_next_w, pf_h);
|
||||
if (m.fuse && !last) l.j_ma.step(c, T, ar);
|
||||
tap(li);
|
||||
}
|
||||
|
||||
@@ -3489,6 +3489,10 @@ The four slots are what the amdgpu kernel driver leaves to user queues. It keeps
|
||||
| `RADIANCE_CALIB_TOKENS` | with `RADIANCE_CALIB_DIR`, the tokens the calibration tap accumulates before it writes and switches itself off (default 65536) |
|
||||
| `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_G4_CALIB_LAYERS` | `lo:hi`: the same for `arch/gemma4`'s tap, which takes the Gemma 4 31B's Grams from its bf16 checkpoint (docs/GEMMA4-31B-GPTQ.md) |
|
||||
| `RADIANCE_G3_FOLD_ROWS` | `arch/gemma3`, an A/B knob: the gated fold (gate_up, GeGLU and down's E4M3 quantiser in one launch, libr4d `gemm_nt_q_gated_mx`) on decode and verify steps of up to this many rows, 9..64 through `gemm_mxfp4a8_gated_gemv_wide`; `0` or unset is 8. Bit for bit at any value. Read at startup; anything but an integer 0..64 refuses to start |
|
||||
| `RADIANCE_G3_PF_EARLY` | `arch/gemma3`, an A/B knob: `1` issues the weight read beside a bare (unfused, lossy-wire) all-reduce in front of it rather than after it, so it overlaps the reduction rather than the post-norm, the junction and the next GEMM; `0` or unset is the old order. The fused all-reduce junction's steps have it in front either way |
|
||||
| `RADIANCE_G3_PF_MIB` | `arch/gemma3`, an A/B knob: the size in MiB (1..256) of the weight reads beside the all-reduces on decode steps of more than 32 rows (default 24, the narrow steps' size); declared only when `RADIANCE_G3_PF_ROWS` reaches past 32 |
|
||||
| `RADIANCE_G3_PF_ROWS` | `arch/gemma3`, an A/B knob: the tallest decode or verify step that reads beside its all-reduces (default 32; 64 covers eight sequences at speculation 7). Never on a step with a prompt's rows |
|
||||
| `RADIANCE_PROFILE_EVERY` | with `--profile-ops`, dump the per-op table every N steps (default 32) |
|
||||
| `RADIANCE_AQL_STAMP`, `RADIANCE_AQL_STAMP_OUT` | `N`: every dispatch of one recorded pass in N on card 0 gets a completion signal of its own, which carries its start and end; the barrier bits and chains stay as unstamped, unlike `--profile-ops`, which plays no recorded pass. One CSV line a dispatch (pass, record, queue, kernel, whether it waited for the one before, whether it was chained, start and end in ns) is appended to `RADIANCE_AQL_STAMP_OUT` (default `/tmp/radiance-stamps-<pid>.csv`) once the card is done with it; `scripts/stampgaps.py` reads it per kind of pass (idle share, gaps before ordered starts, kernels by time). AQL backend only |
|
||||
| `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) |
|
||||
|
||||
+1
-1
@@ -753,7 +753,7 @@ The `--perf*` modes time rows at one shape on a card. Two that cover the dense G
|
||||
| | |
|
||||
|---|---|
|
||||
| `--perf N K M [iters]` | the fp8 decode GEMMs, and the fp8 head (`logits_gemm_fp8`) |
|
||||
| `--perfmxd N K M [iters]` | the dense OCP-MXFP4 rows (`gemm_mxfp4a8_gemv`, `_decode`, `_nt_m64`, `_tiled`) at one shape, with the weight bandwidth each reaches; at a gate_up shape (N a multiple of 256, K of 128) also `gated_quant_fp8` and the fold that replaces the pair, `gemm_mxfp4a8_gated_gemv` |
|
||||
| `--perfmxd N K M [iters]` | the dense OCP-MXFP4 rows (`gemm_mxfp4a8_gemv`, `_decode`, `_nt_m64`, `_tiled`) at one shape, with the weight bandwidth each reaches; at a gate_up shape (N a multiple of 256, K of 128, M up to 64) also the ops after the GEMM, `gated_quant_fp8` (SwiGLU) and `gelu_mul` + `quant_act_fp8` (GeGLU), and the fold that replaces them at both activations, `gemm_mxfp4a8_gated_gemv` (M ≤ 8) or `_gated_gemv_wide` (9..64) |
|
||||
|
||||
`--perfmx` is the MXFP4 expert GEMM (`M`, experts, `top_k`, skew), not the dense rows. `--cold MIB`
|
||||
times each launch alone behind a cache flush instead of back to back.
|
||||
|
||||
@@ -55,6 +55,7 @@ The table lists a selection; `r4d_rows.cpp` and `r4d.h` hold the full set.
|
||||
| `gemm_mxfp4a8_gemv` | the same operands for one to eight rows: the weight streamed straight to registers, no LDS staging; WMMA when K is a multiple of 128, `v_dot4` otherwise, chosen from K alone so a row's bits do not depend on M |
|
||||
| `gemm_mxfp4a8_gemv_wide` | its WMMA form at 9..64 rows: up to four 16-row tiles a wave over each weight word, two 16-column tiles a block on wide, deep weights |
|
||||
| `gemm_mxfp4a8_gated_gemv` | `gemm_nt_q_gated_mx`: the gemv WMMA form over a gate_up weight with `gated_quant_fp8`'s pass (or GeGLU's `gelu_mul` and `quant_act_fp8`) in its epilogue, M ≤ 8; the [M, 2n] product is never written |
|
||||
| `gemm_mxfp4a8_gated_gemv_wide` | the same fold at 9..64 rows over `gemv_wide`'s WMMA form: a block per gate/up tile pair, eight K waves each running both tiles, K ≥ 2688 |
|
||||
| `gemm_mxfp4a8_decode` | the same operands for the decode band (M ≤ 128): split-K across workgroups with a last-arriver reduction, the partial in the op's scratch |
|
||||
| `gemm_mxfp4a8_decode_shortk` | the decode kernel at 33..64 rows of a wide, short weight (N ≥ 8192, K ≤ 2560), above `gemm_mxfp4a8_gemv_wide`, which loses there |
|
||||
| `gemm_mxfp4a8_tiled` | the prefill sibling: the activation re-tiled into fragment order in the op's scratch and read straight into registers, the weight unpacked once per K slab for a 256-row block |
|
||||
@@ -211,6 +212,20 @@ one stream at speculation 7: 144.5 / 144.9 → 151.5 / 150.5 tok/s, measured wit
|
||||
this row (a block per 128-column group, the same bytes); the tile-pair form, Qwen3.8-27B without
|
||||
speculation x1 / x4 / x8 +1.4 / +1.8 / +1.5 %.
|
||||
|
||||
**`gemm_mxfp4a8_gated_gemv_wide`** (`gemm_nt_q_gated_mx`, 9 ≤ M ≤ 64, K a multiple of 128 and at
|
||||
least 2688, N of 256, the same `act` rule). The fold over `gemv_wide`, the GEMM row of that band:
|
||||
`gemv_wide`'s kernel at two column tiles a block with the tiles taken as one gate tile and its up
|
||||
tile, so each of the eight K waves loads both tiles' weight words of its chunks and reads a chunk's
|
||||
activation fragments once for both (the narrow row's sixteen waves, eight a tile, would be
|
||||
`gemv_wide` at one tile a block, which at 32 and 64 rows took TranslateGemma's gate_up 39 % and 22 % longer).
|
||||
Up to four 16-row tiles; the waves' partials meet through LDS one half at a time (32 KB at 64 rows),
|
||||
then the narrow row's epilogue. Byte for byte `gemm_mxfp4a8_gemv_wide` followed by `gated_quant_fp8`,
|
||||
or by `gelu_mul` and `quant_act_fp8` (`r4d_selftest`, M = 9..64). No spills: 101 / 137 / 166 / 196
|
||||
VGPRs at one to four row tiles (per-(row, 128) activation scale), against `gemv_wide`'s 131 / 160 / 190
|
||||
at two to four tiles and two columns. K from 2688 keeps it out of `decode_shortk`'s corner, where the
|
||||
unfused GEMM sums 33..64 rows in the decode kernel's order. gemma3 declares it up to
|
||||
RADIANCE_G3_FOLD_ROWS rows (default 8, i.e. not at all).
|
||||
|
||||
**Tunables.** `gemv`, `gemv_wide` and `gated_gemv` have none, so a `RADIANCE_TUNE` pin on
|
||||
`gemm_nt_q` passes them by. `decode_shortk` shares the decode row's axes (`ks`, `bk`, `nt`; 0 the rule), so a pin of
|
||||
those reaches both. A pin applies to every row serving the op, and an axis a serving row lacks
|
||||
|
||||
+3
-1
@@ -512,7 +512,9 @@ extern "C" int r4d_fuse_gemm_nt_q_gated(const RadParam* p, int n_p, uint64_t pre
|
||||
|
||||
/* ==========================================================================================
|
||||
* gemm_nt_q_gated_mx -- the same fold over an OCP-MXFP4 weight.
|
||||
* (row: gemm_mxfp4a8_gated_gemv; kernel: libr4d/r4d_gemm_mxfp4a8_gemv.hip, the gated form)
|
||||
* (rows: gemm_mxfp4a8_gated_gemv, M <= 8, and gemm_mxfp4a8_gated_gemv_wide, 9..64; kernels:
|
||||
* libr4d/r4d_gemm_mxfp4a8_gemv.hip, the gated forms. The chain's gemm_nt_q resolves to gemv and
|
||||
* gemv_wide in those bands, the rows each form is byte-identical to.)
|
||||
*
|
||||
* Operands: [0] a [1] a_scale [2] b [3] b_scale [4] b_ref [5] q [6] scale [7] out_bf16
|
||||
*
|
||||
|
||||
@@ -30,7 +30,8 @@
|
||||
// shuffles. TN only widens a block; the sum order of a column is fixed by KW, which is constant.
|
||||
//
|
||||
// The column's 2^(ref - 127) is applied once, as in the decode kernel's epilogue. No scratch and no
|
||||
// counters: there is no split across blocks (the gated fold at the end of the file has both).
|
||||
// counters: there is no split across blocks (the gated folds at the end of the file, at 1..8 rows and
|
||||
// at 9..64, have both).
|
||||
#include "r4d.h"
|
||||
#include "r4d_gemm_args.h"
|
||||
#include "r4d_common.h"
|
||||
@@ -506,6 +507,174 @@ void r4d_gemm_mxfp4a8_gated_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================== the gated form at 9..64 rows
|
||||
// (gemm_mxfp4a8_gated_gemv_wide)
|
||||
//
|
||||
// From 9 rows the GEMM the fold replaces is gemv_wide's (r4d_gemm_mxfp4a8_gemvw_kernel, RT row tiles).
|
||||
// This is that kernel over a tile PAIR, gate tile j and up tile n / 16 + j, in place of two adjacent
|
||||
// tiles (its TN = 2): eight K waves, each loading both tiles' words of its chunks and running one WMMA
|
||||
// chain per row tile over each, so a chunk's activation fragments are read once for both halves. The
|
||||
// narrow form's sixteen waves (eight a half, each reading the activation for its own tile) would be
|
||||
// gemv_wide at TN = 1, which at 25..64 rows took TranslateGemma's gate_up 21504 x 5376 from 111 to 154
|
||||
// us at 32 rows and from 150 to 183 at 64 (the comment above r4d_gemm_mxfp4a8_gemvw_kernel). Then the
|
||||
// narrow form's epilogue: a thread a (row, column) of the pair, the GeGLU or SwiGLU product rounded to
|
||||
// bf16, each row's amax over the tile, and the 128-column group's last block quantising it.
|
||||
//
|
||||
// Byte-identical to gemv_wide followed by gelu_mul and quant_act_fp8 (or gated_quant_fp8). A row's sum
|
||||
// in gemv_wide is its row tile's chain alone, the same at every RT and TN; wave v here sums chunks v,
|
||||
// v + KW, .. in order with the same fmaf of each chunk's chain by its activation scale, the waves'
|
||||
// partials meet through LDS one half at a time and are added w = 0, 1, .. from zero (gemv_wide adds them
|
||||
// one column tile at a time, in that order), and the scaled sum is rounded to bf16 by the same
|
||||
// conversion. From there the arithmetic is the narrow form's, which is the pair's.
|
||||
//
|
||||
// LDS: one half's partials at a time, [KW][16 RT][16] floats, 32 KB at four row tiles -- both halves at
|
||||
// once would be 64 KB, past a workgroup's allocation with the counter word beside it.
|
||||
template <bool ABLK, int ACT, int RT>
|
||||
__global__ __launch_bounds__(R4D_MXG_KW * 32)
|
||||
void r4d_gemm_mxfp4a8_gatedw_kernel(
|
||||
const unsigned char* __restrict__ A, const float* __restrict__ As,
|
||||
const unsigned char* __restrict__ W, const unsigned char* __restrict__ Ws,
|
||||
const unsigned char* __restrict__ Wref, uint8_t* __restrict__ Q, float* __restrict__ S,
|
||||
uint16_t* __restrict__ Y, uint16_t* __restrict__ H, float* __restrict__ X,
|
||||
unsigned* __restrict__ cnt, int M, int K, int n, int as_rs, int lda, int ldws, int q_ld,
|
||||
int s_ld, int y_ld) {
|
||||
constexpr int KW = R4D_MXG_KW;
|
||||
constexpr int NT = KW * 32;
|
||||
__shared__ float sP[KW][16 * RT][16];
|
||||
__shared__ unsigned s_last;
|
||||
const int lane = threadIdx.x & 31;
|
||||
const int wave = __builtin_amdgcn_readfirstlane(threadIdx.x >> 5);
|
||||
const int j = blockIdx.x; // the gated output's 16-column tile
|
||||
const int nt[2] = { j, (n >> 4) + j }; // its gate and up weight tiles
|
||||
const int col = lane & 15, khalf = lane >> 4;
|
||||
const int ksteps = K >> 4, chunks = K >> 7;
|
||||
const unsigned* __restrict__ Wp = (const unsigned*)W;
|
||||
int ref[2];
|
||||
#pragma unroll
|
||||
for (int t = 0; t < 2; ++t) ref[t] = (int)Wref[nt[t] * 16 + col];
|
||||
mxg_v8f tot[RT][2];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < RT; ++r)
|
||||
#pragma unroll
|
||||
for (int t = 0; t < 2; ++t) tot[r][t] = (mxg_v8f)(0.f);
|
||||
for (int ch = wave; ch < chunks; ch += KW) {
|
||||
unsigned wv[2][8];
|
||||
#pragma unroll
|
||||
for (int t = 0; t < 2; ++t)
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s)
|
||||
wv[t][s] = __builtin_nontemporal_load(&Wp[((size_t)nt[t] * ksteps + ch * 8 + s) * 32 + lane]);
|
||||
int e8[2][4];
|
||||
#pragma unroll
|
||||
for (int t = 0; t < 2; ++t)
|
||||
#pragma unroll
|
||||
for (int q = 0; q < 4; ++q) e8[t][q] = (int)Ws[(size_t)(ch * 4 + q) * ldws + nt[t] * 16 + col];
|
||||
mxg_v2i wf[2][8];
|
||||
#pragma unroll
|
||||
for (int t = 0; t < 2; ++t)
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
unsigned lo, hi;
|
||||
r4d_mxfp4_unpack8(wv[t][s], r4d_mxfp4_fold_d(ref[t], e8[t][s >> 1]), &lo, &hi);
|
||||
wf[t][s][0] = (int)lo; wf[t][s][1] = (int)hi;
|
||||
}
|
||||
#pragma unroll
|
||||
for (int r = 0; r < RT; ++r) {
|
||||
const int row = r * 16 + col; // A fragment: this lane's row in tile r
|
||||
const bool live = row < M;
|
||||
mxg_v2i af[8];
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
if (live) {
|
||||
const unsigned* pa = (const unsigned*)(A + (size_t)row * lda + (ch * 8 + s) * 16 + khalf * 8);
|
||||
af[s][0] = (int)pa[0]; af[s][1] = (int)pa[1];
|
||||
} else {
|
||||
af[s][0] = 0; af[s][1] = 0;
|
||||
}
|
||||
}
|
||||
const float sc = ABLK ? (live ? As[(size_t)row * as_rs + ch] : 0.f) : 1.f;
|
||||
#pragma unroll
|
||||
for (int t = 0; t < 2; ++t) {
|
||||
mxg_v8f acc = (mxg_v8f)(0.f);
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s)
|
||||
acc = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(wf[t][s], af[s], acc);
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) tot[r][t][e] = fmaf(acc[e], sc, tot[r][t][e]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Each half's K partials, summed per (row, column): a thread takes (row tid / 16 + 16 i, column
|
||||
// tid % 16) for i < RT, so sixteen consecutive lanes hold one row.
|
||||
const int tid = threadIdx.x;
|
||||
float sum[2][RT];
|
||||
#pragma unroll
|
||||
for (int t = 0; t < 2; ++t) {
|
||||
if (t) __syncthreads();
|
||||
#pragma unroll
|
||||
for (int r = 0; r < RT; ++r)
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) sP[wave][r * 16 + col][khalf * 8 + e] = tot[r][t][e];
|
||||
__syncthreads();
|
||||
#pragma unroll
|
||||
for (int i = 0; i < RT; ++i) {
|
||||
const int idx = tid + i * NT, m = idx >> 4, c = idx & 15;
|
||||
float s_ = 0.f;
|
||||
if (m < M) {
|
||||
#pragma unroll
|
||||
for (int w = 0; w < KW; ++w) s_ += sP[w][m][c];
|
||||
}
|
||||
sum[t][i] = s_;
|
||||
}
|
||||
}
|
||||
const int g = j >> 3; // the 128-column group
|
||||
#pragma unroll
|
||||
for (int i = 0; i < RT; ++i) {
|
||||
const int idx = tid + i * NT, m = idx >> 4, c = idx & 15;
|
||||
if (m < M) {
|
||||
const int cg = j * 16 + c, cu = n + j * 16 + c;
|
||||
const float rg = __int_as_float((int)Wref[cg] << 23), ru = __int_as_float((int)Wref[cu] << 23);
|
||||
const float am = ABLK ? 1.f : As[(size_t)m * as_rs];
|
||||
const __hip_bfloat16 hg = (__hip_bfloat16)(ABLK ? sum[0][i] * rg : sum[0][i] * rg * am);
|
||||
const __hip_bfloat16 hu = (__hip_bfloat16)(ABLK ? sum[1][i] * ru : sum[1][i] * ru * am);
|
||||
const float gv = mxg_bf2f(*(const uint16_t*)&hg);
|
||||
const float a = ACT == 0 ? gv / (1.0f + expf(-gv))
|
||||
: 0.5f * gv * (1.0f + tanhf(0.7978845608028654f * (gv + 0.044715f * gv * gv * gv)));
|
||||
const uint16_t h = mxg_f2bf(a * mxg_bf2f(*(const uint16_t*)&hu));
|
||||
if (Y) Y[(size_t)m * y_ld + cg] = h;
|
||||
H[(size_t)m * n + cg] = h;
|
||||
float mx = fabsf(mxg_bf2f(h));
|
||||
#pragma unroll
|
||||
for (int o = 8; o > 0; o >>= 1) mx = fmaxf(mx, __shfl_xor(mx, o, 16));
|
||||
if (c == 0) X[(size_t)j * M + m] = mx;
|
||||
}
|
||||
}
|
||||
// As the narrow form: every thread's stores ordered before the counter, the group's last arriver
|
||||
// quantises, a wave a row.
|
||||
__threadfence();
|
||||
__syncthreads();
|
||||
if (tid == 0)
|
||||
s_last = __hip_atomic_fetch_add(&cnt[g], 1u, __ATOMIC_ACQ_REL, __HIP_MEMORY_SCOPE_AGENT);
|
||||
__syncthreads();
|
||||
if (s_last != 7u) return;
|
||||
if (tid == 0) cnt[g] = 0u;
|
||||
__threadfence();
|
||||
for (int m = wave; m < M; m += KW) {
|
||||
const int c0 = lane * 4;
|
||||
const size_t gcol = (size_t)g * 128 + c0;
|
||||
float mx = 0.f;
|
||||
#pragma unroll
|
||||
for (int t = 0; t < 8; ++t) mx = fmaxf(mx, X[(size_t)(g * 8 + t) * M + m]);
|
||||
float vq[4];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) vq[i] = mxg_bf2f(H[(size_t)m * n + gcol + i]);
|
||||
const float sc = mx > 0.0f ? mx * (1.0f / q8_top<false>()) : 1.0f;
|
||||
if (lane == 0) S[(size_t)m * s_ld + g] = sc;
|
||||
const float inv = 1.0f / sc;
|
||||
*(uint32_t*)(Q + (size_t)m * q_ld + gcol) = q8_pack4<false>(vq[0], vq[1], vq[2], vq[3], inv);
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
// The arrival counters, one block per output buffer, as the decode row's (mxd_out_ctr): not in the
|
||||
// scratch, which other ops overwrite, since they must be zero at entry. One allocation per (device,
|
||||
@@ -554,6 +723,31 @@ void mxgg_launch(bool ablk, int act, const void* a, const void* as, const void*
|
||||
else { if (ablk) MXGG_L(true, 1); else MXGG_L(false, 1); }
|
||||
#undef MXGG_L
|
||||
}
|
||||
|
||||
// The wide form: one block of eight K waves a 16-column tile of the gated output, ceil(M / 16) row
|
||||
// tiles.
|
||||
void mxggw_launch(bool ablk, int act, const void* a, const void* as, const void* w, const void* ws,
|
||||
const void* wr, void* q, void* s, void* y, void* scratch, unsigned* cnt, int M,
|
||||
int K, int n, int as_rs, int lda, int ldws, int q_ld, int s_ld, int y_ld,
|
||||
RadStream stream) {
|
||||
const dim3 grid((uint32_t)(n / 16)), block(R4D_MXG_KW * 32);
|
||||
hipStream_t st = (hipStream_t)stream;
|
||||
uint16_t* h = (uint16_t*)scratch;
|
||||
float* x = (float*)((char*)scratch + mxgg_h_bytes(M, n));
|
||||
const int rt = (M + 15) / 16;
|
||||
#define MXGW_L(AB_, ACT_, RT_) \
|
||||
hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gatedw_kernel<AB_, ACT_, RT_>), grid, block, 0, st, \
|
||||
(const unsigned char*)a, (const float*)as, (const unsigned char*)w, \
|
||||
(const unsigned char*)ws, (const unsigned char*)wr, (uint8_t*)q, (float*)s, \
|
||||
(uint16_t*)y, h, x, cnt, M, K, n, as_rs, lda, ldws, q_ld, s_ld, y_ld)
|
||||
#define MXGW_RT(AB_, ACT_) \
|
||||
{ if (rt <= 1) MXGW_L(AB_, ACT_, 1); else if (rt == 2) MXGW_L(AB_, ACT_, 2); \
|
||||
else if (rt == 3) MXGW_L(AB_, ACT_, 3); else MXGW_L(AB_, ACT_, 4); }
|
||||
if (act == 0) { if (ablk) MXGW_RT(true, 0) else MXGW_RT(false, 0) }
|
||||
else { if (ablk) MXGW_RT(true, 1) else MXGW_RT(false, 1) }
|
||||
#undef MXGW_RT
|
||||
#undef MXGW_L
|
||||
}
|
||||
} // namespace
|
||||
|
||||
/* The gated GEMM's shim. Operand indices are `gemm_nt_q_gated_mx`'s (oGemmNtQGatedMx in
|
||||
@@ -562,7 +756,9 @@ void mxgg_launch(bool ablk, int act, const void* a, const void* as, const void*
|
||||
enum { MXGG_A = 0, MXGG_AS = 1, MXGG_B = 2, MXGG_BS = 3, MXGG_BR = 4, MXGG_Q = 5, MXGG_S = 6,
|
||||
MXGG_Y = 7 };
|
||||
|
||||
extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
|
||||
/* Both rows' entry: the narrow form at 1..8 rows (gemm_mxfp4a8_gated_gemv), the wide one at 9..64
|
||||
* (gemm_mxfp4a8_gated_gemv_wide), each refusing the other's band as its GEMM row does. */
|
||||
static int mxgg_entry(const RadArgs* a, RadStream s, bool wide) {
|
||||
const RadTensor* x = r4d_opt(a, MXGG_A);
|
||||
const RadTensor* xs = r4d_opt(a, MXGG_AS);
|
||||
const RadTensor* w = r4d_opt(a, MXGG_B);
|
||||
@@ -590,7 +786,8 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
|
||||
* weight operands are packed bytes. */
|
||||
const long long M = q->shape[0], nout = q->shape[1];
|
||||
const long long N = r4d_geti_or(a, "N", 0), K = r4d_geti_or(a, "K", 0);
|
||||
if (M < 1 || M > R4D_MXG_MAX_M || nout < 128 || nout % 128 || nout / 128 > R4D_MXGG_MAX_GROUPS)
|
||||
const long long m_lo = wide ? R4D_MXG_MAX_M + 1 : 1, m_hi = wide ? R4D_MXG_WMMA_MAX_M : R4D_MXG_MAX_M;
|
||||
if (M < m_lo || M > m_hi || nout < 128 || nout % 128 || nout / 128 > R4D_MXGG_MAX_GROUPS)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
if (N != 2 * nout || K < 128 || K % 128) return R4D_NO(RAD_E_SHAPE);
|
||||
if (x->shape[0] != M || r4d_numel(x) != M * K) return R4D_NO(RAD_E_SHAPE);
|
||||
@@ -619,17 +816,36 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
|
||||
unsigned* cnt = mxgg_out_ctr(q->data);
|
||||
if (!cnt) return R4D_NO(RAD_E_NOMEM);
|
||||
|
||||
R4D_TRY("gemm_mxfp4a8_gated",
|
||||
mxgg_launch(ablk, actm, x->data, xs->data, w->data, ws->data, wr->data, q->data, sc->data,
|
||||
y ? y->data : nullptr, a->scratch, cnt, (int)M, (int)K, (int)nout, (int)as_rs,
|
||||
(int)lda, (int)N, (int)qp, (int)sp, (int)yp, s));
|
||||
if (wide)
|
||||
R4D_TRY("gemm_mxfp4a8_gated_wide",
|
||||
mxggw_launch(ablk, actm, x->data, xs->data, w->data, ws->data, wr->data, q->data, sc->data,
|
||||
y ? y->data : nullptr, a->scratch, cnt, (int)M, (int)K, (int)nout, (int)as_rs,
|
||||
(int)lda, (int)N, (int)qp, (int)sp, (int)yp, s));
|
||||
else
|
||||
R4D_TRY("gemm_mxfp4a8_gated",
|
||||
mxgg_launch(ablk, actm, x->data, xs->data, w->data, ws->data, wr->data, q->data, sc->data,
|
||||
y ? y->data : nullptr, a->scratch, cnt, (int)M, (int)K, (int)nout, (int)as_rs,
|
||||
(int)lda, (int)N, (int)qp, (int)sp, (int)yp, s));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
|
||||
return mxgg_entry(a, s, false);
|
||||
}
|
||||
extern "C" int r4d_rad_gemm_mxfp4a8_gated_wide(const RadArgs* a, RadStream s) {
|
||||
return mxgg_entry(a, s, true);
|
||||
}
|
||||
|
||||
/* The op's scratch: the product and amaxes at the band's widest M, from N (twice the gated
|
||||
* width). */
|
||||
extern "C" int64_t r4d_rad_gemm_mxfp4a8_gated_scratch(const RadArgs* a) {
|
||||
static int64_t mxgg_scratch(const RadArgs* a, long long m_hi) {
|
||||
const long long M = r4d_geti_or(a, "M", 0), N = r4d_geti_or(a, "N", 0);
|
||||
if (M < 1 || M > R4D_MXG_MAX_M || N < 256 || N % 256) return 0;
|
||||
if (M < 1 || M > m_hi || N < 256 || N % 256) return 0;
|
||||
return (int64_t)mxgg_scratch_bytes(M, N / 2);
|
||||
}
|
||||
extern "C" int64_t r4d_rad_gemm_mxfp4a8_gated_scratch(const RadArgs* a) {
|
||||
return mxgg_scratch(a, R4D_MXG_MAX_M);
|
||||
}
|
||||
extern "C" int64_t r4d_rad_gemm_mxfp4a8_gated_wide_scratch(const RadArgs* a) {
|
||||
return mxgg_scratch(a, R4D_MXG_WMMA_MAX_M);
|
||||
}
|
||||
@@ -481,6 +481,8 @@ int r4d_rad_gemm_m2xfp4a8_tiled(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_gemm_m2xfp4a8_tiled_scratch(const RadArgs*);
|
||||
int r4d_rad_gemm_m2xfp4a8_decode(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_gemm_m2xfp4a8_decode_scratch(const RadArgs*);
|
||||
int r4d_rad_gemm_mxfp4a8_gated_wide(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_gemm_mxfp4a8_gated_wide_scratch(const RadArgs*);
|
||||
int r4d_rad_dequant_w4 (const RadArgs*, RadStream);
|
||||
int r4d_rad_rowtopk (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_rowtopk_scratch(const RadArgs*);
|
||||
|
||||
+29
-1
@@ -4833,6 +4833,16 @@ static const RadConstraint cGemmNtQGatedMx[] = {
|
||||
C_LE("M", 8), C_DIV("K", 128), C_DIV("N", 256), C_EQ("group", 32), C_IN("dtype", "mxfp4a8"),
|
||||
C_IN("act", "silu gelu_tanh"),
|
||||
};
|
||||
/* r4d_gemm_mxfp4a8_gated_gemv_wide: the same fold at 9..64 rows, over gemv_wide's WMMA form (the GEMM
|
||||
* row of that band, which it is byte-identical to), a block per tile pair with both halves' words in
|
||||
* each of its eight K waves. No decode_shortk corner: that row sums 33..64 rows in the decode
|
||||
* kernel's order, so a gate_up in its corner (K <= 2560, N 8192..36864) would differ from its pair
|
||||
* there; its K bound excludes this row's callers (TranslateGemma 5376, Qwen3.8 5120) and the row
|
||||
* refuses the corner rather than claim it. */
|
||||
static const RadConstraint cGemmNtQGatedMxWide[] = {
|
||||
C_GE("M", 9), C_LE("M", 64), C_DIV("K", 128), C_GE("K", 2688), C_DIV("N", 256), C_EQ("group", 32),
|
||||
C_IN("dtype", "mxfp4a8"), C_IN("act", "silu gelu_tanh"),
|
||||
};
|
||||
static const RadConstraint cGateQuantFp8[] = {
|
||||
C_DIV("n", 4), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "bf16"),
|
||||
};
|
||||
@@ -7815,6 +7825,24 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
/* The byte-identity claim above, as a chain of two rows of this plugin (r4d_fuse.cpp). */
|
||||
r4d_fuse_gemm_nt_q_gated_mx },
|
||||
|
||||
{ "gemm_mxfp4a8_gated_gemv_wide", "gemm_nt_q_gated_mx", "gemm",
|
||||
"gemm_mxfp4a8_gated_gemv at 9 to 64 rows: gemv_wide's WMMA form over the gate and up tiles of one "
|
||||
"16-column tile of the gated output, eight K waves each loading both tiles' weight words and "
|
||||
"running one chain per 16-row tile over each, then that row's epilogue (the product and row amaxes "
|
||||
"in the op's scratch, the 128-column group's last block quantising)",
|
||||
"9 <= M <= 64; K a multiple of 128, at least 2688 (above decode_shortk's corner); N a multiple of "
|
||||
"256; act silu, or gelu_tanh without out_bf16; the skinny kernel's fragment-order weight, [K/32][N] "
|
||||
"E8M0 exponents and per-row reference exponent, gate rows then up rows",
|
||||
"e4m3 A with an f32 scale per row or per (row, 128), e2m1 W with an E8M0 scale per 32 K, and the "
|
||||
"QUANTISER's outputs: E4M3 codes, an f32 scale per (row, 128 columns) and an optional bf16 "
|
||||
"product. Byte-identical to gemm_mxfp4a8_gemv_wide followed by gated_quant_fp8 (silu), or by "
|
||||
"gelu_mul and quant_act_fp8 (gelu_tanh)",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cGemmNtQGatedMxWide), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_gemm_mxfp4a8_gated_wide, r4d_rad_gemm_mxfp4a8_gated_wide_scratch,
|
||||
r4d_rad_layout_mxfp4_gated, r4d_rad_relayout_mxfp4_gated, nullptr, nullptr,
|
||||
r4d_fuse_gemm_nt_q_gated_mx },
|
||||
|
||||
{ "gemm_mxfp4a8_decode", "gemm_nt_q", "gemm",
|
||||
"the OCP-MXFP4 GEMM for the DECODE band: split-K across workgroups with a fused last-arriver "
|
||||
"reduction, the weight unpacked through LDS, streaming weight loads",
|
||||
@@ -9814,7 +9842,7 @@ extern "C" const RadKernelInfo* rad_kernel_at(int i) {
|
||||
*
|
||||
* gemm_fp8a8_nt_m16's finisher counters are keyed on the stream and the output;
|
||||
* gemm_mxfp4a8_decode's (and gemm_mxfp4a8_decode_shortk's, the same entry) and
|
||||
* gemm_mxfp4a8_gated_gemv's split partials are the op's scratch and their arrival counters are keyed
|
||||
* gemm_mxfp4a8_gated_gemv's (and _gated_gemv_wide's) split partials are the op's scratch and their arrival counters are keyed
|
||||
* on the output alone; router_gemm_topk_scatter_bf16's arrival counter belongs to its op instance
|
||||
* (taken at init, one per declared op and band). Concurrent launches never share these, so those
|
||||
* five rows are concurrent. A new row with stream-ordered state belongs in the list above.
|
||||
|
||||
+44
-19
@@ -6720,21 +6720,23 @@ static void w4a8_multi_user_exact(const Plugin& pl) {
|
||||
|
||||
/* ---- the MXFP4 gated fold, against the pair it replaces -------------------------------------
|
||||
*
|
||||
* gemm_mxfp4a8_gated_gemv (gemm_nt_q_gated_mx) against gemm_mxfp4a8_gemv followed by
|
||||
* gemm_mxfp4a8_gated_gemv (gemm_nt_q_gated_mx, M <= 8) against gemm_mxfp4a8_gemv followed by
|
||||
* gated_quant_fp8, on the same weight bytes (one relayout, through the gemv row's hooks, which the
|
||||
* gated row shares) and activation. Codes, scales and the bf16 product must be identical, and the
|
||||
* codes mostly non-zero (zeroed buffers compare equal). Run twice: with `out_bf16`, and without it,
|
||||
* where the codes and scales must still match. */
|
||||
* gated row shares) and activation; from 9 rows gemm_mxfp4a8_gated_gemv_wide against
|
||||
* gemm_mxfp4a8_gemv_wide, the row the unfused GEMM takes there. Codes, scales and the bf16 product
|
||||
* must be identical, and the codes mostly non-zero (zeroed buffers compare equal). Run twice: with
|
||||
* `out_bf16`, and without it, where the codes and scales must still match. */
|
||||
static void mxfp4a8_gated_case(const Plugin& pl, int64_t M, int64_t N, int64_t K, bool ablk,
|
||||
bool gelu = false) {
|
||||
const char* row = "gemm_mxfp4a8_gated_gemv";
|
||||
const bool wide = M > 8;
|
||||
const char* row = wide ? "gemm_mxfp4a8_gated_gemv_wide" : "gemm_mxfp4a8_gated_gemv";
|
||||
if (!sel(row)) return;
|
||||
char nm[192];
|
||||
std::snprintf(nm, sizeof nm, "%s byte-identical to gemv + %s, M=%lld N=%lld K=%lld%s", row,
|
||||
gelu ? "gelu_mul + quant_act_fp8" : "gated_quant_fp8", (long long)M, (long long)N,
|
||||
(long long)K, ablk ? " ascale=row x 128" : "");
|
||||
const RadKernelInfo* rg = find(pl, row);
|
||||
const RadKernelInfo* rv = find(pl, "gemm_mxfp4a8_gemv");
|
||||
const RadKernelInfo* rv = find(pl, wide ? "gemm_mxfp4a8_gemv_wide" : "gemm_mxfp4a8_gemv");
|
||||
const RadKernelInfo* rq = find(pl, gelu ? "quant_act_fp8" : "gated_quant_fp8");
|
||||
const RadKernelInfo* rm = find(pl, "gelu_mul_bf16");
|
||||
if (!rg || !rv || !rq || (gelu && !rm) || !rg->layout || !rg->relayout) {
|
||||
@@ -13941,30 +13943,41 @@ static void mx_perf(const Plugin& pl, int64_t N, int64_t K, int64_t M, int iters
|
||||
std::vector<RadParam> ps = { PI("M", M), PI("N", N), PI("K", K), PI("group", 32),
|
||||
PS("dtype", "mxfp4a8") };
|
||||
perf_row(pl, row, ts, ps, d_w, wq.size(), copies, (double)(wq.size() + ws.size()), iters);
|
||||
/* At a gate_up shape the gemv row's GEMM is half of a pair: time the other half
|
||||
* (gated_quant_fp8 over its output) and the fold that replaces both. */
|
||||
if (!std::strcmp(row, "gemm_mxfp4a8_gemv") && N % 256 == 0 && K % 128 == 0) {
|
||||
/* At a gate_up shape the gemv row's GEMM (gemv_wide's from 9 rows) is half of a pair:
|
||||
* time the other half -- gated_quant_fp8 over its output (SwiGLU), and gelu_mul then
|
||||
* quant_act_fp8 (GeGLU) -- and the fold that replaces each, the gated row of the band. */
|
||||
if (!std::strcmp(row, "gemm_mxfp4a8_gemv") && N % 256 == 0 && K % 128 == 0 && M <= 64) {
|
||||
const int64_t n = N / 2;
|
||||
void* d_q = dalloc((size_t)(M * n));
|
||||
void* d_z = dalloc((size_t)(M * (n / 128) * 4));
|
||||
if (!d_q || !d_z) { std::printf(" gated: hipMalloc failed\n"); continue; }
|
||||
void* d_g = dalloc((size_t)(M * n * 2));
|
||||
if (!d_q || !d_z || !d_g) { std::printf(" gated: hipMalloc failed\n"); continue; }
|
||||
RadTensor nob = T(nullptr, RAD_BF16, { M, n });
|
||||
absent(nob);
|
||||
perf_row(pl, "gated_quant_fp8", { T(d_y, RAD_BF16, { M, N }), T(d_q, RAD_F8E4M3, { M, n }),
|
||||
T(d_z, RAD_F32, { M, n / 128 }), nob },
|
||||
{ PI("M", M), PI("n", n), PI("group", 128), PS("dtype", "bf16"), PS("act", "silu") },
|
||||
d_y, (size_t)(M * N * 2), 1, (double)(M * N * 2), iters, 0);
|
||||
const RadKernelInfo* rg = find(pl, "gemm_mxfp4a8_gated_gemv");
|
||||
perf_row(pl, "gelu_mul_bf16", { T(d_y, RAD_BF16, { M, N }), T(d_g, RAD_BF16, { M, n }) },
|
||||
{ PI("M", M), PI("n", n), PS("dtype", "bf16") },
|
||||
d_y, (size_t)(M * N * 2), 1, (double)(M * N * 2 + M * n * 2), iters, 0);
|
||||
perf_row(pl, "quant_act_fp8", { T(d_g, RAD_BF16, { M, n }), T(d_q, RAD_F8E4M3, { M, n }),
|
||||
T(d_z, RAD_F32, { M, n / 128 }) },
|
||||
{ PI("M", M), PI("n", n), PI("group", 128), PS("dtype", "bf16") },
|
||||
d_g, (size_t)(M * n * 2), 1, (double)(M * n * 3), iters, 0);
|
||||
const char* gname = M > 8 ? "gemm_mxfp4a8_gated_gemv_wide" : "gemm_mxfp4a8_gated_gemv";
|
||||
const RadKernelInfo* rg = find(pl, gname);
|
||||
std::vector<uint8_t> gr;
|
||||
if (rg && stored(rg, lp, 4, p, { "scale" }, &gr) == RAD_OK && gr == wr)
|
||||
perf_row(pl, "gemm_mxfp4a8_gated_gemv",
|
||||
{ T(d_a, RAD_F8E4M3, { M, K }), T(d_s, RAD_F32, { M, nkb }),
|
||||
T(d_w, RAD_U8, { (int64_t)wq.size() }), T(d_e, RAD_U8, { (int64_t)ws.size() }),
|
||||
T(d_r, RAD_U8, { (int64_t)wr.size() }), T(d_q, RAD_F8E4M3, { M, n }),
|
||||
T(d_z, RAD_F32, { M, n / 128 }), nob },
|
||||
{ PI("M", M), PI("N", N), PI("K", K), PI("group", 32), PS("dtype", "mxfp4a8"),
|
||||
PS("act", "silu") },
|
||||
d_w, wq.size(), copies, (double)(wq.size() + ws.size()), iters);
|
||||
for (const char* act : { "silu", "gelu_tanh" })
|
||||
perf_row(pl, gname,
|
||||
{ T(d_a, RAD_F8E4M3, { M, K }), T(d_s, RAD_F32, { M, nkb }),
|
||||
T(d_w, RAD_U8, { (int64_t)wq.size() }), T(d_e, RAD_U8, { (int64_t)ws.size() }),
|
||||
T(d_r, RAD_U8, { (int64_t)wr.size() }), T(d_q, RAD_F8E4M3, { M, n }),
|
||||
T(d_z, RAD_F32, { M, n / 128 }), nob },
|
||||
{ PI("M", M), PI("N", N), PI("K", K), PI("group", 32), PS("dtype", "mxfp4a8"),
|
||||
PS("act", act) },
|
||||
d_w, wq.size(), copies, (double)(wq.size() + ws.size()), iters);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28146,6 +28159,18 @@ static void device_phase(const Plugin& pl) {
|
||||
for (const int64_t m : { 1, 4, 8 }) mxfp4a8_gated_case(pl, m, 21504, 5376, true, true);
|
||||
mxfp4a8_gated_case(pl, 3, 256, 256, true, true);
|
||||
mxfp4a8_gated_case(pl, 2, 1024, 640, false, true);
|
||||
/* The fold from 9 to 64 rows against gemv_wide and the same pair: TranslateGemma's per-rank
|
||||
* gate_up (GeGLU) at the heights a verify of two to eight sequences makes and at a ragged
|
||||
* 9, one to four row tiles each partly live; small weights (four groups, one) at the row's
|
||||
* shallowest K and an uneven chunk count a wave, on both activation grids; the SwiGLU
|
||||
* form at Qwen's shape and a small one. */
|
||||
for (const int64_t m : { 9, 16, 24, 32, 48, 64 }) mxfp4a8_gated_case(pl, m, 21504, 5376, true, true);
|
||||
for (const int64_t m : { 9, 33, 64 }) mxfp4a8_gated_case(pl, m, 1024, 2688, false, true);
|
||||
for (const int64_t m : { 17, 50 }) mxfp4a8_gated_case(pl, m, 1024, 2688, true, true);
|
||||
mxfp4a8_gated_case(pl, 31, 256, 2816, true, true);
|
||||
for (const int64_t m : { 24, 64 }) mxfp4a8_gated_case(pl, m, 17408, 5120, true);
|
||||
for (const int64_t m : { 12, 40 }) mxfp4a8_gated_case(pl, m, 1024, 2688, false);
|
||||
mxfp4a8_gated_case(pl, 57, 256, 2816, true);
|
||||
/* Parts (ParoQuant): each weight part against its own activation copy, with bounds off any
|
||||
* kernel's tile, two and three parts, at every band. */
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_nt_m64", 7, 400, 640, 0, true, {}, 144);
|
||||
|
||||
@@ -7618,6 +7618,227 @@ TEST(gemma3_dflash2_drafter_taps_and_block) {
|
||||
CHECK_EQ(q.draft_depth, 0);
|
||||
}
|
||||
|
||||
/* ---- TranslateGemma as served: the MXFP4 trunk with DFlash2 at speculation 7 over two ranks, a lossy
|
||||
* wire whose fused junction takes the steps of up to 12 rows, eight sequences (verify steps of 8..64
|
||||
* rows). The A/B knobs over its decode steps (RADIANCE_G3_PF_ROWS, _PF_MIB, _PF_EARLY, _FOLD_ROWS)
|
||||
* are read at declare. */
|
||||
namespace {
|
||||
|
||||
struct G3Served {
|
||||
MetaKV mk = gemma3_dflash2_meta();
|
||||
RadBuilder b;
|
||||
int rank = 0;
|
||||
int rc = RAD_E_INVAL;
|
||||
using Env = std::vector<std::pair<const char*, const char*>>;
|
||||
explicit G3Served(int rk, const Env& env = {}) : rank(rk) {
|
||||
const RadEncoding mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, rad::arch::RAD_MX_GROUP);
|
||||
b.encs = { { "blk.", mx } };
|
||||
RadBuildCtx ctx = spec_ctx(rank, 2, 7);
|
||||
ctx.max_tok = 2048;
|
||||
ctx.tp_wire_lossy = 1;
|
||||
ctx.tp_wire_min_bytes = 128 * 1024;
|
||||
for (const auto& e : env) setenv(e.first, e.second, 1);
|
||||
rc = stderr_of_rc([&] { return gemma3::declare(&b, mk.meta(), &ctx); });
|
||||
for (const auto& e : env) unsetenv(e.first);
|
||||
}
|
||||
std::vector<std::unique_ptr<Kit>> kits; /* a step's context points into its kit */
|
||||
/* A verify step of `n_seq` sequences, 8 rows each. */
|
||||
std::vector<std::unique_ptr<RadCtx>> verify(int n_seq) {
|
||||
kits.push_back(kit(b, std::vector<int>((size_t)n_seq, 8), {}));
|
||||
return run_passes(b, gemma3::step, { kits.back().get() }, rank, 2);
|
||||
}
|
||||
};
|
||||
|
||||
/* The decode-side fingerprint: three plain decodes, every verify height a deployment of eight
|
||||
* sequences reaches (8..64 rows), a prefill chunk and a mixed step. */
|
||||
uint64_t g3_decode_steps(G3Served& s) {
|
||||
std::vector<std::unique_ptr<Kit>> ks;
|
||||
ks.push_back(kit(s.b, { 1, 1, 1 }, {}));
|
||||
for (int n = 1; n <= 8; ++n) ks.push_back(kit(s.b, std::vector<int>((size_t)n, 8), {}));
|
||||
ks.push_back(kit(s.b, {}, { 24 }));
|
||||
ks.push_back(kit(s.b, { 8 }, { 6 }));
|
||||
std::vector<std::unique_ptr<RadCtx>> all;
|
||||
std::vector<const Kit*> kp;
|
||||
for (const auto& k : ks) {
|
||||
auto v = run_passes(s.b, gemma3::step, { k.get() }, s.rank, 2);
|
||||
CHECK_PASSES(s.b, v);
|
||||
for (auto& c : v) all.push_back(std::move(c));
|
||||
kp.push_back(k.get());
|
||||
}
|
||||
return issue_hash(s.b, all, kp);
|
||||
}
|
||||
|
||||
/* The trunk's reads beside the reductions in one step: issue indices and their ops. */
|
||||
std::vector<size_t> g3_trunk_reads(const RadBuilder& b, const RadCtx& c) {
|
||||
std::vector<size_t> at;
|
||||
for (size_t i = 0; i < c.issues.size(); ++i) {
|
||||
const RecOp* o = b.op_of(c.issues[i].op);
|
||||
if (o && o->op == "prefetch" && o->w.size() == 1 &&
|
||||
b.weights[o->w[0] - 1].name.rfind("blk.", 0) == 0)
|
||||
at.push_back(i);
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* With the knobs set to their defaults the served build declares and steps exactly what it does with
|
||||
* them unset: graph and decode-side steps per rank against the knobs-unset build of the same tree.
|
||||
* RADIANCE_G3_PF_MIB alone changes nothing, since it sizes only the reads above 32 rows. */
|
||||
TEST(gemma3_served_knobs_at_their_defaults_keep_the_graph_and_steps) {
|
||||
struct Pin { uint64_t graph, steps; };
|
||||
Pin pins[2] = {};
|
||||
for (int rank = 0; rank < 2; ++rank) {
|
||||
G3Served k(rank, {});
|
||||
REQUIRE_EQ(k.rc, RAD_OK);
|
||||
pins[rank] = { graph_hash(k.b), g3_decode_steps(k) };
|
||||
}
|
||||
const std::vector<G3Served::Env> envs = {
|
||||
{ { "RADIANCE_G3_PF_ROWS", "32" }, { "RADIANCE_G3_PF_MIB", "24" },
|
||||
{ "RADIANCE_G3_PF_EARLY", "0" }, { "RADIANCE_G3_FOLD_ROWS", "0" } },
|
||||
{ { "RADIANCE_G3_PF_MIB", "12" } },
|
||||
{ { "RADIANCE_G3_FOLD_ROWS", "8" } },
|
||||
};
|
||||
for (int rank = 0; rank < 2; ++rank)
|
||||
for (const auto& env : envs) {
|
||||
G3Served k(rank, env);
|
||||
REQUIRE_EQ(k.rc, RAD_OK);
|
||||
const uint64_t g = graph_hash(k.b), st = g3_decode_steps(k);
|
||||
fprintf(stderr, " pin: gemma3 served rank %d (%zu knobs): graph 0x%016llxull, steps "
|
||||
"0x%016llxull\n", rank, env.size(), (unsigned long long)g,
|
||||
(unsigned long long)st);
|
||||
CHECK_EQ(g, pins[rank].graph);
|
||||
CHECK_EQ(st, pins[rank].steps);
|
||||
}
|
||||
}
|
||||
|
||||
/* RADIANCE_G3_PF_ROWS raises the height up to which a decode step reads the next GEMM's weight
|
||||
* beside each reduction (32 by default, as qwen35_fp8's). At 64 a verify of eight sequences takes
|
||||
* the reads too, every one of the 2L - 1; RADIANCE_G3_PF_MIB sizes the reads of the steps above 32
|
||||
* rows (24 MiB, the narrow steps' size, by default) as ops of their own, so the narrow steps keep
|
||||
* theirs. A prompt's rows never take them. */
|
||||
TEST(gemma3_prefetch_rows_knob_reads_beside_the_wider_verify_steps) {
|
||||
const int L = 12;
|
||||
for (const char* mib : { (const char*)nullptr, "12" }) {
|
||||
for (const char* rows : { (const char*)nullptr, "48", "64" }) {
|
||||
G3Served::Env env;
|
||||
if (rows) env.push_back({ "RADIANCE_G3_PF_ROWS", rows });
|
||||
if (mib) env.push_back({ "RADIANCE_G3_PF_MIB", mib });
|
||||
G3Served s(0, env);
|
||||
REQUIRE_EQ(s.rc, RAD_OK);
|
||||
const int64_t ceil = rows ? std::atoll(rows) : 32;
|
||||
const int64_t wide_bytes = (int64_t)(mib ? std::atoll(mib) : 24) << 20;
|
||||
for (int n = 1; n <= 8; ++n) {
|
||||
const auto v = s.verify(n);
|
||||
CHECK_PASSES(s.b, v);
|
||||
const RadCtx& c = *v[0];
|
||||
const std::vector<size_t> at = g3_trunk_reads(s.b, c);
|
||||
const int64_t T = 8 * n;
|
||||
CHECK_EQ((int)at.size(), T <= ceil ? 2 * L - 1 : 0);
|
||||
for (size_t i : at) {
|
||||
const RecOp* o = s.b.op_of(c.issues[i].op);
|
||||
CHECK_EQ(pint(o, "bytes"), T <= 32 ? (int64_t)24 << 20 : wide_bytes);
|
||||
CHECK_EQ(c.issues[i].lane, 1);
|
||||
}
|
||||
CHECK_EQ(races(s.b, c), 0);
|
||||
}
|
||||
for (const auto& k : { kit(s.b, {}, { 24 }), kit(s.b, { 8, 8 }, { 6 }) }) {
|
||||
const auto v = run_passes(s.b, gemma3::step, { k.get() }, 0, 2);
|
||||
CHECK_PASSES(s.b, v);
|
||||
CHECK(g3_trunk_reads(s.b, *v[0]).empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Out of range or not a number: refused at declare, naming the knob. */
|
||||
for (const auto& bad : { std::make_pair("RADIANCE_G3_PF_ROWS", "0"),
|
||||
std::make_pair("RADIANCE_G3_PF_ROWS", "x"),
|
||||
std::make_pair("RADIANCE_G3_PF_MIB", "0"),
|
||||
std::make_pair("RADIANCE_G3_PF_MIB", "257"),
|
||||
std::make_pair("RADIANCE_G3_PF_EARLY", "2") }) {
|
||||
G3Served s(0, { bad });
|
||||
CHECK(s.rc != RAD_OK);
|
||||
}
|
||||
}
|
||||
|
||||
/* Above 12 rows the lossy wire's all-reduce runs bare (unfused), issued by the block itself, so a
|
||||
* read issued after the block waits for that reduction rather than sitting beside it (its lane-1
|
||||
* join comes after the all-reduce). RADIANCE_G3_PF_EARLY=1 issues the read between the GEMM and the
|
||||
* bare all-reduce, as the fused junction's steps always have it. The fused steps (8 rows) are the
|
||||
* same either way: GEMM, read, fused all-reduce. */
|
||||
TEST(gemma3_prefetch_early_knob_puts_the_read_in_front_of_a_bare_all_reduce) {
|
||||
for (int early : { 0, 1 }) {
|
||||
G3Served s(1, early ? G3Served::Env{ { "RADIANCE_G3_PF_EARLY", "1" } } : G3Served::Env{});
|
||||
REQUIRE_EQ(s.rc, RAD_OK);
|
||||
for (int n : { 1, 2, 4 }) {
|
||||
const auto v = s.verify(n);
|
||||
CHECK_PASSES(s.b, v);
|
||||
const RadCtx& c = *v[0];
|
||||
const std::vector<std::string> ops = issued_ops(s.b, c);
|
||||
const std::vector<size_t> at = g3_trunk_reads(s.b, c);
|
||||
REQUIRE(!at.empty());
|
||||
const bool fused = n == 1;
|
||||
for (size_t i : at) {
|
||||
REQUIRE(i > 0 && i + 1 < ops.size());
|
||||
const std::string& before = ops[i - 1];
|
||||
const std::string& after = ops[i + 1];
|
||||
if (fused) {
|
||||
CHECK_EQ(before, std::string("gemm_nt_q"));
|
||||
CHECK_EQ(after, std::string("ar_rmsnorm_post_quant_fp8"));
|
||||
} else if (early) {
|
||||
CHECK_EQ(before, std::string("gemm_nt_q"));
|
||||
CHECK_EQ(after, std::string("all_reduce"));
|
||||
CHECK(ordered(c, i - 1, i));
|
||||
} else {
|
||||
CHECK_EQ(before, std::string("all_reduce"));
|
||||
}
|
||||
}
|
||||
CHECK_EQ(races(s.b, c), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* RADIANCE_G3_FOLD_ROWS declares the gated fold (gate_up, the GeGLU and down's quantiser in one
|
||||
* launch) up to that many rows instead of 8: a verify step at or under the ceiling issues the fold
|
||||
* once a layer and neither gelu_mul nor down's own quantiser; above it, the unfused three. Only o's
|
||||
* quantiser is left at a folded step. Out of range (past 64, or not a number) is refused. */
|
||||
TEST(gemma3_fold_rows_knob_folds_the_gated_gemm_up_to_its_ceiling) {
|
||||
const int L = 12;
|
||||
for (const char* fr : { (const char*)nullptr, "16", "32", "64" }) {
|
||||
G3Served s(0, fr ? G3Served::Env{ { "RADIANCE_G3_FOLD_ROWS", fr } } : G3Served::Env{});
|
||||
REQUIRE_EQ(s.rc, RAD_OK);
|
||||
const int64_t hi = fr ? std::atoll(fr) : 8;
|
||||
int folds = 0;
|
||||
for (const auto& o : s.b.ops)
|
||||
if (o.op == "gemm_nt_q_gated_mx" && !o.w.empty() &&
|
||||
s.b.weights[o.w[0] - 1].name.rfind("blk.", 0) == 0) {
|
||||
++folds;
|
||||
CHECK_EQ(pstr(&o, "act"), std::string("gelu_tanh"));
|
||||
CHECK_EQ(param(&o, "M")->ihi, hi);
|
||||
CHECK_EQ(pint(&o, "N"), 21504); /* gate|up, this rank's half of 2 x 21504 */
|
||||
}
|
||||
CHECK_EQ(folds, L);
|
||||
for (int n = 1; n <= 8; ++n) {
|
||||
const auto v = s.verify(n);
|
||||
CHECK_PASSES(s.b, v);
|
||||
const std::vector<std::string> ops = issued_ops(s.b, *v[0]);
|
||||
const bool fold = 8 * n <= hi;
|
||||
int gated = 0;
|
||||
for (size_t i = 0; i < ops.size(); ++i)
|
||||
if (ops[i] == "gemm_nt_q_gated_mx") {
|
||||
++gated;
|
||||
CHECK_EQ(v[0]->issues[i].n, 8 * n);
|
||||
}
|
||||
CHECK_EQ(gated, fold ? L : 0);
|
||||
CHECK_EQ(count_issued(ops, "gelu_mul"), fold ? 0 : L);
|
||||
CHECK_EQ(count_issued(ops, "quant_act_fp8"), fold ? L : 2 * L);
|
||||
}
|
||||
}
|
||||
for (const char* bad : { "65", "-1", "x" }) {
|
||||
G3Served s(0, { { "RADIANCE_G3_FOLD_ROWS", bad } });
|
||||
CHECK(s.rc != RAD_OK);
|
||||
}
|
||||
}
|
||||
|
||||
/* --kv-cache-dtype fp8_v4: the trunk's attention cache is E4M3 K (256 bytes), int4 V (128) and
|
||||
* eight f16 scales, 400 bytes a token and head, declared as 25 * 256 / 32 = 200 E4M3 columns so the
|
||||
* core sizes its pages; every trunk op on it bands on kv_dtype fp8_v4. The DFlash2 drafter's own
|
||||
|
||||
Reference in new issue
Block a user