From fd783b2f17489095f6de933a498bf6c90c1dad40 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 12 Nov 2025 10:03:54 +0000 Subject: [PATCH] Optimize NoiseOutput.build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization replaces `latents.size()` calls with `latents.shape` attribute access and caches the shape in a variable to avoid repeated indexing operations. **Key changes:** - Replaced `latents.size()[3]` and `latents.size()[2]` with `shape[3]` and `shape[2]` - Added `shape = latents.shape` to cache the tensor dimensions **Why this is faster:** 1. **Method call elimination**: `latents.shape` is a direct attribute access, while `latents.size()` is a method call that has function call overhead in Python 2. **Single shape computation**: The tensor shape is computed once and reused, rather than calling `latents.size()` twice 3. **Reduced indexing operations**: From the line profiler, dimension access time dropped significantly - width calculation went from 208.6μs to 72.3μs (65% faster) and height from 110μs to 51.4μs (53% faster) **Performance impact:** The optimization delivers an 11% speedup (627μs → 562μs) and shows consistent improvements across all test cases (5-17% faster per test). This is particularly valuable since the function appears to be called frequently during noise generation workflows in the InvokeAI inference pipeline. Even small per-call improvements compound when the function is invoked hundreds of times during image generation. **Test case benefits:** The optimization performs well across all tensor sizes, with particularly strong gains on smaller tensors (up to 17% faster on minimal shapes) where the relative overhead of method calls is highest. --- invokeai/app/invocations/noise.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/invokeai/app/invocations/noise.py b/invokeai/app/invocations/noise.py index 02b917ebf7c..ccef747d3d2 100644 --- a/invokeai/app/invocations/noise.py +++ b/invokeai/app/invocations/noise.py @@ -63,10 +63,11 @@ class NoiseOutput(BaseInvocationOutput): @classmethod def build(cls, latents_name: str, latents: torch.Tensor, seed: int) -> "NoiseOutput": + shape = latents.shape return cls( noise=LatentsField(latents_name=latents_name, seed=seed), - width=latents.size()[3] * LATENT_SCALE_FACTOR, - height=latents.size()[2] * LATENT_SCALE_FACTOR, + width=shape[3] * LATENT_SCALE_FACTOR, + height=shape[2] * LATENT_SCALE_FACTOR, )