hankcs/HanLP · error · ValueError
The `{mask_name}` should be specified for {len(self.layers)}
Error message
The `{mask_name}` should be specified for {len(self.layers)} layers, but it is for {head_mask.size()[0]}. What it means
When a mask is supplied to the elementwise accuracy call, it must match predictions.shape exactly so the metric knows which cells to count. A mask with different shape (e.g. (B,) for (B, C) predictions) raises ValueError.
Source
Thrown at hanlp/components/amr/amrbart/model_interface/modeling_bart.py:1056
# embed positions
positions = self.embed_positions(input_shape, past_key_values_length)
hidden_states = inputs_embeds + positions
hidden_states = self.layernorm_embedding(hidden_states)
hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
# decoder layers
all_hidden_states = () if output_hidden_states else None
all_self_attns = () if output_attentions else None
all_cross_attentions = () if (output_attentions and encoder_hidden_states is not None) else None
next_decoder_cache = () if use_cache else None
# check if head_mask/cross_attn_head_mask has a correct number of layers specified if desired
for attn_mask, mask_name in zip([head_mask, cross_attn_head_mask], ["head_mask", "cross_attn_head_mask"]):
if attn_mask is not None:
if attn_mask.size()[0] != (len(self.layers)):
raise ValueError(
f"The `{mask_name}` should be specified for {len(self.layers)} layers, but it is for"
f" {head_mask.size()[0]}."
)
for idx, decoder_layer in enumerate(self.layers):
# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
if output_hidden_states:
all_hidden_states += (hidden_states,)
dropout_probability = random.uniform(0, 1)
if self.training and (dropout_probability < self.layerdrop):
continue
past_key_value = past_key_values[idx] if past_key_values is not None else None
if self.gradient_checkpointing and self.training:
if use_cache:
logger.warning(View on GitHub (pinned to ddb1299bdd)
Solutions
- Expand the mask to predictions' shape: mask.unsqueeze(-1).expand_as(predictions) or a Boolean multi-hot mask
- Recompute the mask after any reshape of predictions
- Validate mask.shape == predictions.shape before the call
Example fix
# before metric(predictions, gold, mask) # mask (B,) # after metric(predictions, gold, mask.unsqueeze(-1).expand_as(predictions))
Defensive patterns
Strategy: validation
Validate before calling
assert mask is None or mask.size() == predictions.size(), (mask.shape if mask is not None else None, predictions.shape)
Type guard
import torch
def mask_matches(mask: torch.Tensor, predictions: torch.Tensor) -> bool:
return mask.size() == predictions.size() Try / catch
try:
metric(predictions, gold, mask)
except ValueError as e:
if 'mask' in str(e) and mask is not None:
metric(predictions, gold, mask.unsqueeze(-1).expand_as(predictions))
else:
raise Prevention
- Expand 1-D length masks to prediction shape before passing
- Recompute masks after reshaping predictions
- Standardize mask conventions (bool, prediction-shaped) across the codebase
When it happens
Trigger: Passing mask of shape (B,) or (B, T) alongside (B, C) / (B, T, C) predictions to the same-shape __call__ variant.
Common situations: Reusing a length-based 1-D mask from seq labeling in a multi-label metric; mask computed before a view/reshape of predictions; padding mask broadcasting assumptions.
Related errors
- The head_mask should be specified for {len(self.layers)} lay
- You have to specify either decoder_input_ids or decoder_inpu
- the first two dimensions of emissions and mask must match, g
- Unsupported argument type: {item}
- Attention weights should be of size {(bsz * self.num_heads,
AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27).
Data as JSON: /api/errors/ab8d40889becef8d.
Report an issue: GitHub.