From 213403b458d502a7bd61c9655623f3dd6724e675 Mon Sep 17 00:00:00 2001 From: Constantin Date: Sun, 1 Nov 2026 11:25:32 -0500 Subject: [PATCH] Fail Spark TP declaration without exact collectives --- arch/spark25_bf16/spark25_bf16.cpp | 8 ++++ tests/arch_fakes.h | 6 ++- tests/spark25_arch_test.cpp | 64 ++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/arch/spark25_bf16/spark25_bf16.cpp b/arch/spark25_bf16/spark25_bf16.cpp index 8f4d463e..dee6a0a3 100644 --- a/arch/spark25_bf16/spark25_bf16.cpp +++ b/arch/spark25_bf16/spark25_bf16.cpp @@ -266,6 +266,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_NOWEIGHTS), {x}, {x}); }; m.reduce_embed = reduce(m.x, "fp32"); + if (g.world > 1 && !m.reduce_embed) { + fprintf(stderr, "radiance: spark25 requires an exact fp32 all-reduce for TP=%d\n", g.world); + return RAD_E_UNSUPPORTED; + } m.layers.resize((size_t)g.n_layer); for (int l = 0; l < g.n_layer; ++l) { @@ -355,6 +359,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_STR("act", "gelu"), RAD_STR("dtype", "bf16")), RAD_NOWEIGHTS), {m.gu}, {m.ff}); a.dp = gemm(E, F, a.down, m.ff, m.h); a.reduce_ffn = reduce(m.h, "bf16"); + if (g.world > 1 && (!a.reduce_attn || !a.reduce_ffn)) { + fprintf(stderr, "radiance: spark25 requires exact bf16 all-reduces for TP=%d\n", g.world); + return RAD_E_UNSUPPORTED; + } a.widen_ffn = widen(m.delta); a.add_ffn = add(); } diff --git a/tests/arch_fakes.h b/tests/arch_fakes.h index ecc1ae4e..a51dcb52 100644 --- a/tests/arch_fakes.h +++ b/tests/arch_fakes.h @@ -99,6 +99,7 @@ struct RadBuilder { /* Ops the fake kernel hierarchy refuses, to drive the fused-op fallback; and ops no loaded * library declares (rad_op_declared), as without an optional library. */ std::set refuse; + std::set> refuse_dtype; /* (op, dtype) forms */ std::set undeclared; /* When set, the only ops any loaded library declares (a CPU-only engine's: libref's * vocabulary); every other op is undeclared. */ @@ -217,7 +218,10 @@ extern "C" rad_op rad_decl_op(RadBuilder* b, const char* op, const RadParam* par r.p.push_back(p); } for (int i = 0; i < n_weights; ++i) r.w.push_back(weights[i]); - r.resolved = b->refuse.count(r.op) == 0; + bool dtype_refused = false; + for (const auto& p : r.p) + if (p.key == "dtype" && b->refuse_dtype.count({r.op, p.sval})) dtype_refused = true; + r.resolved = b->refuse.count(r.op) == 0 && !dtype_refused; b->ops.push_back(r); /* Declare always completes. An unresolved op is RAD_NULL_HANDLE, an error only if the plugin * issues it (rad_builder.h). */ diff --git a/tests/spark25_arch_test.cpp b/tests/spark25_arch_test.cpp index 982d4b9c..6860e85b 100644 --- a/tests/spark25_arch_test.cpp +++ b/tests/spark25_arch_test.cpp @@ -214,6 +214,70 @@ TEST(tp_projection_rounds_to_bf16_before_the_fp32_residual_add) { } } +TEST(tp2_refuses_without_the_exact_fp32_embedding_collective) { + MetaKV k = spark_meta(); + RadBuilder b; + b.refuse_dtype.insert({"all_reduce", "fp32"}); + RadBuildCtx ctx = build_ctx(); + ctx.world_size = 2; + CHECK_EQ(stderr_of_rc([&] { return spark25_bf16::declare(&b, k.meta(), &ctx); }), + RAD_E_UNSUPPORTED); + REQUIRE_EQ(count_op(b, "all_reduce"), 1); + const RecOp* embedding = find_op(b, "all_reduce"); + REQUIRE(embedding); + CHECK_EQ(pstr(embedding, "dtype"), std::string("fp32")); + CHECK_EQ(pint(embedding, "exact"), 1); + CHECK_EQ(pint(embedding, "world_size"), 2); + CHECK(!embedding->resolved); +} + +TEST(tp2_refuses_without_the_exact_bf16_projection_collective) { + MetaKV k = spark_meta(); + RadBuilder b; + b.refuse_dtype.insert({"all_reduce", "bf16"}); + RadBuildCtx ctx = build_ctx(); + ctx.world_size = 2; + CHECK_EQ(stderr_of_rc([&] { return spark25_bf16::declare(&b, k.meta(), &ctx); }), + RAD_E_UNSUPPORTED); + REQUIRE_EQ(count_op(b, "all_reduce"), 3); + const RecOp* embedding = find_op(b, "all_reduce"); + const RecOp* attn = find_op(b, "all_reduce", 1); + const RecOp* ffn = find_op(b, "all_reduce", 2); + REQUIRE(embedding && attn && ffn); + CHECK_EQ(pstr(embedding, "dtype"), std::string("fp32")); + CHECK(embedding->resolved); + CHECK_EQ(pstr(attn, "dtype"), std::string("bf16")); + CHECK_EQ(pstr(ffn, "dtype"), std::string("bf16")); + CHECK_EQ(pint(attn, "exact"), 1); + CHECK_EQ(pint(ffn, "exact"), 1); + CHECK_EQ(pint(attn, "world_size"), 2); + CHECK_EQ(pint(ffn, "world_size"), 2); + CHECK(!attn->resolved); + CHECK(!ffn->resolved); +} + +TEST(tp1_works_without_collectives) { + MetaKV k = spark_meta(); + RadBuilder b; + b.refuse.insert("all_reduce"); + RadBuildCtx ctx = build_ctx(); + REQUIRE_EQ(spark25_bf16::declare(&b, k.meta(), &ctx), RAD_OK); + CHECK_EQ(count_op(b, "all_reduce"), 0); + const auto& m = spark25_bf16::models[ctx.rank]; + CHECK_EQ(m.reduce_embed, (rad_op)0); + for (const auto& layer : m.layers) { + CHECK_EQ(layer.reduce_attn, (rad_op)0); + CHECK_EQ(layer.reduce_ffn, (rad_op)0); + } + RadBatch batch = decode_batch(); + RadCtx c{.b = &b, .batch = &batch}; + spark25_bf16::step(&c, &batch); + CHECK_LINT(b, &c); + CHECK(c.step_fail.empty()); + const auto issued = issued_ops(b, c); + CHECK(std::find(issued.begin(), issued.end(), "all_reduce") == issued.end()); +} + TEST(refuses_unimplemented_variants) { RadBuildCtx ctx = build_ctx(); for (const auto& change : {std::pair{"tie_word_embeddings", "0"},