hankcs/HanLP · error · ValueError
You cannot specify both decoder_input_ids and decoder_inputs
Error message
You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time
What it means
CategoricalAccuracy verifies that gold label ids are within [0, num_classes). A gold id >= num_classes means the label vocabulary is larger than the prediction head (or the label is a padding/special token id), so __call__ raises ValueError.
Source
Thrown at hanlp/components/amr/amrbart/model_interface/modeling_bart.py:1014
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.
output_hidden_states (`bool`, *optional*):
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors
for more detail.
return_dict (`bool`, *optional*):
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
)
View on GitHub (pinned to ddb1299bdd)
Solutions
- Rebuild/reload the model so its output layer matches the label vocab size
- Remap or clip gold ids: filter out or remap pad/special-token labels before calling the metric
- Verify len(label_vocab) == predictions.size(-1) on a sample batch
Example fix
# before metric(predictions, gold) # gold contains 50 with C=50 # after gold = gold[gold < predictions.size(-1)] # or remap; ensure C == vocab size metric(predictions, gold)
Defensive patterns
Strategy: validation
Validate before calling
num_classes = predictions.size(-1) assert (gold_labels < num_classes).all() and (gold_labels >= 0).all(), 'gold ids out of range'
Type guard
import torch
def labels_in_range(gold: torch.Tensor, num_classes: int) -> bool:
return bool((gold >= 0).all() and (gold < num_classes).all()) Try / catch
try:
metric(predictions, gold)
except ValueError as e:
if 'id >=' in str(e):
raise ValueError('label vocab larger than model head; rebuild model or remap labels') from e
raise Prevention
- Build the label vocab before the output layer and freeze it
- Remap/filter pad and special-token ids before metric calls
- Check len(vocab) == model.out_features after loading checkpoints
When it happens
Trigger: Calling the metric where gold_labels contains ids >= predictions.size(-1), e.g. a vocab with 100 classes feeding gold id 100+ into a 100-logit model, or -1 padding converted to a large id.
Common situations: Label vocab built after the model head (off-by-one from an UNK/pad class); loading a pretrained model on data with extra labels; padding index remapping; finetuning with a new class but old head size.
Related errors
- You cannot specify both input_ids and inputs_embeds at the s
- You have to specify either input_ids or inputs_embeds
- The head_mask should be specified for {len(self.layers)} lay
- You have to specify either decoder_input_ids or decoder_inpu
- The `{mask_name}` should be specified for {len(self.layers)}
AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27).
Data as JSON: /api/errors/07af9e11d1b34e9c.
Report an issue: GitHub.