invoke-ai/InvokeAI · error · ValueError

Latents to blend must be the same size.

Error message

Latents to blend must be the same size.

What it means

BlendLatents.invoke() raises ValueError when, after mask-based tensor replacement, latents_a and latents_b still have different shapes. Both latent tensors must share identical channel/spatial dimensions to be slerp-blended. Mismatch typically comes from the two inputs being produced at different resolutions or from mask replacement producing different shapes.

Source

Thrown at invokeai/app/invocations/blend_latents.py:108

        if output.dtype != torch.float16:
            output = torch.add(output, mask_tensor * torch.sub(other_tensor, tensor))
        else:
            output = torch.add(output, mask_tensor.half() * torch.sub(other_tensor, tensor))
        return output

    def invoke(self, context: InvocationContext) -> LatentsOutput:
        latents_a = context.tensors.load(self.latents_a.latents_name)
        latents_b = context.tensors.load(self.latents_b.latents_name)
        if self.mask is None:
            mask_tensor = torch.zeros(latents_a.shape[-2:])
        else:
            mask_tensor = self.prep_mask_tensor(context.images.get_pil(self.mask.image_name))
            mask_tensor = tv_resize(mask_tensor, latents_a.shape[-2:], T.InterpolationMode.BILINEAR, antialias=False)

        latents_b = self.replace_tensor_from_masked_tensor(latents_b, latents_a, mask_tensor)

        if latents_a.shape != latents_b.shape:
            raise ValueError("Latents to blend must be the same size.")

        device = TorchDevice.choose_torch_device()

        # blend
        blended_latents = slerp(self.alpha, latents_a, latents_b, device)

        # https://discuss.huggingface.co/t/memory-usage-by-later-pipeline-stages/23699
        blended_latents = blended_latents.to("cpu")
        TorchDevice.empty_cache()

        name = context.tensors.save(tensor=blended_latents)
        return LatentsOutput.build(latents_name=name, latents=blended_latents)

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Make both latent sources use identical width/height (and the same scheduler/VAE scaling) before blending.
  2. Insert resize latents nodes on one branch so both latents match in shape.
  3. Verify the mask replacement step: ensure replace_tensor_from_masked_tensor receives tensors of the same shape.
  4. Log latents_a.shape and latents_b.shape just before the blend to find which branch differs.

Example fix

// before
blend = BlendLatents(latents_a=big_latents, latents_b=small_latents, mask=mask, alpha=0.5)

// after
resized = ResizeLatents(latents=small_latents, width=W, height=H)
blend = BlendLatents(latents_a=big_latents, latents_b=resized.latents, mask=mask, alpha=0.5)
Defensive patterns

Strategy: validation

Validate before calling

assert latents_a.shape == latents_b.shape, (
    f"latents shape mismatch: {latents_a.shape} vs {latents_b.shape}; "
    "resize latents to the same width/height before blending"
)

Try / catch

try:
    blended = blend_latents.invoke(context)
except ValueError as e:
    if "same size" in str(e):
        logger.error("latent shapes differ; align denoise resolutions before blending")

Prevention

When it happens

Trigger: Blending latents from denoise nodes configured with different width/height, latents from different VAE encodes, or a mask whose replacement logic yields a different-shaped latents_b; mask resize path only resizes the mask, not latents.

Common situations: Workflow editor graphs wiring BlendLatents with two denoise nodes at different resolutions; using latents from an img2img pass alongside latents from a txt2img pass at other dimensions; prompt/regional blending setups where one region changed size.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/2aa311964463edad. Report an issue: GitHub.