Skip to content

[Bug]: PyTorch backend: logits row counts ignore encoder-replay rows when a step also returns context logits #22703

Done

Description

@lminer

System Info

Reproduction

This affects models whose cache manager issues EncoderReplay plans (CSA2 sparse attention with encoder replay enabled, such as DeepSeek-V4.1), on a step where a request that resumes from a prefix-cache hit shares a prompt step with a request that sets return_context_logits=True.

A request with an EncoderReplay plan recomputes some earlier prompt tokens, so its rows in the step are its chunk plus encoder_replay_tokens(req) (engine/runners/decoder/inputs.py moves begin_compute back by that count, and py_executor.py adds it to num_ctx_tokens). The request that asks for context logits never gets a plan itself: requires_full_decoder_prefill sets its reuse limit to 0, so _resume_context_cache returns before issuing one. The replay rows therefore always belong to a request that did not ask for context logits.

The code that reads the step's logits counts context_chunk_size rows (or 1) per context request and does not add replay rows:

  • the context-logits prefix sum in PyExecutor._sample_async (py_executor.py, num_context_logits_prefix_sum), which HandleLogits uses to slice each request's rows
  • ContextLogitsPlan.project, which advances offset by context_chunk_size and takes hidden.shape[0] - sum(context_chunk_size) as the generation row count

So the context logits stored for the requesting request, the sampling rows of every context request after the replay request, and the generation rows can all be shifted by the replay row count.

Expected behavior

Each request's context logits and sampling row come from its own rows, whatever other requests in the step have encoder replay.

Notes on a fix

The replay row count has to be captured per request before the forward. The model consumes the replay plan, so encoder_replay_tokens() returns 0 afterwards. That count then needs to be applied in the sampler's prefix sum, HandleLogits and the context-logits plan together, and in store_context_logits_from_full_rows if #22038 merges first.

Raised by CodeRabbit on #22038 (thread on context_logits_plan.py); left out of that PR because it is the same on main.

Activity

  1. added
    Pytorch
    on Nov 20, 2026
  2. Super262 commented on Nov 21, 2026

    @Super262
    Contributor

    I’ll work on this. I plan to capture per-request replay row counts before the forward pass and add a regression test for a mixed batch with encoder replay and context logits.

  3. added 5 commits that reference this issue on Nov 22, 2026
    a41cf2c
    e47867d
    ddc75cf
    dc9e363
    66df3a4
  4. added a commit that references this issue on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pytorch

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions