Skip to content

fix: recompute scheduled tokens after prefix-cache allocation#210

Open
tj1235 wants to merge 1 commit into
GeeeekExplorer:mainfrom
tj1235:fix/prefix-cache-schedule-bug
Open

fix: recompute scheduled tokens after prefix-cache allocation#210
tj1235 wants to merge 1 commit into
GeeeekExplorer:mainfrom
tj1235:fix/prefix-cache-schedule-bug

Conversation

@tj1235

@tj1235 tj1235 commented Apr 22, 2026

Copy link
Copy Markdown

This fixes a scheduling bug when prefix cache hits.

Previously, schedule() computed num_tokens before block_manager.allocate(seq).
However, allocate(seq) may increase seq.num_cached_tokens when a prefix block
is reused from cache. This made seq.num_scheduled_tokens stale and could cause
prepare_prefill() to build an invalid [start, end) range, eventually leading
to block_table index overflow.

Fix:

  • recompute num_tokens after allocate(seq)
  • then set seq.num_scheduled_tokens from the updated cached-token count

Reproduction:

  • use repeated-prefix prompts so the first 256-token block can hit prefix cache
  • for a 465-token sequence, allocation may set seq.num_cached_tokens = 256
  • before this fix, seq.num_scheduled_tokens could still remain 465
  • then prepare_prefill() would compute start = 256, end = 721, which exceeds
    the true sequence length and causes out-of-range access on seq.block_table

Validation:

  • after this fix, the same case schedules only the uncached suffix tokens
  • seq.num_scheduled_tokens becomes 465 - 256 = 209
  • prefill range stays valid and no overflow occurs

@GeeeekExplorer

Copy link
Copy Markdown
Owner

Thank your advice. We fixed it in newest PR #218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants