{"record":{"id":"20d09158c5121e22","repo":"PaddlePaddle/PaddleOCR","slug":"if-eos-token-id-is-defined-make-sure-that-pad","errorCode":null,"errorMessage":"If `eos_token_id` is defined, make sure that `pad_token_id` is defined.","messagePattern":"If `eos_token_id` is defined, make sure that `pad_token_id` is defined\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ppocr/modeling/heads/rec_ppformulanet_head.py","lineNumber":1140,"sourceCode":"                decoder_attention_mask=decoder_attention_mask,\n                encoder_outputs=encoder_outputs,\n                past_key_values=past_key_values,\n                return_dict=True,\n                output_attentions=False,\n                output_hidden_states=False,\n            )\n\n            if use_parallel:\n                next_token_logits = outputs.logits[:, -parallel_step:, :]\n            else:\n                next_token_logits = outputs.logits[:, -1, :]\n            next_tokens_scores = self.logits_processor(input_ids, next_token_logits)\n            next_tokens = paddle.argmax(next_tokens_scores, axis=-1)\n\n            if eos_token_id is not None:\n                # False\n                if pad_token_id is None:\n                    raise ValueError(\n                        \"If `eos_token_id` is defined, make sure that `pad_token_id` is defined.\"\n                    )\n                next_tokens = next_tokens * unfinished_sequences + pad_token_id * (\n                    1 - unfinished_sequences\n                )\n            if use_parallel:\n                input_ids = paddle.concat([input_ids, next_tokens], axis=-1)\n                decoder_input_ids = next_tokens\n            else:\n                input_ids = paddle.concat(\n                    [input_ids, next_tokens.unsqueeze(1)], axis=-1\n                )\n                decoder_input_ids = next_tokens.unsqueeze(1)\n\n            past_length = past_key_values[0][0].shape[2]\n\n            past_key_values = outputs.past_key_values\n            cache_position = cache_position[-1:] + 1","sourceCodeStart":1122,"sourceCodeEnd":1158,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/ppocr/modeling/heads/rec_ppformulanet_head.py#L1122-L1158","documentation":"During greedy decoding, once sequences finish they are replaced by pad tokens (next_tokens * unfinished + pad * finished). If eos_token_id is configured but pad_token_id is None, there is no value to pad finished sequences with, so the loop raises.","triggerScenarios":"Running generate/greedy_search with a config that sets eos_token_id but leaves pad_token_id unset (None), and eos_token_id is not None at the call site.","commonSituations":"Custom tokenizers or trimmed vocab configs where pad was removed; porting a config from sample() usage where pad was irrelevant; overriding eos_token_id per-call while forgetting pad_token_id.","solutions":["Set pad_token_id in the model config (any token id safe to fill with, commonly 0 or the tokenizer's pad)","Or pass pad_token_id explicitly to the generate call alongside eos_token_id","If your vocab truly has no pad, allocate a dedicated unused id as pad"],"exampleFix":"# before\nconfig.eos_token_id = 2  # pad_token_id stays None\n# after\nconfig.eos_token_id = 2\nconfig.pad_token_id = 0","handlingStrategy":"validation","validationCode":"def check_gen_tokens(eos_token_id, pad_token_id):\n    if eos_token_id is not None and pad_token_id is None:\n        raise ValueError('pad_token_id must be set when eos_token_id is set')\n    return eos_token_id, pad_token_id\n# check_gen_tokens(cfg.get('eos_token_id'), cfg.get('pad_token_id'))","typeGuard":"def token_config_ok(cfg) -> bool:\n    return cfg.get('eos_token_id') is None or cfg.get('pad_token_id') is not None","tryCatchPattern":"null  # configuration error; validate before generation rather than mid-loop catching","preventionTips":["Set pad_token_id in every config that sets eos_token_id","Add a pre-flight check in your inference entry point","Verify pad/eos ids exist in the tokenizer vocab after any vocab surgery"],"tags":["generation","token-config","ppformulanet"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}