Skip to content

[None][fix] Prevent special tokens leaking into detokenized output on slow tokenizers#16331

Open
Shixiaowei02 wants to merge 1 commit into
NVIDIA:mainfrom
Shixiaowei02:user/xiaoweis/fix-tokenizer-special-ids-cache-staleness
Open

[None][fix] Prevent special tokens leaking into detokenized output on slow tokenizers#16331
Shixiaowei02 wants to merge 1 commit into
NVIDIA:mainfrom
Shixiaowei02:user/xiaoweis/fix-tokenizer-special-ids-cache-staleness

Conversation

@Shixiaowei02

@Shixiaowei02 Shixiaowei02 commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Description

This pull request refactors how special tokens are handled in the TransformersTokenizer class to ensure that any special tokens registered after the tokenizer is wrapped are correctly recognized during detokenization. It also adds new tests to verify this behavior, especially for slow tokenizers. The changes improve correctness and efficiency, particularly when handling streamed detokenization and special tokens.

Special Token Handling Improvements

  • Changed special-token set computation in TransformersTokenizer to be lazy and cached on first detokenization, instead of computed at initialization. This ensures that special tokens registered after wrapping (but before serving) are recognized, and improves performance by avoiding repeated computation. (tensorrt_llm/tokenizer/tokenizer.py) [1] [2]
  • Updated the convert_ids_to_tokens and convert_tokens_to_string methods to use the new cached special-token sets, ensuring correctness when skipping special tokens. (tensorrt_llm/tokenizer/tokenizer.py) [1] [2]

Testing Enhancements

  • Added a minimal "slow" tokenizer class (_ToySlowTokenizer) and two new tests to verify that special tokens registered after wrapping are properly recognized and skipped during detokenization and string conversion. (tests/unittest/llmapi/test_tokenizer_decode.py)

These changes ensure that the tokenizer wrapper behaves correctly with dynamic special token registration and that performance is maintained for streamed detokenization.

Test Coverage

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.

Dev Engineer Review

  • Correctness: Updated TransformersTokenizer special-token handling to avoid stale, init-time snapshots when special tokens are added/changed after the wrapper is constructed. Removed eager cache construction and replaced it with a lazy, first-use cache (self._special_tokens_sets) computed from inner.all_special_ids / inner.all_special_tokens.
  • Behavior changes:
    • convert_ids_to_tokens(ids, skip_special_tokens=True) now uses a slow path only when ids is a list and the wrapped tokenizer is not fast; it filters by the memoized special-id set from _special_tokens()[0].
    • convert_tokens_to_string(..., skip_special_tokens=True) uses the memoized special-token strings set from _special_tokens()[1] when the wrapper’s custom detokenization path is active.
  • Caching/performance: Computes special-token sets once on first use and reuses them thereafter, eliminating repeated O(K) rebuilding per call while still reflecting late special-token registration.
  • Robustness/error handling: The lazy helper falls back to empty sets if inner.all_special_ids / inner.all_special_tokens are unavailable, catching AttributeError and NotImplementedError.
  • CI notes: Multiple CI runs were triggered with some L0 pipeline failures and some successes. Validate that any failures are unrelated to special-token caching/skip-path behavior by relying on the added targeted unit tests.

QA Engineer Review

  • Test changes (unit tests): tests/unittest/llmapi/test_tokenizer_decode.py
    • Added _ToySlowTokenizer(PreTrainedTokenizer) to force TransformersTokenizer through the slow/non-fast filtering logic deterministically.
    • Added test_convert_ids_to_tokens_reflects_special_tokens_registered_after_wrap
    • Added test_convert_tokens_to_string_reflects_special_tokens_registered_after_wrap
  • Integration/CBTS coverage mapping: No references to the newly added unit test names were found in tests/integration/test_lists/.
    • Verdict: needs follow-up.

@Shixiaowei02

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58990 [ run ] triggered by Bot. Commit: 4068de7 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58990 [ run ] completed with state SUCCESS. Commit: 4068de7
/LLM/main/L0_MergeRequest_PR pipeline #47520 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

