Skip to content

fix(turbomind): reject top_k above the sampling kernel limit instead of aborting - #5251

Open
suryacks wants to merge 1 commit into
InternLM:mainfrom
suryacks:fix-turbomind-topk-limit
Open

suryacks wants to merge 1 commit into
InternLM:mainfrom
suryacks:fix-turbomind-topk-limit

Conversation

@suryacks

@suryacks suryacks commented Dec 5, 2026

Copy link
Copy Markdown

Motivation

With the TurboMind backend (the default), one request with top_k above 1024 aborts the whole api_server process.

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

curl localhost:23335/v1/chat/completions -H "Content-Type: application/json" \
  -d '{"model":"q","messages":[{"role":"user","content":"Say hi"}],"max_tokens":8,"top_k":2000}'
# -> connection closed. The server process is gone and every later request fails to connect.

Server log:

terminate called after throwing an instance of 'std::domain_error'
  what():  top-k kernel supports 1<=k<=1024 but got k=2000
  [ 1] Sampling::Forward() @ sampling.cc:94
  ...
  [ 5] ModelExecutor::Impl::InternalThreadEntry() @ model_executor.cc:39

The API only checks top_k >= -1. invokeTopKSortFilter throws std::domain_error for k > 1024. That happens on the engine thread, where nothing catches it, so std::terminate brings down the process.

Modification

  • lmdeploy/messages.py: new TURBOMIND_MAX_TOP_K = 1024, the kernel limit.
  • serve/openai/endpoints/common.py (validate_request, shared by /v1/chat/completions, /v1/completions and /generate): with a TurboMind engine, a larger top_k now gets a 400 with a clear message. The PyTorch backend is unchanged.
  • turbomind/turbomind.py: _get_generation_config raises ValueError for such a top_k before the request reaches the engine. This also covers other callers, such as the pipeline API or endpoints that do not use validate_request. For those, the request fails and the server keeps running.

Tests

  • New tests/test_lmdeploy/serve/openai/test_turbomind_top_k.py. It checks that chat and completion requests get a 400 above the limit and pass at the limit, that the PyTorch engine config is not affected, and that TurboMindInstance raises ValueError. That last test is skipped when the TurboMind extension is not built.
  • tests/test_lmdeploy/serve/openai, test_messages.py and serve/test_generation_config.py: 661 passed. The 2 failures in responses/test_serving.py::test_trace_keeps_oversized_terminal_response_success also fail on main without this change. ruff check is clean.
  • End to end with this change on the 0.18.0 release (Qwen3-0.6B, TurboMind, RTX 4070 SUPER): top_k=2000 returns 400 The top_k 2000 must not exceed 1024 with the TurboMind backend., the next normal request still succeeds, and top_k=1024 works as before.

Related: #5250 fixes the PyTorch-backend counterpart (top_k larger than the vocabulary).

…of aborting

TurboMind's top-k kernel supports k <= 1024. A larger top_k reached
invokeTopKSortFilter, which threw std::domain_error on the engine thread;
nothing catches it there, so the whole api_server process aborted.

Reject such a top_k before the request reaches the engine: the OpenAI
endpoints return 400, and TurboMindInstance raises ValueError for any other
caller (e.g. the pipeline API).

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