Skip to content

fix(pytorch): do not crash the engine when top_k exceeds the vocabulary - #5250

Open
suryacks wants to merge 1 commit into
InternLM:mainfrom
suryacks:fix-topk-larger-than-vocab
Open

suryacks wants to merge 1 commit into
InternLM:mainfrom
suryacks:fix-topk-larger-than-vocab

Conversation

@suryacks

@suryacks suryacks commented Dec 4, 2026

Copy link
Copy Markdown

Motivation

With the PyTorch backend, one request with top_k larger than the vocabulary stops the engine. That request and every later one get 503 The inference engine is unavailable until the server restarts.

Repro on the released lmdeploy 0.18.0 (lmdeploy serve api_server Qwen/Qwen3-0.6B --backend pytorch):

curl localhost:23333/v1/chat/completions -H "Content-Type: application/json" \
  -d '{"model":"q","messages":[{"role":"user","content":"Say hi"}],"max_tokens":8,"top_k":1000000}'
# -> 503 {"message":"The inference engine is unavailable.", ...}
# any later normal request -> 503 as well

Server log:

  File ".../lmdeploy/pytorch/engine/logits_process.py", line 540, in sampling
    scores, indices = _torch_topk(logits, max_topk, dim=1)
  File ".../lmdeploy/pytorch/engine/logits_process.py", line 362, in _torch_topk
    return torch.topk(x, k, dim=dim, largest=largest, sorted=sorted)
RuntimeError: selected index k out of range
... session 1 finished, ResponseType.ENGINE_STOP_ERROR, reason "error"

check_request only enforces top_k >= -1. FusedLogitsProcessor._filter_sorted_logits then passes the batch maximum to torch.topk, which raises when k is larger than the vocabulary. The error is raised inside the engine loop, so it takes the engine down.

Modification

A top_k at or above the vocabulary size keeps every token. It now goes down the same full-sort path as top_k <= 0. The per-row top_k mask still applies, so in a mixed batch the other rows keep their own top_k.

Tests

  • New test_filter_logits_top_k_larger_than_vocab in tests/pytorch/engine/test_logits_process.py. It fails on main with RuntimeError: selected index k out of range and passes with this change. It covers a batch where every row is over the limit and a mixed batch (top_k=[1, 8] over 3 tokens).
  • tests/pytorch/engine/test_logits_process.py and tests/test_lmdeploy/serve/test_generation_config.py pass. ruff check is clean.
  • End to end with this branch (Qwen3-0.6B, PyTorch backend): top_k=1000000 and top_k=40000 requests return normal completions, and later requests still succeed.

A request with top_k larger than the vocabulary made torch.topk raise
"selected index k out of range" inside the engine loop, which stopped the
engine: that request and every later one failed with 503.

A top_k at or above the vocabulary size keeps every token, so take the
full-sort path for it, as for top_k <= 0.

This branch has not been deployed

No deployments
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.

1 participant