Skip to content

Gemma4Unified (12B): audio and video input in the processor - #400

Open
fdagostino wants to merge 1 commit into
ml-explore:mainfrom
fdagostino:feat/gemma4-unified-audio-video
Open

fdagostino wants to merge 1 commit into
ml-explore:mainfrom
fdagostino:feat/gemma4-unified-audio-video

Conversation

@fdagostino

Copy link
Copy Markdown
Contributor

What

Completes multimodal input for the encoder-free gemma4_unified (12B) models. The Gemma4Unified model class already projects and scatters audio/video features (embed_audio, getInputEmbeddings handle input.audio / input.video), but Gemma4UnifiedProcessor only produced image inputs, so audio and video never reached the model. This is the follow-up noted in #391 ("the encoder-free Gemma4Unified (12B) processor video block"), plus the audio path.

Audio (encoder-free — no mel, no conformer)

Unlike the gemma4 E-series (whose audio tower is ported in #392), the unified models project raw waveform frames directly into the LLM embedding space:

  • The 16 kHz waveform is zero-padded to a multiple of audio_samples_per_token (640 = 40 ms) and reshaped into [1, T, 640] frames — each frame is the feature vector, one soft token per frame (25 tokens/s).
  • Clips are truncated to audio_seq_length tokens (750 = 30 s) so the placeholder count always matches the feature rows the model scatters. (The Python processor caps placeholders but does not truncate features; truncating the waveform keeps the two consistent.)
  • Each <|audio|> placeholder expands to boa + <|audio|> × N + eoa, N = ceil(samples / 640) capped at 750.
  • Multiple clips are batched to the longest with the validity mask false on padding (mask semantics: true = valid, used as-is — the unified path does not invert it like the conformer path).

Matches HF Gemma4UnifiedAudioFeatureExtractor / Gemma4UnifiedProcessor and mlx-vlm gemma4_unified (algorithm cross-checked against both, plus the mlx-community/gemma-4-12B-it-4bit checkpoint config/tokenizer files).

Video

  • Samples up to video_max_frames frames (~1 fps via MediaProcessing.asProcessedSequence, matching the gemma4 processor in Add end-to-end video input to the base Gemma 4 (gemma4) VLM #391).
  • Each frame goes through the same aspect-preserving resize + patchify pipeline as images, but with the per-frame vision_soft_tokens_per_video_frame budget (70) instead of 280; per-frame padding uses -1 position ids, which the model already strips before scattering.
  • Each <|video|> placeholder expands into per-frame mm:ss {boi}{<|video|> × N}{eoi} blocks joined by spaces, matching the HF/mlx-vlm processors (timestamps from the actual frame times). The timestamp text is tokenized in isolation, so token merges at the leading text boundary can differ slightly from tokenizing the fully expanded prompt string.

Config

Gemma4UnifiedProcessorConfiguration now decodes: boa_token_id / eoa_token_id (including the eoa_token_index spelling used by the released checkpoints), audio sampling rate and samples-per-token from the nested feature_extractor block of processor_config.json (whose mel-related keys are vestigial and ignored, as in the Python implementations), and the video budget/frame-count keys.

Gemma4MessageGenerator additionally emits {"type": "audio"} content parts (3 lines, also part of #392 — trivial to resolve whichever lands first).

Tests

9 new tests in Gemma4UnifiedAudioVideoTests: config decoding against the real processor_config.json shape, audio framing parity with the reference algorithm (exact values incl. tail zero-padding), the 30 s cap, batch padding/masking, a processor→model round trip that would throw on any placeholder/feature count mismatch, per-frame video budget + -1 position padding, mm:ss formatting, and an end-to-end tiny-model video scatter. xcodebuild test on this branch passes.

🤖 Generated with Claude Code

The Gemma4Unified model already projects and scatters audio/video features
(embed_audio, getInputEmbeddings), but Gemma4UnifiedProcessor only handled
images. This completes the processor so the 12B accepts audio and video:

- Audio (encoder-free): the raw 16 kHz waveform is zero-padded to a multiple
  of audio_samples_per_token (640 = 40 ms) and reshaped into [1, T, 640]
  frames — each frame IS the feature vector, one soft token per frame, no
  mel/no conformer. Clips are truncated to audio_seq_length (750 = 30 s) so
  placeholder and feature-row counts always match. Placeholders expand to
  boa + <audio>×N + eoa. Matches HF transformers
  Gemma4UnifiedAudioFeatureExtractor and mlx-vlm gemma4_unified.
- Video: samples up to video_max_frames frames (~1 fps), patchifies each
  frame like an image but with the per-frame vision_soft_tokens_per_video
  budget (70), and expands each <video> placeholder into per-frame
  "mm:ss BOI + <video>×N + EOI" blocks joined by spaces, matching the
  reference processors.
- Config: decodes boa/eoa ids (incl. the eoa_token_index spelling), audio
  sampling rate / samples-per-token from the nested feature_extractor block
  of processor_config.json, and the video budget/frame-count keys.

Cross-checked against HF transformers gemma4_unified (canonical), mlx-vlm
gemma4_unified, and the mlx-community/gemma-4-12B-it-4bit checkpoint config.

9 new tests (framing parity vs the reference algorithm, 30 s cap, batch
padding/masking, processor→model scatter round trip, per-frame video budget
and -1 position padding, timestamp formatting, end-to-end tiny-model video
scatter). Full suite: 214 tests pass.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@fdagostino
fdagostino force-pushed the feat/gemma4-unified-audio-video branch from 60d46c6 to 46d4106 Compare September 14, 2026 02:13
div0-space added a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Dec 2, 2026
…20cb09) into unified LibraxisAI core

- Integrates MLXWhisper Swift 6 library, POST /v1/audio/transcriptions (--whisper-model), ml-explore#854 compiled SwiGLU, ml-explore#436 PromptTrie/LRUPromptCache, ml-explore#643 bounded Prompt-Lookup Decoding, and ml-explore#392/ml-explore#400 Gemma 4 audio/video
- Reconciles Package.swift (both MLXGuidedGeneration and MLXWhisper linked into MLXLMServer), ServerConfiguration (--batch-v2/--max-concurrency/--kv-cache-mb/--served-model-name + --whisper-model), and MLXServerRunner (CBv2 + Whisper + Embeddings in one mlx-server process)

Authored-By: gemini <agents@vetcoders.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants