{"record":{"id":"e01a12368d40dc62","repo":"PaddlePaddle/PaddleOCR","slug":"decoder-start-token-id-expected-to-have-length-e01a12","errorCode":null,"errorMessage":"`decoder_start_token_id` expected to have length {batch_size} but got {len(decoder_start_token_id)}","messagePattern":"`decoder_start_token_id` expected to have length (.+?) but got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ppocr/modeling/heads/rec_unimernet_head.py","lineNumber":2178,"sourceCode":"        batch_size,\n        model_kwargs,\n        decoder_start_token_id=None,\n        bos_token_id=None,\n    ):\n        if model_kwargs is not None and \"decoder_input_ids\" in model_kwargs:\n            decoder_input_ids = model_kwargs.pop(\"decoder_input_ids\")\n        elif \"input_ids\" in model_kwargs:\n            decoder_input_ids = model_kwargs.pop(\"input_ids\")\n        else:\n            decoder_input_ids = None\n\n        decoder_start_token_id = self._get_decoder_start_token_id(\n            decoder_start_token_id, bos_token_id\n        )\n\n        if isinstance(decoder_start_token_id, list):\n            if len(decoder_start_token_id) != batch_size:\n                raise ValueError(\n                    f\"`decoder_start_token_id` expected to have length {batch_size} but got {len(decoder_start_token_id)}\"\n                )\n            decoder_input_ids_start = paddle.to_tensor(\n                decoder_start_token_id,\n                dtype=paddle.int64,\n            )\n            decoder_input_ids_start = decoder_input_ids_start.view(-1, 1)\n        else:\n            decoder_input_ids_start = (\n                paddle.ones(\n                    (batch_size, 1),\n                    dtype=paddle.int64,\n                )\n                * decoder_start_token_id\n            )\n\n        if decoder_input_ids is None:\n            decoder_input_ids = decoder_input_ids_start","sourceCodeStart":2160,"sourceCodeEnd":2196,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/ppocr/modeling/heads/rec_unimernet_head.py#L2160-L2196","documentation":"decoder_start_token_id may be a per-sample list, in which case it must have exactly batch_size entries so each sequence in the batch starts from its own token. The helper validates len(decoder_start_token_id) == batch_size before converting the list to a tensor.","triggerScenarios":"Providing a list of start ids shorter/longer than the batch, e.g. hardcoding one id while running batch inference, or passing a python range of ids of the wrong length.","commonSituations":"Prompt-batched decoding where each sample needs a distinct start token; mismatch between dataloader batch size and a fixed start-token list.","solutions":["Pass a single int start token if all samples share it","Or build the list dynamically: [start_ids[i % len(start_ids)] for i in range(batch_size)] / tile to batch_size","Verify input batch size before generate and align the list length"],"exampleFix":"# before\ndecoder_start_token_id = [1, 2]  # batch is 4\n# after\ndecoder_start_token_id = [1, 2, 1, 2]  # or just 1","handlingStrategy":"validation","validationCode":"if isinstance(decoder_start_token_id, list):\n    assert len(decoder_start_token_id) == batch_size, 'start ids must match batch size'\n# or normalize:\nif isinstance(decoder_start_token_id, int):\n    decoder_start_token_id = [decoder_start_token_id] * batch_size","typeGuard":"def start_ids_match(start_ids, batch_size: int) -> bool:\n    return not isinstance(start_ids, list) or len(start_ids) == batch_size","tryCatchPattern":null,"preventionTips":["Use a scalar start token unless per-sample starts are required","Tile per-sample lists to the runtime batch size before generate"],"tags":["paddle","generation","batch","validation"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}