invoke-ai/InvokeAI · error · ValueError

Krea-2 regional mask has {conditioning.mask.numel()} values,

Error message

Krea-2 regional mask has {conditioning.mask.numel()} values, expected {image_seq_len}.

What it means

Each regional conditioning may carry a spatial mask aligned to the image-latent token axis. from_text_conditionings validates that mask.numel() equals image_seq_len (the per-image token count). A mismatch means the mask was generated at a different resolution/downscale factor than the one implied by image_seq_len.

Source

Thrown at invokeai/backend/krea2/regional_prompting.py:63

        if not self.has_regional_masks:
            return 0
        return self.image_seq_len**2

    @classmethod
    def from_text_conditionings(
        cls, text_conditionings: list[Krea2TextConditioning], image_seq_len: int
    ) -> "Krea2RegionalPromptingExtension":
        if not text_conditionings:
            raise ValueError("At least one Krea-2 text conditioning is required.")

        prompt_embeds: list[torch.Tensor] = []
        image_masks: list[torch.Tensor | None] = []
        embedding_ranges: list[Range] = []
        current_start = 0
        for conditioning in text_conditionings:
            sequence_length = conditioning.prompt_embeds.shape[1]
            if conditioning.mask is not None and conditioning.mask.numel() != image_seq_len:
                raise ValueError(
                    f"Krea-2 regional mask has {conditioning.mask.numel()} values, expected {image_seq_len}."
                )
            prompt_embeds.append(conditioning.prompt_embeds)
            image_masks.append(conditioning.mask)
            embedding_ranges.append(Range(start=current_start, end=current_start + sequence_length))
            current_start += sequence_length

        regional_text_conditioning = Krea2RegionalTextConditioning(
            prompt_embeds=torch.cat(prompt_embeds, dim=1),
            image_masks=image_masks,
            embedding_ranges=embedding_ranges,
        )
        return cls(regional_text_conditioning=regional_text_conditioning, image_seq_len=image_seq_len)

    def get_attention_mask(self) -> torch.Tensor | None:
        if not self.has_regional_masks:
            return None
        if self._attention_mask is None:

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Resize/interpolate each mask to the latent grid so numel() == image_seq_len before constructing the conditioning.
  2. Compute image_seq_len from the actual latent dimensions (h_latent * w_latent / patch area) and generate masks from that.
  3. Flatten/reshape the mask tensor to one value per image token (no batch/channel dims beyond expected).
  4. Verify masks and embeddings come from the same generation request/resolution.

Example fix

// before
mask = canvas_mask  # e.g. 1024x1024 -> numel 1048576
// after
mask = F.interpolate(canvas_mask[None, None], size=(h_lat, w_lat), mode='nearest').flatten()  # numel == image_seq_len
Defensive patterns

Strategy: validation

Validate before calling

for c in text_conditionings:
    if c.mask is not None and c.mask.numel() != image_seq_len:
        c.mask = torch.nn.functional.interpolate(
            c.mask.float()[None, None], size=latent_hw, mode='nearest').flatten()

Type guard

def mask_matches_seq_len(mask, image_seq_len) -> bool:
    return mask is None or mask.numel() == image_seq_len

Try / catch

try:
    ext = Krea2RegionalPromptingExtension.from_text_conditionings(conds, image_seq_len)
except ValueError as e:
    if 'regional mask has' in str(e):
        for c in conds:
            c.mask = resize_mask_to_seq_len(c.mask, image_seq_len)
        ext = Krea2RegionalPromptingExtension.from_text_conditionings(conds, image_seq_len)
    else:
        raise

Prevention

When it happens

Trigger: Calling from_text_conditionings with a Krea2TextConditioning whose .mask tensor has a number of elements different from image_seq_len — masks computed at a different latent resolution, wrong interpolation to the latent grid, or a mask not flattened to one value per image token.

Common situations: Preparing regional masks from an image-space canvas at the wrong downscale (8x vs latent patch size), forgetting to resize masks after changing output resolution, passing 2D masks that were not reshaped to (1, image_seq_len).

Related errors


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