{"record":{"id":"f256015cdde0e049","repo":"lllyasviel/Fooocus","slug":"wrong-shape-for-input-ids-shape-or-attention-f25601","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/nlvr_encoder.py","lineNumber":690,"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":672,"sourceCodeEnd":708,"githubUrl":"https://github.com/lllyasviel/Fooocus/blob/ae05379cc97bc4361ec8b4ec90193dab21be763f/extras/BLIP/models/nlvr_encoder.py#L672-L708","documentation":"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.","triggerScenarios":"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].","commonSituations":"Feeding NLVR with masks produced by other transformer libraries; batching code that stacks masks with extra dims; masking utilities returning [B, 1, L].","solutions":["Pass a 2-D [batch, seq_len] mask of 1s/0s","Reshape/squeeze any pre-extended mask down to 2-D before the call","For custom pairwise attention patterns in the decoder, use a 3-D [B, from, to] mask"],"exampleFix":"// before\nout = encoder(input_ids=ids, attention_mask=mask4d)\n\n// after\nout = encoder(input_ids=ids, attention_mask=mask4d.reshape(mask4d.shape[0], -1))","handlingStrategy":"validation","validationCode":"def as_2d_mask(mask):\n    if mask.dim() != 2:\n        mask = mask.reshape(mask.shape[0], -1)\n    return mask","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass 2-D masks to the NLVR encoder","Reshape external-library masks at the boundary","Test with masks of unexpected rank in unit tests"],"tags":["blip","nlvr","attention-mask","shape-mismatch"],"backgroundTag":null,"analyzedSha":"ae05379cc97bc4361ec8b4ec90193dab21be763f","analyzedAt":"2026-08-15T04:23:59.533Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}