From d650eca84009b6658f1aaed31808863260a9c428 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sun, 25 Oct 2026 12:38:34 +0100 Subject: [PATCH] gemma4_bf16: image rows before the embedding all-reduce RadBatch::mm_embd holds the image rows at rank 0 and zeros at every other rank. The rows went in after the all-reduce of the token lookup, so at more than one rank every rank past 0 wrote zeros over its image rows, and the ranks ran on different residual streams from layer 0 on. --- arch/gemma4_bf16/gemma4_bf16.cpp | 14 ++++++----- tests/arch_test.cpp | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index f6ede317..6588125d 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -2180,9 +2180,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_STR("dtype", g.dtype), RAD_INT("exact", 1)), RAD_NOWEIGHTS), {m.b_x}, {m.b_x}); - /* An image's rows overwrite its placeholders' embeddings after the (all-reduced) lookup and - * before the first block: the reference scales token embeddings by sqrt(n_embd) and scatters - * the image features in unscaled. */ + /* An image's rows overwrite its placeholders' embeddings after the lookup and before its + * all-reduce (RadBatch::mm_embd: rank 0 holds the rows, every other rank zeros). The + * reference scales token embeddings by sqrt(n_embd) and scatters the image features in + * unscaled. */ if (Gemma4Vision::present(meta) && ctx->max_enc_patches > 0) m.op_mm = rw(b, RAD_OP(b, "scatter_rows", RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), @@ -2833,13 +2834,14 @@ static void step(RadCtx* c, const RadBatch* batch) { RAD_ISSUE(c, m.op_embed, praw(batch->token_ids, RAD_I32, batch->n_tok), RAD_W(m.w_tok), brows(m.b_x, batch->n_tok)); - if (m.op_embed_ar) - RAD_ISSUE_N(c, m.op_embed_ar, (int64_t)batch->n_tok * m.g.n_embd, - brows(m.b_x, batch->n_tok), RAD_NONE); + /* Before the all-reduce: after it, every rank past 0 would write zeros over its image rows. */ if (m.op_mm && batch->n_mm_rows > 0) RAD_ISSUE_N(c, m.op_mm, batch->n_mm_rows, praw2(batch->mm_embd, RAD_BF16, batch->n_mm_rows, m.g.n_embd), praw(batch->mm_rows, RAD_I32, batch->n_mm_rows), brows(m.b_x, batch->n_tok)); + if (m.op_embed_ar) + RAD_ISSUE_N(c, m.op_embed_ar, (int64_t)batch->n_tok * m.g.n_embd, + brows(m.b_x, batch->n_tok), RAD_NONE); const int64_t T = batch->n_tok; if (m.n_ple > 0) { diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 1231d7df..8003cba5 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -3791,6 +3791,47 @@ TEST(gemma4_declares_two_kv_groups_and_binds_the_schedule) { } } +/* A step with an image: the rows go into the token lookup before its all-reduce, since + * RadBatch::mm_embd holds them at rank 0 and zeros at every other rank. */ +TEST(gemma4_scatters_image_rows_into_the_lookup_before_its_all_reduce) { + std::vector keys(std::begin(k_gem4_keys), std::end(k_gem4_keys)); + std::vector vals(std::begin(k_gem4_vals), std::end(k_gem4_vals)); + const char* vk[] = { "vision_config.model_type", "vision_config.hidden_size", + "vision_config.num_attention_heads", "vision_config.head_dim", + "vision_config.intermediate_size", "vision_config.pooling_kernel_size", + "vision_config.position_embedding_size", "vision_config.num_hidden_layers", + "vision_config.patch_size", "vision_config.rope_parameters.rope_theta", + "vision_config.rope_parameters.rope_type", "vision_config.hidden_activation" }; + const char* vv[] = { "gemma4_vision", "768", "12", "64", "3072", "3", "10240", "2", "16", "100", + "default", "gelu_pytorch_tanh" }; + keys.insert(keys.end(), std::begin(vk), std::end(vk)); + vals.insert(vals.end(), std::begin(vv), std::end(vv)); + static const int32_t rows[] = { 4, 5, 6, 7, 8, 9, 10, 11 }; + static uint16_t embd[8 * 5376]; + for (int world : { 1, 2 }) { + for (int rank = 0; rank < world; ++rank) { + RadBuilder b; + RadModelMeta meta = gemma4_meta(); + meta.n_kv = (int)keys.size(); meta.kv_key = keys.data(); meta.kv_val = vals.data(); + RadBuildCtx ctx = build_ctx(rank, world); + ctx.max_enc_patches = 9 * 64; + CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx)); + RadBatch batch = prefill_batch(); + batch.n_mm_rows = 8; batch.mm_rows = rows; batch.mm_embd = embd; + RadCtx c; c.b = &b; c.rank = rank; c.world = world; c.batch = &batch; + gemma4_bf16::step(&c, &batch); + CHECK_LINT(b, &c); + CHECK(c.step_fail.empty()); + const std::vector ops = issued_ops(b, c); + REQUIRE(ops.size() > 3); + CHECK_EQ(ops[0], std::string("embed_lookup")); + CHECK_EQ(ops[1], std::string("scatter_rows")); + CHECK(c.issues[1].opd[0].raw == embd); + if (world == 2) CHECK_EQ(ops[2], std::string("all_reduce")); + } + } +} + /* The E2B/E4B form on the same twelve layers: the last four share KV (no cache of their own), * per-layer embeddings 64 wide, a global class with its own v (attention_k_eq_v 0) and no * num_global_key_value_heads, so the global layers take num_key_value_heads. */