{"record":{"id":"2666a0325f50dd58","repo":"sgl-project/sglang","slug":"causal-block-prompt-count-must-match-causal-block","errorCode":null,"errorMessage":"causal block prompt count must match causal block count, got {} prompts and {} blocks","messagePattern":"causal block prompt count must match causal block count, got (.+?) prompts and (.+?) blocks","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/causal_denoising.py","lineNumber":324,"sourceCode":"            return cond_kwargs\n        return {\n            key: cls._select_block_conditioning(value, block_index, block_count)\n            for key, value in cond_kwargs.items()\n        }\n\n    def _reset_crossattn_cache_for_block(self, batch: Req, *caches) -> None:\n        if self._block_prompt_count(batch) is None:\n            return\n        for cache in caches:\n            if cache is not None:\n                self._reset_crossattn_cache(cache)\n\n    def _validate_block_prompt_count(self, batch: Req, block_sizes: list[int]) -> None:\n        block_count = self._block_prompt_count(batch)\n        if block_count is None:\n            return\n        if block_count != len(block_sizes):\n            raise ValueError(\n                \"causal block prompt count must match causal block count, \"\n                f\"got {block_count} prompts and {len(block_sizes)} blocks\"\n            )\n\n    @staticmethod\n    def _shot_index(batch: Req, block_index: int) -> int:\n        shot_indices = batch.extra.get(CAUSAL_SHOT_INDICES_KEY)\n        if not isinstance(shot_indices, list) or block_index >= len(shot_indices):\n            return 0\n        return int(shot_indices[block_index])\n\n    def _prepare_causal_dmd_forward_context(\n        self,\n        batch: Req,\n        server_args: ServerArgs,\n    ) -> CausalDMDForwardContext:\n        target_dtype = self._target_dtype()\n        autocast_enabled = self._autocast_enabled(target_dtype, server_args)","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/causal_denoising.py#L306-L342","documentation":"During causal denoising forward, _validate_block_prompt_count checks that the number of block prompts derived from the request equals the number of computed causal blocks (block_sizes); mismatch means prompts cannot be aligned to blocks.","triggerScenarios":"Request carries N block prompts (e.g. per-scene prompts) while frame count/block size yields M != N blocks — e.g. 121 frames with block size 25 and independent first frame gives 5 blocks but 6 prompts.","commonSituations":"Changing num_frames or num_frames_per_block without regenerating per-block prompts; independent_first_frame toggling changes block count by 1; prompt-count derived from shot durations that disagree with total frames.","solutions":["Regenerate block prompts to match: len(prompts) == num_blocks computed from frame count and block size","Adjust num_frames so frame math matches the number of prompts","Check independent_first_frame consistency between prompt generation and inference"],"exampleFix":"# before\nreq.num_frames = 121  # 1 + 5*24, prompts for 6 blocks with independent_first_frame=False\n# after\nreq.num_frames = 121  # keep; set independent_first_frame=True and 6 block prompts -> first frame + 5 blocks of 24","handlingStrategy":"validation","validationCode":"if independent_first_frame and image_latent is None:\n    expected = 1 + (num_frames - 1)//block\nelse:\n    expected = num_frames//block\nassert len(block_prompts) == expected","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compute block prompt count from the same formula the stage uses","Automate prompt expansion via expand_causal_block_prompts instead of manual lists"],"tags":["causal-denoising","prompt-validation","block-config"],"backgroundTag":"input-count-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}