From 284feda3a655957845203cb853a60c28eaa43209 Mon Sep 17 00:00:00 2001 From: mici81 Date: Tue, 20 Oct 2026 15:30:18 +0000 Subject: [PATCH] pipelined prefill past two ranks on request: RADIANCE_PREFILL_PIPELINE, narrow all-reduce at three and eight - One switch for qwen35_fp8's and gemma3's pipelined prefill, RADIANCE_PREFILL_PIPELINE=auto|on|off (arch/common/rad_prefill_pipe.h), in place of RADIANCE_QWEN35_PIPE / RADIANCE_GEMMA3_PIPE. auto (or unset) pipelines at two ranks, where it is measured; on also at three, four and eight, with a declare note that it is not yet measured on such hardware; off never. Another value is noted and taken as auto. - libr4d: the narrow `overlap` all-reduce rows at three and eight ranks, as the four-rank ones are: the wide two-shot rows' entry points (exact at three and eight, ti8 at three) at tArTs4Lane's launch, so the collective beside the other piece's compute does not take the card (a wide one slows the GEMM beside it 1.45-2.1x and ate the whole gain at two). Bit-identical to the wide rows by construction; neither launch is tuned on three or eight cards, which this rig does not have. arch_test: at four and eight ranks a 2048-row step of one sequence is serial unless asked; asked, it runs in two pieces on every rank with every collective on the second lane at `overlap` 1 and the embedding's whole (failed before the change: the step stayed serial). Co-Authored-By: Claude Opus 5.5 (1M context) --- arch/common/rad_prefill_pipe.h | 37 ++++++++++++++++++++ arch/gemma3/gemma3.cpp | 15 ++++---- arch/qwen35_fp8/qwen35_fp8.cpp | 12 +++---- libr4d/r4d_rows.cpp | 63 ++++++++++++++++++++++++++++++++++ tests/arch_test.cpp | 62 +++++++++++++++++++++++++++++---- 5 files changed, 170 insertions(+), 19 deletions(-) create mode 100644 arch/common/rad_prefill_pipe.h diff --git a/arch/common/rad_prefill_pipe.h b/arch/common/rad_prefill_pipe.h new file mode 100644 index 00000000..4e8c0c53 --- /dev/null +++ b/arch/common/rad_prefill_pipe.h @@ -0,0 +1,37 @@ +#pragma once +// RADIANCE_PREFILL_PIPELINE: whether an architecture with a pipelined prefill (a prefill step of one +// sequence in two pieces, the collectives between blocks on the second lane, beside the other piece's +// compute) uses it. +// +// auto (or unset) at two ranks, where it was measured (Qwen3.8-27B 41k prefill -6.6 %, +// TranslateGemma-27B 6k -12 %, bit for bit the serial step); +// on also at three, four and eight ranks: libr4d has the narrow `overlap` all-reduce +// there (the wide rows' entry points at a launch that leaves the CUs to compute), +// but the step has not been measured on such hardware; +// off never. +// +// Any other rank count has no narrow row, and a wide all-reduce beside the GEMMs ate the whole gain at +// two (it slows the GEMM 1.45-2.1x), so those stay serial. +#include + +#include +#include + +namespace rad::arch { + +inline bool prefill_pipeline(RadBuilder* b, int world, const char* who) { + const char* e = std::getenv("RADIANCE_PREFILL_PIPELINE"); + const bool on = e && !std::strcmp(e, "on"), off = e && !std::strcmp(e, "off"); + if (e && *e && !on && !off && std::strcmp(e, "auto") != 0) + rad_note(b, "%s: RADIANCE_PREFILL_PIPELINE=%s is not auto, on or off; taken as auto", who, e); + if (off || world < 2) return false; + if (world == 2) return true; + if (on && (world == 3 || world == 4 || world == 8)) { + rad_note(b, "%s: the pipelined prefill at %d ranks (RADIANCE_PREFILL_PIPELINE=on) is not yet " + "measured on hardware", who, world); + return true; + } + return false; +} + +} // namespace rad::arch diff --git a/arch/gemma3/gemma3.cpp b/arch/gemma3/gemma3.cpp index 9e62ff9c..e72da22e 100644 --- a/arch/gemma3/gemma3.cpp +++ b/arch/gemma3/gemma3.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -1187,15 +1188,15 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_NOWEIGHTS), {m.b_x}, {m.b_x}); - /* The pipelined prefill (step_pipelined) at two ranks wherever a step can be wide enough, on an - * MXFP4 trunk with fused junctions (decided below; RADIANCE_GEMMA3_PIPE=0 turns it off). Its + /* The pipelined prefill (step_pipelined) wherever a step can be wide enough, on an MXFP4 trunk + * with fused junctions (decided below), at the rank counts RADIANCE_PREFILL_PIPELINE allows + * (rad_prefill_pipe.h: two by default). Its * second lane touches the stream, the branch output, the shared input pair, the taps and the * bounds, so those leave the sharing pool (Ctx::lane1_); the pair is one for every block so * that this costs one pair, not one a linear. */ { - const char* e = std::getenv("RADIANCE_GEMMA3_PIPE"); - m.pipe_on = g.world == 2 && g.max_tok >= kPipeMinRows && !calib_dir() && - !(e && std::strcmp(e, "0") == 0); + m.pipe_on = g.max_tok >= kPipeMinRows && !calib_dir() && + rad::arch::prefill_pipeline(b, (int)g.world, "gemma3"); } if (m.pipe_on) { m.pipe_in.mx = true; @@ -1489,8 +1490,8 @@ static const int32_t* pipe_ready(RadCtx* c, const RadBatch* batch, Model& m, boo rad_memcpy_async(d, m.pipe_tbl + 4 * T, 4 * (int64_t)sizeof(int32_t), rad_stream(c)) != RAD_OK) { rad_step_fail(c, "gemma3: the pipelined prefill's piece bounds could not be put on the card " - "(no pinned host memory, or the copy failed); RADIANCE_GEMMA3_PIPE=0 runs " - "prefill serially"); + "(no pinned host memory, or the copy failed); RADIANCE_PREFILL_PIPELINE=off " + "runs prefill serially"); *failed = true; return nullptr; } diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index c09fc040..4e0cf5c8 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -87,6 +87,7 @@ #include #include #include +#include #include #include @@ -883,14 +884,13 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_NOWEIGHTS), {m.b_x}, {m.b_x}); #if !QWEN35_MOE - /* The pipelined prefill (step_pipelined), at two ranks wherever a step can be wide enough; - * RADIANCE_QWEN35_PIPE=0 turns it off. Its second lane touches the residual stream, the normed + /* The pipelined prefill (step_pipelined), wherever a step can be wide enough, at the rank + * counts RADIANCE_PREFILL_PIPELINE allows (rad_prefill_pipe.h: two by default). Its second lane touches the residual stream, the normed * input in both forms, the taps and the bounds, so those leave the sharing pool * (Ctx::lane1_); the bounds also because no declared op reads them, which would leave a * transient dead before the kernels that take them raw. */ { - const char* e = std::getenv("RADIANCE_QWEN35_PIPE"); - m.pipe_on = g.world == 2 && g.max_tok >= kPipeMinRows && !(e && std::strcmp(e, "0") == 0); + m.pipe_on = g.max_tok >= kPipeMinRows && rad::arch::prefill_pipeline(b, (int)g.world, "qwen35"); } if (m.pipe_on) { RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_x)); @@ -1329,8 +1329,8 @@ static const int32_t* pipe_ready(RadCtx* c, const RadBatch* batch, Model& m, boo rad_memcpy_async(d, m.pipe_tbl + 3 * T, 3 * (int64_t)sizeof(int32_t), rad_stream(c)) != RAD_OK) { rad_step_fail(c, "qwen35: the pipelined prefill's piece bounds could not be put on the card " - "(no pinned host memory, or the copy failed); RADIANCE_QWEN35_PIPE=0 runs " - "prefill serially"); + "(no pinned host memory, or the copy failed); RADIANCE_PREFILL_PIPELINE=off " + "runs prefill serially"); *failed = true; return nullptr; } diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 7ccec6f1..2d4fa47d 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -4205,6 +4205,20 @@ static const RadConstraint cArTsExact8[] = { C_LE("numel", R4D_AR_TWOSHOT_WIDE_MAX_ELEMS), C_P2P, }; +/* The three- and eight-rank two-shots at `overlap` 1, as cArTsExact4Lane: a pipelined prefill's + * all-reduce beside the other piece's compute. Their narrow launch takes the four-rank lane's + * defaults; neither has been tuned on three or eight cards. */ +static const RadConstraint cArTsExact3Lane[] = { + C_EQ("world_size", 3), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"), + C_GE("numel", R4D_AR_WIDE_ONESHOT_MAX_ELEMS + 1), C_LE("numel", R4D_AR_TWOSHOT_WIDE_MAX_ELEMS), + C_EQ("overlap", 1), C_P2P, +}; +static const RadConstraint cArTsExact8Lane[] = { + C_EQ("world_size", 8), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"), + C_DIV("numel", 64), C_GE("numel", R4D_AR_WIDE_ONESHOT_MAX_ELEMS + 1), + C_LE("numel", R4D_AR_TWOSHOT_WIDE_MAX_ELEMS), + C_EQ("overlap", 1), C_P2P, +}; static const RadConstraint cArWht6[] = { C_EQ("world_size", 2), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 64), C_P2P, @@ -4223,6 +4237,11 @@ static const RadConstraint cArTi8Ts3[] = { C_EQ("world_size", 3), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 32), C_P2P, }; +/* The same at `overlap` 1 (tArTs4Lane, untuned at three ranks). */ +static const RadConstraint cArTi8Ts3Lane[] = { + C_EQ("world_size", 3), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 32), + C_EQ("overlap", 1), C_P2P, +}; static const RadConstraint cArTi8Ts4[] = { C_EQ("world_size", 4), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 128), C_P2P, @@ -7093,6 +7112,21 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_exact, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, +/* The row above at `overlap` 1: the same entry point and the same arithmetic, the four-rank lane's + * narrow launch (tArTs4Lane; not tuned on three cards). */ +{ "ar_twoshot_3rank_exact_lane", "all_reduce", "ar", + "all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch; narrow launch beside " + "compute", + "exactly 3 ranks, all peer-accessible, overlap 1; messages above " + R4D_XSTR(R4D_AR_WIDE_ONESHOT_MAX_ELEMS) " elements; shards as even as the word count allows; " + "in place; cudagraph-safe", + "bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks " + "and to the wide row", + RAD_DOMAIN_DEVICE, R4D_PRIO_AR_TWOSHOT + 1, + ROW(cArTsExact3Lane), ROW(tArTs4Lane), nullptr, + r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_exact, nullptr, nullptr, nullptr, + r4d_shape_all_reduce }, + { "ar_twoshot_4rank_exact", "all_reduce", "ar", "all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch, per-block pipelined " "handshake", @@ -7132,6 +7166,21 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_8rank_exact, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, +/* The row above at `overlap` 1: the same entry point and the same arithmetic, the four-rank lane's + * narrow launch (tArTs4Lane; not tuned on eight cards). */ +{ "ar_twoshot_8rank_exact_lane", "all_reduce", "ar", + "all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch; narrow launch beside " + "compute", + "exactly 8 ranks, all peer-accessible, overlap 1; messages above " + R4D_XSTR(R4D_AR_WIDE_ONESHOT_MAX_ELEMS) " elements; message must shard evenly over the ranks; " + "in place; cudagraph-safe", + "bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks " + "and to the wide row", + RAD_DOMAIN_DEVICE, R4D_PRIO_AR_TWOSHOT + 1, + ROW(cArTsExact8Lane), ROW(tArTs4Lane), nullptr, + r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_8rank_exact, nullptr, nullptr, nullptr, + r4d_shape_all_reduce }, + { "ar_gather_2rank", "all_gather", "ar", "all-gather, one-shot push over P2P IPC scratch, per-block flag handshake", "exactly 2 ranks, peer-accessible; y is [2 * numel] with rank r's input at r * numel; " @@ -7259,6 +7308,20 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_ti8, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, +/* The row above at `overlap` 1: the same entry point and the same arithmetic, the four-rank lane's + * narrow launch (tArTs4Lane; not tuned on three cards). */ +{ "ar_twoshot_3rank_ti8_lane", "all_reduce", "ar", + "all-reduce (sum), reduce-scatter + all-gather on the tiered-int8 wire -- lossy; narrow launch " + "beside compute", + "exactly 3 ranks, all peer-accessible, overlap 1; groups of 32, split over the shards as evenly as " + "they divide; under min_bytes the exact one-shot; cudagraph-safe", + "bf16 / fp16 payload, int8 + fp16 scale + u32 record per group on the wire, fp32 accumulate in " + "ascending rank order, bit-identical across ranks and to the wide row", + RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT + 1, + ROW(cArTi8Ts3Lane), ROW(tArTs4Lane), nullptr, + r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_ti8, nullptr, nullptr, nullptr, + r4d_shape_all_reduce }, + { "ar_twoshot_4rank_ti8", "all_reduce", "ar", "all-reduce (sum), reduce-scatter + all-gather; both hops ride the tiered-int8 wire (double " "quant, the owner storing the decode of its own words) -- lossy", diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index d4a9d2ed..59c6c352 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -6559,20 +6559,20 @@ TEST(gemma3_steps_lint_clean_at_one_and_two_ranks) { * pieces of 1024; every collective is on the second lane, the layers' all-reduces a piece at a time * at `overlap` 1 (two blocks a layer, two pieces a block) and the embedding's whole; the attention and * the DFlash2 taps run a piece at a time; q|k|v and gate|up read one shared input pair. A prompt's - * first chunk and a later one alike. RADIANCE_GEMMA3_PIPE=0 runs the same step serially. */ + * first chunk and a later one alike. RADIANCE_PREFILL_PIPELINE=off runs the same step serially. */ TEST(gemma3_a_wide_prefill_runs_in_two_pieces_with_its_boundaries_on_the_second_lane) { MetaKV mk = gemma3_dflash2_meta(); const RadEncoding mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, rad::arch::RAD_MX_GROUP); for (int off : { 0, 1 }) for (int rank = 0; rank < 2; ++rank) for (int32_t pctx : { 0, 4096 }) { - if (off) setenv("RADIANCE_GEMMA3_PIPE", "0", 1); + if (off) setenv("RADIANCE_PREFILL_PIPELINE", "off", 1); RadBuilder b; b.encs = { { "blk.", mx } }; RadBuildCtx ctx = spec_ctx(rank, 2, 7); ctx.max_tok = 2048; const int rc = stderr_of_rc([&] { return gemma3::declare(&b, mk.meta(), &ctx); }); - if (off) unsetenv("RADIANCE_GEMMA3_PIPE"); + if (off) unsetenv("RADIANCE_PREFILL_PIPELINE"); REQUIRE_EQ(rc, RAD_OK); const gemma3::Model& m = gemma3::g_model[rank]; CHECK(m.fuse_mx); @@ -9215,18 +9215,18 @@ TEST(a_4096_budget_without_a_span_is_the_pinned_graph_and_steps) { } } -/* RADIANCE_QWEN35_PIPE=0 turns the pipelined prefill off: the two-rank build is then the serial +/* RADIANCE_PREFILL_PIPELINE=off turns the pipelined prefill off: the two-rank build is then the serial * one, graph and steps, as pinned before the pipeline existed. */ TEST(qwen35_pipe_off_is_the_serial_graph_and_steps) { MetaKV q35 = q35fp8_meta(); - setenv("RADIANCE_QWEN35_PIPE", "0", 1); + setenv("RADIANCE_PREFILL_PIPELINE", "off", 1); RadBuilder b; RadBuildCtx ctx = spec_ctx(1, 2, 3); ctx.max_tok = 4096; ctx.max_seqs = 8; ctx.max_ctx = 200000; const int rc = stderr_of_rc([&] { return qwen35_fp8::declare(&b, q35.meta(), &ctx); }); - unsetenv("RADIANCE_QWEN35_PIPE"); + unsetenv("RADIANCE_PREFILL_PIPELINE"); REQUIRE_EQ(rc, RAD_OK); CHECK(!qwen35_fp8::g_model[1].pipe_on); CHECK_EQ(graph_hash(b), 0xc55656a96b3db1ceull); @@ -9299,6 +9299,56 @@ TEST(qwen35_a_wide_prefill_runs_in_two_pieces_with_its_boundaries_on_the_second_ } } +/* Past two ranks the pipelined prefill is opt-in (RADIANCE_PREFILL_PIPELINE=on): unset, the step is + * serial; asked, a 2048-row step of one sequence runs in two pieces with every collective on the + * second lane, the layers' all-reduces a piece at a time at `overlap` 1 (libr4d's narrow lane rows), + * the embedding's whole, on every rank. At four and eight ranks. */ +static void four_rank_pipe_case(int world, bool gemma) { + const char* env = "RADIANCE_PREFILL_PIPELINE"; + for (int ask : { 0, 1 }) + for (int rank = 0; rank < world; ++rank) { + if (ask) setenv(env, "on", 1); + RadBuilder b; + RadBuildCtx ctx = spec_ctx(rank, world, 7); + ctx.max_tok = 2048; + MetaKV mk = gemma ? gemma3_dflash2_meta() : with_dflash2(q35fp8_meta()); + if (gemma) b.encs = { { "blk.", rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, rad::arch::RAD_MX_GROUP) } }; + const int rc = stderr_of_rc([&] { + return gemma ? gemma3::declare(&b, mk.meta(), &ctx) : qwen35_fp8::declare(&b, mk.meta(), &ctx); + }); + if (ask) unsetenv(env); + REQUIRE_EQ(rc, RAD_OK); + const bool on = gemma ? gemma3::g_model[rank].pipe_on : qwen35_fp8::g_model[rank].pipe_on; + const int64_t L = gemma ? (int64_t)gemma3::g_model[rank].layers.size() + : (int64_t)qwen35_fp8::g_model[rank].layers.size(); + CHECK_EQ(on, ask == 1); + const auto pf = kit(b, {}, { 2048 }, 0); + const auto v = run_passes(b, gemma ? gemma3::step : qwen35_fp8::step, { pf.get() }, rank, world); + CHECK_PASSES(b, v); + const RadCtx& c = *v[0]; + CHECK(c.step_fail.empty()); + int ar = 0, ar_lane0 = 0, ar_overlap = 0, arn = 0; + for (const RecIssue& i : c.issues) { + const RecOp* o = b.op_of(i.op); + if (!o) continue; + if (o->op == "all_reduce") { ++ar; ar_lane0 += i.lane == 0; ar_overlap += pint(o, "overlap") == 1; } + if (o->op.rfind("ar_", 0) == 0) { ++arn; ar_lane0 += i.lane == 0; } + } + if (!ask) { + CHECK(c.joins.empty()); + continue; + } + CHECK_EQ(ar_lane0, 0); + CHECK_EQ(ar + arn, 4 * (int)L + 1); + CHECK_EQ(ar_overlap, 4 * (int)L); + CHECK(!c.joins.empty()); + } +} +TEST(qwen35_four_ranks_pipeline_only_when_asked) { four_rank_pipe_case(4, false); } +TEST(gemma3_four_ranks_pipeline_only_when_asked) { four_rank_pipe_case(4, true); } +TEST(qwen35_eight_ranks_pipeline_only_when_asked) { four_rank_pipe_case(8, false); } +TEST(gemma3_eight_ranks_pipeline_only_when_asked) { four_rank_pipe_case(8, true); } + /* Under a span a Flash-Next prefill of two intervals runs a pass at a time. At every world its * connection reads take a pass each and the indexer scores its pass-tall plane in windows, the MTP * head's pass included. At four ranks the step is pipelined (step_pipelined): every layer's