{"record":{"id":"c272c543ffbab120","repo":"hankcs/HanLP","slug":"attention-mask-should-be-of-size-bsz-1-tgt-len","errorCode":null,"errorMessage":"Attention mask should be of size {(bsz, 1, tgt_len, src_len)}, but is {attention_mask.size()}","messagePattern":"Attention mask 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":238,"sourceCode":"            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\"\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","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/components/amr/amrbart/model_interface/modeling_bart.py#L220-L256","documentation":"TimeDistributed applies a module to every time step by squashing (batch, time, ...) into (batch*time, ...). If no tensor input survived reshaping (some_input is None — all inputs were non-tensors/None), it cannot infer the output batch/time shape and raises RuntimeError.","triggerScenarios":"Calling a TimeDistributed-wrapped module with only non-tensor arguments (strings, lists, None) so that every reshaped input is skipped and some_input stays None.","commonSituations":"Passing string features or None placeholders (e.g. padded non-numeric fields) through a TimeDistributed layer; refactoring input pipelines so the only tensor argument is dropped; empty batch collation producing None fields.","solutions":["Ensure at least one torch.Tensor input with a batch dimension reaches the TimeDistributed module","Filter/convert non-tensor fields to embeddings before the time-distributed layer","Guard the batch for empty/None samples before forward"],"exampleFix":"# before\nlayer = TimeDistributed(nn.Linear(10, 5))\nout = layer(meta_strings)  # no tensor input\n# after\nout = layer(embedded_tokens)  # shape (B, T, 10)","handlingStrategy":"validation","validationCode":"import torch\nassert any(isinstance(x, torch.Tensor) and x.dim() >= 3 for x in inputs), 'TimeDistributed needs a (B,T,*) tensor input'","typeGuard":"def has_tensor_input(args) -> bool:\n    return any(isinstance(a, torch.Tensor) for a in args)","tryCatchPattern":"try:\n    out = layer(*inputs)\nexcept RuntimeError as e:\n    if 'time-distribute' in str(e):\n        raise ValueError('missing tensor input') from e\n    raise","preventionTips":["Always pass the embedded token tensor into time-distributed layers","Convert non-tensor features to embeddings earlier in the pipeline"],"tags":["hanlp","time-distributed","tensor-input","runtime-error"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}