Archived
libr4d, arch, deploy: coded overlap all-reduces, multi-user verify tuning, the split junction, a compose file
The prefill overlap's lane all-reduces coded losslessly at ~12.2 bits a value; the verify step's GEMM, norm and all-reduces tuned for 17-64 rows; at 4-16 rows each GPU norms its own rows between a reduce-scatter and an int8 all-gather; deploy/compose/gemma4-31b-w4a8-df2-tp4.yaml. Squashed from: - libr4d: the four-rank exact two-shot coded at ~12.2 bits a bf16 value, for the lane row - libr4d: the prefill overlap's lane all-reduces coded by default - libr4d: decode-sized two-shots take fewer blocks - libr4d: gemm_w4a8_nt_m64 at multi-user verify heights (33-64 rows) -- narrower row tiles, wider N - libr4d: rmsnorm_quant_i8's register form at 512 threads too (33 to 72-120 rows) - libr4d: ar_rmsnorm_quant_i8_4rank's `sp`, the all-reduce split around the junction - arch/gemma4_bf16: the 31B's junctions take libr4d's `sp` at 4-16 rows at four ranks - deploy/compose, README: Gemma 4 31B with RedHatAI's DFlash2 drafter on four cards Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ab2b3be6c0
commit
5e8bae8ae6
12 files changed
+950
-44
No files matched your search
@@ -29,8 +29,9 @@ To support the development of radiance: [Patreon](https://patreon.com/StillDeadC
|
||||
| MiniCPM5-2B bf16, DSpark drafter | `minicpm5-2b-bf16.rad` (5 GiB) | [download](https://huggingface.co/StillDeadcode/minicpm5-2b-bf16) | `minicpm5-2b-bf16.yaml` |
|
||||
| Gemma 4 31B: GPTQ int4 trunk, int8 activations, MTP, vision | `gemma4-31b-w4a8.rad` (20 GiB) | [download](https://huggingface.co/keptn/gemma4-31b-w4a8) | `gemma4-31b-w4a8.yaml` |
|
||||
| Gemma 4 31B heretic-ara (refusals removed): GPTQ int4 trunk, int8 activations, MTP, vision | `gemma4-31b-heretic-ara-w4a8.rad` (20 GiB) | [download](https://huggingface.co/keptn/gemma4-31b-heretic-ara-w4a8) | `gemma4-31b-heretic-ara-w4a8.yaml` |
|
||||
| Gemma 4 31B: int4 trunk, int8 activations and lm_head, DFlash drafter, vision | `gemma4-31b-dense-w4a8-df.rad` (20 GiB) | [convert](#convert-a-checkpoint) | `gemma4-31b-w4a8-df.yaml` |
|
||||
| Gemma 4 31B: int4 trunk, int8 activations and lm_head, DFlash drafter, vision | `gemma4-31b-dense-w4a8-df.rad` (22 GiB) | [download](https://huggingface.co/Ashlyn4202025/gemma4-31b-w4a8-dflash) | `gemma4-31b-w4a8-df.yaml` |
|
||||
| Gemma 4 31B Q8: int8 trunk and lm_head (Q8_0's grid at group 128), int8 DFlash drafter, vision | `gemma4-31b-dense-q8-df.rad` (33 GiB) | [convert](#convert-a-checkpoint) | `gemma4-31b-q8-df.yaml` |
|
||||
| Gemma 4 31B on four cards: int4 trunk, int8 activations and lm_head, RedHatAI's DFlash2 drafter, vision | `gemma4-31b-dense-w4a8-df2-qkv.rad` (23 GiB) | [download](https://huggingface.co/Ashlyn4202025/gemma4-31b-w4a8-dflash2) | `gemma4-31b-w4a8-df2-tp4.yaml` |
|
||||
| Qwen-Image 2.1 FP8: text to image and image edits | `qwen-image-2.1-fp8.rad` (16 GiB) | [download](https://huggingface.co/StillDeadcode/radiance-qwen-image-2.1-fp8) | `qwen-image-2.1.yaml` |
|
||||
| Qwen-Image 2.1 Turbo FP8: the same in 8 steps | `qwen-image-2.1-turbo-fp8.rad` (16 GiB) | [download](https://huggingface.co/StillDeadcode/radiance-qwen-image-2.1-turbo-fp8) | `qwen-image-2.1-turbo.yaml` |
|
||||
| Krea 2 Turbo FP8: text to image, one card | `krea-2-turbo-fp8.rad` (16 GiB) | [download](https://huggingface.co/StillDeadcode/krea-2-turbo-radiance-fp8) | `krea-2-turbo.yaml` |
|
||||
|
||||
@@ -888,16 +888,40 @@ void Gemma4Mlp::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool tai
|
||||
* taller step issues the pair. */
|
||||
static constexpr int64_t kArOneShotElems = 524288;
|
||||
static constexpr int64_t kAr4FoldRows = 16;
|
||||
/* From kArSpMinRows rows to the same band's top, libr4d's four-rank row takes `sp` instead: the
|
||||
* all-reduce split around the junction. Each rank reduces only the rows it owns (a quarter, in
|
||||
* rank order), runs the junction on them and gathers the int8 codes the next GEMM reads, so the
|
||||
* second hop carries a byte a column instead of two and the junction a quarter of the rows. The
|
||||
* fold and the sums are the whole form's, so q and the scales are byte for byte the same; the
|
||||
* residual stream then holds this rank's rows only, which is all the next junction reads. A
|
||||
* junction whose stream anything else reads next asks for the residual on every row too (sp 2):
|
||||
* the layers the drafter taps, and the last layer's attention junction, since that layer's
|
||||
* feed-forward closes with its own all-reduce, post-norm and add over every row. One launch at a
|
||||
* time on 4x R9700 (2026-10-19): 19.8 against 21.9 us at 4 rows of 5376 (the one-shot fold), 22.9
|
||||
* against 24.5 at 8 and 30.8 against 34.9 at 16 (the tail), 72.3 against 89.2 at 64. Below 4 rows
|
||||
* a rank would idle (31.7 against 18.7 us at one).
|
||||
*
|
||||
* In the engine it pays only where it replaces the fold: a DFlash2 verify (8 rows) ran 402 / 297 /
|
||||
* 216 / 603 tok/s code / prose / story / long against 392 / 290 / 210 / 585, replies identical,
|
||||
* but steps of 24-64 rows, where the pair runs instead, lost ~10% under load (4 users 588 against
|
||||
* 698 aggregate, 8 users 781 against 867; 12 users even; 4x R9700, 2026-10-19). So it serves to
|
||||
* kArSpMaxRows, the fold's band, within the one-shot's. */
|
||||
static constexpr int64_t kArSpMinRows = 4;
|
||||
static constexpr int64_t kArSpMaxRows = 16;
|
||||
|
||||
struct G4Junction {
|
||||
rad_op op = 0;
|
||||
rad_op op_ar = 0; /* the same with the exact all-reduce of `x` in front, or 0 */
|
||||
int64_t ar_rows = 0; /* the most rows op_ar serves: the step's at two ranks, a band at four */
|
||||
rad_op op_sp = 0; /* ...with the all-reduce split around it (four ranks), or 0 */
|
||||
int64_t sp_hi = 0; /* the most rows op_sp serves, from kArSpMinRows */
|
||||
rad_buf x = 0, res = 0, q8 = 0, s8 = 0;
|
||||
rad_weight w = 0, wp = 0, sc = 0;
|
||||
|
||||
/* `full_res`: something besides the next junction reads the residual stream after this one,
|
||||
* so the split form must leave it on every row. */
|
||||
int declare(RadBuilder* b, const Geom& g, rad_buf x_, rad_buf res_, rad_weight w_,
|
||||
rad_weight wp_, rad_weight sc_, const G4Linear& into) {
|
||||
rad_weight wp_, rad_weight sc_, const G4Linear& into, bool full_res = false) {
|
||||
x = x_; res = res_; w = w_; wp = wp_; sc = sc_; q8 = into.q8; s8 = into.s8;
|
||||
if (!into.quant || into.shared_in || !q8 || !s8 || !w || (sc && !wp) || ((wp || sc) && !res))
|
||||
return RAD_E_INVAL;
|
||||
@@ -934,11 +958,32 @@ struct G4Junction {
|
||||
rad_op_writes(b, op_ar, wa, 4);
|
||||
ar_rows = hi;
|
||||
}
|
||||
/* The split form from kArSpMinRows to kArSpMaxRows rows. It needs the post-norm, as
|
||||
* every trunk junction past the first has; `x` is declared written, as the fold's. */
|
||||
const int64_t top = std::min<int64_t>(
|
||||
std::min<int64_t>(g.max_tok, kArOneShotElems / g.n_embd), kArSpMaxRows);
|
||||
if (op_ar && four && wp && top >= kArSpMinRows) {
|
||||
const RadParam ps[] = { RAD_RANGE("M", 1, top), RAD_INT("n", g.n_embd),
|
||||
RAD_F64("eps", g.eps), RAD_INT("world_size", g.world),
|
||||
RAD_STR("dtype", g.dtype), RAD_F64("wadd", g.wadd),
|
||||
RAD_INT("sp", full_res ? 2 : 1) };
|
||||
op_sp = rad_decl_op(b, "ar_rmsnorm_quant_i8", ps, (int)(sizeof ps / sizeof ps[0]),
|
||||
ws, nw);
|
||||
if (op_sp) {
|
||||
const rad_buf wa[4] = { x, q8, s8, res };
|
||||
rad_op_reads (b, op_sp, rd, 2);
|
||||
rad_op_writes(b, op_sp, wa, 4);
|
||||
sp_hi = top;
|
||||
}
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
/* The most rows a step may have for step(..., true): the fold's band, and the split form's. */
|
||||
int64_t ar_top() const { return op_sp ? std::max(ar_rows, sp_hi) : ar_rows; }
|
||||
void step(RadCtx* c, int64_t T, bool with_ar = false) const {
|
||||
RAD_ISSUE_N(c, with_ar ? op_ar : op, T, brows(x, T), res ? brows(res, T) : RAD_NONE, RAD_W(w),
|
||||
const rad_op o = !with_ar ? op : (op_sp && T >= kArSpMinRows && T <= sp_hi) ? op_sp : op_ar;
|
||||
RAD_ISSUE_N(c, o, T, brows(x, T), res ? brows(res, T) : RAD_NONE, RAD_W(w),
|
||||
brows(q8, T), brows(s8, T), RAD_NONE,
|
||||
wp ? RAD_W(wp) : RAD_NONE, sc ? RAD_W(sc) : RAD_NONE);
|
||||
}
|
||||
@@ -1941,13 +1986,18 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
ok = m.j0.op != 0;
|
||||
for (int64_t l = 0; ok && l < m.n_main; ++l) {
|
||||
Layer& lay = m.layers[(size_t)l];
|
||||
/* the split form's residual on every row: before the last layer's feed-forward
|
||||
* (its own reduction, post-norm and add over every row), and where the drafter
|
||||
* taps the stream */
|
||||
bool tapped = false;
|
||||
for (int t = 0; t < m.n_taps; ++t) tapped = tapped || m.tap_layer[t] == (int)l;
|
||||
RAD_ARCH_TRY(lay.j_am.declare(b, g, m.b_h, m.b_x, lay.mlp.w_norm, lay.attn.w_post, 0,
|
||||
lay.mlp.ln_gu));
|
||||
lay.mlp.ln_gu, l + 1 == m.n_main));
|
||||
ok = lay.j_am.op != 0;
|
||||
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_in()));
|
||||
lay.w_scalar, nx.attn.ln_in(), tapped));
|
||||
ok = lay.j_ma.op != 0;
|
||||
}
|
||||
}
|
||||
@@ -1958,8 +2008,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
for (int64_t l = 0; ar && l < m.n_main; ++l) {
|
||||
const Layer& lay = m.layers[(size_t)l];
|
||||
ar = lay.j_am.op_ar != 0 && (l + 1 >= m.n_main || lay.j_ma.op_ar != 0);
|
||||
if (ar) rows = std::min(rows, lay.j_am.ar_rows);
|
||||
if (ar && l + 1 < m.n_main) rows = std::min(rows, lay.j_ma.ar_rows);
|
||||
if (ar) rows = std::min(rows, lay.j_am.ar_top());
|
||||
if (ar && l + 1 < m.n_main) rows = std::min(rows, lay.j_ma.ar_top());
|
||||
}
|
||||
m.fuse_ar = ar;
|
||||
m.ar_rows = ar ? rows : 0;
|
||||
@@ -2178,6 +2228,11 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
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");
|
||||
else if (g.world == 4 && m.layers[0].j_am.op_sp)
|
||||
rad_note(b, "residual junctions: the all-reduce in front of each folded in at steps of "
|
||||
"%lld rows or fewer (libr4d's four-rank row: the fold to %lld rows, the "
|
||||
"all-reduce split around the junction from %lld), issued apart above",
|
||||
(long long)m.ar_rows, (long long)kArSpMinRows - 1, (long long)kArSpMinRows);
|
||||
else if (g.world == 4)
|
||||
rad_note(b, "residual junctions: the all-reduce in front of each folded in at steps of "
|
||||
"%lld rows or fewer (the four-rank one-shot), issued apart above",
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# Gemma 4 31B-it on four cards: int4 trunk (round-to-nearest with a scale search, from the bf16
|
||||
# checkpoint; q, k and v as one linear) with int8 activations and lm_head, RedHatAI's DFlash2
|
||||
# speculator merged in (gemma-4-31B-it-speculator.dflash2: block 8, 7 drafts a pass, int8 head), and
|
||||
# the vision tower. Converted with data/recipes/gemma4-31b-dense-bf16-w4a8-df2.recipe, or downloaded
|
||||
# from https://huggingface.co/Ashlyn4202025/gemma4-31b-w4a8-dflash2. The drafter
|
||||
# serves the depth it was trained at (`auto`). Sampling defaults are the checkpoint's
|
||||
# generation_config; thinking is switched per request (chat_template_kwargs).
|
||||
#
|
||||
# --tp 4 over four cards; chained launches, the weight reads beside the collectives and the prefill
|
||||
# overlap turn themselves on there (each has its RADIANCE_* override for an A/B, none needed here).
|
||||
# A prompt alone takes 8192-token prefill chunks; beside other users a chunk takes at most 512
|
||||
# tokens of a step, with one decode-only step after it, so a long prompt holds the others up by a
|
||||
# fraction of a chunk rather than all of it. 16 sequences, as measured below.
|
||||
#
|
||||
# On 4x R9700 (PCIe 4.0 x16), 2026-10-20: one user 390 code / 288 prose / 208 story tok/s, and
|
||||
# 590 after a 6442-token prompt (prefill 5962 tok/s); 302 / 512 / 658 / 835 / 981 tok/s aggregate
|
||||
# at 1 / 2 / 4 / 8 / 16 users (BetterBench, thinking off).
|
||||
#
|
||||
# Measured with the host CPU's performance governor and its idle states past C1 off. The cards'
|
||||
# peer-to-peer traffic crosses the root complex, so the host's power state shows in every step, and
|
||||
# a compose file can't set it.
|
||||
name: radiance-gemma4-31b-w4a8-df2-tp4
|
||||
services:
|
||||
radiance:
|
||||
extends: { file: common.yaml, service: radiance }
|
||||
container_name: radiance-gemma4-31b-w4a8-df2-tp4
|
||||
command: >
|
||||
--model /models/gemma4-31b-dense-w4a8-df2-qkv.rad
|
||||
--host :: --port ${PORT:-8000} --api-key ${RADIANCE_API_KEY:?set RADIANCE_API_KEY in .env}
|
||||
--tp 4 --max-num-seqs 16 --max-model-len 65536 --kv-cache-dtype fp8
|
||||
--num-speculative-tokens auto --max-num-batched-tokens 8192
|
||||
--busy-prefill-chunk 512 --prefill-interleave 1
|
||||
--temp 1.0 --top-p 0.95 --top-k 64
|
||||
+9
-1
@@ -2171,7 +2171,7 @@ device code.
|
||||
| `ar_rmsnorm_quant_fp8` | `M` `n` `eps`:f64 `group` `world_size` `dtype` `wadd`:f64/opt `wire`/opt | `x`:inout, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? |
|
||||
| `ar_rmsnorm_post_quant_fp8` | `M` `n` `eps`:f64 `group` `world_size` `dtype` `wadd`:f64/opt `wire`/opt | `x`:inout, `residual`:inout, `w`w → `q`, `scale`, `out_bf16`?; `w_post`w |
|
||||
| `ar_rmsnorm_had_quant_i8g` | `M` `n` `eps`:f64 `group` `world_size` `dtype` `wadd`:f64/opt `wire`/opt | `x`:inout, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? |
|
||||
| `ar_rmsnorm_quant_i8` | `M` `n` `eps`:f64 `world_size` `dtype` `wadd`:f64/opt | `x`:inout, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`?; `w_post`w?, `scalar`w? |
|
||||
| `ar_rmsnorm_quant_i8` | `M` `n` `eps`:f64 `world_size` `dtype` `wadd`:f64/opt `sp`/opt | `x`:inout, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`?; `w_post`w?, `scalar`w? |
|
||||
| `ar_hc_write` | `M`(range) `n` `hc` `world_size` `dtype` `wire`/opt | `y`:inout, `inj`, `h`:inout |
|
||||
| `ar_gather_hc_write` | `M`(range) `n` `hc` `top_k` `world_size` `dtype` `act`/opt `wire`/opt | `y_expert`, `expert_w`, `sorted_tok`, `shared`?, `shared_gate`?, `y`:inout, `inj`, `h`:inout |
|
||||
| `ar_ln_had_quant_i8` | `M` `n` `eps`:f64 `group` `world_size` `dtype` `wadd`:f64/opt `wire`/opt | `x`, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? |
|
||||
@@ -2183,6 +2183,14 @@ the message and the input, and the reduced sum lands in it.
|
||||
`ar_rmsnorm_had_quant_i8g` is `ar_rmsnorm_quant_fp8` with `rmsnorm_had_quant_i8g`'s quantiser:
|
||||
`x` and the residual are the same bytes, and the codes are int8 for a weight at `i8a8h`.
|
||||
|
||||
`sp` on `ar_rmsnorm_quant_i8` (0 when absent) lets a row split the all-reduce around the
|
||||
junction: reduce-scatter, the junction on the rows this rank owns (`M` split evenly over the
|
||||
ranks in rank order), all-gather of what the next GEMM reads. At 1 the op promises `q` and
|
||||
`scale` on every row and `residual` on this rank's rows only; at 2 `residual` on every row too;
|
||||
either way `x` is left unspecified. A row may always run the whole form, which keeps every
|
||||
promise. The caller must not read a residual row it does not own after an `sp` 1 junction
|
||||
until a junction or reduction writes it whole again.
|
||||
|
||||
The norm-carrying forms are two-rank only. `ar_hc_write` and `ar_gather_hc_write` also have an
|
||||
exact-wire four-rank form. At other widths the arch blocks issue the plain ops.
|
||||
|
||||
|
||||
+15
-2
@@ -432,6 +432,17 @@ void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long pee
|
||||
long slot_stride16, long inp, long out, long n_elem, long dtype,
|
||||
long rank, long stream, long nblocks, long nthreads, long drain,
|
||||
long acq, long pub, long gate, long gate_ctr, long chain);
|
||||
// The four-rank exact two-shot with both pushes coded losslessly at ~12.2 bits a bf16 value (the
|
||||
// same bytes out; r4d_ar_twoshot_Nrank_exact.hip). r4d_ar_twoshot_coded_ok says whether a message
|
||||
// and slot fit the coded form: bf16, shards of whole 256-value waves, two coded regions a slot.
|
||||
int r4d_ar_twoshot_coded_ok(long n_elem, long dtype, long world, long slot_stride16);
|
||||
void r4d_ar_twoshot_4rank_exact_coded(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 rank, long stream, long nblocks, long nthreads,
|
||||
long drain, long acq, long pub, long gate, long gate_ctr,
|
||||
long chain);
|
||||
void r4d_ar_twoshot_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,
|
||||
@@ -794,7 +805,9 @@ int r4d_ar_rmsnorm_quant_i8(long peer_scratch, long my_scratch, long peer_flags
|
||||
// arguments (peers in ascending rank, this rank's own, the counters, the slot stride), `pub`, the
|
||||
// engine's link gate with the instance's landed counter (0 for none; r4d_ar_seq.h), and, with
|
||||
// `tail_nbt` blocks (positive), the standalone two-shot on that many with the junction as its tail,
|
||||
// counting on the two words at `tail_ctr` (ar_rmsnorm_quant_i8_tail_kernel).
|
||||
// counting on the two words at `tail_ctr` (ar_rmsnorm_quant_i8_tail_kernel). With `sp` 1 or 2 the
|
||||
// all-reduce split around the junction instead (ar_rmsnorm_quant_i8_sp_kernel): q and S on every
|
||||
// row, the residual on this rank's rows (every row at 2), `x` left as it came.
|
||||
int r4d_ar_rmsnorm_quant_i8_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,
|
||||
@@ -803,7 +816,7 @@ int r4d_ar_rmsnorm_quant_i8_4rank(long peer_scratch0, long peer_scratch1, long
|
||||
float wadd, int w_f32, int r_ld, int y_ld, int rank,
|
||||
long stream, int nblocks, int drain, int acq, int pub,
|
||||
long gate, long gate_ctr, long tail_ctr, int tail_nbt,
|
||||
int chain);
|
||||
int chain, int sp);
|
||||
|
||||
// ---- sampling ----------------------------------------------------------------------------------
|
||||
//
|
||||
|
||||
+40
-11
@@ -667,23 +667,27 @@ static int nb_for(long words16, int max_nb) {
|
||||
return (int)nb;
|
||||
}
|
||||
|
||||
/* The four-rank exact one-shot's two-shot band (r4d_rad_ar_oneshot_4rank_exact from ts_min), over
|
||||
* the words of one shard: one block per 672 words, floor 2,
|
||||
* ceiling max_nb. A two-shot takes two handshakes, each a PCIe round trip a block, so small shards
|
||||
* want fewer blocks than nb_for's floor of 4: at a DFlash2 verify's 8 rows of 5376 at tp 4 (1344
|
||||
* words a shard) two blocks run the exact two-shot in 20.2 us against 22.8 at four, and from 16
|
||||
* rows (2688 words, four blocks either way) the two rules agree (4x R9700, a four-card
|
||||
* microbenchmark of this kernel, 2026-10-17). The block count moves no byte: every word is folded
|
||||
* by one thread in ascending rank order whatever the grid. */
|
||||
/* The four-rank exact two-shot at decode and verify heights (the one-shot row's band from ts_min,
|
||||
* the junction's tail form, the two-shot row up to kTsDecodeShard words a shard), over the words of
|
||||
* one shard: one block per 2688 words, floor 2, ceiling 8 (and max_nb). A two-shot takes two
|
||||
* handshakes, each a PCIe round trip a block, so a block more costs more than its share of the
|
||||
* bytes saves until the shards are large. One per 672 (the rule before 2026-10-19) launched 16
|
||||
* blocks at 64 rows of 5376, where the multi-user verify runs (8 users x DFlash2's 8 rows): on 4x
|
||||
* R9700, one launch at a time on the real payload, 64 rows took 85.6 us at 16 blocks against 75.3
|
||||
* at 4, 32 rows 58.9 at 16 / 47.1 at 8 against 44.2 at 4, 128 rows 143.5 at 16 against 137.9 at 8;
|
||||
* 8 rows (two blocks either way) 19.6. The block count moves no byte: every word is folded by one
|
||||
* thread in ascending rank order whatever the grid. */
|
||||
/* The chained two-shot (r4d_chain.h) where the row resolved with `chain`, for a decode-sized
|
||||
* message only (64 rows of an 8192-wide residual): DFlash2's verify-height reductions. */
|
||||
static long ar_chain(const RadArgs* a, long numel) {
|
||||
return r4d_geti_or(a, "chain", 0) != 0 && numel <= 64L * 8192L ? 1L : 0L;
|
||||
}
|
||||
|
||||
static constexpr long kTsDecodeShard = 43008; /* 256 rows of 5376 at tp 4 */
|
||||
static int nb_for_shard(long shard16, int max_nb) {
|
||||
long nb = shard16 / 672;
|
||||
long nb = shard16 / 2688;
|
||||
if (nb < 2) nb = 2;
|
||||
if (nb > 8) nb = 8;
|
||||
if (nb > max_nb) nb = max_nb;
|
||||
if (nb > shard16) nb = shard16 > 0 ? shard16 : 1;
|
||||
return (int)nb;
|
||||
@@ -740,7 +744,9 @@ static int ar_setup(const RadArgs* a, ArCall* c, int shard_nb) {
|
||||
* flag rounds cost more than the added reduce bandwidth (8 against 16 blocks measured up to 3 %
|
||||
* faster from 2097152 elements on). */
|
||||
if (shard_nb && in->world == 8 && cap > 8) cap = 8;
|
||||
c->nblocks = cfgv.nblocks > 0 ? cfgv.nblocks : nb_for(basis, cap);
|
||||
/* a decode-sized shard takes the decode rule (nb_for_shard), a prompt-sized one nb_for's */
|
||||
c->nblocks = cfgv.nblocks > 0 ? cfgv.nblocks
|
||||
: (shard_nb && basis <= kTsDecodeShard) ? nb_for_shard(basis, cap) : nb_for(basis, cap);
|
||||
if (c->nblocks > in->max_blocks) c->nblocks = in->max_blocks;
|
||||
return RAD_OK;
|
||||
}
|
||||
@@ -1114,6 +1120,25 @@ extern "C" int r4d_rad_ar_twoshot_4rank_exact(const RadArgs* a, RadStream s) {
|
||||
if (words16 % c.in->world) return RAD_E_SHAPE;
|
||||
if (2 * (c.numel * c.in->esize) / c.in->world > c.in->slot_bytes) return RAD_E_SCRATCH;
|
||||
const ArInst* in = c.in;
|
||||
/* A reduction beside compute (overlap 1) carries its pushes coded at ~12.2 bits a bf16 value
|
||||
* (r4d_ar_twoshot_Nrank_exact.hip, the coded form), at 4 blocks of 1024 threads, wherever the
|
||||
* message fits that form; the same bytes either way. The 31B's prefill (4x R9700, a
|
||||
* 5033-token prompt): 5724 -> 5949 tok/s (+3.9%), every reply identical; 8 x 512 gave +2.1%,
|
||||
* 16 x 256 +1.3%, 6 x 1024 -1.9% (the blocks crowd the GEMM beside them). `coded` 0, a launch
|
||||
* parameter no declaration carries, keeps the plain wire: r4d_selftest's plain arm. */
|
||||
const bool lane = r4d_geti_or(a, "overlap", 0) == 1;
|
||||
if (lane && r4d_geti_or(a, "coded", 1) != 0 &&
|
||||
r4d_ar_twoshot_coded_ok(c.numel, in->dtype, 4, in->slot16)) {
|
||||
const long gate = link_gate(a), early = in->host ? 0L : gate;
|
||||
R4D_TRY("ar_twoshot_4rank_exact (coded)",
|
||||
r4d_ar_twoshot_4rank_exact_coded(in->peer_scratch[0], in->peer_scratch[1], in->peer_scratch[2],
|
||||
in->peer_flags[0], in->peer_flags[1], in->peer_flags[2],
|
||||
in->my_scratch, in->my_flags, in->my_seq, in->slot16,
|
||||
c.inp, c.out, c.numel, in->rank, (long)(uintptr_t)s,
|
||||
4, 1024, c.drain, c.acq, c.pub, early,
|
||||
early ? in->gate_ctr : 0L, ar_chain(a, c.numel)));
|
||||
return early ? RAD_LINK_ENDED : RAD_OK;
|
||||
}
|
||||
/* The kernel ends the hold after its second handshake (its reduce is fused with the phase-2
|
||||
* push); through the host mailbox early stays 0 and the engine releases by barrier. */
|
||||
const long gate = link_gate(a), early = in->host ? 0L : gate;
|
||||
@@ -1548,6 +1573,10 @@ extern "C" int r4d_rad_ar_rmsnorm_quant_i8_4rank(const RadArgs* a, RadStream s)
|
||||
if (tail_nbt > in->max_blocks) tail_nbt = in->max_blocks;
|
||||
}
|
||||
const long tail_ctr = in->my_seq + (2L * in->max_blocks + 8) * 4;
|
||||
/* `sp` (0 unless asked): the all-reduce split around the junction, P2P only. The host mailbox
|
||||
* runs the whole form, which leaves every byte the split one does. */
|
||||
const long long sp = r4d_geti_or(a, "sp", 0);
|
||||
if (sp < 0 || sp > 2) return RAD_E_INVAL;
|
||||
/* The junction after the last handshake is local, so either form releases there. */
|
||||
const long gate = link_gate(a), early = in->host ? 0L : gate;
|
||||
const int e = r4d_ar_rmsnorm_quant_i8_4rank(
|
||||
@@ -1559,7 +1588,7 @@ extern "C" int r4d_rad_ar_rmsnorm_quant_i8_4rank(const RadArgs* a, RadStream s)
|
||||
(int)v.M, (int)v.K, (float)r4d_getf(a, "eps", 1e-6), (float)r4d_getf(a, "wadd", 0.0),
|
||||
v.w->dtype == RAD_F32, (int)v.rp, (int)v.yp, (int)in->rank, (long)(uintptr_t)s, nblocks,
|
||||
cfgv.drain, cfgv.acq, cfgv.pub, early, early ? in->gate_ctr : 0L, tail_ctr, tail_nbt,
|
||||
r4d_geti_or(a, "chain", 0) != 0 ? 1 : 0);
|
||||
r4d_geti_or(a, "chain", 0) != 0 ? 1 : 0, in->host ? 0 : (int)sp);
|
||||
if (e != 0) return RAD_E_SHAPE;
|
||||
return early ? RAD_LINK_ENDED : RAD_OK;
|
||||
}
|
||||
|
||||
@@ -158,6 +158,222 @@ __device__ __forceinline__ void r4d_ar_twoshot_wide_exact_body(
|
||||
if (CHAIN) r4d_chain_done();
|
||||
}
|
||||
|
||||
// ---- the coded form: both pushes carry ~12.2 bits a bf16 value instead of 16, losslessly
|
||||
//
|
||||
// The prefill overlap's reductions are link-bound, and a residual stream's bf16 words have few
|
||||
// distinct exponents: on the 31B (4x R9700, 64 groups of 512 x 5376 values from a 9.9k-token
|
||||
// prefill) 2.5-3.2 bits of exponent entropy against 8 stored. A chunk is one int4 word (8
|
||||
// values); 8 consecutive chunks (64 values) form a group that shares a base exponent, and each
|
||||
// value keeps its sign, its 7 mantissa bits and a 4-bit exponent offset (0 = an exact zero, 1..15 =
|
||||
// base .. base + 14), 8 values to 3 dwords. A group holding a subnormal, an inf or NaN, or an
|
||||
// exponent range past 14 (0.02% of groups there) is stored raw in a shadow area, its base byte
|
||||
// 0xFF. A region (one source's shard in an owner's scratch) is [3 dwords a chunk][a base byte a
|
||||
// group][the shadow, 16 bytes a chunk]; A and B are two regions in a slot. A wave codes 32
|
||||
// consecutive chunks: the group's exponents meet by lane shuffles, and the packed dwords are
|
||||
// redistributed so every store a wave makes is contiguous, keeping the P2P writes coalesced.
|
||||
//
|
||||
// Decoding gives back the same bf16 values, so the reduce folds the same numbers in the same rank
|
||||
// order: the bytes are the plain kernel's. Four-card microbenchmark (the real payload above, the
|
||||
// two-shot of a 1024-row slice beside the w4a8 prefill GEMM): byte-identical; the reduction 1383
|
||||
// us at the plain lane launch (2 x 256) against 996 us coded at 4 x 1024, with the GEMM beside it
|
||||
// 746 -> 724 us. Below 1024 threads a launch codes too slowly (2 x 256: 2492 us).
|
||||
__device__ __forceinline__ unsigned r4d_arc_shfl(unsigned v, int src) {
|
||||
return (unsigned)__builtin_amdgcn_ds_bpermute(src << 2, (int)v);
|
||||
}
|
||||
struct R4dArcEnc { unsigned w[3]; unsigned base; };
|
||||
// Code this lane's chunk; every lane of the wave calls (a lane past the end with valid false).
|
||||
__device__ __forceinline__ R4dArcEnc r4d_arc_enc(int4 v, bool valid) {
|
||||
const unsigned d[4] = {(unsigned)v.x, (unsigned)v.y, (unsigned)v.z, (unsigned)v.w};
|
||||
unsigned emin = 255u, emax = 0u, bad = 0u;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
const unsigned u = (d[i >> 1] >> ((i & 1) * 16)) & 0xffffu;
|
||||
const unsigned e = (u >> 7) & 0xffu, m = u & 0x7fu;
|
||||
if (e) { emin = e < emin ? e : emin; emax = e > emax ? e : emax; }
|
||||
bad |= (unsigned)(e == 0u && m != 0u) | (unsigned)(e == 255u);
|
||||
}
|
||||
if (!valid) { emin = 255u; emax = 0u; bad = 0u; }
|
||||
const int lane = (int)(threadIdx.x & 31u);
|
||||
#pragma unroll
|
||||
for (int s = 1; s < 8; s <<= 1) {
|
||||
const unsigned a = r4d_arc_shfl(emin, lane ^ s), b = r4d_arc_shfl(emax, lane ^ s);
|
||||
const unsigned c = r4d_arc_shfl(bad, lane ^ s);
|
||||
emin = a < emin ? a : emin; emax = b > emax ? b : emax; bad |= c;
|
||||
}
|
||||
R4dArcEnc r;
|
||||
const unsigned base = emin == 255u ? 1u : emin;
|
||||
r.base = (bad || emax > base + 14u) ? 0xFFu : base;
|
||||
unsigned c[8];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
const unsigned u = (d[i >> 1] >> ((i & 1) * 16)) & 0xffffu;
|
||||
const unsigned e = (u >> 7) & 0xffu, m = u & 0x7fu, sg = u >> 15;
|
||||
c[i] = (sg << 11) | (m << 4) | (e ? (e - base + 1u) & 15u : 0u);
|
||||
}
|
||||
r.w[0] = c[0] | (c[1] << 12) | (c[2] << 24);
|
||||
r.w[1] = (c[2] >> 8) | (c[3] << 4) | (c[4] << 16) | (c[5] << 28);
|
||||
r.w[2] = (c[5] >> 4) | (c[6] << 8) | (c[7] << 20);
|
||||
return r;
|
||||
}
|
||||
__device__ __forceinline__ int4 r4d_arc_dec(const unsigned (&w)[3], unsigned base) {
|
||||
unsigned c[8];
|
||||
c[0] = w[0] & 0xfffu; c[1] = (w[0] >> 12) & 0xfffu; c[2] = (w[0] >> 24) | ((w[1] & 0xfu) << 8);
|
||||
c[3] = (w[1] >> 4) & 0xfffu; c[4] = (w[1] >> 16) & 0xfffu; c[5] = (w[1] >> 28) | ((w[2] & 0xffu) << 4);
|
||||
c[6] = (w[2] >> 8) & 0xfffu; c[7] = w[2] >> 20;
|
||||
unsigned u[8];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
const unsigned off = c[i] & 15u;
|
||||
u[i] = ((c[i] >> 11) << 15) | ((off ? base + off - 1u : 0u) << 7) | ((c[i] >> 4) & 0x7fu);
|
||||
}
|
||||
int4 v;
|
||||
v.x = (int)(u[0] | (u[1] << 16)); v.y = (int)(u[2] | (u[3] << 16));
|
||||
v.z = (int)(u[4] | (u[5] << 16)); v.w = (int)(u[6] | (u[7] << 16));
|
||||
return v;
|
||||
}
|
||||
struct R4dArcRegion { unsigned* packed; unsigned* bases; int4* shadow; };
|
||||
__host__ __device__ constexpr size_t r4d_arc_shadow_off(long SL) {
|
||||
return ((size_t)12 * SL + (size_t)SL / 8 + 15) & ~(size_t)15;
|
||||
}
|
||||
__host__ __device__ constexpr size_t r4d_arc_region_bytes(long SL) {
|
||||
return (r4d_arc_shadow_off(SL) + (size_t)16 * SL + 255) & ~(size_t)255;
|
||||
}
|
||||
__device__ __forceinline__ R4dArcRegion r4d_arc_region(const void* p, int SL) {
|
||||
char* b = (char*)p;
|
||||
return R4dArcRegion{(unsigned*)b, (unsigned*)(b + (size_t)12 * SL), (int4*)(b + r4d_arc_shadow_off(SL))};
|
||||
}
|
||||
// A wave's coded chunks [k0, k0 + 32) (k0 a multiple of 32) into ND regions, every store contiguous.
|
||||
template <int ND>
|
||||
__device__ __forceinline__ void r4d_arc_put(R4dArcRegion (&dst)[ND], int k0, const R4dArcEnc& e,
|
||||
int4 raw, bool valid) {
|
||||
const int lane = (int)(threadIdx.x & 31u);
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
const int p = 32 * j + lane, sl = p / 3, wi = p - sl * 3;
|
||||
const unsigned a0 = r4d_arc_shfl(e.w[0], sl), a1 = r4d_arc_shfl(e.w[1], sl);
|
||||
const unsigned a2 = r4d_arc_shfl(e.w[2], sl), vs = r4d_arc_shfl(valid ? 1u : 0u, sl);
|
||||
const unsigned val = wi == 0 ? a0 : wi == 1 ? a1 : a2;
|
||||
if (vs) {
|
||||
#pragma unroll
|
||||
for (int q = 0; q < ND; ++q) dst[q].packed[(size_t)k0 * 3 + p] = val;
|
||||
}
|
||||
}
|
||||
const unsigned b0 = r4d_arc_shfl(e.base, 0), b1 = r4d_arc_shfl(e.base, 8);
|
||||
const unsigned b2 = r4d_arc_shfl(e.base, 16), b3 = r4d_arc_shfl(e.base, 24);
|
||||
if (lane == 0 && valid) {
|
||||
#pragma unroll
|
||||
for (int q = 0; q < ND; ++q) dst[q].bases[k0 / 32] = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
|
||||
}
|
||||
if (valid && e.base == 0xFFu) {
|
||||
#pragma unroll
|
||||
for (int q = 0; q < ND; ++q) dst[q].shadow[k0 + lane] = raw;
|
||||
}
|
||||
}
|
||||
// A wave's chunks [k0, k0 + 32) back from a region; every lane calls.
|
||||
__device__ __forceinline__ int4 r4d_arc_get(const R4dArcRegion& src, int k0, bool valid) {
|
||||
const int lane = (int)(threadIdx.x & 31u);
|
||||
unsigned ld[3];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 3; ++j) ld[j] = valid ? src.packed[(size_t)k0 * 3 + 32 * j + lane] : 0u;
|
||||
unsigned w[3];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
const int q = 3 * lane + i, sl = q & 31, j = q >> 5;
|
||||
const unsigned a0 = r4d_arc_shfl(ld[0], sl), a1 = r4d_arc_shfl(ld[1], sl);
|
||||
const unsigned a2 = r4d_arc_shfl(ld[2], sl);
|
||||
w[i] = j == 0 ? a0 : j == 1 ? a1 : a2;
|
||||
}
|
||||
const unsigned base = ((valid ? src.bases[k0 / 32] : 0u) >> ((lane >> 3) * 8)) & 0xffu;
|
||||
if (base == 0xFFu) return valid ? src.shadow[k0 + lane] : make_int4(0, 0, 0, 0);
|
||||
return r4d_arc_dec(w, base);
|
||||
}
|
||||
|
||||
// The coded two-shot, bf16 only; the launcher checks that each shard is a whole number of waves'
|
||||
// 32 chunks and that a slot holds two regions.
|
||||
template <int WS, bool CHAIN>
|
||||
__device__ __forceinline__ void r4d_ar_twoshot_coded_body(
|
||||
const int4* __restrict__ in4, R4DArWidePeers<int4, WS - 1> peers,
|
||||
const int4* __restrict__ my_base, unsigned int* __restrict__ my_flags,
|
||||
int4* __restrict__ out4, unsigned int* __restrict__ seq_ctrs, int n16,
|
||||
int slot_stride16, int rank, int drain, int acq, int pub,
|
||||
long long* gate, unsigned int* gate_ctr) {
|
||||
if (CHAIN) r4d_chain_wait();
|
||||
constexpr int NPEERS = WS - 1;
|
||||
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
|
||||
__shared__ unsigned int s_seq, s_slot;
|
||||
const long long gate_word = r4d_ar_link_word(gate, (unsigned int)tid);
|
||||
r4d_ar_seq_take(seq_ctrs, R4D_AR_WIDE_MAX_BLOCKS, s_seq, s_slot);
|
||||
const unsigned int s = s_seq;
|
||||
const int parity = (int)s_slot;
|
||||
const int SL = n16 / WS; /* every shard: a multiple of 32 chunks */
|
||||
int w = (SL + nb - 1) / nb;
|
||||
w = (w + 31) & ~31;
|
||||
const int w0 = b * w;
|
||||
int w1 = w0 + w; if (w1 > SL) w1 = SL;
|
||||
const size_t RB = r4d_arc_region_bytes(SL);
|
||||
const int lane = tid & 31;
|
||||
|
||||
// phase 1: my contribution to each peer-owned shard, coded, into the owner's region A for me
|
||||
for (int p = 0; p < NPEERS; ++p) {
|
||||
const int owner = peers.rank[p];
|
||||
const int4* src = in4 + (size_t)owner * SL;
|
||||
R4dArcRegion dst[1] = {r4d_arc_region(peers.scratch[p] + ((size_t)parity * WS + rank) * (size_t)slot_stride16, SL)};
|
||||
for (int k = w0 + tid; k - lane < w1; k += nt) {
|
||||
const bool valid = k < w1;
|
||||
const int4 v = valid ? src[k] : make_int4(0, 0, 0, 0);
|
||||
r4d_arc_put<1>(dst, k - lane, r4d_arc_enc(v, valid), v, valid);
|
||||
}
|
||||
}
|
||||
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s - 1u, drain, acq, pub);
|
||||
|
||||
// reduce my shard (fp32, ascending rank, my own contribution raw from the input), code it once,
|
||||
// and push it to every peer's region B
|
||||
R4dArcRegion srcA[WS], dstB[NPEERS];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r)
|
||||
srcA[r] = r4d_arc_region(my_base + ((size_t)parity * WS + r) * (size_t)slot_stride16, SL);
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p)
|
||||
dstB[p] = r4d_arc_region((const char*)(peers.scratch[p] + ((size_t)parity * WS + rank) * (size_t)slot_stride16) + RB, SL);
|
||||
const int4* own = in4 + (size_t)rank * SL;
|
||||
int4* my_out = out4 + (size_t)rank * SL;
|
||||
for (int k = w0 + tid; k - lane < w1; k += nt) {
|
||||
const bool valid = k < w1;
|
||||
const int k0 = k - lane;
|
||||
float acc[8];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j) acc[j] = 0.0f;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r) {
|
||||
const int4 v = (r == rank) ? (valid ? own[k] : make_int4(0, 0, 0, 0)) : r4d_arc_get(srcA[r], k0, valid);
|
||||
const __hip_bfloat16* pv = reinterpret_cast<const __hip_bfloat16*>(&v);
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j) acc[j] += (float)pv[j];
|
||||
}
|
||||
int4 o;
|
||||
__hip_bfloat16* po = reinterpret_cast<__hip_bfloat16*>(&o);
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j) po[j] = (__hip_bfloat16)acc[j];
|
||||
if (valid) my_out[k] = o;
|
||||
r4d_arc_put<NPEERS>(dstB, k0, r4d_arc_enc(o, valid), o, valid);
|
||||
}
|
||||
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s, drain, acq, pub);
|
||||
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
|
||||
|
||||
// assemble: every peer-owned shard from my region B
|
||||
for (int p = 0; p < NPEERS; ++p) {
|
||||
const int owner = peers.rank[p];
|
||||
const R4dArcRegion src = r4d_arc_region((const char*)(my_base + ((size_t)parity * WS + owner) * (size_t)slot_stride16) + RB, SL);
|
||||
int4* dst = out4 + (size_t)owner * SL;
|
||||
for (int k = w0 + tid; k - lane < w1; k += nt) {
|
||||
const bool valid = k < w1;
|
||||
const int4 v = r4d_arc_get(src, k - lane, valid);
|
||||
if (valid) dst[k] = v;
|
||||
}
|
||||
}
|
||||
if (CHAIN) r4d_chain_done();
|
||||
}
|
||||
|
||||
#define R4D_ARTS_PARAMS const int4* __restrict__ in4, R4DArWidePeers<int4, WS - 1> peers, \
|
||||
const int4* __restrict__ my_base, unsigned int* __restrict__ my_flags, \
|
||||
int4* __restrict__ out4, unsigned int* __restrict__ seq_ctrs, R4DArShardOff<WS> sh, \
|
||||
@@ -175,6 +391,24 @@ __global__ void r4dchain_ar_twoshot_wide_exact(R4D_ARTS_PARAMS) {
|
||||
}
|
||||
#undef R4D_ARTS_ARGS
|
||||
#undef R4D_ARTS_PARAMS
|
||||
// The coded form keeps the even split (its launcher checks every shard is whole waves' chunks), so
|
||||
// it takes the message length, not the plain form's shard table and pieces.
|
||||
#define R4D_ARTC_PARAMS const int4* __restrict__ in4, R4DArWidePeers<int4, WS - 1> peers, \
|
||||
const int4* __restrict__ my_base, unsigned int* __restrict__ my_flags, \
|
||||
int4* __restrict__ out4, unsigned int* __restrict__ seq_ctrs, int n16, int slot_stride16, \
|
||||
int rank, int drain, int acq, int pub, long long* gate, unsigned int* gate_ctr
|
||||
#define R4D_ARTC_ARGS in4, peers, my_base, my_flags, out4, seq_ctrs, n16, slot_stride16, rank, \
|
||||
drain, acq, pub, gate, gate_ctr
|
||||
template <int WS>
|
||||
__global__ void r4d_ar_twoshot_coded_kernel(R4D_ARTC_PARAMS) {
|
||||
r4d_ar_twoshot_coded_body<WS, false>(R4D_ARTC_ARGS);
|
||||
}
|
||||
template <int WS>
|
||||
__global__ void r4dchain_ar_twoshot_coded(R4D_ARTC_PARAMS) {
|
||||
r4d_ar_twoshot_coded_body<WS, true>(R4D_ARTC_ARGS);
|
||||
}
|
||||
#undef R4D_ARTC_ARGS
|
||||
#undef R4D_ARTC_PARAMS
|
||||
|
||||
// --- host side ---
|
||||
template <int WS>
|
||||
@@ -306,6 +540,50 @@ void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long pee
|
||||
nullptr, 1L, chain != 0);
|
||||
}
|
||||
|
||||
/* The coded form's conditions: bf16, a message that shards evenly into whole waves' 32 chunks
|
||||
* (256 values) a rank, and a slot that holds two coded regions. */
|
||||
int r4d_ar_twoshot_coded_ok(long n_elem, long dtype, long world, long slot_stride16) {
|
||||
if (dtype != 0 || world <= 0 || n_elem % (world * 256) != 0) return 0;
|
||||
const long SL = n_elem / 8 / world;
|
||||
return 2 * r4d_arc_region_bytes(SL) <= (size_t)slot_stride16 * 16 ? 1 : 0;
|
||||
}
|
||||
|
||||
void r4d_ar_twoshot_4rank_exact_coded(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 rank, long stream, long nblocks, long nthreads,
|
||||
long drain, long acq, long pub, long gate, long gate_ctr,
|
||||
long chain) {
|
||||
constexpr int WS = 4;
|
||||
if (!r4d_ar_twoshot_coded_ok(n_elem, 0, WS, slot_stride16))
|
||||
throw std::runtime_error("all_reduce (coded): message or slot outside the coded form");
|
||||
if (rank < 0 || rank >= WS) throw std::runtime_error("all_reduce (coded): rank out of range");
|
||||
const long n16 = n_elem / 8, SL = n16 / WS;
|
||||
int nb = (int)nblocks;
|
||||
if (nb < 1) nb = 4;
|
||||
if (nb > R4D_AR_WIDE_MAX_BLOCKS) nb = R4D_AR_WIDE_MAX_BLOCKS;
|
||||
if (nb > SL / 32) nb = (int)(SL / 32);
|
||||
int nt = (int)nthreads;
|
||||
if (nt < 256) nt = 256;
|
||||
if (nt > 1024) nt = 1024;
|
||||
nt &= ~31;
|
||||
R4DArWidePeers<int4, WS - 1> peers{};
|
||||
const long ps[3] = {peer_scratch0, peer_scratch1, peer_scratch2};
|
||||
const long pf[3] = {peer_flags0, peer_flags1, peer_flags2};
|
||||
for (int i = 0; i < WS - 1; ++i) {
|
||||
if (!ps[i] || !pf[i]) throw std::runtime_error("all_reduce (coded): null peer pointer");
|
||||
peers.scratch[i] = (int4*)ps[i];
|
||||
peers.flags[i] = (unsigned int*)pf[i];
|
||||
peers.rank[i] = (i < (int)rank) ? i : i + 1;
|
||||
}
|
||||
hipStream_t st = (hipStream_t)stream;
|
||||
(chain ? r4dchain_ar_twoshot_coded<WS> : r4d_ar_twoshot_coded_kernel<WS>)<<<nb, nt, 0, st>>>(
|
||||
(const int4*)inp, peers, (const int4*)my_scratch, (unsigned int*)my_flags, (int4*)out,
|
||||
(unsigned int*)seq_ctrs, (int)n16, (int)slot_stride16, (int)rank, (int)drain, (int)acq,
|
||||
(int)pub, (long long*)gate, (unsigned int*)gate_ctr);
|
||||
}
|
||||
|
||||
void r4d_ar_twoshot_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,
|
||||
|
||||
@@ -347,6 +347,41 @@ int r4d_gemm_w4a8_nt_m64_group(void) { return R4D_GEMM_W4A8_GROUP; }
|
||||
// The operand reader and the split law are in r4d_gemm_args.h; see libr4d/r4d_args.h.
|
||||
/* ------------------------------------------------------------------ int8-activation grids */
|
||||
|
||||
/* The multi-user verify heights (33 to 64 rows: five to eight users of a DFlash2 verify, 8 rows a
|
||||
* user). The law's point there covers every row tile in one block at one N tile a wave (mb = all,
|
||||
* npw = 1), which re-reads the activation once a 16-column tile and holds MT accumulators a wave;
|
||||
* on 4x R9700 (one card, the 31B's per-card trunk shapes, every point byte-identical to the law's)
|
||||
* it ran a layer's four GEMMs in 259 us at 64 rows against 157 us here:
|
||||
*
|
||||
* q|k|v 3584x5376 o_proj 5376x2048 gate_up 10752x5376 down 5376x5376
|
||||
* 40 rows law 46.4 27.4 67.1 36.4
|
||||
* here 22.0 (mb1 npw2) 21.1 (mb1 npw2) 58.8 (mb1 npw4) 30.8 (mb1 npw2)
|
||||
* 48 rows law 31.4 30.4 71.9 42.4
|
||||
* here 22.2 21.2 58.9 31.6
|
||||
* 56 rows law 56.8 35.0 94.2 62.4
|
||||
* here 24.0 (mb1 npw2) 22.3 (mb2 npw1) 69.0 (mb2 npw4) 43.0 (mb1 npw2)
|
||||
* 64 rows law 58.6 37.3 97.6 65.2
|
||||
* here 24.7 22.7 69.6 44.0
|
||||
*
|
||||
* Neither axis moves a byte: the split stays the law's (a point whose wider block cannot hold it
|
||||
* is not taken, since a smaller split would regroup the K partials), and every output element
|
||||
* keeps its accumulation order. Only where neither `mb` nor `npw` was tuned. */
|
||||
static void multi_user_point(const RadArgs* a, long long M, long long N, long long K, Cfg* c) {
|
||||
if (M <= 32 || M > 64) return;
|
||||
R4dGemmCfg cv;
|
||||
r4d_cfg_gemm(a, &cv);
|
||||
if (cv.mb > 0 || cv.npw > 1) return;
|
||||
int mb, npw;
|
||||
if (M <= 48) { mb = 1; npw = N >= 8192 ? 4 : 2; }
|
||||
else if (N >= 8192) { mb = 2; npw = 4; }
|
||||
else if (K <= 2048) { mb = 2; npw = 1; }
|
||||
else { mb = 1; npw = 2; }
|
||||
if (c->wv * c->sk * WAVE32 > block_bound(BR_NPW, mb, npw)) return;
|
||||
if ((size_t)c->wv * npw * c->sk * 256 * sizeof(float) > 64 * 1024) return;
|
||||
c->mb = mb;
|
||||
c->npw = npw;
|
||||
}
|
||||
|
||||
/* The asymmetric grid is selected by `dtype`, not by the presence of a_sum (see is_asym). */
|
||||
|
||||
extern "C" int r4d_rad_gemm_w4a8_m64(const RadArgs* a, RadStream s) {
|
||||
@@ -360,6 +395,7 @@ extern "C" int r4d_rad_gemm_w4a8_m64(const RadArgs* a, RadStream s) {
|
||||
Cfg c;
|
||||
rc = cfg_for(a, g.K, g.M, group, BR_NPW, &c);
|
||||
if (rc != RAD_OK) return rc;
|
||||
multi_user_point(a, g.M, g.N, g.K, &c);
|
||||
|
||||
/* the engine's chained launch (RadKernelChainFn), at decode heights only */
|
||||
const bool chain = r4d_geti_or(a, "chain", 0) != 0 && g.M <= 64;
|
||||
|
||||
@@ -614,6 +614,212 @@ __global__ __launch_bounds__(1024) void r4dchain_ar_rmsnorm_quant_i8_tail(R4D_AR
|
||||
#undef R4D_ARQ8T_ARGS
|
||||
#undef R4D_ARQ8T_PARAMS
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// The junction with the all-reduce split around it (`sp`): reduce-scatter, the junction on the rows
|
||||
// this rank owns, all-gather of what the next GEMM reads. Rank r owns rows [r0(r), r0(r) + nr(r)),
|
||||
// M split as evenly as the rank order allows, and block b serves local row b of every rank:
|
||||
//
|
||||
// 1. it pushes this rank's partial sums of each peer's local row b into that peer's slot (bf16,
|
||||
// the two-shot's first hop) and takes the handshake;
|
||||
// 2. on its own row it folds the four partials in rank order -- r4d_ar_to_f, f32, rank 0 first,
|
||||
// r4d_ar_from_f, the standalone exact all-reduce's fold, so the same bf16 sum -- and runs the
|
||||
// junction on it in the register form at the standalone norm's width (r4d_act_threads), each
|
||||
// thread holding the columns the standalone's does, so every sum is the standalone's; it
|
||||
// stores the residual row and pushes the row's int8 codes and scale (and with `gres` the
|
||||
// residual row) into every peer's slot past the first hop's region, then takes the handshake;
|
||||
// 3. it copies the peers' local row b of codes and scales (and residual) into place.
|
||||
//
|
||||
// So every rank leaves q and S on every row, byte for byte the chain's (all_reduce, then
|
||||
// rmsnorm_quant_i8), and the residual on its own rows only -- on every row with `gres`. `x` keeps
|
||||
// this rank's partial sums: nothing past the junction reads the reduced branch output. Against the
|
||||
// tail form the second hop carries int8 instead of bf16 and no block waits beside the transport;
|
||||
// one launch at a time on 4x R9700 (2026-10-19) it took 22.9 against 24.5 us at 8 rows of 5376,
|
||||
// 30.8 against 34.9 at 16, 72.3 against 89.2 at 64, and 19.8 against the one-shot's 21.9 at 4.
|
||||
template <uint32_t EPT, uint32_t THR, int WS>
|
||||
__device__ __forceinline__ void ar_rmsnorm_quant_i8_sp_body(
|
||||
const uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w,
|
||||
const void* __restrict__ wp, const void* __restrict__ sc, uint32_t sc_f32, uint32_t wf32,
|
||||
uint32_t gres, signed char* __restrict__ q, float* __restrict__ S,
|
||||
R4DArWidePeers<int4, WS - 1> peers, const int4* __restrict__ my_base,
|
||||
unsigned int* __restrict__ my_flags, unsigned int* __restrict__ seq_ctrs, int M, uint32_t n,
|
||||
float eps, float wadd, uint32_t r_ld, int slot_stride16, int rank, int drain, int acq, int pub,
|
||||
long long* gate, unsigned int* gate_ctr) {
|
||||
constexpr int NPEERS = WS - 1;
|
||||
extern __shared__ uint16_t ybuf[];
|
||||
signed char* const cbuf = (signed char*)(ybuf + n);
|
||||
__shared__ float l1[THR / kWave], l2[THR / kWave], lm[THR / kWave];
|
||||
__shared__ unsigned int s_seq, s_slot;
|
||||
const int b = (int)blockIdx.x;
|
||||
const uint32_t tid = threadIdx.x;
|
||||
const long long gate_word = r4d_ar_link_word(gate, tid);
|
||||
r4d_ar_seq_take(seq_ctrs, R4D_AR_WIDE_MAX_BLOCKS, s_seq, s_slot);
|
||||
const unsigned int s = s_seq;
|
||||
const int parity = (int)s_slot;
|
||||
const int base = M / WS, rem = M % WS;
|
||||
#define R4D_SP_R0(r) ((r) * base + ((r) < rem ? (r) : rem))
|
||||
#define R4D_SP_NR(r) (base + ((r) < rem ? 1 : 0))
|
||||
const int n16 = (int)(n >> 3), c16 = (int)(n >> 4);
|
||||
const size_t offB = (size_t)(base + (rem ? 1 : 0)) * n16; /* past the first hop's rows */
|
||||
const size_t rowB = (size_t)c16 + 1 + (gres ? n16 : 0); /* codes, a scale word, residual */
|
||||
const size_t my_slot = ((size_t)parity * WS + rank) * (size_t)slot_stride16;
|
||||
|
||||
// ---- the first hop: this rank's partials of each peer's local row b
|
||||
for (int p = 0; p < NPEERS; ++p) {
|
||||
const int d = peers.rank[p];
|
||||
if (b >= R4D_SP_NR(d)) continue;
|
||||
const int4* src = (const int4*)(x + (size_t)(R4D_SP_R0(d) + b) * n);
|
||||
int4* dst = peers.scratch[p] + my_slot + (size_t)b * n16;
|
||||
for (int k = (int)tid; k < n16; k += (int)THR) dst[k] = src[k];
|
||||
}
|
||||
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s - 1u, drain, acq, pub);
|
||||
|
||||
if (b < R4D_SP_NR(rank)) {
|
||||
const int row = R4D_SP_R0(rank) + b;
|
||||
const __hip_bfloat16* srcs[WS];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r)
|
||||
srcs[r] = r == rank ? (const __hip_bfloat16*)(x + (size_t)row * n)
|
||||
: (const __hip_bfloat16*)(my_base + ((size_t)parity * WS + r) *
|
||||
(size_t)slot_stride16 + (size_t)b * n16);
|
||||
uint16_t* const rr = res + (size_t)row * r_ld;
|
||||
uint16_t xv[EPT], rv[EPT];
|
||||
float gp[EPT], gw[EPT];
|
||||
#pragma unroll
|
||||
for (uint32_t e = 0; e < EPT; ++e) {
|
||||
const uint32_t i = tid + e * THR;
|
||||
xv[e] = 0; rv[e] = 0; gp[e] = 0.0f; gw[e] = 0.0f;
|
||||
if (i < n) {
|
||||
float acc = 0.0f;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r) acc += r4d_ar_to_f(srcs[r][i]);
|
||||
__hip_bfloat16 o;
|
||||
r4d_ar_from_f(acc, o);
|
||||
__builtin_memcpy(&xv[e], &o, 2);
|
||||
rv[e] = rr[i];
|
||||
gp[e] = gain(wp, i, wf32) + wadd;
|
||||
gw[e] = gain(w, i, wf32) + wadd;
|
||||
}
|
||||
}
|
||||
const float sv = sc ? scalar_of(sc, sc_f32) : 1.0f;
|
||||
|
||||
// ---- junction_reg_row's passes over the folded row: the post-norm's sum of squares,
|
||||
// the post-norm, add and scalar with the norm's sum of squares, the norm and its absmax
|
||||
float ss = 0.0f;
|
||||
#pragma unroll
|
||||
for (uint32_t e = 0; e < EPT; ++e)
|
||||
if (tid + e * THR < n) { const float v = bf2f(xv[e]); ss += v * v; }
|
||||
const float s1 = rsqrtf(block_sum<THR / kWave>(ss, l1) / (float)n + eps);
|
||||
float ss2 = 0.0f;
|
||||
#pragma unroll
|
||||
for (uint32_t e = 0; e < EPT; ++e) {
|
||||
const uint32_t i = tid + e * THR;
|
||||
if (i < n) {
|
||||
uint16_t v = f32_to_bf16(bf2f(xv[e]) * s1 * gp[e]);
|
||||
v = f32_to_bf16(bf2f(rv[e]) + bf2f(v));
|
||||
if (sc) v = f32_to_bf16(bf2f(v) * sv);
|
||||
rr[i] = v;
|
||||
xv[e] = v;
|
||||
const float f = bf2f(v);
|
||||
ss2 += f * f;
|
||||
}
|
||||
}
|
||||
const float s2 = rsqrtf(block_sum<THR / kWave>(ss2, l2) / (float)n + eps);
|
||||
float m = 0.0f;
|
||||
#pragma unroll
|
||||
for (uint32_t e = 0; e < EPT; ++e) {
|
||||
const uint32_t i = tid + e * THR;
|
||||
if (i < n) {
|
||||
const uint16_t y = f32_to_bf16(bf2f(xv[e]) * s2 * gw[e]);
|
||||
ybuf[i] = y;
|
||||
m = fmaxf(m, fabsf(bf2f(y)));
|
||||
}
|
||||
}
|
||||
m = r4d_wave_max(m);
|
||||
if ((tid & 31) == 0) lm[tid >> 5] = m;
|
||||
__syncthreads(); // also publishes ybuf to every thread
|
||||
m = lm[0];
|
||||
#pragma unroll
|
||||
for (uint32_t i = 1; i < THR / kWave; i++) m = fmaxf(m, lm[i]);
|
||||
const float scale = fmaxf(m, 1e-8f) / 127.f;
|
||||
const float inv = 1.f / scale;
|
||||
// ---- the codes, in junction_row's fragment order, into LDS
|
||||
for (uint32_t i = tid; i < n; i += THR) {
|
||||
const uint32_t sg = i >> 4, j = i & 15;
|
||||
const uint32_t src = (sg << 4) + 8 * ((j & 7) >> 2) + 4 * (j >> 3) + (j & 3);
|
||||
const int t = __float2int_rn(bf2f(ybuf[src]) * inv);
|
||||
cbuf[i] = (signed char)(t < -128 ? -128 : (t > 127 ? 127 : t));
|
||||
}
|
||||
__syncthreads(); // the codes, and this block's residual row in memory, whole
|
||||
// ---- the second hop: the row's codes and scale (and residual) to every peer, and here
|
||||
const int4* const c4 = (const int4*)cbuf;
|
||||
int4* const q4 = (int4*)(q + (size_t)row * n);
|
||||
int4* dB[NPEERS];
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) dB[p] = peers.scratch[p] + my_slot + offB + (size_t)b * rowB;
|
||||
for (int k = (int)tid; k < c16; k += (int)THR) {
|
||||
const int4 v = c4[k];
|
||||
q4[k] = v;
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) dB[p][k] = v;
|
||||
}
|
||||
if (gres) {
|
||||
const int4* const r4 = (const int4*)rr;
|
||||
for (int k = (int)tid; k < n16; k += (int)THR) {
|
||||
const int4 v = r4[k];
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) dB[p][c16 + 1 + k] = v;
|
||||
}
|
||||
}
|
||||
if (tid == 0) {
|
||||
S[row] = scale;
|
||||
const int4 sw = make_int4(__float_as_int(scale), 0, 0, 0);
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) dB[p][c16] = sw;
|
||||
}
|
||||
}
|
||||
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s, drain, acq, pub);
|
||||
r4d_ar_link_landed(gate, gate_word, gate_ctr, gridDim.x, tid);
|
||||
// ---- the peers' local row b, codes and scale (and residual), into place
|
||||
for (int p = 0; p < NPEERS; ++p) {
|
||||
const int sr = peers.rank[p];
|
||||
if (b >= R4D_SP_NR(sr)) continue;
|
||||
const int4* const src = my_base + ((size_t)parity * WS + sr) * (size_t)slot_stride16 + offB +
|
||||
(size_t)b * rowB;
|
||||
const int row = R4D_SP_R0(sr) + b;
|
||||
int4* const q4 = (int4*)(q + (size_t)row * n);
|
||||
for (int k = (int)tid; k < c16; k += (int)THR) q4[k] = src[k];
|
||||
if (gres) {
|
||||
int4* const r4 = (int4*)(res + (size_t)row * r_ld);
|
||||
for (int k = (int)tid; k < n16; k += (int)THR) r4[k] = src[c16 + 1 + k];
|
||||
}
|
||||
if (tid == 0) S[row] = __int_as_float(src[c16].x);
|
||||
}
|
||||
#undef R4D_SP_R0
|
||||
#undef R4D_SP_NR
|
||||
}
|
||||
|
||||
#define R4D_ARQ8S_PARAMS const uint16_t* __restrict__ x, uint16_t* __restrict__ res, \
|
||||
const void* __restrict__ w, const void* __restrict__ wp, const void* __restrict__ sc, \
|
||||
uint32_t sc_f32, uint32_t wf32, uint32_t gres, signed char* __restrict__ q, \
|
||||
float* __restrict__ S, R4DArWidePeers<int4, WS - 1> peers, const int4* __restrict__ my_base, \
|
||||
unsigned int* __restrict__ my_flags, unsigned int* __restrict__ seq_ctrs, int M, uint32_t n, \
|
||||
float eps, float wadd, uint32_t r_ld, int slot_stride16, int rank, int drain, int acq, int pub, \
|
||||
long long* gate, unsigned int* gate_ctr
|
||||
#define R4D_ARQ8S_ARGS x, res, w, wp, sc, sc_f32, wf32, gres, q, S, peers, my_base, my_flags, \
|
||||
seq_ctrs, M, n, eps, wadd, r_ld, slot_stride16, rank, drain, acq, pub, gate, gate_ctr
|
||||
template <uint32_t EPT, uint32_t THR, int WS>
|
||||
__global__ __launch_bounds__(THR) void ar_rmsnorm_quant_i8_sp_kernel(R4D_ARQ8S_PARAMS) {
|
||||
ar_rmsnorm_quant_i8_sp_body<EPT, THR, WS>(R4D_ARQ8S_ARGS);
|
||||
}
|
||||
template <uint32_t EPT, uint32_t THR, int WS>
|
||||
__global__ __launch_bounds__(THR) void r4dchain_ar_rmsnorm_quant_i8_sp(R4D_ARQ8S_PARAMS) {
|
||||
r4d_chain_wait();
|
||||
ar_rmsnorm_quant_i8_sp_body<EPT, THR, WS>(R4D_ARQ8S_ARGS);
|
||||
r4d_chain_done();
|
||||
}
|
||||
#undef R4D_ARQ8S_ARGS
|
||||
#undef R4D_ARQ8S_PARAMS
|
||||
|
||||
} // namespace
|
||||
|
||||
// ============================================================== the plugin ABI entry point
|
||||
@@ -667,24 +873,33 @@ extern "C" int r4d_rad_rmsnorm_quant_i8(const RadArgs* a, RadStream stream) {
|
||||
const float wadd = (float)r4d_getf(a, "wadd", 0.0);
|
||||
const uint32_t scf = sc ? (sc->dtype == RAD_F32) : 0u;
|
||||
|
||||
// The decode form keeps a thread's columns in registers: at 1024 threads, EPT of them.
|
||||
const int ept = (thr != 1024 || !r4d_cfg_rmsq8_reg(a)) ? 0
|
||||
: n <= 4096 ? 4 : n <= 6144 ? 6 : n <= 8192 ? 8 : 0;
|
||||
// The decode form keeps a thread's columns in registers, EPT of them: at 1024 threads (to 32
|
||||
// rows) and at 512 (33 to 72-120 rows, r4d_act_threads), the same partition and arithmetic as
|
||||
// the re-read form at that width, so the same bytes. At 512 it serves the multi-user verify
|
||||
// (five to fifteen users of a DFlash2 verify, 8 rows a user), where the re-read form took ~14 us
|
||||
// a junction under --profile-ops at 64 rows of 5376.
|
||||
const int ept = !r4d_cfg_rmsq8_reg(a) ? 0
|
||||
: thr == 1024 ? (n <= 4096 ? 4 : n <= 6144 ? 6 : n <= 8192 ? 8 : 0)
|
||||
: thr == 512 ? (n <= 4096 ? 8 : n <= 6144 ? 12 : n <= 8192 ? 16 : 0)
|
||||
: 0;
|
||||
|
||||
#define R4D_RQI8_T(WF, RS, PO, SC, T) emit_rc = R4D_EMIT(stream, \
|
||||
(rmsnorm_quant_i8_kernel<WF, RS, PO, SC, T>), dim3((uint32_t)M), dim3(T), lds, \
|
||||
(const uint16_t*)r4d_p(x), (uint16_t*)r4d_wp(rs), r4d_p(w), r4d_p(wp), r4d_p(sc), scf, \
|
||||
(signed char*)r4d_wp(q), (float*)r4d_wp(s), (uint16_t*)r4d_wp(y), (uint32_t)n, eps, wadd, \
|
||||
(uint32_t)xp, (uint32_t)rp, (uint32_t)yp)
|
||||
#define R4D_RQI8_R(WF, RS, PO, SC, E) emit_rc = R4D_EMIT(stream, \
|
||||
(rmsnorm_quant_i8_reg_kernel<WF, RS, PO, SC, 1024, E>), dim3((uint32_t)M), dim3(1024), lds, \
|
||||
#define R4D_RQI8_R(WF, RS, PO, SC, T, E) emit_rc = R4D_EMIT(stream, \
|
||||
(rmsnorm_quant_i8_reg_kernel<WF, RS, PO, SC, T, E>), dim3((uint32_t)M), dim3(T), lds, \
|
||||
(const uint16_t*)r4d_p(x), (uint16_t*)r4d_wp(rs), r4d_p(w), r4d_p(wp), r4d_p(sc), scf, \
|
||||
(signed char*)r4d_wp(q), (float*)r4d_wp(s), (uint16_t*)r4d_wp(y), (uint32_t)n, eps, wadd, \
|
||||
(uint32_t)xp, (uint32_t)rp, (uint32_t)yp)
|
||||
#define R4D_RQI8(WF, RS, PO, SC) \
|
||||
do { if (ept == 4) R4D_RQI8_R(WF, RS, PO, SC, 4); \
|
||||
else if (ept == 6) R4D_RQI8_R(WF, RS, PO, SC, 6); \
|
||||
else if (ept == 8) R4D_RQI8_R(WF, RS, PO, SC, 8); \
|
||||
do { if (thr == 1024 && ept == 4) R4D_RQI8_R(WF, RS, PO, SC, 1024, 4); \
|
||||
else if (thr == 1024 && ept == 6) R4D_RQI8_R(WF, RS, PO, SC, 1024, 6); \
|
||||
else if (thr == 1024 && ept == 8) R4D_RQI8_R(WF, RS, PO, SC, 1024, 8); \
|
||||
else if (thr == 512 && ept == 8) R4D_RQI8_R(WF, RS, PO, SC, 512, 8); \
|
||||
else if (thr == 512 && ept == 12) R4D_RQI8_R(WF, RS, PO, SC, 512, 12); \
|
||||
else if (thr == 512 && ept == 16) R4D_RQI8_R(WF, RS, PO, SC, 512, 16); \
|
||||
else if (thr == 1024) R4D_RQI8_T(WF, RS, PO, SC, 1024); \
|
||||
else if (thr == 512) R4D_RQI8_T(WF, RS, PO, SC, 512); \
|
||||
else if (thr == 256) R4D_RQI8_T(WF, RS, PO, SC, 256); \
|
||||
@@ -771,7 +986,7 @@ extern "C" int r4d_ar_rmsnorm_quant_i8_4rank(long peer_scratch0, long peer_scrat
|
||||
float eps, float wadd, int w_f32, int r_ld, int y_ld,
|
||||
int rank, long stream, int nblocks, int drain, int acq,
|
||||
int pub, long gate, long gate_ctr,
|
||||
long tail_ctr, int tail_nbt, int chain) {
|
||||
long tail_ctr, int tail_nbt, int chain, int sp) {
|
||||
constexpr int WS = 4, NPEERS = WS - 1;
|
||||
const long ps[NPEERS] = { peer_scratch0, peer_scratch1, peer_scratch2 };
|
||||
const long pf[NPEERS] = { peer_flags0, peer_flags1, peer_flags2 };
|
||||
@@ -815,6 +1030,50 @@ extern "C" int r4d_ar_rmsnorm_quant_i8_4rank(long peer_scratch0, long peer_scrat
|
||||
const int thr = r4d_act_threads(M, n);
|
||||
const size_t lds = (size_t)n * sizeof(uint16_t);
|
||||
|
||||
/* The all-reduce split around the junction (ar_rmsnorm_quant_i8_sp_kernel): `sp` 1, or 2 with
|
||||
* the residual gathered too. A block a local row, ceil(M / 4) of them; its two hops in the slot
|
||||
* the message would fill. Where it cannot run (no residual or post-norm, a width without a
|
||||
* register form, rows past the flag array or the slot, a residual pitch off 16 bytes) the
|
||||
* forms below run instead, which leave every byte this one does and more. It leaves no bf16
|
||||
* norm output, so one asked for (`y`) takes those forms too. */
|
||||
if (sp > 0 && res && wp && !y) {
|
||||
const int ep = thr == 1024 ? (n <= 4096 ? 4 : n <= 6144 ? 6 : n <= 8192 ? 8 : 0)
|
||||
: thr == 512 ? (n <= 4096 ? 8 : n <= 6144 ? 12 : n <= 8192 ? 16 : 0) : 0;
|
||||
const int nbs = (M + WS - 1) / WS;
|
||||
const long need = (long)nbs * (n / 8) + (long)nbs * (n / 16 + 1 + (sp == 2 ? n / 8 : 0));
|
||||
if (ep > 0 && nbs <= R4D_AR_WIDE_MAX_BLOCKS && need <= slot_stride16 &&
|
||||
(sp != 2 || r_ld % 8 == 0)) {
|
||||
try {
|
||||
const long fb = (long)nbs * WS * (long)sizeof(unsigned int);
|
||||
r4d_ar_check_extent(my_flags, fb, "ar_rmsnorm_quant_i8", "my_flags");
|
||||
for (int i = 0; i < NPEERS; ++i)
|
||||
r4d_ar_check_extent(pf[i], fb, "ar_rmsnorm_quant_i8", "peer flags");
|
||||
} catch (...) {
|
||||
return -8;
|
||||
}
|
||||
const size_t lds_sp = (size_t)n * (sizeof(uint16_t) + 1);
|
||||
#define R4D_ARQ8S_K(E, T) \
|
||||
hipLaunchKernelGGL((chain ? r4dchain_ar_rmsnorm_quant_i8_sp<E, T, WS> \
|
||||
: ar_rmsnorm_quant_i8_sp_kernel<E, T, WS>), \
|
||||
dim3((uint32_t)nbs), dim3(T), lds_sp, (hipStream_t)stream, \
|
||||
(const uint16_t*)x, (uint16_t*)res, (const void*)w, \
|
||||
(const void*)wp, (const void*)sc, (uint32_t)sc_f32, \
|
||||
(uint32_t)w_f32, (uint32_t)(sp == 2), (signed char*)q, (float*)s, \
|
||||
peers, (const int4*)my_scratch, (unsigned int*)my_flags, \
|
||||
(unsigned int*)seq_ctrs, M, (uint32_t)n, eps, wadd, \
|
||||
(uint32_t)r_ld, (int)slot_stride16, rank, drain, acq, pub, \
|
||||
(long long*)gate, (unsigned int*)gate_ctr)
|
||||
if (thr == 1024 && ep == 4) R4D_ARQ8S_K(4, 1024);
|
||||
else if (thr == 1024 && ep == 6) R4D_ARQ8S_K(6, 1024);
|
||||
else if (thr == 1024) R4D_ARQ8S_K(8, 1024);
|
||||
else if (ep == 8) R4D_ARQ8S_K(8, 512);
|
||||
else if (ep == 12) R4D_ARQ8S_K(12, 512);
|
||||
else R4D_ARQ8S_K(16, 512);
|
||||
#undef R4D_ARQ8S_K
|
||||
return hipGetLastError() == hipSuccess ? 0 : -4;
|
||||
}
|
||||
}
|
||||
|
||||
/* The junction as the standalone two-shot's tail (ar_rmsnorm_quant_i8_tail_kernel): at the
|
||||
* junction's register width (1024 threads), up to R4D_ARQ8_TAIL_MAX_ROWS rows. */
|
||||
const int ept = n <= 4096 ? 4 : n <= 6144 ? 6 : n <= 8192 ? 8 : 0;
|
||||
|
||||
+9
-2
@@ -1499,7 +1499,11 @@ static const RadOperandSpec oGeluMulQuantI8[] = {
|
||||
};
|
||||
|
||||
/* `ar_rmsnorm_quant_i8` is rmsnorm_quant_i8 with the two-rank exact all-reduce of `x` in front:
|
||||
* same operands with `x` inout (it receives the reduced sum), plus `world_size`. */
|
||||
* same operands with `x` inout (it receives the reduced sum), plus `world_size`. `sp` (optional,
|
||||
* 0) lets a row split the all-reduce around the junction at tensor parallel: 1 asks only for q and
|
||||
* the scales on every row and the residual on the rows this rank owns (M split evenly over the
|
||||
* ranks in rank order), 2 for the residual on every row too; `x` is then left unspecified. A row
|
||||
* may always do more than asked (the whole form). */
|
||||
static const RadParamSpec pArRmsNormQuantI8[] = {
|
||||
{ "M", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "n", RAD_P_INT, RAD_REQUIRED },
|
||||
@@ -1507,6 +1511,7 @@ static const RadParamSpec pArRmsNormQuantI8[] = {
|
||||
{ "world_size", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "dtype", RAD_P_STR, RAD_REQUIRED },
|
||||
P_F64O("wadd"),
|
||||
P_INTO("sp"),
|
||||
};
|
||||
static const RadOperandSpec oArRmsNormQuantI8[] = {
|
||||
{ "x", RAD_OPD_INOUT, 0 },
|
||||
@@ -7923,7 +7928,9 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
"sum, residual add, scalar, RMS norm and quant_act_i8's quantiser -- in one kernel",
|
||||
"exactly 4 ranks; n a multiple of 16, x unpitched (it is the message); a block owns whole rows; "
|
||||
"exact wire; one-shot ((ranks - 1) x the message a rank) -- for decode heights; from 5 to 32 "
|
||||
"rows (axis `tail`) the standalone two-shot with the junction as its tail",
|
||||
"rows (axis `tail`) the standalone two-shot with the junction as its tail; with `sp` the "
|
||||
"all-reduce split around the junction (reduce-scatter, the junction on this rank's rows, "
|
||||
"all-gather of the int8 codes), a block per local row, P2P",
|
||||
"bf16 in, int8 out in the w4a8 GEMM's fragment order with an f32 per-row scale; the reduced sum "
|
||||
"rounded to bf16 as the standalone exact all-reduce stores it",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
|
||||
+153
-9
@@ -11326,7 +11326,9 @@ static void rmsnorm_quant_i8_numeric(const Plugin& pl) {
|
||||
/* The row's reg axis at 1024 threads: 1 (the default) holds a thread's columns in registers,
|
||||
* 0 re-reads the row each pass; both must leave the chain's bytes. */
|
||||
struct RqCase { int64_t M; int reg; };
|
||||
for (const RqCase rqc : { RqCase{ 1, 1 }, RqCase{ 8, 1 }, RqCase{ 8, 0 }, RqCase{ 40, 1 } }) {
|
||||
/* 40 and 64 rows: the 512-thread width, its register form and its re-read form */
|
||||
for (const RqCase rqc : { RqCase{ 1, 1 }, RqCase{ 8, 1 }, RqCase{ 8, 0 }, RqCase{ 40, 1 },
|
||||
RqCase{ 40, 0 }, RqCase{ 64, 1 } }) {
|
||||
const int64_t M = rqc.M;
|
||||
const std::string form = rqc.reg ? "" : " (re-read form)";
|
||||
const int64_t K = 5376; /* Gemma 4 31B's hidden width: 336 sixteen-byte k steps */
|
||||
@@ -21734,6 +21736,9 @@ struct ArPattern {
|
||||
std::vector<int64_t> sizes;
|
||||
int out_of_place = -1; /* index into sizes, or -1 */
|
||||
std::vector<int> pieces; /* per size: a launch `pieces` parameter, 0 or absent none */
|
||||
int overlap = 0; /* 1: launched as beside compute (the lane rows) */
|
||||
int coded_pattern = 0; /* 1: ar_coded_value, not ar_pattern_value */
|
||||
int coded = -1; /* >= 0: the lane row's `coded` launch parameter */
|
||||
void* inst = nullptr;
|
||||
int rc_init = RAD_E_STATE, rc_launch = RAD_OK;
|
||||
std::vector<std::vector<float>> out;
|
||||
@@ -21745,6 +21750,30 @@ static float ar_pattern_value(int rank, int64_t i) {
|
||||
return (float)((int)((i * 37 + (int64_t)rank * 101) % 509) - 254) / 64.0f;
|
||||
}
|
||||
|
||||
/* The pattern for the two-shot's coded form (r4d_ar_twoshot_Nrank_exact.hip), which codes a group
|
||||
* of 64 consecutive values on a shared exponent and sends a group it cannot code raw: the
|
||||
* per-element pattern, with groups sent down each path -- an exponent range of exactly 14 (coded)
|
||||
* and 15 (raw), one far past it, a subnormal, an infinity, all zeros, and exponents near the top.
|
||||
* Every shard starts on a group, so an element's group is its index over 64; each path holds on
|
||||
* both pushes (the inputs and the folded shard). */
|
||||
static float ar_coded_value(int rank, int64_t i) {
|
||||
const int64_t g = i / 64, j = i % 64;
|
||||
const float sg = (j & 1) ? -1.0f : 1.0f;
|
||||
switch (g % 16) {
|
||||
case 3: return std::ldexp(ar_pattern_value(rank, i), 3 * (int)(j % 8));
|
||||
case 5: return j == 17 ? (rank == 1 ? h_bf16(0x0005) : 1.0f) : ar_pattern_value(rank, i);
|
||||
case 7: return (j == 40 && rank == 0) ? INFINITY : ar_pattern_value(rank, i);
|
||||
case 9: return 0.0f;
|
||||
case 11: return sg * (j == 5 ? 16384.0f : 1.0f);
|
||||
case 13: return sg * (j == 5 ? 32768.0f : 1.0f);
|
||||
case 14: return std::ldexp(ar_pattern_value(rank, i), 100);
|
||||
default: return ar_pattern_value(rank, i);
|
||||
}
|
||||
}
|
||||
static float ar_value(int coded_pattern, int rank, int64_t i) {
|
||||
return coded_pattern ? ar_coded_value(rank, i) : ar_pattern_value(rank, i);
|
||||
}
|
||||
|
||||
static void ar_pattern_worker(ArPattern* t) {
|
||||
if (st_set_device(t->dev) != hipSuccess) { t->rc_init = RAD_E_DEVICE; return; }
|
||||
const bool bf = !std::strcmp(t->dtype, "bf16");
|
||||
@@ -21754,6 +21783,7 @@ static void ar_pattern_worker(ArPattern* t) {
|
||||
std::vector<RadParam> ps = {
|
||||
PI("world_size", t->world), PI("numel", most), PS("dtype", t->dtype), PI("exact", 1),
|
||||
};
|
||||
if (t->overlap) ps.push_back(PI("overlap", 1));
|
||||
t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, t->world, &t->inst);
|
||||
if (t->rc_init != RAD_OK) return;
|
||||
void* in = nullptr;
|
||||
@@ -21764,7 +21794,7 @@ static void ar_pattern_worker(ArPattern* t) {
|
||||
for (size_t k = 0; k < t->sizes.size(); ++k) {
|
||||
const int64_t n = t->sizes[k];
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
const float v = ar_pattern_value(t->rank, i);
|
||||
const float v = ar_value(t->coded_pattern, t->rank, i);
|
||||
if (bf) { const uint16_t b = h_to_bf16(v); std::memcpy(&h[(size_t)i * 2], &b, 2); }
|
||||
else std::memcpy(&h[(size_t)i * 4], &v, 4);
|
||||
}
|
||||
@@ -21778,6 +21808,7 @@ static void ar_pattern_worker(ArPattern* t) {
|
||||
a.t = ts; a.n_t = oop ? 2 : 1;
|
||||
std::vector<RadParam> lp = ps;
|
||||
if (k < t->pieces.size() && t->pieces[k] > 0) lp.push_back(PI("pieces", t->pieces[k]));
|
||||
if (t->coded >= 0) lp.push_back(PI("coded", t->coded));
|
||||
a.p = lp.data(); a.n_p = (int)lp.size();
|
||||
a.instance = t->inst;
|
||||
a.rank = t->rank;
|
||||
@@ -22420,6 +22451,91 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty
|
||||
}
|
||||
}
|
||||
|
||||
/* The four-rank two-shot's lane row (overlap 1: the prefill's reductions beside compute) in both
|
||||
* its forms, which the `coded` launch parameter picks: the narrow plain launch (0), and the coded
|
||||
* one (1), whose pushes carry ~12.2 bits a bf16 value. Each must equal the fp32 ascending-rank
|
||||
* fold bit for bit, on the per-element pattern and on ar_coded_value. A message outside the coded
|
||||
* form (shards that are not whole 256-value waves, or fp32) runs plain whatever the parameter
|
||||
* says; each label says which form a size took, by the library's rule restated here. */
|
||||
static bool ar_coded_fits(const char* dtype, int64_t n, int64_t most) {
|
||||
if (std::strcmp(dtype, "bf16") != 0 || n % 1024 != 0) return false;
|
||||
const int64_t SL = n / 32; /* a shard's 16-byte chunks */
|
||||
const int64_t shadow = (12 * SL + SL / 8 + 15) & ~(int64_t)15;
|
||||
const int64_t region = (shadow + 16 * SL + 255) & ~(int64_t)255;
|
||||
return 2 * region <= ((most * 2 + 15) & ~(int64_t)15);
|
||||
}
|
||||
static void ar_lane_forms(const Plugin& pl, const std::vector<int>& devs, const char* name) {
|
||||
const RadKernelInfo* row = find(pl, name);
|
||||
if (!row) { ok(name, false, "row absent"); return; }
|
||||
if ((int)devs.size() < 4) { skip(name, "needs 4 gfx1201 cards"); return; }
|
||||
struct Run { bool coded; int pat; const char* dtype; std::vector<int64_t> sizes; int oop; };
|
||||
/* 1024: one wave a shard; 1055744: 1031 waves, an odd split over the blocks; 524352: bf16
|
||||
* outside the coded form; 46137344: the feed-forward reduce at an 8192-token chunk. */
|
||||
const std::vector<Run> runs = {
|
||||
{ false, 0, "bf16", { 4194304, 524352, 1055744, 20971520 }, 1 },
|
||||
{ true, 0, "bf16", { 4194304, 1024, 524352, 1055744, 20971520, 4194304 }, 3 },
|
||||
{ true, 1, "bf16", { 4194304, 1024, 1055744, 46137344, 4194304 }, 2 },
|
||||
{ true, 0, "fp32", { 524352 }, 0 },
|
||||
};
|
||||
for (const Run& run : runs) {
|
||||
std::vector<ArPattern> t(4);
|
||||
std::vector<std::thread> th;
|
||||
for (int r = 0; r < 4; ++r) {
|
||||
t[(size_t)r].row = row; t[(size_t)r].rank = r; t[(size_t)r].dev = devs[(size_t)r];
|
||||
t[(size_t)r].world = 4;
|
||||
t[(size_t)r].dtype = run.dtype; t[(size_t)r].sizes = run.sizes;
|
||||
t[(size_t)r].out_of_place = run.oop;
|
||||
t[(size_t)r].overlap = 1;
|
||||
t[(size_t)r].coded_pattern = run.pat;
|
||||
t[(size_t)r].coded = run.coded ? 1 : 0;
|
||||
th.emplace_back(ar_pattern_worker, &t[(size_t)r]);
|
||||
}
|
||||
for (std::thread& x : th) x.join();
|
||||
const std::string what = std::string(name) + " " + run.dtype +
|
||||
(run.coded ? ", coded 1" : ", coded 0") +
|
||||
(run.pat ? ", the coded form's pattern" : "");
|
||||
bool up = true, ran = true;
|
||||
std::string rcs;
|
||||
for (const ArPattern& a : t) {
|
||||
up = up && a.rc_init == RAD_OK; ran = ran && a.rc_launch == RAD_OK;
|
||||
rcs += std::to_string(a.rc_init) + "/" + std::to_string(a.rc_launch) + " ";
|
||||
}
|
||||
ok((what + " init (peer rendezvous)").c_str(), up, "init/launch rc " + rcs);
|
||||
if (!up) return;
|
||||
ok((what + " launches").c_str(), ran, "init/launch rc " + rcs);
|
||||
int64_t most = 0;
|
||||
for (int64_t n : run.sizes) most = n > most ? n : most;
|
||||
if (ran) {
|
||||
const bool bf = !std::strcmp(run.dtype, "bf16");
|
||||
for (size_t k = 0; k < run.sizes.size(); ++k) {
|
||||
const int64_t n = run.sizes[k];
|
||||
int64_t bad = 0, first = -1;
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
float acc = 0.0f;
|
||||
for (int r = 0; r < 4; ++r) {
|
||||
const float v = ar_value(run.pat, r, i);
|
||||
acc += bf ? h_bf16(h_to_bf16(v)) : v;
|
||||
}
|
||||
const float want = bf ? h_bf16(h_to_bf16(acc)) : acc;
|
||||
for (int r = 0; r < 4; ++r)
|
||||
if (t[(size_t)r].out.size() <= k || t[(size_t)r].out[k][(size_t)i] != want) {
|
||||
if (first < 0) first = i;
|
||||
++bad;
|
||||
}
|
||||
}
|
||||
const bool coded = run.coded && ar_coded_fits(run.dtype, n, most);
|
||||
const std::string label =
|
||||
what + " numel " + std::to_string(n) + " (" + (coded ? "coded" : "plain") +
|
||||
((int)k == run.oop ? ", out of place" : ", in place") +
|
||||
") == fp32 ascending-rank fold on every rank, bit for bit";
|
||||
ok(label.c_str(), bad == 0,
|
||||
std::to_string(bad) + " elements wrong, first at " + std::to_string(first));
|
||||
}
|
||||
}
|
||||
if (row->fini) for (ArPattern& a : t) if (a.inst) row->fini(a.inst);
|
||||
}
|
||||
}
|
||||
|
||||
/* The three-rank tiered-int8 wire.
|
||||
*
|
||||
* Exact: every rank ends with the same bytes (the owner stores the decode of what it published),
|
||||
@@ -23060,6 +23176,7 @@ struct ArQ8 {
|
||||
bool gated = false; /* hand the launch an odd `link_gate` word */
|
||||
long long gate_after = -1; /* ...and what the launch left in it */
|
||||
int tail = -1; /* the `tail` axis pinned, or -1: the default */
|
||||
int sp = 0; /* `sp`: the all-reduce split around the junction */
|
||||
void* inst = nullptr;
|
||||
int rc_init = RAD_E_STATE, rc_launch = RAD_OK;
|
||||
const std::vector<uint16_t>* r_in = nullptr; /* the replicated residual, w and w_post */
|
||||
@@ -23081,6 +23198,7 @@ static void ar_q8_worker(ArQ8* t) {
|
||||
if (t->rc_init != RAD_OK) return;
|
||||
/* The tuned point arrives as a parameter of the launch, as the core appends it. */
|
||||
if (t->tail >= 0) ps.push_back(PI("tail", t->tail));
|
||||
if (t->sp > 0) ps.push_back(PI("sp", t->sp));
|
||||
|
||||
/* Allocated here and not through dalloc, whose list four rank threads would race on. */
|
||||
void* x = nullptr; void* r = nullptr; void* w = nullptr; void* wp = nullptr; void* sc = nullptr;
|
||||
@@ -23111,6 +23229,7 @@ static void ar_q8_worker(ArQ8* t) {
|
||||
T(wp, RAD_BF16, { K }), T(sc, RAD_BF16, { 1 }) };
|
||||
if (!t->res) { absent(ts[1]); absent(ts[6]); }
|
||||
if (!t->scal) absent(ts[7]);
|
||||
if (t->sp) absent(ts[5]); /* the split form leaves no bf16 output */
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
a.t = ts.data(); a.n_t = (int)ts.size();
|
||||
@@ -23149,12 +23268,19 @@ static void ar_rmsnorm_quant_i8_wide_case(const Plugin& pl, const std::vector<in
|
||||
const int world = 4;
|
||||
const bool host = nm_ends_with(name, "_host"); /* the mailbox twin leaves the hold alone */
|
||||
/* tail: the `tail` axis pinned (1 from 5 to 32 rows: the standalone two-shot with the junction
|
||||
* as its tail) */
|
||||
struct Shape { int64_t M, K; int tail; };
|
||||
* as its tail); sp: the all-reduce split around the junction (1: the residual on each rank's
|
||||
* rows, 2: on every row), including heights where a rank owns no row (1, 3) and the 31B's
|
||||
* tp-4 band top (97) */
|
||||
struct Shape { int64_t M, K; int tail; int sp = 0; };
|
||||
const Shape shapes[] = { { 1, 5376, 0 }, { 8, 5376, 0 }, { 40, 5376, 0 }, { 97, 5376, 0 },
|
||||
{ 64, 2816, 0 },
|
||||
{ 5, 5376, 1 }, { 8, 5376, 1 }, { 16, 5376, 1 }, { 32, 5376, 1 },
|
||||
{ 8, 2816, 1 } };
|
||||
{ 8, 2816, 1 },
|
||||
{ 1, 5376, 0, 2 }, { 3, 5376, 0, 1 }, { 4, 5376, 0, 1 },
|
||||
{ 5, 5376, 0, 1 }, { 8, 5376, 0, 1 }, { 8, 5376, 0, 2 },
|
||||
{ 16, 5376, 0, 2 }, { 33, 5376, 0, 1 }, { 64, 5376, 0, 1 },
|
||||
{ 64, 5376, 0, 2 }, { 97, 5376, 0, 2 }, { 64, 2816, 0, 1 },
|
||||
{ 8, 2816, 0, 2 } };
|
||||
const std::string nm(name);
|
||||
for (const Shape& sh : shapes) {
|
||||
const int64_t M = sh.M, K = sh.K;
|
||||
@@ -23191,7 +23317,9 @@ static void ar_rmsnorm_quant_i8_wide_case(const Plugin& pl, const std::vector<in
|
||||
const std::string at = " M=" + std::to_string(M) + " n=" + std::to_string(K) +
|
||||
(form == 0 ? ", plain" : form == 1 ? ", junction"
|
||||
: ", junction with scalar") +
|
||||
(sh.tail ? ", two-shot with the junction as its tail" : ", one-shot");
|
||||
(sh.sp == 2 ? ", split around the junction, residual gathered"
|
||||
: sh.sp ? ", split around the junction"
|
||||
: sh.tail ? ", two-shot with the junction as its tail" : ", one-shot");
|
||||
|
||||
/* The chain on the first card: rmsnorm_quant_i8 over the reduced sum. */
|
||||
st_set_device(devs[0]);
|
||||
@@ -23236,6 +23364,7 @@ static void ar_rmsnorm_quant_i8_wide_case(const Plugin& pl, const std::vector<in
|
||||
x.row = row; x.rank = r; x.dev = devs[(size_t)r];
|
||||
x.M = M; x.K = K; x.res = res; x.scal = scal; x.gated = scal;
|
||||
x.tail = sh.tail;
|
||||
x.sp = sh.sp;
|
||||
x.r_in = &hr; x.w = &hw; x.wp = &hp; x.sc = hs;
|
||||
x.x_in = hx[(size_t)r];
|
||||
th.emplace_back(ar_q8_worker, &x);
|
||||
@@ -23266,10 +23395,18 @@ static void ar_rmsnorm_quant_i8_wide_case(const Plugin& pl, const std::vector<in
|
||||
if (ran) {
|
||||
int64_t bx = 0, br = 0, by = 0, bq = 0, bs = 0, nz = 0;
|
||||
for (const ArQ8& x : t) {
|
||||
/* With `sp` the launch leaves x and the bf16 output alone, and at 1 the
|
||||
* residual only on the rows this rank owns: [a, a + na), M split evenly in
|
||||
* rank order. */
|
||||
const int64_t base = M / world, rem = M % world;
|
||||
const int64_t a = x.rank * base + std::min<int64_t>(x.rank, rem);
|
||||
const int64_t na = base + (x.rank < rem ? 1 : 0);
|
||||
for (int64_t i = 0; i < M * K; ++i) {
|
||||
bx += x.x_out[(size_t)i] != xsum[(size_t)i];
|
||||
if (res) br += x.r_out[(size_t)i] != want_r[(size_t)i];
|
||||
by += x.y_out[(size_t)i] != want_y[(size_t)i];
|
||||
const int64_t row = i / K;
|
||||
if (!sh.sp) bx += x.x_out[(size_t)i] != xsum[(size_t)i];
|
||||
if (res && (sh.sp != 1 || (row >= a && row < a + na)))
|
||||
br += x.r_out[(size_t)i] != want_r[(size_t)i];
|
||||
if (!sh.sp) by += x.y_out[(size_t)i] != want_y[(size_t)i];
|
||||
bq += x.q_out[(size_t)i] != want_q[(size_t)i];
|
||||
}
|
||||
for (int64_t i = 0; i < M; ++i)
|
||||
@@ -26509,6 +26646,13 @@ static void device_phase(const Plugin& pl) {
|
||||
}
|
||||
}
|
||||
}
|
||||
/* The four-rank two-shot's lane row and its host twin, plain and coded (ar_lane_forms). */
|
||||
for (const char* n : { "ar_twoshot_4rank_exact_lane", "ar_twoshot_4rank_exact_lane_host" }) {
|
||||
if (!sel(n)) continue;
|
||||
if (!g_case) std::printf("\ncross-rank, beside compute (overlap 1)\n");
|
||||
std::printf(" %s\n", n);
|
||||
ar_lane_forms(pl, devs, n);
|
||||
}
|
||||
/* The fused write rows at both widths and both gathers. The `_host` twins take the same
|
||||
* calls; they differ only in where the mailbox lives. */
|
||||
for (const char* base : { "ar_hc_write", "ar_gather_hc_write",
|
||||
|
||||
+46
-3
@@ -4612,8 +4612,9 @@ TEST(gemma4_attention_join_writes_the_routed_inputs_on_fp8_experts) {
|
||||
* at two ranks (only the embedding's and the last layer's feed-forward reduction stand alone),
|
||||
* through ar_rmsnorm_quant_i8 declared at world_size 4 up to 16 rows (kAr4FoldRows: a DFlash
|
||||
* verify's rows, which libr4d folds in its tail form, inside its one-shot band of 524288 / 5376 =
|
||||
* 97). A taller step issues each reduction and its junction apart, and where no library declares
|
||||
* the fold every step issues the pair. */
|
||||
* 97), and beside each the same op with `sp` (the all-reduce split around the junction) over the
|
||||
* same 16 rows, which a step from 4 rows takes. A taller step issues each reduction and its
|
||||
* junction apart, and where no library declares the fold every step issues the pair. */
|
||||
TEST(a_gemma4_w4a8_step_at_four_ranks_folds_the_all_reduce_into_the_junction_at_decode_heights) {
|
||||
const int L = 12;
|
||||
std::vector<std::string> folded = { "embed_lookup", "all_reduce", "rmsnorm_quant_i8" };
|
||||
@@ -4684,7 +4685,7 @@ TEST(a_gemma4_w4a8_step_at_four_ranks_folds_the_all_reduce_into_the_junction_at_
|
||||
int64_t hi = -1;
|
||||
*n = 0;
|
||||
for (const auto& o : bb.ops) {
|
||||
if (o.op != "ar_rmsnorm_quant_i8") continue;
|
||||
if (o.op != "ar_rmsnorm_quant_i8" || pint(&o, "sp") != -12345) continue;
|
||||
++*n;
|
||||
CHECK_EQ(pint(&o, "world_size"), 4);
|
||||
for (const auto& p : o.p)
|
||||
@@ -4695,6 +4696,37 @@ TEST(a_gemma4_w4a8_step_at_four_ranks_folds_the_all_reduce_into_the_junction_at_
|
||||
int n_ar = 0;
|
||||
CHECK_EQ(fold_hi(b, &n_ar), 16);
|
||||
CHECK_EQ(n_ar, 2 * L - 1);
|
||||
/* the split form beside each fold, over its 16 rows; the residual on every row (sp 2) only
|
||||
* before the last layer's feed-forward, with no drafter tapping the stream here */
|
||||
auto sp_hi = [](const RadBuilder& bb, int* n, int* n2) {
|
||||
int64_t hi = -1;
|
||||
*n = 0; *n2 = 0;
|
||||
for (const auto& o : bb.ops) {
|
||||
if (o.op != "ar_rmsnorm_quant_i8" || pint(&o, "sp") == -12345) continue;
|
||||
++*n;
|
||||
if (pint(&o, "sp") == 2) ++*n2;
|
||||
else CHECK_EQ(pint(&o, "sp"), 1);
|
||||
CHECK_EQ(pint(&o, "world_size"), 4);
|
||||
for (const auto& p : o.p)
|
||||
if (p.key == "M") hi = p.ihi;
|
||||
}
|
||||
return hi;
|
||||
};
|
||||
int n_sp = 0, n_sp2 = 0;
|
||||
CHECK_EQ(sp_hi(b, &n_sp, &n_sp2), 16);
|
||||
CHECK_EQ(n_sp, 2 * L - 1);
|
||||
CHECK_EQ(n_sp2, 1);
|
||||
/* which form a step's junctions take: the split one from 4 rows, the fold below */
|
||||
auto sp_issued = [&b](const RadCtx& c) {
|
||||
int n = 0, folds = 0;
|
||||
for (const auto& i : c.issues) {
|
||||
const RecOp* o = b.op_of(i.op);
|
||||
if (!o || o->op != "ar_rmsnorm_quant_i8") continue;
|
||||
++folds;
|
||||
if (pint(o, "sp") != -12345) ++n;
|
||||
}
|
||||
return std::make_pair(n, folds);
|
||||
};
|
||||
|
||||
/* 20 MB of gate_up beside the attention's reduction and of the next layer's q beside the
|
||||
* feed-forward's, and 9 MB of down_proj beside the attention */
|
||||
@@ -4725,6 +4757,17 @@ TEST(a_gemma4_w4a8_step_at_four_ranks_folds_the_all_reduce_into_the_junction_at_
|
||||
gemma4_bf16::step(&d, &tall);
|
||||
CHECK(d.step_fail.empty());
|
||||
CHECK_EQ(join(issued_ops(b, d)), join(rows <= 16 ? folded : apart_tall));
|
||||
if (rows <= 16) CHECK(sp_issued(d) == std::make_pair(2 * L - 1, 2 * L - 1));
|
||||
}
|
||||
/* three rows: the fold; eight users' verify (64 rows): the pair */
|
||||
for (int rows : { 3, 64 }) {
|
||||
RadBatch v = one_prompt(rows, out);
|
||||
v.phase = RAD_PHASE_DECODE;
|
||||
RadCtx d; d.b = &b; d.rank = 0; d.world = 4; d.batch = &v;
|
||||
gemma4_bf16::step(&d, &v);
|
||||
CHECK(d.step_fail.empty());
|
||||
if (rows == 64) CHECK_EQ(join(issued_ops(b, d)), join(apart_tall));
|
||||
CHECK(sp_issued(d) == (rows == 3 ? std::make_pair(0, 2 * L - 1) : std::make_pair(0, 0)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user