hankcs/HanLP · error · ValueError
You have to specify either decoder_input_ids or decoder_inpu
Error message
You have to specify either decoder_input_ids or decoder_inputs_embeds
What it means
This variant of __call__ (e.g. for multi-label / elementwise accuracy) requires gold_labels to have exactly the same shape as predictions. A mismatch (e.g. class indices of lower rank, or a different batch size) raises ValueError.
Source
Thrown at hanlp/components/amr/amrbart/model_interface/modeling_bart.py:1021
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
)
use_cache = use_cache if use_cache is not None else self.config.use_cache
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
# retrieve input_ids and inputs_embeds
if input_ids is not None and inputs_embeds is not None:
raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
elif input_ids is not None:
input_shape = input_ids.size()
input_ids = input_ids.view(-1, input_shape[-1])
elif inputs_embeds is not None:
input_shape = inputs_embeds.size()[:-1]
else:
raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
# past_key_values_length
past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
attention_mask = self._prepare_decoder_attention_mask(
attention_mask, input_shape, inputs_embeds, past_key_values_length
)
# expand encoder attention mask
if encoder_hidden_states is not None and encoder_attention_mask is not None:
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
encoder_attention_mask = _expand_mask(encoder_attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1])
# embed positions
positions = self.embed_positions(input_shape, past_key_values_length)View on GitHub (pinned to ddb1299bdd)
Solutions
- Make gold the same shape as predictions (e.g. one-hot / multi-hot encode targets)
- Verify batch alignment between predictions and targets
- Use the index-style CategoricalAccuracy call path for single-label data
Example fix
# before metric(predictions, gold_idx) # (B,C) vs (B,) # after from torch.nn.functional import one_hot metric(predictions, one_hot(gold_idx, num_classes=predictions.size(-1)))
Defensive patterns
Strategy: validation
Validate before calling
assert gold_labels.size() == predictions.size(), (gold_labels.shape, predictions.shape)
Type guard
import torch
def same_shape(a: torch.Tensor, b: torch.Tensor) -> bool:
return a.size() == b.size() Try / catch
try:
metric(predictions, gold, mask)
except ValueError:
if gold.dim() == predictions.dim() - 1:
gold = torch.nn.functional.one_hot(gold, predictions.size(-1))
metric(predictions, gold, mask)
else:
raise Prevention
- One-hot/multi-hot encode targets for elementwise metrics
- Ensure predictions and targets come from the same batch
- Add shape asserts in eval harnesses
When it happens
Trigger: Calling the metric with gold_labels.shape != predictions.shape — e.g. predictions (B, C) scores with gold (B,) indices, or mismatched batch sizes from misaligned batches.
Common situations: Using elementwise/multi-label metrics with single-label data or vice versa; batching bugs where predictions and targets come from different loaders; leftover code assuming index-style gold labels.
Related errors
- The head_mask should be specified for {len(self.layers)} lay
- The `{mask_name}` should be specified for {len(self.layers)}
- Unsupported argument type: {item}
- Attention weights should be of size {(bsz * self.num_heads,
- You cannot specify both input_ids and inputs_embeds at the s
AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27).
Data as JSON: /api/errors/19211abd47dea194.
Report an issue: GitHub.