{"record":{"id":"20ad0604d67fe2cd","repo":"hankcs/HanLP","slug":"attention-weights-should-be-of-size-bsz-self-n","errorCode":null,"errorMessage":"Attention weights should be of size {(bsz * self.num_heads, tgt_len, src_len)}, but is {attn_weights.size()}","messagePattern":"Attention weights should be of size (.+?), but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hanlp/components/amr/amrbart/model_interface/modeling_bart.py","lineNumber":231,"sourceCode":"            # if cross_attention save Tuple(torch.Tensor, torch.Tensor) of all cross attention key/value_states.\n            # Further calls to cross_attention layer can then reuse all cross-attention\n            # key/value_states (first \"if\" case)\n            # if uni-directional self-attention (decoder) save Tuple(torch.Tensor, torch.Tensor) of\n            # all previous decoder key/value_states. Further calls to uni-directional self-attention\n            # can concat previous decoder key/value_states to current projected key/value_states (third \"elif\" case)\n            # if encoder bi-directional self-attention `past_key_value` is always `None`\n            past_key_value = (key_states, value_states)\n\n        proj_shape = (bsz * self.num_heads, -1, self.head_dim)\n        query_states = self._shape(query_states, tgt_len, bsz).view(*proj_shape)\n        key_states = key_states.view(*proj_shape)\n        value_states = value_states.view(*proj_shape)\n\n        src_len = key_states.size(1)\n        attn_weights = torch.bmm(query_states, key_states.transpose(1, 2))\n\n        if attn_weights.size() != (bsz * self.num_heads, tgt_len, src_len):\n            raise ValueError(\n                f\"Attention weights should be of size {(bsz * self.num_heads, tgt_len, src_len)}, but is\"\n                f\" {attn_weights.size()}\"\n            )\n\n        if attention_mask is not None:\n            if attention_mask.size() != (bsz, 1, tgt_len, src_len):\n                raise ValueError(\n                    f\"Attention mask should be of size {(bsz, 1, tgt_len, src_len)}, but is {attention_mask.size()}\"\n                )\n            attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len) + attention_mask\n            attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)\n\n        attn_weights = nn.functional.softmax(attn_weights, dim=-1)\n\n        if layer_head_mask is not None:\n            if layer_head_mask.size() != (self.num_heads,):\n                raise ValueError(\n                    f\"Head mask for a single layer should be of size {(self.num_heads,)}, but is\"","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/components/amr/amrbart/model_interface/modeling_bart.py#L213-L249","documentation":"At forward time ScalarMix checks (after slicing by mixture_range) that the number of tensors passed equals the mixture_size it was initialized with. If the encoder stack returns a different number of layers than the mix was built for, forward raises ValueError.","triggerScenarios":"Calling scalar_mix(tensors) with len(tensors) != mixture_size and tensors outside the mixture_range slice, e.g. feeding 12 layer outputs to a ScalarMix built for 3.","commonSituations":"Loading a fine-tuned model with a mismatched number of encoder layers; custom encoders returning only last_hidden_state (1 tensor) into a multi-layer mix; changing layer aggregation without rebuilding ScalarMix.","solutions":["Match the tensors passed to forward with mixture_size, passing exactly the intended layer outputs","Select the specific layers before calling: ScalarMix over layers[k] list","Rebuild the ScalarMix with the correct mixture_size for your encoder"],"exampleFix":"# before\nmix = ScalarMix(mixture_size=12)\nout = mix(encoder_outputs.last_hidden_state)  # 1 tensor\n# after\nmix = ScalarMix(mixture_size=1)\nout = mix([encoder_outputs.last_hidden_state])","handlingStrategy":"validation","validationCode":"assert len(tensors) == mix.mixture_size or mix.mixture_range[1] > mix.mixture_range[0], 'tensor count mismatch'","typeGuard":null,"tryCatchPattern":"try:\n    out = mix(tensors)\nexcept ValueError as e:\n    raise ValueError(f'encoder returns {len(tensors)} layers but ScalarMix expects {mix.mixture_size}') from e","preventionTips":["Log encoder layer counts and mixture_size at setup","Select intended layers explicitly before mixing","Rebuild ScalarMix whenever the encoder config changes"],"tags":["hanlp","scalar-mix","forward","shape-mismatch"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}