Reuse BF16 add for HYV3 shared/routed combination

Replace the equivalent FP32 cast/add/cast chain with the existing BF16 add and enable the existing fused router for HYV3. Preserve separate tensor-parallel arm reductions and BF16 rounding boundaries; add the reference arithmetic check.
This commit is contained in:
Constantin authored and StillDeadcode committed 2026-10-27 18:28:46 +01:00
1 parent 492d33c470
commit bb48b14005
4 files changed
+53 -50

No files matched your search

+15 -10
View File
@@ -729,8 +729,13 @@ TEST(hyv3_fp8_declares_dense_then_sigmoid_moe_with_fp32_combine) {
CHECK(gather != nullptr);
CHECK_EQ(gather->writes.size(), 1u);
CHECK_EQ(gather->writes[0], moe.w.moe);
CHECK(moe.op_moe_to_f32 && moe.op_shared_to_f32 && moe.op_combine_f32 &&
moe.op_combined_to_bf16);
REQUIRE(moe.op_combine);
const RecOp* combine = b.op_of(moe.op_combine);
REQUIRE(combine);
CHECK_EQ(combine->op, std::string("add"));
CHECK_EQ(std::string(pstr(combine, "dtype")), std::string("bf16"));
CHECK(combine->reads == (std::vector<rad_buf>{moe.w.moe, moe.w.sout}));
CHECK(combine->writes == (std::vector<rad_buf>{moe.w.h.x}));
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch;
@@ -740,11 +745,12 @@ TEST(hyv3_fp8_declares_dense_then_sigmoid_moe_with_fp32_combine) {
if (run.issues[i].op == h) return (int)i;
return -1;
};
const int gather_i = issue_of(moe.op_gather), routed_i = issue_of(moe.op_moe_to_f32);
const int shared_i = issue_of(moe.op_shared_to_f32), add_i = issue_of(moe.op_combine_f32);
const int cast_i = issue_of(moe.op_combined_to_bf16);
CHECK(gather_i >= 0 && routed_i >= 0 && shared_i >= 0 && add_i >= 0 && cast_i >= 0);
CHECK(gather_i < routed_i && routed_i < add_i && shared_i < add_i && add_i < cast_i);
const int gather_i = issue_of(moe.op_gather), add_i = issue_of(moe.op_combine);
CHECK(gather_i >= 0 && add_i >= 0 && gather_i < add_i);
REQUIRE(moe.op_sigmoid_scatter);
CHECK(issue_of(moe.op_sigmoid_scatter) >= 0);
CHECK_EQ(issue_of(moe.op_scatter), -1);
CHECK_EQ(issue_of(moe.op_topk), -1);
const auto lint = lint_step(b, {&run});
CHECK(lint.empty());
}
@@ -778,9 +784,8 @@ TEST(hyv3_fp8_tensor_parallel_partitions_experts_and_shared_width) {
return -1;
};
CHECK(issue_of(moe.op_gather) < issue_of(moe.op_moe_ar));
CHECK(issue_of(moe.op_moe_ar) < issue_of(moe.op_moe_to_f32));
CHECK(issue_of(moe.op_shared_ar) < issue_of(moe.op_shared_to_f32));
CHECK(issue_of(moe.op_combine_f32) < issue_of(moe.op_combined_to_bf16));
CHECK(issue_of(moe.op_moe_ar) < issue_of(moe.op_combine));
CHECK(issue_of(moe.op_shared_ar) < issue_of(moe.op_combine));
CHECK(lint_step(b, {&run}).empty());
}
}
+23
View File
@@ -1298,6 +1298,29 @@ TEST(op_add_mul) {
CHECK_EQ(y.at(3), 204.0f);
}
/* HYV3's two already-rounded arms must be added in FP32, then narrowed once. BF16 add is
* exactly that, without materialising three FP32 scratch planes. Exercise every BF16 bit pattern,
* including subnormals, ties, signed zero and nonfinite values, against the former cast chain. */
TEST(bf16_add_matches_explicit_fp32_combine) {
constexpr int64_t M = 32, N = 2048;
T routed(RAD_BF16, {M, N}), shared(RAD_BF16, {M, N});
T rf(RAD_F32, {M, N}), sf(RAD_F32, {M, N}), sum(RAD_F32, {M, N});
T chain(RAD_BF16, {M, N}), folded(RAD_BF16, {M, N});
for (uint32_t i = 0; i < M * N; ++i) {
const uint16_t r = (uint16_t)i, s = (uint16_t)(i * 40503u + 73u);
std::memcpy(routed.mem.data() + i * 2, &r, 2);
std::memcpy(shared.mem.data() + i * 2, &s, 2);
}
A r; r.t(routed).t(rf).i("M", M).i("n", N).s("from", "bf16").s("to", "f32");
A s; s.t(shared).t(sf).i("M", M).i("n", N).s("from", "bf16").s("to", "f32");
A a; a.t(rf).t(sf).t(sum).i("M", M).i("n", N).s("dtype", "f32");
A c; c.t(sum).t(chain).i("M", M).i("n", N).s("from", "f32").s("to", "bf16");
A f; f.t(routed).t(shared).t(folded).i("M", M).i("n", N).s("dtype", "bf16");
CHECK_OK(run("cast", r)); CHECK_OK(run("cast", s));
CHECK_OK(run("add", a)); CHECK_OK(run("cast", c)); CHECK_OK(run("add", f));
CHECK_EQ(std::memcmp(chain.mem.data(), folded.mem.data(), M * N * 2), 0);
}
/* add's `div` divides the sum after its rounding to y's dtype: 1 + 0.01953125 rounds to bf16
* 1.015625 (a tie, to even), and 1.015625 / 3 to 0.337890625; the unrounded sum's third would be
* 0.33984375. In f32 the quotient is f32(4 / 3). */