Skip to content

[https://nvbugs/6209806][fix] Add an idempotent _relax_llama_validate_architecture() helper that installs a pe - #14707

Open
tensorrt-cicd wants to merge 2 commits into
NVIDIA:mainfrom
tensorrt-cicd:repair-bot-bug6209806
Open

[https://nvbugs/6209806][fix] Add an idempotent _relax_llama_validate_architecture() helper that installs a pe#14707
tensorrt-cicd wants to merge 2 commits into
NVIDIA:mainfrom
tensorrt-cicd:repair-bot-bug6209806

Conversation

@tensorrt-cicd

@tensorrt-cicd tensorrt-cicd commented May 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Root cause: transformers 5.5.x LlamaConfig.validate_architecture rejects configs where hidden_size % num_attention_heads != 0 even when head_dim is explicitly set, breaking valid models like kanana-1.5-2.1b (hidden_size=1792, num_attention_heads=24, head_dim=128).
  • Fix: Add an idempotent _relax_llama_validate_architecture() helper that installs a permissive validator (passes when head_dim is a positive int) and substitutes it in class_validators; call it at the start of load_pretrained_config and load_hf_tokenizer; remove the stale waives.txt entry.
  • Automated fix generated by repair-bot

Test plan

  • Verify fix on the same GPU type as the original failure
  • Check for regressions in related tests

Links

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Fixed loading of Llama models with explicit head_dim configuration settings that previously failed validation checks.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR adds a compatibility patch for HuggingFace Transformers 5.x LlamaConfig validation that allows models with explicit head_dim to load without failing strict divisibility checks. The patch is applied in config loading and tokenizer initialization paths, and a previously waived test is now enabled.

Changes

Llama Architecture Validation Bypass

Layer / File(s) Summary
LlamaConfig validation patch definition and config integration
tensorrt_llm/_torch/pyexecutor/config_utils.py
_relax_llama_validate_architecture() conditionally patches LlamaConfig.validate_architecture to return early when head_dim is a positive integer, bypassing the hidden_size % num_attention_heads check. The patch also updates __class_validators__ for consistency and is applied at the start of load_pretrained_config() before config materialization.
Tokenizer validation bypass
tensorrt_llm/tokenizer/tokenizer.py
load_hf_tokenizer imports and calls _relax_llama_validate_architecture() prior to TransformersTokenizer.from_pretrained() to ensure Transformers 5.x validation is relaxed before tokenizer loading.
Test waiver cleanup
tests/integration/test_lists/waives.txt
Removes TestKanana_Instruct::test_auto_dtype from the waives list, confirming the test now passes without requiring a skip.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • NVIDIA/TensorRT-LLM#13790: Both PRs modify tests/integration/test_lists/waives.txt, with the current PR removing a waived test entry while the related PR manages other test-waiver entries.

Suggested reviewers

  • galagam
  • nvchenghaoz
  • shaharmor98
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% 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 clearly identifies the main change: adding an idempotent helper function to relax Llama architecture validation, with proper ticket reference and type tag.
Description check ✅ Passed The description explains the root cause, the fix, how it integrates into the codebase, and includes test verification and links to the bug tracking the issue.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@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 `@tensorrt_llm/tokenizer/tokenizer.py`:
- Around line 633-639: The compatibility patch calls
(_ensure_gpt2_bytes_to_unicode_compat and _relax_llama_validate_architecture)
are placed outside the try block in load_hf_tokenizer so import/patch failures
escape instead of being caught; move the two calls (and their import of
_relax_llama_validate_architecture from
tensorrt_llm._torch.pyexecutor.config_utils) inside the existing try: block in
load_hf_tokenizer so any ImportError or runtime error is logged and the function
returns None as intended.
🪄 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: 207680b0-a4cc-4c57-9764-b4cdb1c74c10

📥 Commits

Reviewing files that changed from the base of the PR and between f6ba936 and f3d2b34.

📒 Files selected for processing (3)
  • tensorrt_llm/_torch/pyexecutor/config_utils.py
  • tensorrt_llm/tokenizer/tokenizer.py
  • tests/integration/test_lists/waives.txt
💤 Files with no reviewable changes (1)
  • tests/integration/test_lists/waives.txt

Comment on lines 633 to +639
_ensure_gpt2_bytes_to_unicode_compat()
# AutoTokenizer.from_pretrained internally calls AutoConfig.from_pretrained,
# which triggers transformers 5.x LlamaConfig.validate_architecture.
# Relax it so models with explicit `head_dim` (e.g. kanana-1.5-2.1b) load.
from tensorrt_llm._torch.pyexecutor.config_utils import \
_relax_llama_validate_architecture
_relax_llama_validate_architecture()

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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Move the compatibility patch inside the existing try block.

These new lines run before the try: at Line 641, so any import or patching failure now escapes instead of following load_hf_tokenizer()'s current "log and return None" behavior.

💡 Proposed fix
-    _ensure_gpt2_bytes_to_unicode_compat()
-    # AutoTokenizer.from_pretrained internally calls AutoConfig.from_pretrained,
-    # which triggers transformers 5.x LlamaConfig.validate_architecture.
-    # Relax it so models with explicit `head_dim` (e.g. kanana-1.5-2.1b) load.
-    from tensorrt_llm._torch.pyexecutor.config_utils import \
-        _relax_llama_validate_architecture
-    _relax_llama_validate_architecture()
-
     try:
+        _ensure_gpt2_bytes_to_unicode_compat()
+        # AutoTokenizer.from_pretrained internally calls AutoConfig.from_pretrained,
+        # which triggers transformers 5.x LlamaConfig.validate_architecture.
+        # Relax it so models with explicit `head_dim` (e.g. kanana-1.5-2.1b) load.
+        from tensorrt_llm._torch.pyexecutor.config_utils import \
+            _relax_llama_validate_architecture
+        _relax_llama_validate_architecture()
         tokenizer = TransformersTokenizer.from_pretrained(
             model_dir,
             legacy=False,
🤖 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 633 - 639, The
compatibility patch calls (_ensure_gpt2_bytes_to_unicode_compat and
_relax_llama_validate_architecture) are placed outside the try block in
load_hf_tokenizer so import/patch failures escape instead of being caught; move
the two calls (and their import of _relax_llama_validate_architecture from
tensorrt_llm._torch.pyexecutor.config_utils) inside the existing try: block in
load_hf_tokenizer so any ImportError or runtime error is logged and the function
returns None as intended.

@tensorrt-cicd
tensorrt-cicd force-pushed the repair-bot-bug6209806 branch 3 times, most recently from 0d285b8 to a5a48f3 Compare June 2, 2026 23:23
Comment on lines +458 to +460
head_dim = getattr(self, "head_dim", None)
if isinstance(head_dim, int) and head_dim > 0:
return

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.

It seems that LlamaConfig will set head_dim if it is None here. So it might always be int and very likely larger than 0. And this seems might disable the self.hidden_size % self.num_attention_heads != 0 check for all cases.

@tensorrt-cicd
tensorrt-cicd force-pushed the repair-bot-bug6209806 branch 2 times, most recently from 5320bf6 to e5cc5e4 Compare June 8, 2026 11:12
@tensorrt-cicd
tensorrt-cicd force-pushed the repair-bot-bug6209806 branch 2 times, most recently from 6f26e1c to 2460154 Compare June 27, 2026 04:00
transformers 5.5.x LlamaConfig.validate_architecture rejects configs where
hidden_size % num_attention_heads != 0, ignoring an explicit head_dim that
decouples q/k/v projection size from hidden_size (e.g. kanana-1.5-2.1b:
hidden_size=1792, num_attention_heads=24, head_dim=128). Replace the
validator (and its captured entry in __class_validators__) with a permissive
version that allows positive explicit head_dim.

Apply the patch at the two TRT-LLM entry points that load HF configs and
tokenizers, and remove the now-stale test waiver.

Signed-off-by: tensorrt-cicd <90828364+tensorrt-cicd@users.noreply.github.com>
@trtllm-agent
trtllm-agent force-pushed the repair-bot-bug6209806 branch from 2460154 to 219063f Compare July 7, 2026 00:50
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.

3 participants