From c5d61eafbbfb9bd1ad24bf4b87a5293beb4d0734 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sun, 25 Oct 2026 20:54:21 +0100 Subject: [PATCH] mm: Gemma 4 audio settings from processor_config.json's feature_extractor Gemma 4 E2B/E4B keep their feature extractor settings there, not in a preprocessor_config.json. AudioConfig reads preprocessor.* first, as before, then processor.feature_extractor.*. --- core/mm/processor.cpp | 39 +++++++++++++++++++++++---------------- tests/mm_test.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/core/mm/processor.cpp b/core/mm/processor.cpp index f6d002ad..4960668f 100644 --- a/core/mm/processor.cpp +++ b/core/mm/processor.cpp @@ -1891,32 +1891,39 @@ int AudioConfig::from_meta(const RadModelMeta& m, std::string* why) { "model_type gemma4_audio or qwen3_asr_encoder)"; return RAD_E_NOTFOUND; } - rate = (int32_t)rad_meta_geti(&m, "preprocessor.sampling_rate", 16000); - frame_length = (int32_t)rad_meta_geti(&m, "preprocessor.frame_length", 320); - hop = (int32_t)rad_meta_geti(&m, "preprocessor.hop_length", 160); - n_mels = (int32_t)rad_meta_geti(&m, "preprocessor.feature_size", 128); - fft = (int32_t)rad_meta_geti(&m, "preprocessor.fft_length", 0); + /* preprocessor_config.json's keys (`preprocessor.`), else processor_config.json's + * `feature_extractor` object, where Gemma 4 E2B/E4B keep them. */ + auto key = [&m](const char* k) { + std::string pp = std::string("preprocessor.") + k; + if (rad_meta_gets(&m, pp.c_str(), nullptr)) return pp; + return std::string("processor.feature_extractor.") + k; + }; + rate = (int32_t)rad_meta_geti(&m, key("sampling_rate").c_str(), 16000); + frame_length = (int32_t)rad_meta_geti(&m, key("frame_length").c_str(), 320); + hop = (int32_t)rad_meta_geti(&m, key("hop_length").c_str(), 160); + n_mels = (int32_t)rad_meta_geti(&m, key("feature_size").c_str(), 128); + fft = (int32_t)rad_meta_geti(&m, key("fft_length").c_str(), 0); if (fft <= 0) { fft = 1; while (fft < frame_length) fft *= 2; } - if (rad_meta_geti(&m, "preprocessor.fft_overdrive", 0)) fft *= 2; - min_freq = rad_meta_getf(&m, "preprocessor.min_frequency", 0.0); - max_freq = rad_meta_getf(&m, "preprocessor.max_frequency", 8000.0); - mel_floor = rad_meta_getf(&m, "preprocessor.mel_floor", 1e-3); - preemphasis = rad_meta_getf(&m, "preprocessor.preemphasis", 0.0); - preemphasis_htk = rad_meta_geti(&m, "preprocessor.preemphasis_htk_flavor", 1) != 0; - input_scale = rad_meta_getf(&m, "preprocessor.input_scale_factor", 1.0); - bin_mean = read_list(m, "preprocessor.per_bin_mean"); - bin_std = read_list(m, "preprocessor.per_bin_stddev"); + if (rad_meta_geti(&m, key("fft_overdrive").c_str(), 0)) fft *= 2; + min_freq = rad_meta_getf(&m, key("min_frequency").c_str(), 0.0); + max_freq = rad_meta_getf(&m, key("max_frequency").c_str(), 8000.0); + mel_floor = rad_meta_getf(&m, key("mel_floor").c_str(), 1e-3); + preemphasis = rad_meta_getf(&m, key("preemphasis").c_str(), 0.0); + preemphasis_htk = rad_meta_geti(&m, key("preemphasis_htk_flavor").c_str(), 1) != 0; + input_scale = rad_meta_getf(&m, key("input_scale_factor").c_str(), 1.0); + bin_mean = read_list(m, key("per_bin_mean").c_str()); + bin_std = read_list(m, key("per_bin_stddev").c_str()); max_samples = 480000; tok_audio = (int32_t)rad_meta_geti(&m, "audio_token_id", -1); tok_start = (int32_t)rad_meta_geti(&m, "boa_token_id", -1); tok_end = (int32_t)rad_meta_geti(&m, "eoa_token_id", rad_meta_geti(&m, "eoa_token_index", -1)); /* Dither adds Gaussian noise of this deviation to every sample, a new draw each call, so no * deterministic front end can reproduce it. */ - if (rad_meta_getf(&m, "preprocessor.dither", 0.0) != 0.0) { - if (why) *why = "the feature extractor dithers its input (preprocessor.dither), which no " + if (rad_meta_getf(&m, key("dither").c_str(), 0.0) != 0.0) { + if (why) *why = "the feature extractor dithers its input (dither), which no " "deterministic front end reproduces"; return RAD_E_UNSUPPORTED; } diff --git a/tests/mm_test.cpp b/tests/mm_test.cpp index 908a1afb..8109c7a2 100644 --- a/tests/mm_test.cpp +++ b/tests/mm_test.cpp @@ -1427,6 +1427,41 @@ TEST(each_audio_front_end_runs_its_fft_at_any_length_it_takes) { CHECK(!p.has_audio()); } +/* Gemma 4's feature extractor settings come from preprocessor_config.json (`preprocessor.`, as + * EmbeddingGemma 2 ships them) or from processor_config.json's `feature_extractor` object (as + * Gemma 4 E2B/E4B ship them); where both are present, the first wins. */ +TEST(gemma4_audio_settings_come_from_either_processor_file) { + const char* keys[] = { "audio_config.model_type", "audio_token_id", "boa_token_id", + "eoa_token_id", "processor.feature_extractor.sampling_rate", + "processor.feature_extractor.frame_length", + "processor.feature_extractor.hop_length", + "processor.feature_extractor.feature_size", + "processor.feature_extractor.fft_length", + "processor.feature_extractor.mel_floor", + "processor.feature_extractor.dither", "preprocessor.hop_length" }; + const char* vals[] = { "gemma4_audio", "258881", "256000", "258883", "16000", "320", "160", + "128", "1024", "0.002", "0", "200" }; + RadModelMeta m{}; + m.n_kv = (int)(sizeof keys / sizeof keys[0]) - 1; /* without preprocessor.hop_length */ + m.kv_key = keys; + m.kv_val = vals; + mm::AudioConfig c; + std::string why; + REQUIRE_EQ(c.from_meta(m, &why), RAD_OK); + CHECK_EQ(c.fft, 1024); + CHECK_EQ(c.hop, 160); + CHECK_NEAR(c.mel_floor, 0.002, 1e-12); + m.n_kv += 1; + mm::AudioConfig d; + REQUIRE_EQ(d.from_meta(m, &why), RAD_OK); + CHECK_EQ(d.hop, 200); + + /* a dithering extractor is refused from either file */ + vals[10] = "1e-5"; + mm::AudioConfig e; + CHECK_EQ(e.from_meta(m, &why), RAD_E_UNSUPPORTED); +} + /* A whole chunk keeps 13 rows: three convolutions of kernel 3, stride 2, padding 1 -- and the * chunk shorter than that keeps what the reference's _post_cnn_length gives it. */ TEST(a_chunk_keeps_the_rows_three_convolutions_leave) {