{"record":{"id":"7a695b2382f4c557","repo":"lllyasviel/Fooocus","slug":"wrong-shape-for-input-ids-shape-or-attention","errorCode":null,"errorMessage":"Wrong shape for input_ids (shape {}) or attention_mask (shape {})","messagePattern":"Wrong shape for input_ids \\(shape (.+?)\\) or attention_mask \\(shape (.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"extras/BLIP/models/med.py","lineNumber":655,"sourceCode":"                # in case past_key_values are used we need to add a prefix ones mask to the causal mask\n                # causal and attention masks must have same type with pytorch version < 1.3\n                causal_mask = causal_mask.to(attention_mask.dtype)\n   \n                if causal_mask.shape[1] < attention_mask.shape[1]:\n                    prefix_seq_len = attention_mask.shape[1] - causal_mask.shape[1]\n                    causal_mask = torch.cat(\n                        [\n                            torch.ones((batch_size, seq_length, prefix_seq_len), device=device, dtype=causal_mask.dtype),\n                            causal_mask,\n                        ],\n                        axis=-1,\n                    )                     \n\n                extended_attention_mask = causal_mask[:, None, :, :] * attention_mask[:, None, None, :]\n            else:\n                extended_attention_mask = attention_mask[:, None, None, :]\n        else:\n            raise ValueError(\n                \"Wrong shape for input_ids (shape {}) or attention_mask (shape {})\".format(\n                    input_shape, attention_mask.shape\n                )\n            )\n\n        # Since attention_mask is 1.0 for positions we want to attend and 0.0 for\n        # masked positions, this operation will create a tensor which is 0.0 for\n        # positions we want to attend and -10000.0 for masked positions.\n        # Since we are adding it to the raw scores before the softmax, this is\n        # effectively the same as removing these entirely.\n        extended_attention_mask = extended_attention_mask.to(dtype=self.dtype)  # fp16 compatibility\n        extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0\n        return extended_attention_mask\n    \n    def forward(\n        self,\n        input_ids=None,\n        attention_mask=None,","sourceCodeStart":637,"sourceCodeEnd":673,"githubUrl":"https://github.com/lllyasviel/Fooocus/blob/ae05379cc97bc4361ec8b4ec90193dab21be763f/extras/BLIP/models/med.py#L637-L673","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a plain 2-D mask of shape [batch_size, seq_length] (1.0 keep, 0.0 mask) and let the model extend it","If you need a custom 2-D attention pattern, supply a 3-D [batch, from_len, to_len] mask (decoder path only)","Squeeze/remove the head/broadcast dims: mask = mask.squeeze(1).squeeze(1) for HF-style masks"],"exampleFix":"// before\nout = model(input_ids=ids, attention_mask=huggingface_4d_mask)\n\n// after\nmask_2d = hf_mask.squeeze(1).squeeze(1)  # [B, L]\nout = model(input_ids=ids, attention_mask=mask_2d)","handlingStrategy":"validation","validationCode":"def normalize_attention_mask(mask, is_decoder=False):\n    allowed = (2, 3) if is_decoder else (2,)\n    if mask.dim() not in allowed:\n        mask = mask.reshape(mask.shape[0], -1)\n    assert mask.dim() in allowed\n    return mask","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize on 2-D [B, L] masks across your pipeline","Squeeze HF-style 4-D masks before passing to BLIP models","Log mask shape in debug builds to catch rank drift early"],"tags":["blip","bert","attention-mask","shape-mismatch"],"backgroundTag":null,"analyzedSha":"ae05379cc97bc4361ec8b4ec90193dab21be763f","analyzedAt":"2026-08-15T04:23:59.533Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}