invoke-ai/InvokeAI · error · ValueError

All Krea-2 conditioning batch items must have the same valid

Error message

All Krea-2 conditioning batch items must have the same valid token count.

What it means

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.

Source

Thrown at invokeai/app/invocations/krea2_denoise.py:170

        text_conditionings: list[Krea2TextConditioning] = []
        for field in conditioning_fields:
            cond_data = context.conditioning.load(field.conditioning_name)
            assert len(cond_data.conditionings) == 1
            conditioning = cond_data.conditionings[0]
            assert isinstance(conditioning, Krea2ConditioningInfo)
            conditioning = conditioning.to(dtype=dtype, device=device)
            embeds = conditioning.prompt_embeds
            if conditioning.prompt_embeds_mask is not None:
                mask = conditioning.prompt_embeds_mask.to(device=device, dtype=torch.bool)
                if mask.shape != embeds.shape[:2]:
                    raise ValueError(
                        f"Krea-2 conditioning mask shape {tuple(mask.shape)} does not match "
                        f"prompt embedding shape {tuple(embeds.shape[:2])}."
                    )
                valid_token_counts = mask.sum(dim=1)
                if not torch.equal(valid_token_counts, valid_token_counts[:1].expand_as(valid_token_counts)):
                    raise ValueError("All Krea-2 conditioning batch items must have the same valid token count.")
                embeds = torch.stack(
                    [batch_embeds[batch_mask] for batch_embeds, batch_mask in zip(embeds, mask, strict=True)]
                )
            regional_mask = None
            if field.mask is not None:
                mask = context.tensors.load(field.mask.tensor_name)
                regional_mask = Krea2RegionalPromptingExtension.preprocess_regional_prompt_mask(
                    mask=mask,
                    grid_height=grid_height,
                    grid_width=grid_width,
                    dtype=dtype,
                    device=device,
                )
            text_conditionings.append(Krea2TextConditioning(prompt_embeds=embeds, mask=regional_mask))

        # Masked padding does not contribute to attention. Remove it before concatenation to avoid multiplying
        # the text sequence length by the encoder's fixed 512-token allocation for every conditioning.
        return Krea2RegionalPromptingExtension.from_text_conditionings(

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Ensure all conditioning fields in the batch have the same valid token count (pad/truncate masks identically).
  2. Use identical tokenization settings (max_length, truncation) for every prompt in the batch.
  3. Split into separate denoise invocations if prompts genuinely need different valid token counts.

Example fix

// before: masks with differing valid counts per batch item
mask[0] = [1,1,1,0,0]; mask[1] = [1,1,1,1,1]
// after: pad/truncate so every batch item has the same valid count
mask[0] = [1,1,1,1,0]; mask[1] = [1,1,1,1,1]  # or re-tokenize with fixed length
Defensive patterns

Strategy: validation

Validate before calling

counts = [int(m.sum(dim=1)[0]) for m in masks]  # per-batch-item valid counts
if len(set(counts)) > 1:
    raise ValueError("Batch conditionings must share the same valid token count; pad or truncate masks.")

Type guard

def batch_token_counts_equal(mask) -> bool:
    counts = mask.sum(dim=1)
    return bool(torch.all(counts == counts[0]).item())

Try / catch

try:
    out = invoke_krea2_denoise(conditioning_field=fields)
except ValueError as e:
    if "same valid token count" in str(e):
        fields = [pad_conditioning_to_max_tokens(f) for f in fields]
        out = invoke_krea2_denoise(conditioning_field=fields)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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/3346a51d26019a1e. Report an issue: GitHub.