@Shixiaowei02
Shixiaowei02 force-pushed the user/xiaoweis/fix-tokenizer-special-ids-cache-staleness branch from 4068de7 to fcfa02f Compare July 16, 2026 06:18
@Shixiaowei02

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59639 [ run ] triggered by Bot. Commit: fcfa02f Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59639 [ run ] completed with state SUCCESS. Commit: fcfa02f
/LLM/main/L0_MergeRequest_PR pipeline #48076 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

@Shixiaowei02
Shixiaowei02 force-pushed the user/xiaoweis/fix-tokenizer-special-ids-cache-staleness branch from fcfa02f to 00925b2 Compare July 17, 2026 06:50
@Shixiaowei02

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59912 [ run ] triggered by Bot. Commit: 00925b2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59912 [ run ] completed with state SUCCESS. Commit: 00925b2
/LLM/main/L0_MergeRequest_PR pipeline #48313 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

@Shixiaowei02
Shixiaowei02 force-pushed the user/xiaoweis/fix-tokenizer-special-ids-cache-staleness branch from 00925b2 to 47049c2 Compare July 21, 2026 08:31
@Shixiaowei02

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60652 [ run ] triggered by Bot. Commit: 47049c2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60652 [ run ] completed with state FAILURE. Commit: 47049c2
/LLM/main/L0_MergeRequest_PR pipeline #48951 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

@Shixiaowei02

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60943 [ run ] triggered by Bot. Commit: 47049c2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60943 [ run ] completed with state SUCCESS. Commit: 47049c2
/LLM/main/L0_MergeRequest_PR pipeline #49208 completed with status: 'SUCCESS'

CI Report

Link to invocation

@Shixiaowei02

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast --only-multi-gpu-test

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61152 [ run ] triggered by Bot. Commit: 47049c2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61152 [ run ] completed with state FAILURE. Commit: 47049c2
/LLM/main/L0_MergeRequest_PR pipeline #49401 (Partly Tested) 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

@Shixiaowei02
Shixiaowei02 marked this pull request as ready for review July 23, 2026 06:08
@Shixiaowei02
Shixiaowei02 requested a review from a team as a code owner July 23, 2026 06:08
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

TransformersTokenizer replaces eager special-token snapshots with lazy memoization. Slow-token decoding and string conversion use the lazy metadata, with tests covering special tokens registered after wrapper construction.

Changes

Tokenizer special-token caching

Layer / File(s) Summary
Use lazy special-token metadata
tensorrt_llm/tokenizer/tokenizer.py
Defers special-token cache construction and uses the memoized IDs and strings for slow decoding and string conversion, with empty-set fallback when metadata is unavailable.
Validate late registration
tests/unittest/llmapi/test_tokenizer_decode.py
Adds a slow tokenizer fixture and regression tests for special tokens registered after wrapping.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: asfiyab-nvidia, chuangz0, reasonsolo

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.14% 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
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.
Title check ✅ Passed The title is specific, concise, and accurately reflects the main tokenizer fix and affected slow tokenizers.
Description check ✅ Passed The description explains the issue, solution, and validation, though the dedicated Test Coverage section is left blank.
✨ 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: 2

