Skip to content

fix: guard against None chat_template in _post_process_chat_template#1371

Open
yeyu-nvidia wants to merge 1 commit intomainfrom
fix/chat-template-none-guard
Open

fix: guard against None chat_template in _post_process_chat_template#1371
yeyu-nvidia wants to merge 1 commit intomainfrom
fix/chat-template-none-guard

Conversation

@yeyu-nvidia
Copy link
Copy Markdown
Contributor

@yeyu-nvidia yeyu-nvidia commented Apr 29, 2026

Problem

When training with a model that has no chat_template in its tokenizer (e.g. base Llama-3.2 models), _post_process_chat_template() crashes:

AttributeError: 'NoneType' object has no attribute 'replace'

The DeepSeek WAR at the top of _post_process_chat_template called .replace() directly on self.tokenizer.chat_template without checking for None first.

Fixes NVBug 6120958

Fix

Add an early return when chat_template is None. The existing check at line 164 (if self.tokenizer.chat_template is None: raise ValueError) still provides a clear error message if no valid template is available after post-processing.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a crash in chat template processing that occurred when a chat template configuration was not set, improving system stability and reliability during initialization.

@yeyu-nvidia yeyu-nvidia requested review from a team as code owners April 29, 2026 16:24
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 29, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: f28f1df7-c971-4eed-a828-1ac12d2b4e00

📥 Commits

Reviewing files that changed from the base of the PR and between 0fb9ead and 378d3e9.

📒 Files selected for processing (1)
  • modelopt/torch/utils/plugins/transformers_dataset.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • modelopt/torch/utils/plugins/transformers_dataset.py

📝 Walkthrough

Walkthrough

A null guard is added to the _post_process_chat_template method to check if self.tokenizer.chat_template is None before attempting string operations on it, preventing null reference errors when the template is unset.

Changes

Cohort / File(s) Summary
Null Safety Fix
modelopt/torch/utils/plugins/transformers_dataset.py
Added None guard in _post_process_chat_template to prevent null dereference when self.tokenizer.chat_template is unset.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding a None guard for chat_template in the _post_process_chat_template method to prevent null dereference errors.
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.
Security Anti-Patterns ✅ Passed Security audit found no torch.load(weights_only=False), numpy.load(allow_pickle=True), hardcoded trust_remote_code=True, eval/exec on untrusted input, # nosec comments, or non-permissive license dependencies.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/chat-template-none-guard

Review rate limit: 9/10 reviews remaining, refill in 6 minutes.

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 29, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://NVIDIA.github.io/Model-Optimizer/pr-preview/pr-1371/

Built to branch gh-pages at 2026-04-29 16:42 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@yeyu-nvidia yeyu-nvidia enabled auto-merge (squash) April 29, 2026 16:36
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.97%. Comparing base (077e29a) to head (378d3e9).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...delopt/torch/utils/plugins/transformers_dataset.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1371      +/-   ##
==========================================
+ Coverage   76.48%   76.97%   +0.48%     
==========================================
  Files         471      471              
  Lines       50487    50489       +2     
==========================================
+ Hits        38617    38862     +245     
+ Misses      11870    11627     -243     
Flag Coverage Δ
examples 41.58% <50.00%> (+10.82%) ⬆️
gpu 59.71% <0.00%> (-0.44%) ⬇️
regression 14.91% <0.00%> (+0.19%) ⬆️
unit 52.78% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

When a tokenizer has no chat_template (e.g. base Llama-3.2 models),
_post_process_chat_template() crashed with:
  AttributeError: 'NoneType' object has no attribute 'replace'

Add an early return when chat_template is None. The existing check at
line 164 will then raise a clear ValueError("No valid chat template!")
if no template is available after post-processing.

Fixes NVBug 6120958

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Ye Yu <yeyu@nvidia.com>
@yeyu-nvidia yeyu-nvidia force-pushed the fix/chat-template-none-guard branch from 0fb9ead to 378d3e9 Compare April 29, 2026 16:38
@yeyu-nvidia yeyu-nvidia added the cherry-pick-0.44.0 After code freeze, cherry-pick to release branch for next rc (bulk update). Only for bug fixes / doc label Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-pick-0.44.0 After code freeze, cherry-pick to release branch for next rc (bulk update). Only for bug fixes / doc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants