lllyasviel/Fooocus · error · ValueError

Wrong shape for input_ids (shape {}) or attention_mask (shap

Error message

Wrong shape for input_ids (shape {}) or attention_mask (shape {})

What it means

In get_extended_attention_mask, the model accepts attention_mask of dim 2 ([batch, seq]) or dim 3 ([batch, from_seq, to_seq], decoder-only). Any other dimensionality (e.g. a 4-D mask or a scalar/1-D mask) falls through to the else branch and raises this ValueError showing both the input shape and mask shape.

Source

Thrown at extras/BLIP/models/med.py:655

                # in case past_key_values are used we need to add a prefix ones mask to the causal mask
                # causal and attention masks must have same type with pytorch version < 1.3
                causal_mask = causal_mask.to(attention_mask.dtype)
   
                if causal_mask.shape[1] < attention_mask.shape[1]:
                    prefix_seq_len = attention_mask.shape[1] - causal_mask.shape[1]
                    causal_mask = torch.cat(
                        [
                            torch.ones((batch_size, seq_length, prefix_seq_len), device=device, dtype=causal_mask.dtype),
                            causal_mask,
                        ],
                        axis=-1,
                    )                     

                extended_attention_mask = causal_mask[:, None, :, :] * attention_mask[:, None, None, :]
            else:
                extended_attention_mask = attention_mask[:, None, None, :]
        else:
            raise ValueError(
                "Wrong shape for input_ids (shape {}) or attention_mask (shape {})".format(
                    input_shape, attention_mask.shape
                )
            )

        # Since attention_mask is 1.0 for positions we want to attend and 0.0 for
        # masked positions, this operation will create a tensor which is 0.0 for
        # positions we want to attend and -10000.0 for masked positions.
        # Since we are adding it to the raw scores before the softmax, this is
        # effectively the same as removing these entirely.
        extended_attention_mask = extended_attention_mask.to(dtype=self.dtype)  # fp16 compatibility
        extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0
        return extended_attention_mask
    
    def forward(
        self,
        input_ids=None,
        attention_mask=None,

View on GitHub (pinned to ae05379cc9)

Solutions

  1. Pass a plain 2-D mask of shape [batch_size, seq_length] (1.0 keep, 0.0 mask) and let the model extend it
  2. If you need a custom 2-D attention pattern, supply a 3-D [batch, from_len, to_len] mask (decoder path only)
  3. Squeeze/remove the head/broadcast dims: mask = mask.squeeze(1).squeeze(1) for HF-style masks

Example fix

// before
out = model(input_ids=ids, attention_mask=huggingface_4d_mask)

// after
mask_2d = hf_mask.squeeze(1).squeeze(1)  # [B, L]
out = model(input_ids=ids, attention_mask=mask_2d)
Defensive patterns

Strategy: validation

Validate before calling

def normalize_attention_mask(mask, is_decoder=False):
    allowed = (2, 3) if is_decoder else (2,)
    if mask.dim() not in allowed:
        mask = mask.reshape(mask.shape[0], -1)
    assert mask.dim() in allowed
    return mask

Prevention

When it happens

Trigger: Forward pass with attention_mask of dim != 2 (non-decoder) or dim not in {2,3} (decoder); e.g. passing an already-extended 4-D mask [batch, 1, 1, seq], passing a 1-D mask, or passing a mask whose batch dim doesn't match input_ids batch.

Common situations: Reusing a HuggingFace-style 4-D attention mask with BLIP's MED encoder; building masks manually with an extra head dimension; libraries that pass extended masks directly.

Related errors


AI-assisted analysis of lllyasviel/Fooocus@ae05379cc9 (2026-08-15). Data as JSON: /api/errors/7a695b2382f4c557. Report an issue: GitHub.