sgl-project/sglang · error · ValueError

causal block prompt count must match causal block count, got

Error message

causal block prompt count must match causal block count, got {} prompts and {} blocks

What it means

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.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines_core/stages/causal_denoising.py:324

            return cond_kwargs
        return {
            key: cls._select_block_conditioning(value, block_index, block_count)
            for key, value in cond_kwargs.items()
        }

    def _reset_crossattn_cache_for_block(self, batch: Req, *caches) -> None:
        if self._block_prompt_count(batch) is None:
            return
        for cache in caches:
            if cache is not None:
                self._reset_crossattn_cache(cache)

    def _validate_block_prompt_count(self, batch: Req, block_sizes: list[int]) -> None:
        block_count = self._block_prompt_count(batch)
        if block_count is None:
            return
        if block_count != len(block_sizes):
            raise ValueError(
                "causal block prompt count must match causal block count, "
                f"got {block_count} prompts and {len(block_sizes)} blocks"
            )

    @staticmethod
    def _shot_index(batch: Req, block_index: int) -> int:
        shot_indices = batch.extra.get(CAUSAL_SHOT_INDICES_KEY)
        if not isinstance(shot_indices, list) or block_index >= len(shot_indices):
            return 0
        return int(shot_indices[block_index])

    def _prepare_causal_dmd_forward_context(
        self,
        batch: Req,
        server_args: ServerArgs,
    ) -> CausalDMDForwardContext:
        target_dtype = self._target_dtype()
        autocast_enabled = self._autocast_enabled(target_dtype, server_args)

View on GitHub (pinned to 0132848349)

Solutions

  1. Regenerate block prompts to match: len(prompts) == num_blocks computed from frame count and block size
  2. Adjust num_frames so frame math matches the number of prompts
  3. Check independent_first_frame consistency between prompt generation and inference

Example fix

# before
req.num_frames = 121  # 1 + 5*24, prompts for 6 blocks with independent_first_frame=False
# after
req.num_frames = 121  # keep; set independent_first_frame=True and 6 block prompts -> first frame + 5 blocks of 24
Defensive patterns

Strategy: validation

Validate before calling

if independent_first_frame and image_latent is None:
    expected = 1 + (num_frames - 1)//block
else:
    expected = num_frames//block
assert len(block_prompts) == expected

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/2666a0325f50dd58. Report an issue: GitHub.