Skip to content

[https://nvbugs/6226016][fix] Avoid trusting request-controlled router tokenizers#16697

Open
yibinl-nvidia wants to merge 4 commits into
NVIDIA:mainfrom
yibinl-nvidia:dev-yibinl-bug6226016-kvcache-tokenizer
Open

[https://nvbugs/6226016][fix] Avoid trusting request-controlled router tokenizers#16697
yibinl-nvidia wants to merge 4 commits into
NVIDIA:mainfrom
yibinl-nvidia:dev-yibinl-bug6226016-kvcache-tokenizer

Conversation

@yibinl-nvidia

@yibinl-nvidia yibinl-nvidia commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Dev Engineer Review

  • Hardened BlockHashMixin._get_tokenizer by setting trust_remote_code = (self._tokenizer_dir is not None), aligning tokenizer loading behavior with whether the tokenizer directory is operator-configured vs request-controlled.
  • Propagates trust_remote_code consistently to both loading paths:
    • load_custom_tokenizer(..., trust_remote_code=trust_remote_code) when using an operator-provided tokenizer_dir.
    • TransformersTokenizer.from_pretrained(..., trust_remote_code=trust_remote_code) for the standard HF loading path.
  • Updated test_block_hash_mixin_routes_through_transformers_tokenizer to verify:
    • Default mode (no explicit tokenizer_dir): TransformersTokenizer.from_pretrained(..., trust_remote_code=False).
    • tokenizer_dir mode: TransformersTokenizer.from_pretrained(..., trust_remote_code=True).
    • Caching/returned objects in _Probe._tokenizers[model] are the underlying raw HF tokenizer object (.tokenizer), not the TransformersTokenizer wrapper, in both modes.
  • No public API/config changes identified.

QA Engineer Review

  • Modified test: test_block_hash_mixin_routes_through_transformers_tokenizer (tests/unittest/disaggregated/test_router.py)
    • Strengthened assertions covering trust_remote_code handling and tokenizer caching/identity (raw HF tokenizer object).
    • Covered in tests/integration/test_lists/test-db/ via inclusion of unittest/disaggregated/test_router.py in:
      • l0_h100.yml
      • l0_a10.yml
    • Verdict: sufficient

Description

Harden kv-cache-aware router tokenizer loading for the request-controlled model path.

Previously, when tokenizer_dir was unset, _get_tokenizer() used the request's model value as the tokenizer source while still allowing trusted remote code. That meant a client-provided model reference could point at a malicious Hugging Face tokenizer repo and cause custom tokenizer Python code to be loaded by the router.

This change keeps online standard tokenizer loading available, but only enables trust_remote_code when the tokenizer source comes from an operator-configured tokenizer_dir. Request-derived model paths now load with trust_remote_code=False for both custom and Transformers tokenizer paths, while explicitly configured tokenizer directories preserve the existing custom-tokenizer behavior.

Test Coverage

  • python -m pytest tests/unittest/disaggregated/test_router.py -k block_hash_mixin_routes_through_transformers_tokenizer

PR 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-compatible or api-breaking. For api-breaking, include BREAKING in 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.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

BlockHashMixin._get_tokenizer derives trust_remote_code from the configured tokenizer directory, applies it to both tokenizer loaders, and tests default and explicit-directory behavior.

Changes

Tokenizer routing

Layer / File(s) Summary
Directory-dependent tokenizer loading
tensorrt_llm/serve/router_utils.py
_get_tokenizer passes trust_remote_code=False by default and True when _tokenizer_dir is set to custom and Transformers tokenizer loaders.
Tokenizer loading assertions
tests/unittest/disaggregated/test_router.py
Tests verify loader arguments and cached raw tokenizer identity for both default and explicit-directory paths.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: chuangz0, junyixu-nv, bo-nv

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the change and follows the required [https://nvbugs/ID][fix] format.
Description check ✅ Passed The description explains the bug, the fix, test coverage, and includes the checklist.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/unittest/disaggregated/test_router.py (1)

1787-1805: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Expand BlockHashMixin._get_tokenizer coverage. test_block_hash_mixin_routes_through_transformers_tokenizer only exercises TransformersTokenizer.from_pretrained; add load_custom_tokenizer cases for default trust, explicit tokenizer_dir, and tokenizer_dir="" so the remote-code/path behavior stays covered.

Coverage summary: modified test_block_hash_mixin_routes_through_transformers_tokenizer; no tests added or removed. Listed in tests/integration/test_lists/test-db/l0_a10.yml and tests/integration/test_lists/test-db/l0_h100.yml; no matching tests/integration/test_lists/qa/ entry found. Verdict: needs follow-up.

🤖 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/disaggregated/test_router.py` around lines 1787 - 1805, Expand
test_block_hash_mixin_routes_through_transformers_tokenizer to also patch and
exercise load_custom_tokenizer through BlockHashMixin._get_tokenizer. Cover
default trust behavior, an explicit tokenizer_dir, and tokenizer_dir=""; assert
each call receives the expected path and trust_remote_code value, and verify the
returned and cached tokenizer objects.

Source: Path instructions

🤖 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/router_utils.py`:
- Line 209: Update the tokenizer-path handling around trust_remote_code and
model_path so empty tokenizer_dir overrides are treated consistently: reject
empty overrides or derive trust_remote_code from the same normalized-path
predicate used by model_path. Ensure remote-code trust is enabled only when the
effective tokenizer path is non-empty.

---

Nitpick comments:
In `@tests/unittest/disaggregated/test_router.py`:
- Around line 1787-1805: Expand
test_block_hash_mixin_routes_through_transformers_tokenizer to also patch and
exercise load_custom_tokenizer through BlockHashMixin._get_tokenizer. Cover
default trust behavior, an explicit tokenizer_dir, and tokenizer_dir=""; assert
each call receives the expected path and trust_remote_code value, and verify the
returned and cached tokenizer objects.
🪄 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: 815bceca-27c9-4940-b2d2-6307a0c8b547

📥 Commits

Reviewing files that changed from the base of the PR and between 6fabfdc and c00e7c9.

📒 Files selected for processing (2)
  • tensorrt_llm/serve/router_utils.py
  • tests/unittest/disaggregated/test_router.py

Comment thread tensorrt_llm/serve/router_utils.py
@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60834 [ run ] triggered by Bot. Commit: c00e7c9 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60834 [ run ] completed with state SUCCESS. Commit: c00e7c9
/LLM/main/L0_MergeRequest_PR pipeline #49110 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

1 similar comment
@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60985 [ run ] triggered by Bot. Commit: c00e7c9 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60985 [ run ] completed with state FAILURE. Commit: c00e7c9
/LLM/main/L0_MergeRequest_PR pipeline #49245 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61016 [ run ] triggered by Bot. Commit: c00e7c9 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61016 [ run ] completed with state FAILURE. Commit: c00e7c9
/LLM/main/L0_MergeRequest_PR pipeline #49274 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61098 [ run ] triggered by Bot. Commit: c00e7c9 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61098 [ run ] completed with state FAILURE. Commit: c00e7c9
/LLM/main/L0_MergeRequest_PR pipeline #49350 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yibinl-nvidia
yibinl-nvidia force-pushed the dev-yibinl-bug6226016-kvcache-tokenizer branch from c00e7c9 to c3cfdf1 Compare July 22, 2026 22:57
@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@tests/unittest/disaggregated/test_router.py`:
- Around line 1787-1805: Add a custom-tokenizer branch to
test_block_hash_mixin_routes_through_transformers_tokenizer that mocks
load_custom_tokenizer and verifies it receives trust_remote_code=False for
request-controlled model paths and True when _init_block_hashing sets
tokenizer_dir, asserting the returned and cached tokenizer values. Register the
expanded test in the appropriate tests/integration/test_lists/test-db or
tests/integration/test_lists/qa test list.
🪄 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: a6989a1f-e79d-464a-b80f-721b0787c21a

📥 Commits

Reviewing files that changed from the base of the PR and between c00e7c9 and c3cfdf1.

📒 Files selected for processing (2)
  • tensorrt_llm/serve/router_utils.py
  • tests/unittest/disaggregated/test_router.py

Comment thread tests/unittest/disaggregated/test_router.py
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61108 [ run ] triggered by Bot. Commit: c3cfdf1 Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61108 [ run ] completed with state SUCCESS. Commit: c3cfdf1
/LLM/main/L0_MergeRequest_PR pipeline #49362 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61138 [ run ] triggered by Bot. Commit: c3cfdf1 Link to invocation

def _get_tokenizer(self, model: str):
if model not in self._tokenizers:
model_path = self._tokenizer_dir or model
trust_remote_code = self._tokenizer_dir is not None

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add some comments here.

@yibinl-nvidia
yibinl-nvidia force-pushed the dev-yibinl-bug6226016-kvcache-tokenizer branch from 9f3f385 to e9db3e5 Compare July 23, 2026 16:24
@yibinl-nvidia
yibinl-nvidia requested review from a team as code owners July 23, 2026 16:24
@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61335 [ run ] triggered by Bot. Commit: e9db3e5 Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61335 [ run ] completed with state FAILURE. Commit: e9db3e5
/LLM/main/L0_MergeRequest_PR pipeline #49563 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61339 [ run ] triggered by Bot. Commit: e9db3e5 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61339 [ run ] completed with state FAILURE. Commit: e9db3e5
/LLM/main/L0_MergeRequest_PR pipeline #49566 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61393 [ run ] triggered by Bot. Commit: e9db3e5 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61393 [ run ] completed with state SUCCESS. Commit: e9db3e5
/LLM/main/L0_MergeRequest_PR pipeline #49620 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61423 [ run ] triggered by Bot. Commit: e9db3e5 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61423 [ run ] completed with state SUCCESS. Commit: e9db3e5
/LLM/main/L0_MergeRequest_PR pipeline #49648 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61446 [ run ] triggered by Bot. Commit: e9db3e5 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61446 [ run ] completed with state FAILURE. Commit: e9db3e5

Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61448 [ run ] triggered by Bot. Commit: e9db3e5 Link to invocation

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot kill

@yibinl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

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.

4 participants