From 378d3e9f5866d1ff9b66e47490da936e90cdf6f3 Mon Sep 17 00:00:00 2001 From: Ye Yu Date: Wed, 29 Apr 2026 09:24:45 -0700 Subject: [PATCH] fix: guard against None chat_template in _post_process_chat_template 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 Signed-off-by: Ye Yu --- modelopt/torch/utils/plugins/transformers_dataset.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modelopt/torch/utils/plugins/transformers_dataset.py b/modelopt/torch/utils/plugins/transformers_dataset.py index 56b1e4f07b..162bdbd8cf 100644 --- a/modelopt/torch/utils/plugins/transformers_dataset.py +++ b/modelopt/torch/utils/plugins/transformers_dataset.py @@ -181,6 +181,8 @@ def _post_process_tokenizer(self): def _post_process_chat_template(self): # [WAR]: For DeepSeek-V3/R1 tokenizer, we modify the chat_template such that the # tokens are preserved for supervised learning. + if self.tokenizer.chat_template is None: + return self.tokenizer.chat_template = self.tokenizer.chat_template.replace( REMOVE_THINK_CHAT_TEMPLATE, "" )