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

Clap processor: remove wasteful np.stack operations #27454

Merged
merged 1 commit into from
Nov 14, 2023

Conversation

m-bain
Copy link
Contributor

@m-bain m-bain commented Nov 12, 2023

What does this PR do?

Upon profiling, it showed some strange result that the ClapProcessor was taking 0.5s to apply _get_input_mel(...) on short audio (less than 10s), whereas medium length audio (10s-20s) was taking only 0.02s

As it turns out there was a wasteful np.stack operation on the 1-D waveform numpy array, meaning that the 1-D array is unpacked then stacked back together again, with no effect. This PR removes this wasteful op and short audio is now also processed in 0.02s

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@ArthurZucker
@sanchit-gandhi

Np.stack on large 1-D tensor, causing ~0.5s processing time on short audio (<10s). Compared to 0.02s for medium length audio
@m-bain m-bain changed the title remove wasteful np.stack Clap processor: remove wasteful np.stack operations Nov 12, 2023
Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for updating @m-bain!

Could you provide an example snippet you used to run this for future reference of anyone visiting this PR?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@m-bain
Copy link
Contributor Author

m-bain commented Nov 14, 2023

@amyeroberts hows this ?

import time
import numpy as np

waveform = np.random.rand(100_000)
n_repeat = 10

t1_p = time.time()
prev_impl = np.stack(np.tile(waveform, n_repeat))
t2_p = time.time()

t1_n = time.time()
new_impl = np.tile(waveform, n_repeat)
t2_n = time.time()

assert (prev_impl == new_impl).all()
print(f"Time to process [prev. impl.]: {t2_p-t1_p:.3f}s")
print(f"Time to process [new. impl.]: {t2_n-t1_n:.3f}s")
Time to process [prev. impl.]: 0.883s
Time to process [new. impl.]: 0.001s

@amyeroberts
Copy link
Collaborator

@m-bain Thanks!

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.

Thanks for the catch! 🤗

@amyeroberts amyeroberts merged commit b86c54d into huggingface:main Nov 14, 2023
3 checks passed
Saibo-creator pushed a commit to epfl-dlab/transformers-GCD-PR that referenced this pull request Nov 15, 2023
remove wasteful np.stack

Np.stack on large 1-D tensor, causing ~0.5s processing time on short audio (<10s). Compared to 0.02s for medium length audio
Copy link
Contributor

@sanchit-gandhi sanchit-gandhi left a comment

Choose a reason for hiding this comment

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

Awesome - thanks @m-bain! Also cc @ylacombe

EduardoPach pushed a commit to EduardoPach/transformers that referenced this pull request Nov 19, 2023
remove wasteful np.stack

Np.stack on large 1-D tensor, causing ~0.5s processing time on short audio (<10s). Compared to 0.02s for medium length audio
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.

5 participants