diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index 7f3c9294..df0f0743 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -1,4 +1,5 @@ -/* gemma4_bf16.cpp -- Gemma 4 dense, bf16, with its vision tower (rad_block_gemma4_vision.h). +/* gemma4_bf16.cpp -- Gemma 4 dense, bf16, with its vision tower (rad_block_gemma4_vision.h) and, + * for E2B/E4B, its audio encoder (rad_block_gemma4_audio.h). * * Two attention classes over one residual stream, alternating by layer. A "sliding" layer is * sliding-window attention over head_dim 256 with a v projection and standard RoPE; a "global" @@ -15,6 +16,7 @@ * RedHatAI's gemma-4-31B-it-speculator.dflash2 (arch/common/rad_block_dflash2.h; 7 a pass). */ #include +#include #include #include #include @@ -1765,9 +1767,11 @@ struct Model { Gemma4Head head{}; bool have_head = false; - /* The vision tower (rad_block_gemma4_vision.h), declared when the container carries one and - * the deployment serves media; `op_mm` writes its rows over the placeholders' embeddings. */ + /* The vision tower and the audio encoder, each declared when the container carries it and + * the deployment serves that medium; `op_mm` writes their rows over the placeholders' + * embeddings. */ Gemma4Vision vis{}; + Gemma4Audio aud{}; rad_op op_mm = 0; G4Junction j0{}; /* the first block's entry: norm + quantiser of the embedding stream */ @@ -2221,11 +2225,12 @@ 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 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) + /* Media rows (an image's or an audio clip's) overwrite their 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 media + * features in unscaled. */ + if ((Gemma4Vision::present(meta) && ctx->max_enc_patches > 0) || + (Gemma4Audio::present(meta) && ctx->max_audio_frames > 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), RAD_STR("dtype", "bf16")), @@ -2607,7 +2612,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c /* ---- the vision tower, its weight groups past the trunk's and the draft model's: the * assistant's layers and its exit, or the DFlash drafter's layers. They are counted even when - * the draft model is not declared, so the groups do not move with max_spec. */ + * the draft model is not declared, so the groups do not move with max_spec. The audio + * encoder's groups follow the tower's. */ { int64_t head_groups = 0; if (has_assistant(meta)) @@ -2621,6 +2627,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c } RAD_ARCH_TRY(m.vis.declare(b, meta, ctx, nm, g.n_embd, (int)(m.n_main + head_groups), "model.", "gemma4_bf16")); + RAD_ARCH_TRY(m.aud.declare(b, meta, ctx, nm, g.n_embd, + (int)(m.n_main + head_groups) + (int)m.vis.L.size(), "model.", + "gemma4_bf16")); } rad_note(b, "rank %d/%d: %lld layers (%lld global, %lld sliding), sliding window %lld, " @@ -2846,9 +2855,10 @@ static void trunk_overlap(RadCtx* c, const Model& m, const RadBatch* batch, int6 static void step(RadCtx* c, const RadBatch* batch) { Model& m = g_model[rad_rank(c)]; - /* An encoder pass runs only the tower. */ + /* An encoder pass runs only its encoder. */ if (batch->enc) { - m.vis.step(c, batch); + if (batch->enc == RAD_MM_AUDIO) m.aud.step(c, batch); + else m.vis.step(c, batch); return; } /* A draft pass runs only the draft model. The trunk already left its final-normed hidden state @@ -2988,12 +2998,13 @@ static int probe(const RadModelMeta* meta, RadArchProbe* out) { const int64_t k = rad_meta_geti(meta, "vision_config.pooling_kernel_size", 0); if (k > 0) out->enc_merge = k * k; } + if (Gemma4Audio::present(meta)) out->audio_frames = Gemma4Audio::frames_for(meta); return RAD_OK; } #ifndef RAD_ARCH_NO_EXPORTS RAD_ARCH_PLUGIN(gemma4_bf16, "gemma4", "compressed-tensors", "0.1.0", - "Gemma 4 dense (text and images), w4a8 or w8a8 from its bf16 or its CT-INT4 (QAT) " + "Gemma 4 dense (text, images and E2B/E4B audio), w4a8 or w8a8 from its bf16 or its CT-INT4 (QAT) " "release: sliding-window and proportional-RoPE global attention over a plain " "RMSNorm stream with GELU feed-forward, per-layer scalar scaling and soft-capped " "logits; Google's assistant head, z-lab's DFlash drafter or a DFlash2 drafter") diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index ea4f8d20..3b81ddea 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -8683,6 +8683,57 @@ TEST(embgemma2_text_and_media_passes_lint_clean_and_feed_gemms_whole_rows) { } } +/* Gemma 4 E4B with its audio encoder: the conformer EmbeddingGemma 2 declares + * (rad_block_gemma4_audio.h), read under the conditional-generation checkpoint's `model.` prefix, + * its mel bins from processor_config.json's feature_extractor. Its rows go through the same media + * scatter as an image's, the pass lints clean, and the probe states 30 s of frames. */ +TEST(gemma4_e4b_declares_its_audio_encoder) { + std::vector keys(std::begin(k_gem4e_keys), std::end(k_gem4e_keys)); + std::vector vals(std::begin(k_gem4e_vals), std::end(k_gem4e_vals)); + const char* ak[] = { "audio_config.model_type", "audio_config.hidden_size", + "audio_config.num_attention_heads", "audio_config.output_proj_dims", + "audio_config.attention_chunk_size", "audio_config.attention_context_left", + "audio_config.attention_context_right", "audio_config.attention_logit_cap", + "audio_config.num_hidden_layers", "audio_config.conv_kernel_size", + "audio_config.residual_weight", "audio_config.hidden_act", + "audio_config.subsampling_conv_channels", + "processor.feature_extractor.feature_size" }; + const char* av[] = { "gemma4_audio", "1024", "8", "1536", "12", "13", "0", "50", "2", "5", "0.5", + "silu", "128 32", "128" }; + keys.insert(keys.end(), std::begin(ak), std::end(ak)); + vals.insert(vals.end(), std::begin(av), std::end(av)); + RadModelMeta meta = gemma4_e4b_meta(); + meta.n_kv = (int)keys.size(); meta.kv_key = keys.data(); meta.kv_val = vals.data(); + RadBuilder b; + RadBuildCtx ctx = build_ctx(); + ctx.max_audio_frames = 400; + REQUIRE_EQ(gemma4_bf16::declare(&b, &meta, &ctx), RAD_OK); + CHECK_EQ(b.audio_encoder_name, std::string("audio")); + CHECK_EQ(b.audio_encoder.max_patches, 400); + CHECK_EQ(b.audio_encoder.merge, 4); + CHECK_EQ(b.audio_encoder.n_embd, meta.n_embd); + const RecMap* emb = find_map(b, "a.embed"); + REQUIRE(emb != nullptr); + CHECK(emb->src == std::vector{ "model.embed_audio.embedding_projection.weight" }); + const RecMap* c0 = find_map(b, "a.sscp.c0"); + REQUIRE(c0 != nullptr); + CHECK(c0->src == std::vector{ + "model.audio_tower.subsample_conv_projection.layer0.conv.weight" }); + /* the media scatter, and the per-layer inputs' pad rows over the same positions */ + CHECK_EQ(count_op(b, "scatter_rows"), 2); + + const auto aud = enc_pass(RAD_MM_AUDIO, 400, 128); + RadCtx c{ .b = &b, .batch = &aud->b }; + gemma4_bf16::step(&c, &aud->b); + CHECK(c.step_fail.empty()); + CHECK(!c.issues.empty()); + CHECK_LINT(b, &c); + + RadArchProbe pr{}; + REQUIRE_EQ(gemma4_bf16::probe(&meta, &pr), RAD_OK); + CHECK_EQ(pr.audio_frames, 3000); +} + /* ==================================================================== qwenimage21 */ namespace {