⚡️ Speed up method FreeInitMixin._apply_freq_filter by 51%#146
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
⚡️ Speed up method FreeInitMixin._apply_freq_filter by 51%#146codeflash-ai[bot] wants to merge 1 commit into
FreeInitMixin._apply_freq_filter by 51%#146codeflash-ai[bot] wants to merge 1 commit into
Conversation
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:** - Fused the FFT and shift operations for both `x` and `noise`. - Used `torch.sub` instead of `1 - low_pass_filter` for better performance and clarity. - Performed multiplications and addition in-place (`mul_`, `add_`) to reduce memory overhead and temporary tensors. - Used the same tensor (`x_freq`) as the accumulator for the frequency-domain mixture. - Reused `dims` variable to avoid repeated tuple construction. - No extra allocations beyond what is mathematically necessary; all outputs and logic are unchanged.
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.
📄 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.