Fail Spark TP declaration without exact collectives

This commit is contained in:
Constantin authored and StillDeadcode committed 2026-11-03 19:08:58 +01:00
1 parent a5b2f84450
commit 213403b458
3 files changed
+77 -1

No files matched your search

+8
View File
@@ -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();
}
+5 -1
View File
@@ -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<std::string> refuse;
std::set<std::pair<std::string, std::string>> refuse_dtype; /* (op, dtype) forms */
std::set<std::string> 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). */
+64
View File
@@ -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"},