@@ -196,10 +196,21 @@ def compute_mel_spectrogram(
196196 n_mels : int = 128 ,
197197 n_fft : int = 400 ,
198198 hop_length : int = 160 ,
199- ) -> np .ndarray :
199+ ) -> tuple [ np .ndarray , np . ndarray ] :
200200 """Compute log-mel spectrogram using WhisperFeatureExtractor.
201201
202- Returns array of shape ``(1, n_mels, time_frames)``.
202+ The Whisper extractor pads to a fixed 30-second window
203+ (``max_length=3000`` mel frames). Without the matching padding
204+ mask, the mobius audio encoder treats trailing zero-padded frames
205+ as real audio and the LLM downstream emits degenerate loops. We
206+ therefore always request ``return_attention_mask=True`` so the
207+ encoder can crop padding from the audio token stream.
208+
209+ Returns:
210+ ``(input_features, feature_attention_mask)`` where
211+ ``input_features`` has shape ``(1, n_mels, time_frames)`` and
212+ ``feature_attention_mask`` has shape ``(1, time_frames)``
213+ (1 = real audio, 0 = padding).
203214 """
204215 from transformers import WhisperFeatureExtractor
205216
@@ -213,11 +224,12 @@ def compute_mel_spectrogram(
213224 audio ,
214225 sampling_rate = sample_rate ,
215226 return_tensors = "np" ,
216- padding = False ,
227+ return_attention_mask = True ,
217228 )
218- return out ["input_features" ].astype (
219- np .float32
220- ) # Always float32; caller casts to model dtype
229+ # Always float32 for input_features; caller casts to model dtype.
230+ input_features = out ["input_features" ].astype (np .float32 )
231+ feature_attention_mask = out ["attention_mask" ].astype (np .int64 )
232+ return input_features , feature_attention_mask
221233
222234
223235# ---------------------------------------------------------------------------
@@ -312,11 +324,24 @@ def transcribe(
312324 batch_size = 1
313325
314326 # Step 1: Compute mel spectrogram
315- mel = compute_mel_spectrogram (audio ).astype (model_dtype ) # (1, n_mels, time)
327+ mel , feature_attention_mask = compute_mel_spectrogram (audio )
328+ mel = mel .astype (model_dtype ) # (1, n_mels, time)
316329
317330 # Step 2: Run audio encoder
318- audio_out = sessions ["audio_encoder" ].run ({"input_features" : mel })
331+ audio_out = sessions ["audio_encoder" ].run (
332+ {
333+ "input_features" : mel ,
334+ "feature_attention_mask" : feature_attention_mask ,
335+ }
336+ )
319337 audio_features = audio_out ["audio_features" ] # (1, audio_seq, dim)
338+ audio_feature_lengths = audio_out ["audio_feature_lengths" ] # (1,)
339+ # Crop padding-derived rows so they never reach the embedding's
340+ # gather. The encoder emits a fixed-length output equal to the
341+ # padded mel length / 8, but only ``audio_feature_lengths[0]`` of
342+ # those rows correspond to real audio.
343+ valid_audio_tokens = int (audio_feature_lengths [0 ])
344+ audio_features = audio_features [:, :valid_audio_tokens , :]
320345 num_audio_tokens = audio_features .shape [1 ]
321346
322347 # Flatten to (num_audio_tokens, output_dim) for the embedding model
0 commit comments