🤖 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/tokenizer/tokenizer.py`:
- Around line 344-376: The fingerprint in _special_token_ids currently uses
len(inner._added_tokens_decoder) as a proxy for additional_special_tokens
changes, which can remain unchanged after reassignment. Replace that proxy with
inner.additional_special_tokens_ids in the fingerprint while retaining the named
special-token IDs and existing fallback behavior.

In `@tests/unittest/llmapi/test_tokenizer_decode.py`:
- Around line 74-89: Reorder the test around tokenizer.convert_ids_to_tokens so
the cache is primed before registering tok42, then add tok42 and perform the
second conversion to verify invalidation and filtering of both special tokens.
Remove the duplicate no-op add_special_tokens call, and update the
setup/comments to reflect the genuine pre-mutation and post-mutation sequence.
Do not add the optional removal/reassignment regression test unless the
underlying fingerprint fix is part of this change.
🪄 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: 97aec9da-0e25-4138-a30a-ba66971494e6

📥 Commits

Reviewing files that changed from the base of the PR and between 0f21fb8 and 47049c2.

📒 Files selected for processing (2)
  • tensorrt_llm/tokenizer/tokenizer.py
  • tests/unittest/llmapi/test_tokenizer_decode.py

Comment thread tensorrt_llm/tokenizer/tokenizer.py Outdated
Comment thread tests/unittest/llmapi/test_tokenizer_decode.py Outdated
@Shixiaowei02
Shixiaowei02 requested a review from longlee0622 July 23, 2026 06:16
@Shixiaowei02
Shixiaowei02 force-pushed the user/xiaoweis/fix-tokenizer-special-ids-cache-staleness branch from 47049c2 to d870ca4 Compare July 23, 2026 06:31
@Shixiaowei02
Shixiaowei02 force-pushed the user/xiaoweis/fix-tokenizer-special-ids-cache-staleness branch 2 times, most recently from 8af372e to 8f481f6 Compare July 23, 2026 06:57
@Shixiaowei02

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@Shixiaowei02
Shixiaowei02 requested a review from chuangz0 July 23, 2026 06:58
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61225 [ run ] triggered by Bot. Commit: 8f481f6 Link to invocation

@longlee0622 longlee0622 left a comment

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.

fine to merge. If we care about slow-tokenizer + large-special-token-set models under streaming, it'd be worth a quick before/after profile of detok host overhead on such a model to confirm the added per-token cost is acceptable.

@chuangz0
chuangz0 requested a review from reasonsolo July 23, 2026 07:33
@chuangz0

Copy link
Copy Markdown
Collaborator

Is there a real-world use case where special tokens are modified after detokenization has already started?

tuple(inner.all_special_tokens) is rebuilt on every detokenization call. This adds O(K) overhead per call and may become significant when the
tokenizer has a large number of special tokens.

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61225 [ run ] completed with state FAILURE. Commit: 8f481f6
/LLM/main/L0_MergeRequest_PR pipeline #49466 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

@Shixiaowei02

Copy link
Copy Markdown
Collaborator Author

Thank you both for the suggestions. After verifying the issue, I confirmed that this bug is deterministic: the cache is generated before Gemma4's mutation step, which can cause <|video|> to bypass filtering. As a result, I changed the implementation to a lazy-once approach to improve performance while addressing the issue.

@Shixiaowei02
Shixiaowei02 force-pushed the user/xiaoweis/fix-tokenizer-special-ids-cache-staleness branch from 8f481f6 to 12267f9 Compare July 24, 2026 06:41
TransformersTokenizer snapshotted the tokenizer's special tokens in __init__
(all_special_ids for slow convert_ids_to_tokens, all_special_tokens for
convert_tokens_to_string) and reused them forever. That snapshot is taken too
early: a model's input processor can register special tokens after the wrapper
is built but before serving (e.g. gemma4 adds <|video|>), so the affected
tokens stopped being skipped and leaked into detokenized output when
skip_special_tokens=True -- an accuracy regression.

Compute the (ids, strings) sets once, lazily, on the first detokenization call.
By request time all build-time special-token registration is done and nothing
mutates it further, so a first-use cache is correct; it also keeps the
slow-tokenizer detok path O(1) per streamed token (avoiding both the original
per-id all_special_ids rebuild and any per-call refresh).

Add regression tests on a genuine slow tokenizer: special tokens registered
after wrapping must be reflected by both convert_ids_to_tokens and
convert_tokens_to_string.

Signed-off-by: Shixiaowei02 <39303645+Shixiaowei02@users.noreply.github.com>
@Shixiaowei02
Shixiaowei02 force-pushed the user/xiaoweis/fix-tokenizer-special-ids-cache-staleness branch from 12267f9 to ebfc905 Compare July 24, 2026 06:42
@Shixiaowei02

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@Shixiaowei02 Shixiaowei02 changed the title [None][fix] Invalidate cached special-id set in slow convert_ids_to_tokens [None][fix] Prevent special tokens leaking into detokenized output on slow tokenizers Jul 24, 2026

@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)
tensorrt_llm/tokenizer/tokenizer.py (1)

200-201: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Apply the repository’s Python typing requirements across the changed code.

  • tensorrt_llm/tokenizer/tokenizer.py#L200-L201: annotate the lazy cache as tuple[set[int], set[str]] | None.
  • tensorrt_llm/tokenizer/tokenizer.py#L334-L349: use tuple[set[int], set[str]] for _special_tokens().
  • tests/unittest/llmapi/test_tokenizer_decode.py#L25-L54: annotate all new tokenizer methods and their return values.
🤖 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 `@tensorrt_llm/tokenizer/tokenizer.py` around lines 200 - 201, The lazy cache
at tensorrt_llm/tokenizer/tokenizer.py lines 200-201 must be annotated as
tuple[set[int], set[str]] | None; update _special_tokens() at lines 334-349 to
use tuple[set[int], set[str]]; and annotate all newly added tokenizer methods
and return values in tests/unittest/llmapi/test_tokenizer_decode.py lines 25-54
according to the repository’s Python typing requirements.

Source: Coding guidelines

🤖 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/llmapi/test_tokenizer_decode.py`:
- Around line 56-94: Add tests/unittest/llmapi/test_tokenizer_decode.py to an
appropriate llmapi unit-test list under tests/integration/test_lists/test-db/ or
tests/integration/test_lists/qa/, using the existing list-file conventions so
both newly added tests are included in the test suite.

