Skip to content

fix: finish chunked prefill when first chunk reaches prompt end (#179) - #183

Merged
inureyes merged 2 commits into
lablup:mainfrom
ujwal-setlur:fix/179-chunked-prefill-empty-continuation
Jun 9, 2026
Merged

inureyes merged 2 commits into
lablup:mainfrom
ujwal-setlur:fix/179-chunked-prefill-empty-continuation

Conversation

@ujwal-setlur

@ujwal-setlur ujwal-setlur commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #179 — a std::invalid_argument: [squeeze] Cannot squeeze axis 1 with size 0 crash on the third turn of a multi-turn chat when the prompt-prefix cache is enabled (Qwen3-Coder-30B-A3B-Instruct, but model-agnostic).

Root cause

The chunked-vs-full prefill decision in prefill_sequence keys off the full prompt length, but start_chunked_prefill only processes the suffix [prefill_start_offset..]. On a prompt-cache hit the adopted prefix can cover all but a short suffix, so when the full prompt clears the 512-token chunk threshold the suffix still fits entirely in chunk 0.

In that case chunk 0 already reaches the end of the prompt, yet start_chunked_prefill unconditionally stored the sequence for continuation — unlike continue_chunked_prefill, which guards on end < total. On the next tick continue_chunked_prefill ran with offset == total, sliced an empty [total..total] chunk, and fed a [1, 0] input to the model. The resulting [1, 0, vocab] logits then crashed in slice_last_logits, which slices the last position and squeezes axis 1 (size 0).

This is why the crash is cache-only (a cold prefill has prefill_start_offset == 0, so chunk 0 never reaches the end) and turn-3-specific (turn 3 is where the prompt first crosses the chunk threshold; turn 2's 455-token prompt stays under 512 and takes the full-prefill path).

Fix

After processing chunk 0, if end >= prompt_tokens.len() finish the prefill and sample the first token (mirroring the final-chunk path in continue_chunked_prefill) instead of queuing an empty continuation. This guarantees chunked_prefill_seq is only ever stored with prefill_offset < total, so continue_chunked_prefill can never receive an empty chunk.

Adds two regression tests covering the first-chunk-terminal boundary: the long-cache-hit case (terminal → finish) and the cold long-prompt case (non-terminal → continue).

Verification

  • cargo test --features metal,accelerate scheduler — all scheduler tests pass, including the two new ones.
  • End-to-end repro.mjs from the issue (Qwen3-Coder-30B-A3B-Instruct-MLX-8bit, prompt cache on):
    • Released build: crashes on turn 3 with the reported [squeeze] abort.
    • This branch: all 5 turns complete cleanly; TTFT stays flat (792 ms cold → 126–208 ms warm), confirming the cache is still active and the chunked-prefill-on-cache-hit path is genuinely exercised.

…up#179)

The chunked-vs-full prefill decision in `prefill_sequence` keys off the
*full* prompt length, but `start_chunked_prefill` only processes the
suffix `[prefill_start_offset..]`. On a prompt-cache hit the adopted
prefix can cover all but a short suffix, so when the full prompt clears
the 512-token chunk threshold the suffix still fits entirely in chunk 0.

In that case chunk 0 already reaches the end of the prompt, yet
`start_chunked_prefill` unconditionally stored the sequence for
continuation (unlike `continue_chunked_prefill`, which guards on
`end < total`). On the next tick `continue_chunked_prefill` ran with
`offset == total`, slicing an empty `[total..total]` chunk and feeding a
`[1, 0]` input to the model. The resulting `[1, 0, vocab]` logits then
crashed in `slice_last_logits`:

  std::invalid_argument: [squeeze] Cannot squeeze axis 1 with size 0

This reproduced on multi-turn chat (Qwen3-Coder-30B-A3B-Instruct, prompt
cache on) on turn 3, where the prompt first crosses the chunk threshold —
exactly matching the report. Disabling the cache forces `start_offset=0`,
so chunk 0 never reaches the end and the bug is masked.

Fix: after processing chunk 0, if `end >= prompt_tokens.len()` finish the
prefill and sample the first token (mirroring the final-chunk path in
`continue_chunked_prefill`) instead of queuing an empty continuation. This
guarantees `chunked_prefill_seq` is only ever stored with
`prefill_offset < total`, so `continue_chunked_prefill` can never receive
an empty chunk.

Adds two regression tests covering the first-chunk-terminal boundary for
both the long-cache-hit case (terminal) and the cold long-prompt case
(non-terminal).

Fixes lablup#179

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cla-assistant

cla-assistant Bot commented Jun 8, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@inureyes

inureyes commented Jun 9, 2026

Copy link
Copy Markdown
Member

Hardening Review Update

Target PR reviewed: #183
Linked issue reviewed: #179

Summary

I reviewed the implementation against the issue's cache-enabled turn-3 crash scenario and pushed follow-up hardening commit 656a5f1.

Findings and fixes

  • Confirmed the root-cause fit: after prompt-prefix cache adoption, the first chunked-prefill chunk can cover the whole remaining suffix even when the full prompt length selected the chunked path.
  • Kept the existing terminal-first-chunk behavior, but factored the chunk-bound calculation into the production helper next_chunked_prefill_range so the regression tests exercise the same predicate used by the scheduler.
  • Added defensive no-progress guards for chunked prefill states where offset >= prompt_len or chunk_size == 0; these now abort the request cleanly instead of allowing a zero-token MLX forward.
  • Added regression coverage for the empty-continuation and zero-chunk-size guard cases.

Security review

No authentication, authorization, file, network, shell, secret-handling, or user-input parsing surface is changed. I did not find a security issue in this PR's scope.

Performance / reliability review

The valid hot path adds only small integer range calculation before the existing forward pass. It avoids no-progress scheduler states and prevents the zero-length input shape from reaching MLX.

Validation

  • cargo fmt --check
  • cargo test --features metal,accelerate scheduler
  • cargo clippy --all-targets --features metal,accelerate -- -D warnings
  • cargo test --release --features metal,accelerate scheduler

I did not rerun the large Qwen3-Coder real-checkpoint repro locally; the original PR body records that E2E validation on this branch.

@inureyes
inureyes merged commit 0f48863 into lablup:main Jun 9, 2026
@ujwal-setlur

Copy link
Copy Markdown
Contributor Author

Thank you for your prompt attention to this!

@ujwal-setlur
ujwal-setlur deleted the fix/179-chunked-prefill-empty-continuation branch June 9, 2026 14:58
@inureyes inureyes added this to the 0.2 milestone Jun 21, 2026
@inureyes inureyes added status:done Completed type:bug Bug fixes, error corrections, or issue resolutions labels Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash ([squeeze] Cannot squeeze axis 1 with size 0) on 3rd turn of multi-turn chat when prompt-prefix cache is enabled — Qwen3-Coder-30B-A3B-Instruct

2 participants