⚡️ Speed up method FreeInitMixin._apply_freq_filter by 51%
#146
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.
📄 51% (0.51x) speedup for
FreeInitMixin._apply_freq_filterinsrc/diffusers/pipelines/free_init_utils.py⏱️ Runtime :
32.0 milliseconds→21.2 milliseconds(best of113runs)📝 Explanation and details
Here’s an optimized version that reduces memory footprint, in-place where safe, avoids unnecessarily repeated calculations, and combines chained calls. The function logic and return value are unchanged.
Key Optimizations:
xandnoise.torch.subinstead of1 - low_pass_filterfor better performance and clarity.mul_,add_) to reduce memory overhead and temporary tensors.x_freq) as the accumulator for the frequency-domain mixture.dimsvariable to avoid repeated tuple construction.✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-FreeInitMixin._apply_freq_filter-mbdyc1arand push.