transformers
04cb96f3 - Fix missing Gemma4Processor._compute_audio_num_tokens (#46416)

Commit
40 days ago
Fix missing Gemma4Processor._compute_audio_num_tokens (#46416) * Fix missing Gemma4Processor._compute_audio_num_tokens `Gemma4Processor._get_num_multimodal_tokens` calls `self._compute_audio_num_tokens` to size audio placeholder spans, but the method was never defined on the class (it only exists on `Gemma4UnifiedProcessor`). Any audio input therefore raises `AttributeError: 'Gemma4Processor' object has no attribute '_compute_audio_num_tokens'`. Add the method, replicating the audio encoder's sequence-length arithmetic so the placeholder count exactly matches the audio tower output: `Gemma4AudioFeatureExtractor` mel framing (semicausal pad + unfold) followed by the two stride-2 Conv2d subsampling layers in `Gemma4AudioSubSampleConvProjection`, capped at `audio_seq_length`. Validated against the real model: feeding the feature-extractor mel frames through `Gemma4AudioSubSampleConvProjection` and counting surviving positions matches this function across all sample lengths in [100, 160000]. * Regenerate Gemma4UnifiedProcessor for modular consistency processing_gemma4_unified.py is auto-generated from modular_gemma4_unified.py, and Gemma4UnifiedProcessor flattens the Gemma4Processor base. Adding _compute_audio_num_tokens to the base in the previous commit shifted the generated member order, so regenerate the modular output to keep the check_modular_conversion CI check green. Generated via: python utils/check_modular_conversion.py \ --files src/transformers/models/gemma4_unified/modular_gemma4_unified.py \ --fix_and_overwrite * Add audio token-count test for Gemma4Processor Validates that `_get_num_multimodal_tokens` predicts the correct number of audio placeholder tokens across several durations, including the `audio_seq_length` cap. Golden values were captured from the real `Gemma4AudioFeatureExtractor` and `Gemma4AudioSubSampleConvProjection`; several lengths are chosen where a naive ceil(duration_ms / audio_ms_per_token) approximation would be off by one, so the test guards against that shortcut. * Pin 16 kHz feature extractor in Gemma4 audio token test The golden token counts are keyed on raw sample counts and assume 16 kHz framing (frame_length=320, hop_length=160 = round(16000 * {20, 10} ms)). Those framing params derive from the feature extractor's sampling_rate and, due to integer rounding, are not rate-invariant, so the counts are only valid at 16 kHz. --------- Co-authored-by: Claudio dos Santos Fernandes <csantosbh@fb.com>
Author
Parents
Loading