mm: Gemma 4 12B's pictures and audio

The 12B (gemma4_unified) has no tower: its embedder takes a whole
48 x 48 pooling window as one patch, so a patch is a row (merge 1),
its values u / 255. Its audio is frames of 640 samples as they are,
one frame a token, the last zero-padded. Goldens from transformers.
This commit is contained in:
keptn42 committed 2026-11-04 07:20:29 +01:00
1 parent 9782a9afb5
commit a00e5e7094
6 files changed
+406 -9

No files matched your search

+7 -1
View File
@@ -3205,7 +3205,13 @@ int Engine::run_server() {
return rc;
}
o.allow_audio = true;
if (mm::decoder_available())
if (mm::decoder_available() && ac.family == mm::AudioFamily::Gemma4Unified)
RAD_INFO("media: audio through '%s'; a clip is cut at %.3g s, a token is a frame of %d "
"samples (%d ms); %lld frames a pass",
ranks_[0]->program.audio_encoder_name.c_str(),
(double)mm_proc->audio_config().max_samples / (double)ac.rate, ac.frame_length,
ac.frame_length * 1000 / ac.rate, (long long)aend.max_patches);
else if (mm::decoder_available())
RAD_INFO("media: audio through '%s'; a clip is cut at %.3g s, %d mel bins a %d ms frame, "
"%d frames a token; %lld frames a pass",
ranks_[0]->program.audio_encoder_name.c_str(),
+82 -7
View File
@@ -244,10 +244,16 @@ int VisionConfig::from_meta(const RadModelMeta& m, std::string* why) {
}
return RAD_OK;
}
if (mt && !std::strcmp(mt, "gemma4_vision")) {
/* Gemma 4 12B ("unified") has no tower: its embedder takes a whole 48 x 48 pooling window as
* one patch, so a patch is a row (merge 1), and its values are u / 255. The sizes are the
* same: a window is 48 pixels a side either way. */
const bool unified = mt && !std::strcmp(mt, "gemma4_unified_vision");
if (mt && (!std::strcmp(mt, "gemma4_vision") || unified)) {
family = Family::Gemma4;
patch = (int32_t)rad_meta_geti(&m, "vision_config.patch_size", 0);
merge = (int32_t)rad_meta_geti(&m, "vision_config.pooling_kernel_size", 0);
unit_pixels = unified;
patch = (int32_t)rad_meta_geti(&m, unified ? "vision_config.model_patch_size"
: "vision_config.patch_size", 0);
merge = unified ? 1 : (int32_t)rad_meta_geti(&m, "vision_config.pooling_kernel_size", 0);
temporal = 1;
if (patch <= 0 || merge <= 0) {
if (why) *why = "the container states no vision tower geometry (vision_config."
@@ -267,7 +273,8 @@ int VisionConfig::from_meta(const RadModelMeta& m, std::string* why) {
image_soft_tokens = (int32_t)rad_meta_geti(
&m, "processor.image_processor.max_soft_tokens",
rad_meta_geti(&m, "vision_soft_tokens_per_image",
rad_meta_geti(&m, "vision_config.default_output_length", 280)));
rad_meta_geti(&m, "vision_config.default_output_length",
rad_meta_geti(&m, "vision_config.num_soft_tokens", 280))));
video_soft_tokens = (int32_t)rad_meta_geti(&m, "processor.video_processor.max_soft_tokens",
70);
fps = rad_meta_getf(&m, "processor.video_processor.fps", 1.0);
@@ -1071,7 +1078,8 @@ void patchify(const VisionConfig& c, const std::vector<const Frame*>& frames, It
* processor's rescale u * (1/255). The processors AutoProcessor loads take that product in f32 for
* pictures (Gemma4ImageProcessor) and in double, rounded to f32, for video (Gemma 4's and
* EmbeddingGemma 2's video processors); `video` picks which, and the two differ for u = 127. One
* frame per segment. */
* frame per segment. With `unit_pixels` (the 12B, merge 1: a row is one 48 x 48 window) the value
* is p itself, rounded to bf16. */
void patchify_gemma4(const VisionConfig& c, const std::vector<const Frame*>& frames, bool video,
Item* it) {
const int P = c.patch, M = c.merge;
@@ -1080,7 +1088,7 @@ void patchify_gemma4(const VisionConfig& c, const std::vector<const Frame*>& fra
for (int v = 0; v < 256; ++v) {
const float p = video ? (float)((double)v * (1.0 / 255.0))
: (float)v * (float)(1.0 / 255.0);
lut[v] = rad_f32_to_bf16(2.0f * (p - 0.5f));
lut[v] = rad_f32_to_bf16(c.unit_pixels ? p : 2.0f * (p - 0.5f));
}
const int64_t dim = it->patch_dim;
it->pixels.assign((size_t)it->n_patch * (size_t)dim, 0);
@@ -2131,12 +2139,40 @@ static int qwen3asr_from_meta(const RadModelMeta& m, AudioConfig* c, std::string
return RAD_OK;
}
/* Gemma4UnifiedAudioFeatureExtractor's framing (Gemma 4 12B): processor_config.json's
* `feature_extractor` and the clip cap `audio_seq_length` (in frames). The frame's width is what the
* embedder takes, so it stands in `n_mels` as a mel spectrum's bins do. */
static int unified_from_meta(const RadModelMeta& m, AudioConfig* c, std::string* why) {
c->family = AudioFamily::Gemma4Unified;
c->rate = (int32_t)rad_meta_geti(&m, "processor.feature_extractor.sampling_rate", 16000);
c->frame_length = (int32_t)rad_meta_geti(
&m, "processor.feature_extractor.audio_samples_per_token",
rad_meta_geti(&m, "audio_config.audio_samples_per_token",
rad_meta_geti(&m, "audio_config.audio_embed_dim", 0)));
c->hop = c->frame_length;
c->n_mels = c->frame_length;
c->max_samples = rad_meta_geti(&m, "processor.audio_seq_length", 750) * c->frame_length;
c->tok_audio = (int32_t)rad_meta_geti(&m, "audio_token_id", -1);
c->tok_start = (int32_t)rad_meta_geti(&m, "boa_token_id", -1);
c->tok_end = (int32_t)rad_meta_geti(&m, "eoa_token_id",
rad_meta_geti(&m, "eoa_token_index", -1));
if (c->rate <= 0 || c->frame_length <= 0 || c->max_samples <= 0 || c->tok_audio < 0 ||
c->tok_start < 0 || c->tok_end < 0) {
if (why) *why = "the container's audio framing is incomplete (sampling_rate, "
"audio_samples_per_token, audio_seq_length, audio_token_id, boa_token_id, "
"eoa_token_id)";
return RAD_E_INVAL;
}
return RAD_OK;
}
int AudioConfig::from_meta(const RadModelMeta& m, std::string* why) {
const char* mt = rad_meta_gets(&m, "audio_config.model_type", "");
if (mt && !std::strcmp(mt, "qwen3_asr_encoder")) return qwen3asr_from_meta(m, this, why);
if (mt && !std::strcmp(mt, "gemma4_unified_audio")) return unified_from_meta(m, this, why);
if (!mt || std::strcmp(mt, "gemma4_audio")) {
if (why) *why = "the container states no audio tower this processor knows (audio_config."
"model_type gemma4_audio or qwen3_asr_encoder)";
"model_type gemma4_audio, gemma4_unified_audio or qwen3_asr_encoder)";
return RAD_E_NOTFOUND;
}
/* preprocessor_config.json's keys (`preprocessor.`), else processor_config.json's
@@ -2273,6 +2309,12 @@ int Processor::init_audio(const AudioConfig& cfg, std::string* why) {
return refuse(RAD_E_INVAL, "the audio encoder's pass (" + std::to_string(cfg.max_frames) +
" frames) is not whole rows of " +
std::to_string(cfg.merge) + " frames");
/* The 12B's frames are samples: no window, no FFT, no mel bank. */
if (cfg.family == AudioFamily::Gemma4Unified) {
acfg_ = cfg;
audio_on_ = true;
return RAD_OK;
}
const bool qwen = cfg.family == AudioFamily::Qwen3ASR;
/* Gemma 4's FFT is radix 2; Qwen3-ASR's runs mixed radix over any length. */
if (!qwen && (cfg.fft < 2 || (cfg.fft & (cfg.fft - 1)) != 0))
@@ -2481,11 +2523,44 @@ int Processor::audio_qwen3asr(const std::vector<float>& samples, std::shared_ptr
return RAD_OK;
}
/* The 12B's clip (AudioFamily::Gemma4Unified): ceil(n / frame_length) frames of samples, the last
* zero-padded, each sample rounded to bf16 as the reference casts the frames for its embedder; one
* frame a placeholder. The reference cuts nothing; a clip past `max_samples` is cut there. */
int Processor::audio_unified(const std::vector<float>& samples, std::shared_ptr<Item>* out,
std::string* why) const {
const AudioConfig& c = acfg_;
const int64_t n = std::min<int64_t>((int64_t)samples.size(), c.max_samples);
const int64_t fl = c.frame_length, rows = (n + fl - 1) / fl;
if (rows * c.merge > c.max_frames) {
if (why) *why = "the clip is more frames than one audio encoder pass holds";
return RAD_E_INVAL;
}
auto it = std::make_shared<Item>();
it->kind = Kind::Audio;
it->grid_t = 1;
it->grid_h = (int32_t)rows;
it->grid_w = 1;
it->patch_dim = fl;
it->n_patch = rows;
it->valid = (int32_t)rows;
it->rows_h = (int32_t)rows;
it->rows_w = 1;
it->seg_rows = (int32_t)rows;
it->n_seg = rows > 0 ? 1 : 0;
it->pixels.assign((size_t)(rows * fl), 0);
for (int64_t i = 0; i < n; ++i) it->pixels[(size_t)i] = rad_f32_to_bf16(samples[(size_t)i]);
one_run(it.get(), c.tok_start, rows, c.tok_audio, c.tok_end);
if (rows == 0) it->encoded = true;
*out = std::move(it);
return RAD_OK;
}
int Processor::audio_from(const std::vector<float>& samples, std::shared_ptr<Item>* out,
std::string* why) const {
if (!audio_on_) { if (why) *why = "this program declares no audio encoder"; return RAD_E_UNSUPPORTED; }
const AudioConfig& c = acfg_;
if (c.family == AudioFamily::Qwen3ASR) return audio_qwen3asr(samples, out, why);
if (c.family == AudioFamily::Gemma4Unified) return audio_unified(samples, out, why);
const int64_t n = std::min<int64_t>((int64_t)samples.size(), c.max_samples);
const int64_t real = c.real_frames(n);
const int64_t rows = (real + c.merge - 1) / c.merge;
+10 -1
View File
@@ -50,6 +50,8 @@ enum class Kind { Image, Video, Audio };
* channel) as 2 * (u / 255) - 1, the reference model's own input scaling; a video is
* frames sampled at `fps` (at most `max_frames`, thinned evenly), each its own segment
* at the per-frame budget, each spelled <boi> placeholders <eoi> with no timestamp.
* Gemma 4 12B ("unified", no tower) is the same with a whole pooling window as one
* patch (patch = 48, merge 1) and its values u / 255 (`unit_pixels`).
* Lfm2 LFM2-VL's: a picture within its token budget (times a tolerance) is resized whole --
* sides multiples of patch * merge, pixels within the budget's band -- and one past it
* is cut into tile x tile crops, min_tiles..max_tiles of them in the ratio nearest its
@@ -88,6 +90,8 @@ struct VisionConfig {
/* Gemma4: soft tokens (output rows) an image and a video frame may take. One of 70, 140,
* 280, 560, 1120, the budgets the model was trained at. */
int32_t image_soft_tokens = 0, video_soft_tokens = 0;
/* Gemma4: patch values u / 255 rather than 2 * (u / 255) - 1 (the 12B's embedder). */
bool unit_pixels = false;
int64_t image_min_pixels = 0, image_max_pixels = 0;
int64_t video_min_pixels = 0, video_max_pixels = 0;
/* Glm5Next: the band an image's canvas falls in, in output rows (tokens). */
@@ -165,6 +169,9 @@ struct ImageMode {
* through an fft-point FFT, magnitudes through an HTK mel bank, log(x + mel_floor).
* The clip's real frames, then zero frames to whole output rows.
*
* Gemma4Unified Gemma4UnifiedAudioFeatureExtractor (the 12B's): no spectrum; a frame is
* `frame_length` samples as they are, the last zero-padded, one frame a row.
*
* Qwen3ASR Qwen3ASRFeatureExtractor (Whisper's): a clip shorter than `min_samples` is
* zero-padded to it; frames are torch.stft's, centred with a reflected n_fft / 2 at
* each end, a periodic Hann window and power spectra, the last frame dropped, so n
@@ -176,7 +183,7 @@ struct ImageMode {
* padding, and, where that exceeds a whole chunk, frames past it that the encoder
* reads as zero. Plane 1 of the coordinates marks those (RadBatch::enc_coord), which
* keeps one chunk's convolution from seeing the next. */
enum class AudioFamily { Gemma4, Qwen3ASR };
enum class AudioFamily { Gemma4, Qwen3ASR, Gemma4Unified };
/* Audio features, configured from the container: Gemma 4's framing from `preprocessor.*` (the
* checkpoint's preprocessor_config.json), Qwen3-ASR's from `processor.feature_extractor.*`
@@ -434,6 +441,8 @@ private:
bool audio_on_ = false;
int audio_qwen3asr(const std::vector<float>& samples, std::shared_ptr<Item>* out,
std::string* why) const;
int audio_unified(const std::vector<float>& samples, std::shared_ptr<Item>* out,
std::string* why) const;
std::vector<float> hann_; /* the window, float32 as the reference stores it */
std::vector<double> mel_; /* [n_mels, fft/2 + 1]: filter b's weight on bin k */