{"record":{"id":"07af9e11d1b34e9c","repo":"hankcs/HanLP","slug":"you-cannot-specify-both-decoder-input-ids-and-deco","errorCode":null,"errorMessage":"You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time","messagePattern":"You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hanlp/components/amr/amrbart/model_interface/modeling_bart.py","lineNumber":1014,"sourceCode":"            output_attentions (`bool`, *optional*):\n                Whether or not to return the attentions tensors of all attention layers. See `attentions` under\n                returned tensors for more detail.\n            output_hidden_states (`bool`, *optional*):\n                Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors\n                for more detail.\n            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        use_cache = use_cache if use_cache is not None else self.config.use_cache\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 decoder_input_ids and decoder_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 decoder_input_ids or decoder_inputs_embeds\")\n\n        # past_key_values_length\n        past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0\n\n        if inputs_embeds is None:\n            inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale\n\n        attention_mask = self._prepare_decoder_attention_mask(\n            attention_mask, input_shape, inputs_embeds, past_key_values_length\n        )\n","sourceCodeStart":996,"sourceCodeEnd":1032,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/components/amr/amrbart/model_interface/modeling_bart.py#L996-L1032","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nmetric(predictions, gold)  # gold contains 50 with C=50\n# after\ngold = gold[gold < predictions.size(-1)]  # or remap; ensure C == vocab size\nmetric(predictions, gold)","handlingStrategy":"validation","validationCode":"num_classes = predictions.size(-1)\nassert (gold_labels < num_classes).all() and (gold_labels >= 0).all(), 'gold ids out of range'","typeGuard":"import torch\ndef labels_in_range(gold: torch.Tensor, num_classes: int) -> bool:\n    return bool((gold >= 0).all() and (gold < num_classes).all())","tryCatchPattern":"try:\n    metric(predictions, gold)\nexcept ValueError as e:\n    if 'id >=' in str(e):\n        raise ValueError('label vocab larger than model head; rebuild model or remap labels') from e\n    raise","preventionTips":["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"],"tags":["hanlp","metrics","label-index","vocab-mismatch"],"backgroundTag":"index-out-of-range","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}