{"record":{"id":"9e63b4fa9f3d7575","repo":"hankcs/HanLP","slug":"you-have-to-specify-either-input-ids-or-inputs-emb","errorCode":null,"errorMessage":"You have to specify either input_ids or inputs_embeds","messagePattern":"You have to specify either input_ids or inputs_embeds","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hanlp/components/amr/amrbart/model_interface/modeling_bart.py","lineNumber":796,"sourceCode":"            return_dict (`bool`, *optional*):\n                Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n        \"\"\"\n        output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions\n        output_hidden_states = (\n            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states\n        )\n        return_dict = return_dict if return_dict is not None else self.config.use_return_dict\n\n        # retrieve input_ids and inputs_embeds\n        if input_ids is not None and inputs_embeds is not None:\n            raise ValueError(\"You cannot specify both input_ids and inputs_embeds at the same time\")\n        elif input_ids is not None:\n            input_shape = input_ids.size()\n            input_ids = input_ids.view(-1, input_shape[-1])\n        elif inputs_embeds is not None:\n            input_shape = inputs_embeds.size()[:-1]\n        else:\n            raise ValueError(\"You have to specify either input_ids or inputs_embeds\")\n\n        if inputs_embeds is None:\n            inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale\n\n        embed_pos = self.embed_positions(input_shape)\n\n        hidden_states = inputs_embeds + embed_pos\n        hidden_states = self.layernorm_embedding(hidden_states)\n        hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)\n\n        # expand attention_mask\n        if attention_mask is not None:\n            # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]\n            attention_mask = _expand_mask(attention_mask, inputs_embeds.dtype)\n\n        encoder_states = () if output_hidden_states else None\n        all_attentions = () if output_attentions else None\n","sourceCodeStart":778,"sourceCodeEnd":814,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/components/amr/amrbart/model_interface/modeling_bart.py#L778-L814","documentation":"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__.","triggerScenarios":"Constructing CategoricalAccuracy(top_k=0) or CategoricalAccuracy(top_k=-1), often via a miscomputed config value.","commonSituations":"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.","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"],"exampleFix":"# before\nmetric = CategoricalAccuracy(top_k=0)\n# after\nmetric = CategoricalAccuracy(top_k=1)","handlingStrategy":"validation","validationCode":"assert isinstance(top_k, int) and top_k >= 1, f'top_k must be >= 1, got {top_k}'","typeGuard":"def valid_top_k(k) -> bool:\n    return isinstance(k, int) and k >= 1","tryCatchPattern":"try:\n    m = CategoricalAccuracy(top_k=top_k)\nexcept ValueError:\n    m = CategoricalAccuracy(top_k=1)","preventionTips":["Validate computed config values before constructing metrics","Default to omitting top_k (defaults to 1)"],"tags":["hanlp","metrics","accuracy","invalid-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}