---

Nitpick comments:
In `@tensorrt_llm/tokenizer/tokenizer.py`:
- Around line 200-201: The lazy cache at tensorrt_llm/tokenizer/tokenizer.py
lines 200-201 must be annotated as tuple[set[int], set[str]] | None; update
_special_tokens() at lines 334-349 to use tuple[set[int], set[str]]; and
annotate all newly added tokenizer methods and return values in
tests/unittest/llmapi/test_tokenizer_decode.py lines 25-54 according to the
repository’s Python typing requirements.
🪄 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: cd4de593-eac4-4fce-a50d-5d3dc9333c44

📥 Commits

Reviewing files that changed from the base of the PR and between 8f481f6 and 12267f9.

📒 Files selected for processing (2)
  • tensorrt_llm/tokenizer/tokenizer.py
  • tests/unittest/llmapi/test_tokenizer_decode.py

Comment on lines +56 to +94
def test_convert_ids_to_tokens_reflects_special_tokens_registered_after_wrap() -> None:
# Specials registered after wrapping (before first detok) must be reflected.
inner = _ToySlowTokenizer()
inner.add_special_tokens(
{
"bos_token": "tok1",
"eos_token": "tok2",
"additional_special_tokens": ["tok5"],
}
)
tokenizer = TransformersTokenizer(inner)
assert inner.is_fast is False

inner.add_special_tokens({"additional_special_tokens": ["tok5", "tok42"]})

ids = [10, 42, 11, 5, 12]
got = tokenizer.convert_ids_to_tokens(ids, skip_special_tokens=True)
assert got == inner.convert_ids_to_tokens(ids, skip_special_tokens=True)
assert got == ["tok10", "tok11", "tok12"]


def test_convert_tokens_to_string_reflects_special_tokens_registered_after_wrap() -> None:
# The same must hold for convert_tokens_to_string's skip path.
inner = _ToySlowTokenizer()
inner.add_special_tokens(
{
"bos_token": "tok1",
"eos_token": "tok2",
"additional_special_tokens": ["tok5"],
}
)
tokenizer = TransformersTokenizer(inner)
assert inner.is_fast is False

inner.add_special_tokens({"additional_special_tokens": ["tok5", "tok42"]})

tokens = ["tok10", "tok42", "tok11"]
got = tokenizer.convert_tokens_to_string(tokens, skip_special_tokens=True)
assert got == "tok10 tok11"

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

test_file='tests/unittest/llmapi/test_tokenizer_decode.py'

rg -n --fixed-strings "$test_file" \
  tests/integration/test_lists/test-db \
  tests/integration/test_lists/qa || true

pytest tests/unittest/ \
  -k 'test_convert_ids_to_tokens_reflects_special_tokens_registered_after_wrap or test_convert_tokens_to_string_reflects_special_tokens_registered_after_wrap'

