{"record":{"id":"5d679959606ee460","repo":"hankcs/HanLP","slug":"head-mask-for-a-single-layer-should-be-of-size-s","errorCode":null,"errorMessage":"Head mask for a single layer should be of size {(self.num_heads,)}, but is {layer_head_mask.size()}","messagePattern":"Head mask for a single layer 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":248,"sourceCode":"        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\"\n                    f\" {layer_head_mask.size()}\"\n                )\n            attn_weights = layer_head_mask.view(1, -1, 1, 1) * attn_weights.view(bsz, self.num_heads, tgt_len, src_len)\n            attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)\n\n        if output_attentions:\n            # this operation is a bit awkward, but it's required to\n            # make sure that attn_weights keeps its gradient.\n            # In order to do so, attn_weights have to be reshaped\n            # twice and have to be reused in the following\n            attn_weights_reshaped = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)\n            attn_weights = attn_weights_reshaped.view(bsz * self.num_heads, tgt_len, src_len)\n        else:\n            attn_weights_reshaped = None\n\n        attn_probs = nn.functional.dropout(attn_weights, p=self.dropout, training=self.training)\n","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/components/amr/amrbart/model_interface/modeling_bart.py#L230-L266","documentation":"TimeDistributed._reshape_tensor requires an input tensor with at least 3 dims (batch, time, features) so it can squash batch and time. A 2-D (or 1-D) tensor has no time dimension to distribute over and raises RuntimeError.","triggerScenarios":"Passing a (batch, features) tensor into a TimeDistributed-wrapped module, e.g. feeding sentence-level pooled vectors into a per-token layer.","commonSituations":"Mixing up sequence vs sentence-level inputs in a pipeline; a previous layer squeezed/pooled away the time dimension; feeding already-flattened (B*T, F) tensors that were meant to stay 3-D.","solutions":["Keep inputs 3-D: (batch, time_steps, features) — unsqueeze(1) for single-step sequences","Apply the inner module directly (no TimeDistributed) for 2-D sentence-level inputs","Check intermediate shapes with prints/asserts when building custom architectures"],"exampleFix":"# before\nout = time_dist_layer(pooled_2d)  # (B, F)\n# after\nout = time_dist_layer(pooled_2d.unsqueeze(1))  # (B, 1, F)","handlingStrategy":"type-guard","validationCode":"import torch\nassert x.dim() >= 3, f'TimeDistributed needs (B,T,F), got {tuple(x.shape)}'","typeGuard":"def is_sequence_tensor(x) -> bool:\n    return isinstance(x, torch.Tensor) and x.dim() >= 3","tryCatchPattern":"try:\n    out = layer(x)\nexcept RuntimeError as e:\n    if 'No dimension to distribute' in str(e):\n        out = layer(x.unsqueeze(1)) if x.dim() == 2 else None\n    if out is None:\n        raise\n","preventionTips":["Keep (batch, time, features) rank through sequence pipelines","Apply the inner module directly for 2-D sentence-level inputs","Assert ranks between pipeline stages during development"],"tags":["hanlp","time-distributed","dimension-error","tensor-shape"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}