⚡️ Speed up method PixelNorm.forward
by 7%
#7741
Open
+2
−1
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.
📄 7% (0.07x) speedup for
PixelNorm.forward
incomfy/ldm/lightricks/vae/pixel_norm.py
⏱️ Runtime :
2.28 milliseconds
→2.13 milliseconds
(best of51
runs)📝 Explanation and details
To optimize the runtime of this program, we can leverage some of PyTorch's functions for better performance. Specifically, we can use
torch.rsqrt
andtorch.mean
wisely to optimize the normalization calculation. This can be beneficial from a performance perspective since certain operations might be optimized internally.Here is an optimized version of the code.
Explanation.
torch.mean(x * x, dim=self.dim, keepdim=True)
: Calculating the mean of the squared values directly.torch.rsqrt(mean_square)
: Usingtorch.rsqrt
to compute the reciprocal of the square root. This can be more efficient than computing the square root and then taking the reciprocal separately.x * torch.rsqrt(mean_square)
: Multiplyingx
by the reciprocal square root we computed above.This reformulation can lead to improved performance because it reduces the number of operations by specifically leveraging PyTorch's optimized backend operations.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-PixelNorm.forward-m9j2ee0l
and push.