[None][feat] Add PyTorch reset_prefix_cache API - #14970
Conversation
Signed-off-by: milesial <milesial@users.noreply.github.com>
📝 WalkthroughWalkthroughThis PR adds a ChangesPrefix Cache Reset API
Sequence DiagramsequenceDiagram
participant OpenAIClient
participant OpenAIServer
participant TorchLLM
participant Executor
participant Engine
OpenAIClient->>OpenAIServer: POST /reset_prefix_cache
OpenAIServer->>TorchLLM: reset_prefix_cache()
alt Collective RPC supported
TorchLLM->>Executor: collective_rpc("reset_prefix_cache")
else Direct reset
TorchLLM->>Executor: reset_prefix_cache()
Executor->>Engine: reset_prefix_cache()
Engine-->>Executor: cache invalidated
end
Executor-->>TorchLLM: success
TorchLLM-->>OpenAIServer: return
OpenAIServer-->>OpenAIClient: HTTP 200
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/unittest/llmapi/test_llm.py (1)
2744-2793: ⚡ Quick winAdd missing negative-path coverage for the new reset API contracts.
Coverage is good for dispatch basics, but it’s still missing two high-value guard/error cases in
tests/unittest/llmapi/test_llm.py:
_TorchLLM.reset_prefix_cache()whenllm._encode_only = True(should reject).OpenAIServer.reset_prefix_cache()whenserver.generator.reset_prefix_cache()exists but raisesNotImplementedError(should map to 501, if that is the handler contract).Suggested test additions
+def test_llm_reset_prefix_cache_rejects_encode_only() -> None: + llm = object.__new__(LLM_torch) + llm._encode_only = True + llm._executor = _FakeResetExecutor() + + with pytest.raises(NotImplementedError): + llm.reset_prefix_cache() + + +class _FakeNotImplementedResetGenerator: + def reset_prefix_cache(self): + raise NotImplementedError("not supported") + + +def test_openai_reset_prefix_cache_endpoint_maps_not_implemented() -> None: + server = object.__new__(OpenAIServer) + server.generator = _FakeNotImplementedResetGenerator() + + response = asyncio.run(server.reset_prefix_cache()) + assert response.status_code == 501As per coding guidelines,
tests/**reviews should explicitly assess whether coverage is sufficient and call out concrete follow-up test files when it is not.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unittest/llmapi/test_llm.py` around lines 2744 - 2793, Add two negative-path tests: (1) for LLM_torch.reset_prefix_cache create an instance via object.__new__(LLM_torch), set llm._encode_only = True and llm._executor to any executor (e.g., _FakeResetExecutor), call llm.reset_prefix_cache() and assert it raises the expected rejection (e.g., NotImplementedError or the same error type used for encode-only rejection); (2) for OpenAIServer.reset_prefix_cache set server = object.__new__(OpenAIServer) and server.generator to a stub whose reset_prefix_cache method raises NotImplementedError, call asyncio.run(server.reset_prefix_cache()) and assert the response.status_code == 501 to verify the NotImplementedError is mapped to 501. Ensure tests reference LLM_torch.reset_prefix_cache and OpenAIServer.reset_prefix_cache so they cover the described guard paths.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tensorrt_llm/serve/openai_server.py`:
- Around line 997-1001: The current exception handler around
reset_prefix_cache() only catches NotImplementedError and ValueError so
RuntimeError from _TorchLLM.reset_prefix_cache() bubbles up as a 500; update the
try/except to also catch RuntimeError and map it to the same handler (return
self._create_not_supported_error(str(e))) so encode-only / no-executor cases are
returned as not-supported errors; locate the call to reset_prefix_cache() in
openai_server.py and extend the except clause to include RuntimeError alongside
NotImplementedError and ValueError.
---
Nitpick comments:
In `@tests/unittest/llmapi/test_llm.py`:
- Around line 2744-2793: Add two negative-path tests: (1) for
LLM_torch.reset_prefix_cache create an instance via object.__new__(LLM_torch),
set llm._encode_only = True and llm._executor to any executor (e.g.,
_FakeResetExecutor), call llm.reset_prefix_cache() and assert it raises the
expected rejection (e.g., NotImplementedError or the same error type used for
encode-only rejection); (2) for OpenAIServer.reset_prefix_cache set server =
object.__new__(OpenAIServer) and server.generator to a stub whose
reset_prefix_cache method raises NotImplementedError, call
asyncio.run(server.reset_prefix_cache()) and assert the response.status_code ==
501 to verify the NotImplementedError is mapped to 501. Ensure tests reference
LLM_torch.reset_prefix_cache and OpenAIServer.reset_prefix_cache so they cover
the described guard paths.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: c27f4417-0459-4abc-9663-4477f3c2151d
📒 Files selected for processing (5)
tensorrt_llm/executor/base_worker.pytensorrt_llm/llmapi/llm.pytensorrt_llm/serve/openai_server.pytests/unittest/api_stability/references/llm.yamltests/unittest/llmapi/test_llm.py
9e649a7 to
2c86847
Compare
Signed-off-by: milesial <milesial@users.noreply.github.com>
2c86847 to
b47c43e
Compare
|
Devin raised a TOCTOU race, could you check it? https://app.devin.ai/review/NVIDIA/TensorRT-LLM/pull/14970 |
Signed-off-by: milesial <milesial@users.noreply.github.com>
@achartier thanks, implemented Devin's feedback |
Signed-off-by: milesial <milesial@users.noreply.github.com>
|
/bot run |
|
PR_Github #53606 [ run ] triggered by Bot. Commit: |
|
PR_Github #53606 [ run ] completed with state
|
|
/bot run |
|
PR_Github #53642 [ run ] triggered by Bot. Commit: |
|
PR_Github #53642 [ run ] completed with state
|
|
/bot run |
|
PR_Github #53648 [ run ] triggered by Bot. Commit: |
|
PR_Github #53648 [ run ] completed with state
|
|
/bot run |
|
PR_Github #53678 [ run ] triggered by Bot. Commit: |
|
PR_Github #53678 [ run ] completed with state
|
|
/bot skip --comment "Known flaky tests" |
|
PR_Github #53711 [ skip ] triggered by Bot. Commit: |
|
PR_Github #53711 [ skip ] completed with state |
|
Hi @milesial — heads up: this PR introduced a regression on Root cause: this PR adds This kills the Ray worker at init, so all Note the Re-landing suggestion: either pick a non-colliding method name on |

Description
Following vLLM
reset_prefix_cacheand SGLangflush_cache, add a python API + HTTP endpoint to reset the local KV cache state.This is useful during benchmarking to reset the state between runs in a concurrency sweep for example.
Test Coverage
Added unit tests to
tests/unittest/llmapi/test_llm.pyPR Checklist
Please review the following before submitting your PR:
PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.
PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.
Test cases are provided for new code paths (see test instructions)
If PR introduces API changes, an appropriate PR label is added - either
api-compatibleorapi-breaking. Forapi-breaking, includeBREAKINGin the PR title.Any new dependencies have been scanned for license and vulnerabilities
CODEOWNERS updated if ownership changes
Documentation updated as needed
Update tava architecture diagram if there is a significant design change in PR.
The reviewers assigned automatically/manually are appropriate for the PR.
Please check this after reviewing the above items as appropriate for this PR.
Summary by CodeRabbit
New Features
reset_prefix_cache()method to the LLM API (beta status) for PyTorch backend/reset_prefix_cacheendpoint to OpenAI-compatible serverTests