Fix WavLM compute_bias performance on CUDA (~100x speedup)#4
Open
mhenrichsen wants to merge 1 commit intofrothywater:mainfrom
Open
Fix WavLM compute_bias performance on CUDA (~100x speedup)#4mhenrichsen wants to merge 1 commit intofrothywater:mainfrom
mhenrichsen wants to merge 1 commit intofrothywater:mainfrom
Conversation
Monkey-patch WavLMSelfAttention.compute_bias to create tensors directly on the model device. The upstream torchaudio implementation creates them on CPU, causing a ~100x slowdown for sequences longer than ~180 frames (~3.6s audio) due to CPU-GPU synchronization. Applied at module import time so all WavLM-based models benefit automatically. Upstream fix: pytorch/audio#4176 Co-Authored-By: Claude Opus 4.6 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
WavLMSelfAttention.compute_biasto create tensors on the model device instead of CPUProblem
torchaudio's
WavLMSelfAttention.compute_bias()callstorch.arange()without adeviceargument, creating position tensors on CPU then transferring to GPU. This causes a severe performance cliff on CUDA: for sequences above ~180 frames (~3.6s audio), encoding time jumps from ~0.1s to ~2.5s due to CPU-GPU synchronization overhead.This makes Kanade encoding unexpectedly slow for any audio longer than ~3.6 seconds.
Fix
Apply a module-level monkey-patch in
ssl_extractor.pythat replacescompute_biaswith a version that creates tensors directly on the model device. The patch is applied at import time so all WavLM-based models benefit automatically.I've also submitted the proper fix upstream to torchaudio: pytorch/audio#4176. This monkey-patch can be removed once that lands in a torchaudio release.
Benchmark
RTX 4090,
kanade-25hz-clean,torch.inference_mode(), best of 3:🤖 Generated with Claude Code