Skip to content

[perf] optimize whisper GPU performance - #43139

Draft
JaredforReal wants to merge 9 commits into
huggingface:mainfrom
JaredforReal:perf/whisper
Draft

[perf] optimize whisper GPU performance#43139
JaredforReal wants to merge 9 commits into
huggingface:mainfrom
JaredforReal:perf/whisper

Conversation

@JaredforReal

Copy link
Copy Markdown
Contributor

What does this PR do?

Optimize WhisperFeatureExtractor GPU performance

Problem

The current WhisperFeatureExtractor._torch_extract_fbank_features() has significant performance overhead when using GPU:

  • Creates torch.hann_window() on every call
  • Transfers mel_filters from CPU→GPU on every call
  • Forces GPU→CPU synchronization even when downstream ops need GPU tensors
  • Multiple unnecessary device transfers cause ~300ms latency

Solution

Implement lazy caching mechanism to reuse GPU tensors across calls:

Key Changes:

  1. Add caching (_ensure_gpu_cache): Cache hann_window and mel_filters on target device
  2. Optimize mel_filters: Pre-transpose and make contiguous for faster matmul
  3. Support tensor input: Accept both np.ndarray and torch.Tensor inputs
  4. Add return_tensors parameter: Keep results on GPU when device != "cpu" and return_tensors="pt"
  5. Minor optimizations: Use torch.amax() instead of chained .max() calls

Performance Impact

  • First call: One-time cache initialization overhead
  • Subsequent calls: Significant speedup by eliminating repeated CPU↔GPU transfers
  • vLLM use case: Reduces feature extraction from ~300ms to ~1-2ms (150-300x faster)

Backward Compatibility

✅ Fully backward compatible:

  • Default behavior unchanged (device="cpu")
  • Existing code works without modifications
  • All 19 core tests pass

Testing

  • ✅ All existing CPU tests pass
  • ✅ Numerical precision maintained (1e-5 tolerance)
  • ✅ Works with both numpy and torch inputs

Fixes # (issue)

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?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

Signed-off-by: JaredforReal <w13431838023@gmail.com>
Signed-off-by: JaredforReal <w13431838023@gmail.com>
Signed-off-by: JaredforReal <w13431838023@gmail.com>
Signed-off-by: JaredforReal <w13431838023@gmail.com>
Signed-off-by: JaredforReal <w13431838023@gmail.com>
Signed-off-by: JaredforReal <w13431838023@gmail.com>
Signed-off-by: JaredforReal <w13431838023@gmail.com>
Signed-off-by: JaredforReal <w13431838023@gmail.com>
Signed-off-by: JaredforReal <w13431838023@gmail.com>
@github-actions

github-actions Bot commented Jan 7, 2026

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: glmasr, whisper

@vasqu

vasqu commented Jan 7, 2026

Copy link
Copy Markdown
Collaborator

cc @eustlb @ebezzam

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.

2 participants