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

Update feature_extractor.py #1038

Closed
wants to merge 8 commits into from

Conversation

BBC-Esq
Copy link
Contributor

@BBC-Esq BBC-Esq commented Oct 5, 2024

Added mel filter bank caching to FeatureExtractor class to optimize memory usage and reduce computational overhead when processing multiple audio files with identical parameters, particularly beneficial for batch processing scenarios.

Copy link

@abodacs abodacs left a comment

Choose a reason for hiding this comment

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

@BBC-Esq
Thanks for your work

if padding:
waveform = torch.nn.functional.pad(waveform, (0, self.n_samples))

window = torch.hann_window(self.n_fft).to(waveform.device)
Copy link

Choose a reason for hiding this comment

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

Why was the hann_window deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll take a look...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actually an optimization. In the old version, a new Hann window was being created and moved to the device every time call was executed. The new version creates it once during initialization and caches it as an instance variable (self.window).

else waveform
)
# Move waveform to the target device if necessary
if self.device == "cuda" and not waveform.is_cuda:
Copy link

Choose a reason for hiding this comment

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

Improved readability, thank you.

@MahmoudAshraf97
Copy link
Collaborator

MahmoudAshraf97 commented Nov 3, 2024

Hello, the way you implemented caching is not going to work because the only parameter that might change when creating the mel filters is n_mels, to change this parameter you need to initialize a new FeatureExtractor instance which will clear cache thus invalidating its purpose. Anyways, creating window takes around 6us which represents less than 0.05% of the execution time and the mel filters is created only once thus there is no need for caching.

In the future, it's preferable to use a caching decorator such as functools.lru_cache as an easy caching solution instead of implementing it as a dictionary

@BBC-Esq BBC-Esq closed this Nov 3, 2024
@BBC-Esq BBC-Esq deleted the feature_extractor.py branch November 3, 2024 18:05
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.

3 participants