Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Extractors] Fix kwargs to pre-trained #30260

Merged

Conversation

sanchit-gandhi
Copy link
Contributor

@sanchit-gandhi sanchit-gandhi commented Apr 15, 2024

What does this PR do?

It was reported by @osanseviero that when instantiating a feature extractor from pre-trained, setting kwargs occasionally leads to incorrect behaviour. This is particularly the case for Whisper, where setting the argument feature_size can have no effect on the number of Mel-bins:

from transformers import WhisperFeatureExtractor

# initialise from kwargs -> mel filters matches feature size
feature_extractor = WhisperFeatureExtractor(feature_size=100)
print(f"From kwargs: feature size {feature_extractor.feature_size}, mel filters {feature_extractor.mel_filters.shape[1]}")

# initialise from pre-trained with kwargs -> mel filters not updated to new feature size
feature_extractor = WhisperFeatureExtractor.from_pretrained("openai/whisper-tiny", feature_size=100)
print(f"From pre-trained: feature size {feature_extractor.feature_size}, mel filters {feature_extractor.mel_filters.shape[1]}")

Print Output:

From kwargs: feature size 100, mel filters 100
From pre-trained: feature size 100, mel filters 80

This is because of the order in which we set the args in the feature extractor. We first pass all the arguments that we have saved in the preprocessor_config.json file:

feature_extractor = cls(**feature_extractor_dict)

And subsequently override any kwargs that we got from the user:

for key, value in kwargs.items():
if hasattr(feature_extractor, key):
setattr(feature_extractor, key, value)

The problem with this method is that for Whisper, we set some attributes based on the values of others in the init. E.g. we set mel_filters based on the value of feature_size:

self.mel_filters = mel_filter_bank(
num_frequency_bins=1 + n_fft // 2,
num_mel_filters=feature_size,

These won't be updated properly with our current method, since we're only updating the attribute feature_size, and not subsequently recomputing the correct mel_filters.

The simple fix is to give priority to the user kwargs and ensure these are passed to the init.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM good catch both!

@sanchit-gandhi sanchit-gandhi merged commit cd09a8d into huggingface:main Apr 19, 2024
21 checks passed
@sanchit-gandhi sanchit-gandhi deleted the feature-extractor-kwargs branch April 19, 2024 10:16
ArthurZucker pushed a commit that referenced this pull request Apr 22, 2024
ydshieh pushed a commit that referenced this pull request Apr 23, 2024
itazap pushed a commit that referenced this pull request May 14, 2024
@guynich
Copy link

guynich commented Jun 4, 2024

Well done for this change. I was hitting the same issue with transformers 4.39.3 package - with transformers 4.41.0 I can use kwargs as expected. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants