Keep audio feature extractors right padded when loading processors (#6157)

* Keep audio feature extractors right padded when loading processors

FastBaseModel.from_pretrained passes padding_side=left to
AutoProcessor.from_pretrained for generation, and ProcessorMixin forwards
the kwarg to every sub-component, including audio feature extractors.
Stock transformers right-pads audio: frame-validity masks assume trailing
padding. The leaked left padding shifts mel content to the end of the 30s
window for Whisper and gives Gemma 4 one extra valid mel frame on clip
lengths off the hop boundary, desyncing audio features from placeholder
tokens and crashing training on transformers 5.5.0 to 5.9.x with 'Audio
features and audio tokens do not match'.

Reset the feature extractor to right padding at the single processor
finalization point. Text tokenizer padding stays left. Verified on
transformers 5.5.0: the loaded processor now matches a fresh stock
AutoProcessor exactly, and a 9-combination Gemma 4 audio forward probe
goes from 4 failures to none.

* Add explicit None check before reading feature extractor attributes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Tighten comments

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Daniel Han 2026-06-11 05:18:12 -07:00 committed by GitHub
commit d0ffe26383
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1280,6 +1280,15 @@ class FastBaseModel:
tokenizer.padding_side = "left" # Force inference
if hasattr(tokenizer, "tokenizer"):
tokenizer.tokenizer.padding_side = "left" # Force inference
# Audio feature extractors must stay right padded: left (a text setting,
# forwarded by from_pretrained) shifts Whisper mels and desyncs Gemma 4
# audio token counts (crash on transformers < 5.10).
feature_extractor = getattr(tokenizer, "feature_extractor", None)
if (
feature_extractor is not None
and getattr(feature_extractor, "padding_side", None) == "left"
):
feature_extractor.padding_side = "right"
m = model
while hasattr(m, "model"):
m.max_seq_length = max_seq_length