⚡️ Speed up method AutoencoderKLWan.clear_cache
by 886%
#11665
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_cache
insrc/diffusers/models/autoencoders/autoencoder_kl_wan.py
⏱️ Runtime :
1.60 milliseconds
→162 microseconds
(best of5
runs)📝 Explanation and details
Key optimizations:
WanCausalConv3d
modules in each model (encoder
/decoder
) only once during initialization, store inself._cached_conv_counts
. This removes unnecessary repeated tree traversals at everyclear_cache
call, which was the main bottleneck (from profiling)._count_conv3d_fast
is optimized via a generator expression withsum
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.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-AutoencoderKLWan.clear_cache-mb6bxvte
and push.