{"record":{"id":"92788b801408c760","repo":"invoke-ai/InvokeAI","slug":"krea-2-regional-mask-has-conditioning-mask-numel","errorCode":null,"errorMessage":"Krea-2 regional mask has {conditioning.mask.numel()} values, expected {image_seq_len}.","messagePattern":"Krea-2 regional mask has (.+?) values, expected (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/krea2/regional_prompting.py","lineNumber":63,"sourceCode":"        if not self.has_regional_masks:\n            return 0\n        return self.image_seq_len**2\n\n    @classmethod\n    def from_text_conditionings(\n        cls, text_conditionings: list[Krea2TextConditioning], image_seq_len: int\n    ) -> \"Krea2RegionalPromptingExtension\":\n        if not text_conditionings:\n            raise ValueError(\"At least one Krea-2 text conditioning is required.\")\n\n        prompt_embeds: list[torch.Tensor] = []\n        image_masks: list[torch.Tensor | None] = []\n        embedding_ranges: list[Range] = []\n        current_start = 0\n        for conditioning in text_conditionings:\n            sequence_length = conditioning.prompt_embeds.shape[1]\n            if conditioning.mask is not None and conditioning.mask.numel() != image_seq_len:\n                raise ValueError(\n                    f\"Krea-2 regional mask has {conditioning.mask.numel()} values, expected {image_seq_len}.\"\n                )\n            prompt_embeds.append(conditioning.prompt_embeds)\n            image_masks.append(conditioning.mask)\n            embedding_ranges.append(Range(start=current_start, end=current_start + sequence_length))\n            current_start += sequence_length\n\n        regional_text_conditioning = Krea2RegionalTextConditioning(\n            prompt_embeds=torch.cat(prompt_embeds, dim=1),\n            image_masks=image_masks,\n            embedding_ranges=embedding_ranges,\n        )\n        return cls(regional_text_conditioning=regional_text_conditioning, image_seq_len=image_seq_len)\n\n    def get_attention_mask(self) -> torch.Tensor | None:\n        if not self.has_regional_masks:\n            return None\n        if self._attention_mask is None:","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/krea2/regional_prompting.py#L45-L81","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Resize/interpolate each mask to the latent grid so numel() == image_seq_len before constructing the conditioning.","Compute image_seq_len from the actual latent dimensions (h_latent * w_latent / patch area) and generate masks from that.","Flatten/reshape the mask tensor to one value per image token (no batch/channel dims beyond expected).","Verify masks and embeddings come from the same generation request/resolution."],"exampleFix":"// before\nmask = canvas_mask  # e.g. 1024x1024 -> numel 1048576\n// after\nmask = F.interpolate(canvas_mask[None, None], size=(h_lat, w_lat), mode='nearest').flatten()  # numel == image_seq_len","handlingStrategy":"validation","validationCode":"for c in text_conditionings:\n    if c.mask is not None and c.mask.numel() != image_seq_len:\n        c.mask = torch.nn.functional.interpolate(\n            c.mask.float()[None, None], size=latent_hw, mode='nearest').flatten()","typeGuard":"def mask_matches_seq_len(mask, image_seq_len) -> bool:\n    return mask is None or mask.numel() == image_seq_len","tryCatchPattern":"try:\n    ext = Krea2RegionalPromptingExtension.from_text_conditionings(conds, image_seq_len)\nexcept ValueError as e:\n    if 'regional mask has' in str(e):\n        for c in conds:\n            c.mask = resize_mask_to_seq_len(c.mask, image_seq_len)\n        ext = Krea2RegionalPromptingExtension.from_text_conditionings(conds, image_seq_len)\n    else:\n        raise","preventionTips":["Derive masks from the same latent dimensions used to compute image_seq_len","Always interpolate image-space masks to the latent grid and flatten","Recompute masks whenever output resolution changes","Add a mask-size assertion in your regional prompt preprocessing"],"tags":["krea2","regional-prompting","mask","shape-mismatch"],"backgroundTag":"mask-size-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}