{"record":{"id":"3346a51d26019a1e","repo":"invoke-ai/InvokeAI","slug":"all-krea-2-conditioning-batch-items-must-have-the","errorCode":null,"errorMessage":"All Krea-2 conditioning batch items must have the same valid token count.","messagePattern":"All Krea-2 conditioning batch items must have the same valid token count\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/krea2_denoise.py","lineNumber":170,"sourceCode":"\n        text_conditionings: list[Krea2TextConditioning] = []\n        for field in conditioning_fields:\n            cond_data = context.conditioning.load(field.conditioning_name)\n            assert len(cond_data.conditionings) == 1\n            conditioning = cond_data.conditionings[0]\n            assert isinstance(conditioning, Krea2ConditioningInfo)\n            conditioning = conditioning.to(dtype=dtype, device=device)\n            embeds = conditioning.prompt_embeds\n            if conditioning.prompt_embeds_mask is not None:\n                mask = conditioning.prompt_embeds_mask.to(device=device, dtype=torch.bool)\n                if mask.shape != embeds.shape[:2]:\n                    raise ValueError(\n                        f\"Krea-2 conditioning mask shape {tuple(mask.shape)} does not match \"\n                        f\"prompt embedding shape {tuple(embeds.shape[:2])}.\"\n                    )\n                valid_token_counts = mask.sum(dim=1)\n                if not torch.equal(valid_token_counts, valid_token_counts[:1].expand_as(valid_token_counts)):\n                    raise ValueError(\"All Krea-2 conditioning batch items must have the same valid token count.\")\n                embeds = torch.stack(\n                    [batch_embeds[batch_mask] for batch_embeds, batch_mask in zip(embeds, mask, strict=True)]\n                )\n            regional_mask = None\n            if field.mask is not None:\n                mask = context.tensors.load(field.mask.tensor_name)\n                regional_mask = Krea2RegionalPromptingExtension.preprocess_regional_prompt_mask(\n                    mask=mask,\n                    grid_height=grid_height,\n                    grid_width=grid_width,\n                    dtype=dtype,\n                    device=device,\n                )\n            text_conditionings.append(Krea2TextConditioning(prompt_embeds=embeds, mask=regional_mask))\n\n        # Masked padding does not contribute to attention. Remove it before concatenation to avoid multiplying\n        # the text sequence length by the encoder's fixed 512-token allocation for every conditioning.\n        return Krea2RegionalPromptingExtension.from_text_conditionings(","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/krea2_denoise.py#L152-L188","documentation":"When batching Krea-2 conditionings, each batch item's attention mask may mark a different number of valid tokens. After masking, embeddings are stacked into a regular tensor, which requires every batch item to keep the same number of valid tokens. This ValueError fires when mask.sum(dim=1) differs across batch items.","triggerScenarios":"Passing multiple Krea2ConditioningFields whose text embeddings have different numbers of unmasked (valid) tokens — e.g. prompts tokenized to different effective lengths with per-token masks — into the same denoise call.","commonSituations":"Regional prompting setups mixing conditioning entries produced by different prompts/tokenizer settings; batch composition where one prompt was truncated and another wasn't; custom nodes building masks inconsistently across batch items.","solutions":["Ensure all conditioning fields in the batch have the same valid token count (pad/truncate masks identically).","Use identical tokenization settings (max_length, truncation) for every prompt in the batch.","Split into separate denoise invocations if prompts genuinely need different valid token counts."],"exampleFix":"// before: masks with differing valid counts per batch item\nmask[0] = [1,1,1,0,0]; mask[1] = [1,1,1,1,1]\n// after: pad/truncate so every batch item has the same valid count\nmask[0] = [1,1,1,1,0]; mask[1] = [1,1,1,1,1]  # or re-tokenize with fixed length","handlingStrategy":"validation","validationCode":"counts = [int(m.sum(dim=1)[0]) for m in masks]  # per-batch-item valid counts\nif len(set(counts)) > 1:\n    raise ValueError(\"Batch conditionings must share the same valid token count; pad or truncate masks.\")","typeGuard":"def batch_token_counts_equal(mask) -> bool:\n    counts = mask.sum(dim=1)\n    return bool(torch.all(counts == counts[0]).item())","tryCatchPattern":"try:\n    out = invoke_krea2_denoise(conditioning_field=fields)\nexcept ValueError as e:\n    if \"same valid token count\" in str(e):\n        fields = [pad_conditioning_to_max_tokens(f) for f in fields]\n        out = invoke_krea2_denoise(conditioning_field=fields)\n    else:\n        raise","preventionTips":["Tokenize all batch prompts with identical max_length and padding settings.","Pad masks to a common valid-token count before stacking batch conditionings.","Unit-test regional prompting batches for uniform mask sums."],"tags":["invokeai","krea2","batch","shape-mismatch"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}