⚡️ Speed up function get_up_block_adapter by 8%
#140
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.
📄 8% (0.08x) speedup for
get_up_block_adapterinsrc/diffusers/models/controlnets/controlnet_xs.py⏱️ Runtime :
12.4 milliseconds→11.4 milliseconds(best of33runs)📝 Explanation and details
Here is an optimized version of your code. The bottleneck is the creation and zero-initialization of a bunch of Conv2d modules within a tight loop. Instead of calling
zero_module(which loops through every tensor and callszeros_in a Python loop), we can usenn.Conv2d(..., bias=False)(if biases are not needed — but since you rely on zero_module, preserve bias), and then assign the weights and bias in one go with.data.zero_()to avoid extra Python loops.Additionally, combine the list-building and ModuleList construction using a list comprehension, and avoid needless variable assignments.
Preserved function signatures and comments.
Key optimizations:
make_zero_conv, manually set.data.zero_()for weights and biases for improved speed vs looping withzero_module.get_up_block_adapterto reduce Python loop overhead.If
UpBlockControlNetXSAdapteris a large or slow object, further optimization would involve passing control in a more batch-oriented fashion, but that is not within the scope of the provided code. This will match return values and behavior with improved speed for the Conv2d initialization.✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-get_up_block_adapter-mbdrtpitand push.