hankcs/HanLP · error · ValueError
Attention mask should be of size {(bsz, 1, tgt_len, src_len)
Error message
Attention mask should be of size {(bsz, 1, tgt_len, src_len)}, but is {attention_mask.size()} What it means
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.
Source
Thrown at hanlp/components/amr/amrbart/model_interface/modeling_bart.py:238
past_key_value = (key_states, value_states)
proj_shape = (bsz * self.num_heads, -1, self.head_dim)
query_states = self._shape(query_states, tgt_len, bsz).view(*proj_shape)
key_states = key_states.view(*proj_shape)
value_states = value_states.view(*proj_shape)
src_len = key_states.size(1)
attn_weights = torch.bmm(query_states, key_states.transpose(1, 2))
if attn_weights.size() != (bsz * self.num_heads, tgt_len, src_len):
raise ValueError(
f"Attention weights should be of size {(bsz * self.num_heads, tgt_len, src_len)}, but is"
f" {attn_weights.size()}"
)
if attention_mask is not None:
if attention_mask.size() != (bsz, 1, tgt_len, src_len):
raise ValueError(
f"Attention mask should be of size {(bsz, 1, tgt_len, src_len)}, but is {attention_mask.size()}"
)
attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len) + attention_mask
attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)
attn_weights = nn.functional.softmax(attn_weights, dim=-1)
if layer_head_mask is not None:
if layer_head_mask.size() != (self.num_heads,):
raise ValueError(
f"Head mask for a single layer should be of size {(self.num_heads,)}, but is"
f" {layer_head_mask.size()}"
)
attn_weights = layer_head_mask.view(1, -1, 1, 1) * attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)
if output_attentions:
# this operation is a bit awkward, but it's required toView on GitHub (pinned to ddb1299bdd)
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
Example fix
# before layer = TimeDistributed(nn.Linear(10, 5)) out = layer(meta_strings) # no tensor input # after out = layer(embedded_tokens) # shape (B, T, 10)
Defensive patterns
Strategy: validation
Validate before calling
import torch assert any(isinstance(x, torch.Tensor) and x.dim() >= 3 for x in inputs), 'TimeDistributed needs a (B,T,*) tensor input'
Type guard
def has_tensor_input(args) -> bool:
return any(isinstance(a, torch.Tensor) for a in args) Try / catch
try:
out = layer(*inputs)
except RuntimeError as e:
if 'time-distribute' in str(e):
raise ValueError('missing tensor input') from e
raise Prevention
- Always pass the embedded token tensor into time-distributed layers
- Convert non-tensor features to embeddings earlier in the pipeline
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Head mask for a single layer should be of size {(self.num_he
- error
- output ({}) must be of type bool or str
- Call fit or load before evaluate.
- Unrecognized devices {devices}
AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27).
Data as JSON: /api/errors/c272c543ffbab120.
Report an issue: GitHub.