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

get_extended_attention_mask in the NLVR encoder only handles attention_mask of dim 2 ([B, L]) or dim 3 ([B, from, to], decoder with prefix). Any other rank — 4-D pre-extended masks, 1-D masks, scalars — hits the else branch and raises with both shapes printed.

Source

Thrown at extras/BLIP/models/nlvr_encoder.py:690

                # 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 2-D [batch, seq_len] mask of 1s/0s
  2. Reshape/squeeze any pre-extended mask down to 2-D before the call
  3. For custom pairwise attention patterns in the decoder, use a 3-D [B, from, to] mask

Example fix

// before
out = encoder(input_ids=ids, attention_mask=mask4d)

// after
out = encoder(input_ids=ids, attention_mask=mask4d.reshape(mask4d.shape[0], -1))
Defensive patterns

Strategy: validation

Validate before calling

def as_2d_mask(mask):
    if mask.dim() != 2:
        mask = mask.reshape(mask.shape[0], -1)
    return mask

Prevention

When it happens

Trigger: Forward pass over image pairs with a 4-D HF-style mask, a 1-D per-sample mask, or a mask whose batch dimension doesn't match input_shape[0].

Common situations: Feeding NLVR with masks produced by other transformer libraries; batching code that stacks masks with extra dims; masking utilities returning [B, 1, L].

Related errors


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