Repository navigation
Gemma4Unified (12B): audio and video input in the processor - #400
Open
fdagostino wants to merge 1 commit into
Open
fdagostino wants to merge 1 commit into
fdagostino wants to merge 1 commit into
Conversation
fdagostino
marked this pull request as ready for review
July 9, 2026 22:35
This was referenced Jul 10, 2026
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
force-pushed
the
feat/gemma4-unified-audio-video
branch
from
September 14, 2026 02:13
60d46c6 to
46d4106
Compare
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Completes multimodal input for the encoder-free
gemma4_unified(12B) models. TheGemma4Unifiedmodel class already projects and scatters audio/video features (embed_audio,getInputEmbeddingshandleinput.audio/input.video), butGemma4UnifiedProcessoronly 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
gemma4E-series (whose audio tower is ported in #392), the unified models project raw waveform frames directly into the LLM embedding space: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).audio_seq_lengthtokens (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.)<|audio|>placeholder expands toboa + <|audio|> × N + eoa,N = ceil(samples / 640)capped at 750.Matches HF
Gemma4UnifiedAudioFeatureExtractor/Gemma4UnifiedProcessorand mlx-vlmgemma4_unified(algorithm cross-checked against both, plus themlx-community/gemma-4-12B-it-4bitcheckpoint config/tokenizer files).Video
video_max_framesframes (~1 fps viaMediaProcessing.asProcessedSequence, matching thegemma4processor in Add end-to-end video input to the base Gemma 4 (gemma4) VLM #391).vision_soft_tokens_per_video_framebudget (70) instead of 280; per-frame padding uses-1position ids, which the model already strips before scattering.<|video|>placeholder expands into per-framemm: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
Gemma4UnifiedProcessorConfigurationnow decodes:boa_token_id/eoa_token_id(including theeoa_token_indexspelling used by the released checkpoints), audio sampling rate and samples-per-token from the nestedfeature_extractorblock ofprocessor_config.json(whose mel-related keys are vestigial and ignored, as in the Python implementations), and the video budget/frame-count keys.Gemma4MessageGeneratoradditionally 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 realprocessor_config.jsonshape, 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 +-1position padding,mm:ssformatting, and an end-to-end tiny-model video scatter.xcodebuild teston this branch passes.🤖 Generated with Claude Code