Skip to content

[MRV2] Cap torch.topk rows in chunked prompt logprobs - #64914

Merged
ivanium merged 5 commits into
vllm-project:mainfrom
njhill:prompt-logprobs-topk-split
Nov 2, 2026
Merged

ivanium merged 5 commits into
vllm-project:mainfrom
njhill:prompt-logprobs-topk-split

Conversation

@njhill

@njhill njhill commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Overview

Bound the torch.topk workspace in Model Runner V2's chunked prompt-logprobs computation by ranking each logits chunk in row blocks of at most 64M elements. Follow-up to #64841, whose preallocated outputs raised the peak for full-vocab prompt logprobs on short multi-chunk prompts.

Claims

  • Full-vocab prompt logprobs (152k vocab, fp16 logits) peak at 4.98 GiB for a 2048-row prompt, down from 7.54 GiB before [ModelRunner V2] Reduce peak memory of chunked prompt logprobs #64841 and 8.12 GiB with it; 11.94 GiB at 8192 rows, down from 28.12 / 15.07 GiB.
  • ~3.5% added latency, for full-vocab prompt logprobs only.
  • No change for k below ~65k (e.g. the default max_logprobs=20): each chunk is still ranked in one topk call, with identical latency and peak.

Validation

GB200, compute_prompt_logprobs_with_chunking on a 151936-vocab, 1536-hidden fp16 head, median of 20 runs (5 at full vocab); all variants return identical results:

rows k before #64841 #64841 (+ early frees) this PR
2048 5 / 20 / 100 / 1000 — 3.50 / 3.51 / 3.52 / 3.59 ms 3.50 / 3.50 / 3.52 / 3.57 ms
2048 151936 7.54 GiB, 21.7 ms 8.12 GiB, 21.45 ms 4.98 GiB, 22.19 ms
8192 5 / 20 / 100 / 1000 — 13.76 / 13.77 / 13.81 / 14.06 ms 13.75 / 13.78 / 13.81 / 14.07 ms
8192 151936 28.12 GiB, 86.3 ms 15.07 GiB, 85.56 ms 11.94 GiB, 88.54 ms

Peak memory for k ≤ 1000 is 0.30–0.37 GiB in every variant.

pytest tests/v1/sample/test_logprobs.py -k "chunked_prompt_logprobs_match_unchunked or test_all_logprobs or prompt_logprobs_with_chunking_and_preemption or test_prompt_logprobs_mode or test_get_logprobs_and_prompt_logprobs"
16 passed, 7 skipped   # GB200, with a 32M cap; the unit test cases rerun with the final 64M cap: 4 passed

test_chunked_prompt_logprobs_match_unchunked gains a split_topk case that shrinks the cap so 100-row blocks straddle the chunk boundaries.

Details

  • torch.topk with k near the vocab size sorts each row; its workspace grows with rows × k, ~5.5 GiB for a 1024-row chunk of a 152k vocab, and was the step's peak. [ModelRunner V2] Reduce peak memory of chunked prompt logprobs #64841 preallocates the whole prompt's outputs after the first chunk, so for a 2-chunk prompt they (plus the previous chunk's still-referenced results) sat on top of the second chunk's topk. The per-block results and per-chunk logits are now freed before the next block / chunk.
  • Motivation: with measured KV cache sizing ([Core] Extensible KV cache #56492), the sizing warmup's largest prefill requests max_logprobs prompt logprobs, and tests/entrypoints/openai/chat_completion/test_chat_echo.py (--max-logprobs 151936) reserved ~7.5 GiB for this on an 18 GB H200 MIG slice, leaving no KV cache.
  • Cap choice: 32M elements gives 3.80 GiB at 2048 rows but +20% latency; 128M would leave the 2048-row peak near 7.3 GiB.
  • The Triton top-k/top-p sampler kernel isn't a substitute: it masks below a pivot rather than returning sorted ids, and logprob ranks are assigned by position, so full-vocab requests still need a full sort.

Pull Request Checklist
  • I used vLLM's /pr-checklist skill. (Mandatory for agents, optional for humans).

  • AI assistance was used during the creation of this PR.

  • Design Fit: Minimizes impact on core components, reuses existing functionality, and justifies added complexity.

  • Testing and Validation: Validates the change and ensures any added tests are meaningful and reliable, with CI coverage or documented CI resource constraints and validation performed outside CI.

  • Code Quality and Style: Keeps code and comments clear and concise, and updates relevant documentation and examples.

  • Pull Request Contents: Includes a brief summary and relevant links, supports claims with evidence, explains root causes and implementation trade-offs, and follows the contributing guide.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xgy3B2NXY4GnQaw2WwsqpC

torch.topk with k near the vocab size sorts each row, and its workspace
grows with rows x k: ~5.5 GiB for a 1024-row chunk over a 152k vocab.
That dominated the step's peak for full-vocab prompt logprobs, and the
output buffer preallocated since vllm-project#64841 sat on top of it, so a 2048-row
prompt peaked at 9.9 GiB, up from 7.5 GiB.

Rank each chunk in blocks of at most 64M elements and free each block's
results and chunk's logits before the next. Chunks split only for k above
~65k, so smaller k runs as before. For full-vocab prompt logprobs the peak
drops to 5.0 GiB at 2048 rows and 11.9 GiB at 8192 (from 15.1), at ~3.5%
latency.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xgy3B2NXY4GnQaw2WwsqpC
Signed-off-by: Nick Hill <nickhill123@gmail.com>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@mergify mergify Bot added the mrv2 Model Runner V2 specific label Oct 31, 2026
@njhill njhill added the ready ONLY add when PR is ready to merge/full CI is needed label Oct 31, 2026
@njhill

njhill commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

/ci run

@github-actions

Copy link
Copy Markdown

✅ Triggered Buildkite CI #98957 for commit 7041d356dff7.

@njhill

njhill commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

/ci retry

@github-actions

Copy link
Copy Markdown

✅ Triggered Buildkite CI #98968 for commit ea9f0c931943, running 2 failed step(s) from Buildkite CI #98957.

@njhill

njhill commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

/ci retry

@github-actions

Copy link
Copy Markdown

✅ No failed, timed-out, or expired jobs need retrying in Buildkite CI #98968.

@njhill

njhill commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

/ci run

@github-actions

Copy link
Copy Markdown

✅ Triggered Buildkite CI #98969 for commit 47ede135b7cc.

@njhill njhill changed the title [ModelRunner V2] Cap torch.topk rows in chunked prompt logprobs [MRV2] Cap torch.topk rows in chunked prompt logprobs Nov 2, 2026
@ivanium

ivanium commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

/ci retry

@ivanium
ivanium enabled auto-merge (squash) November 2, 2026 00:56
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

✅ Queued 1 failed job(s) for retry in Buildkite CI #98969.

@njhill

njhill commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

/ci retry

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

✅ Triggered Buildkite CI #99030 for commit 5a97420943e4, running 1 failed step(s) from Buildkite CI #98969.

@ivanium
ivanium merged commit 16d6673 into vllm-project:main Nov 2, 2026
15 checks passed
@njhill
njhill deleted the prompt-logprobs-topk-split branch November 2, 2026 01:48
mvanhorn added a commit to mvanhorn/vllm that referenced this pull request Nov 2, 2026
…ogprobs

Resolve the conflict in compute_prompt_logprobs_with_chunking by keeping
the top-k row cap from vllm-project#64914 and this branch's per-token ranks, so the
preallocated ranks tensor stays 2D.

Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mrv2 Model Runner V2 specific ready ONLY add when PR is ready to merge/full CI is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants