⚡️ Speed up method AutoencoderKLWan.clear_cache by 886%#11665
Merged
a-r-r-o-w merged 7 commits intohuggingface:mainfrom Jun 18, 2025
Merged
Conversation
**Key optimizations:** - Compute the number of `WanCausalConv3d` modules in each model (`encoder`/`decoder`) **only once during initialization**, store in `self._cached_conv_counts`. This removes unnecessary repeated tree traversals at every `clear_cache` call, which was the main bottleneck (from profiling). - The internal helper `_count_conv3d_fast` is optimized via a generator expression with `sum` for efficiency. All comments from the original code are preserved, except for updated or removed local docstrings/comments relevant to changed lines. **Function signatures and outputs remain unchanged.**
Contributor
|
@bot /style |
Contributor
|
Style fixes have been applied. View the workflow run here. |
|
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. |
a-r-r-o-w
reviewed
Jun 16, 2025
Contributor
a-r-r-o-w
left a comment
There was a problem hiding this comment.
for some reason, my actual review comments did not go through 🤔
Co-authored-by: Aryan <[email protected]>
Contributor
Author
|
accepted your code review suggestions. This should be ready to merge |
Contributor
|
@bot /style |
Contributor
|
Style fixes have been applied. View the workflow run here. |
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.
Saurabh's comments - This is used frequently in every encode and decode call. So speeding this up would be very helpful.
Let me know what feedback you have for me for the next set of optimization PRs. I want to ensure an easy merge experience for you.
📄 886% (8.86x) speedup for
AutoencoderKLWan.clear_cacheinsrc/diffusers/models/autoencoders/autoencoder_kl_wan.py⏱️ Runtime :
1.60 milliseconds→162 microseconds(best of5runs)📝 Explanation and details
Key optimizations:
WanCausalConv3dmodules in each model (encoder/decoder) only once during initialization, store inself._cached_conv_counts. This removes unnecessary repeated tree traversals at everyclear_cachecall, which was the main bottleneck (from profiling)._count_conv3d_fastis optimized via a generator expression withsumfor efficiency.All comments from the original code are preserved, except for updated or removed local docstrings/comments relevant to changed lines.
Function signatures and outputs remain unchanged.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-AutoencoderKLWan.clear_cache-mb6bxvteand push.