Repository: NVIDIA/TensorRT-LLM

Length of output: 202


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '## tracked test-related files\n'
git ls-files 'tests/unittest/llmapi/test_tokenizer_decode.py' \
  'tests/integration/test_lists/test-db/*' \
  'tests/integration/test_lists/qa/*' | sed 's#^`#-` #'

printf '\n## locate references to the unittest file in test lists\n'
rg -n --fixed-strings 'tests/unittest/llmapi/test_tokenizer_decode.py' tests/integration/test_lists || true

printf '\n## inspect the unittest file around the added tests\n'
cat -n tests/unittest/llmapi/test_tokenizer_decode.py | sed -n '1,180p'

Repository: NVIDIA/TensorRT-LLM

Length of output: 9995


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '## read list-file docs\n'
sed -n '1,220p' tests/integration/test_lists/test-db/README.md
printf '\n---\n'
sed -n '1,220p' tests/integration/test_lists/qa/README.md

printf '\n## any list entries referencing unittest paths\n'
rg -n 'tests/unittest/|test_tokenizer_decode|llmapi' tests/integration/test_lists || true

printf '\n## sample list file structure\n'
sed -n '1,120p' tests/integration/test_lists/test-db/l0_sanity_check.yml

Repository: NVIDIA/TensorRT-LLM

Length of output: 38410


Test coverage summary — insufficient.

  • Added tests: test_convert_ids_to_tokens_reflects_special_tokens_registered_after_wrap, test_convert_tokens_to_string_reflects_special_tokens_registered_after_wrap.
  • Neither test is listed under tests/integration/test_lists/test-db/ or tests/integration/test_lists/qa/; add tests/unittest/llmapi/test_tokenizer_decode.py to the relevant list file (for example, one of the llmapi unit-test tests/integration/test_lists/test-db/l0_*.yml lists).
🤖 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_tokenizer_decode.py` around lines 56 - 94, Add
tests/unittest/llmapi/test_tokenizer_decode.py to an appropriate llmapi
unit-test list under tests/integration/test_lists/test-db/ or
tests/integration/test_lists/qa/, using the existing list-file conventions so
both newly added tests are included in the test suite.

Source: Path instructions

@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.

🧹 Nitpick comments (1)
tensorrt_llm/tokenizer/tokenizer.py (1)

200-201: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use precise Python 3.10 type annotations for the lazy cache.

The new cache and helper return type currently lose the distinction between integer IDs and string tokens. Use tuple[set[int], set[str]] | None for the field and tuple[set[int], set[str]] for _special_tokens().

As per coding guidelines, use precise annotations, built-in generics, and | types.

Proposed typing update
-        self._special_tokens_sets = None
+        self._special_tokens_sets: tuple[set[int], set[str]] | None = None

-    def _special_tokens(self) -> Tuple[set, set]:
+    def _special_tokens(self) -> tuple[set[int], set[str]]:

Also applies to: 334-349

🤖 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 `@tensorrt_llm/tokenizer/tokenizer.py` around lines 200 - 201, Update the
_special_tokens_sets field to use the precise Python 3.10 annotation
tuple[set[int], set[str]] | None, and annotate _special_tokens() to return
tuple[set[int], set[str]]. Preserve the lazy cache behavior while distinguishing
integer IDs from string tokens.

Source: Coding guidelines

🤖 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.

Nitpick comments:
In `@tensorrt_llm/tokenizer/tokenizer.py`:
- Around line 200-201: Update the _special_tokens_sets field to use the precise
Python 3.10 annotation tuple[set[int], set[str]] | None, and annotate
_special_tokens() to return tuple[set[int], set[str]]. Preserve the lazy cache
behavior while distinguishing integer IDs from string tokens.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 5cac293a-ebc9-4997-82f7-d9be38b91d09

📥 Commits

Reviewing files that changed from the base of the PR and between 12267f9 and ebfc905.

📒 Files selected for processing (2)
  • tensorrt_llm/tokenizer/tokenizer.py
  • tests/unittest/llmapi/test_tokenizer_decode.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/unittest/llmapi/test_tokenizer_decode.py

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