PaddlePaddle/PaddleOCR · 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

The decoder forward rejects calls that supply both input_ids and inputs_embeds. They are two mutually exclusive ways to provide the input sequence (token ids vs pre-computed embeddings); giving both makes the input ambiguous.

Source

Thrown at ppocr/modeling/heads/rec_ppformulanet_head.py:446

        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 = input_ids
            input_shape = input.shape
            input_ids = input_ids.reshape([-1, input_shape[-1]])
        elif inputs_embeds is not None:
            input_shape = inputs_embeds.shape[:-1]
            input = inputs_embeds[:, :, -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
        )

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. Pass exactly one of the two: input_ids OR inputs_embeds
  2. When injecting embeddings, pop input_ids from the kwargs dict first (input_ids = kwargs.pop('input_ids', None) before setting inputs_embeds)
  3. Add an assert in your wrapper that not (input_ids is not None and inputs_embeds is not None) to fail with your own message

Example fix

# before
out = decoder(input_ids=ids, inputs_embeds=emb, ...)
# after
out = decoder(inputs_embeds=emb, ...)  # ids dropped
Defensive patterns

Strategy: validation

Validate before calling

def exclusive_inputs(**kw):
    ids, emb = kw.get('input_ids'), kw.get('inputs_embeds')
    if ids is not None and emb is not None:
        raise ValueError('pass input_ids or inputs_embeds, not both')
    return kw
# forward(**exclusive_inputs(input_ids=ids, inputs_embeds=emb))

Type guard

null

Try / catch

try:
    out = decoder(input_ids=ids, inputs_embeds=emb)
except ValueError as e:
    if 'cannot specify both' in str(e):
        out = decoder(inputs_embeds=emb)
    else:
        raise

Prevention

When it happens

Trigger: Calling the PP-FormulaNet decoder forward (or a wrapper that forwards **kwargs into it) with both input_ids=... and inputs_embeds=... non-None, e.g. when a caller pre-embeds tokens but forgets to drop input_ids from kwargs.

Common situations: Adapting generation code that always passes input_ids while adding embedding injection for prompt/prefix tuning; kwargs plumbing where model_kwargs retains input_ids and the caller also sets inputs_embeds; copying HF Bart-like code into this ported decoder.

Related errors


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/f17e807d50140266. Report an issue: GitHub.