hankcs/HanLP · error · ValueError
You have to specify either input_ids or inputs_embeds
Error message
You have to specify either input_ids or inputs_embeds
What it means
CategoricalAccuracy requires top_k >= 1 since it computes the top-k predictions; top_k=0 or negative values are meaningless and raise ValueError immediately in __init__.
Source
Thrown at hanlp/components/amr/amrbart/model_interface/modeling_bart.py:796
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
)
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 input_ids and 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 input_ids or inputs_embeds")
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
embed_pos = self.embed_positions(input_shape)
hidden_states = inputs_embeds + embed_pos
hidden_states = self.layernorm_embedding(hidden_states)
hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
# expand attention_mask
if attention_mask is not None:
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
attention_mask = _expand_mask(attention_mask, inputs_embeds.dtype)
encoder_states = () if output_hidden_states else None
all_attentions = () if output_attentions else None
View on GitHub (pinned to ddb1299bdd)
Solutions
- Set top_k to a positive int (1 for exact match, k<=num_classes for top-k)
- Validate top_k > 0 in config-loading code before constructing the metric
- Default to top_k=1 by omitting the argument
Example fix
# before metric = CategoricalAccuracy(top_k=0) # after metric = CategoricalAccuracy(top_k=1)
Defensive patterns
Strategy: validation
Validate before calling
assert isinstance(top_k, int) and top_k >= 1, f'top_k must be >= 1, got {top_k}' Type guard
def valid_top_k(k) -> bool:
return isinstance(k, int) and k >= 1 Try / catch
try:
m = CategoricalAccuracy(top_k=top_k)
except ValueError:
m = CategoricalAccuracy(top_k=1) Prevention
- Validate computed config values before constructing metrics
- Default to omitting top_k (defaults to 1)
When it happens
Trigger: Constructing CategoricalAccuracy(top_k=0) or CategoricalAccuracy(top_k=-1), often via a miscomputed config value.
Common situations: Config generation code computing top_k arithmetically (e.g. num_classes - num_classes); JSON/YAML typo 0 or -1; passing None-like defaults that become 0.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- You cannot specify both input_ids and inputs_embeds at the s
- The head_mask should be specified for {len(self.layers)} lay
- You cannot specify both decoder_input_ids and decoder_inputs
- 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/9e63b4fa9f3d7575.
Report an issue: GitHub.