diff --git a/src/transformers/models/blenderbot/tokenization_blenderbot.py b/src/transformers/models/blenderbot/tokenization_blenderbot.py index 6ce85fa644..6772453823 100644 --- a/src/transformers/models/blenderbot/tokenization_blenderbot.py +++ b/src/transformers/models/blenderbot/tokenization_blenderbot.py @@ -411,13 +411,6 @@ class BlenderbotTokenizer(PreTrainedTokenizer): """ A very simple chat template that just adds whitespace between messages. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return ( "{% for message in messages %}" "{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}" diff --git a/src/transformers/models/blenderbot/tokenization_blenderbot_fast.py b/src/transformers/models/blenderbot/tokenization_blenderbot_fast.py index 0735b4666b..3a5206cdf4 100644 --- a/src/transformers/models/blenderbot/tokenization_blenderbot_fast.py +++ b/src/transformers/models/blenderbot/tokenization_blenderbot_fast.py @@ -293,13 +293,6 @@ class BlenderbotTokenizerFast(PreTrainedTokenizerFast): """ A very simple chat template that just adds whitespace between messages. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return ( "{% for message in messages %}" "{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}" diff --git a/src/transformers/models/blenderbot_small/tokenization_blenderbot_small.py b/src/transformers/models/blenderbot_small/tokenization_blenderbot_small.py index 2d8b5f97de..832b5315ed 100644 --- a/src/transformers/models/blenderbot_small/tokenization_blenderbot_small.py +++ b/src/transformers/models/blenderbot_small/tokenization_blenderbot_small.py @@ -224,13 +224,6 @@ class BlenderbotSmallTokenizer(PreTrainedTokenizer): """ A very simple chat template that just adds whitespace between messages. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return ( "{% for message in messages %}" "{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}" diff --git a/src/transformers/models/blenderbot_small/tokenization_blenderbot_small_fast.py b/src/transformers/models/blenderbot_small/tokenization_blenderbot_small_fast.py index 1c8a2656e6..1f647d2430 100644 --- a/src/transformers/models/blenderbot_small/tokenization_blenderbot_small_fast.py +++ b/src/transformers/models/blenderbot_small/tokenization_blenderbot_small_fast.py @@ -104,13 +104,6 @@ class BlenderbotSmallTokenizerFast(PreTrainedTokenizerFast): """ A very simple chat template that just adds whitespace between messages. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return ( "{% for message in messages %}" "{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}" diff --git a/src/transformers/models/bloom/tokenization_bloom_fast.py b/src/transformers/models/bloom/tokenization_bloom_fast.py index 95afa8c45a..afd3bfaaee 100644 --- a/src/transformers/models/bloom/tokenization_bloom_fast.py +++ b/src/transformers/models/bloom/tokenization_bloom_fast.py @@ -155,11 +155,4 @@ class BloomTokenizerFast(PreTrainedTokenizerFast): """ A simple chat template that ignores role information and just concatenates messages with EOS tokens. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return "{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}" diff --git a/src/transformers/models/code_llama/tokenization_code_llama.py b/src/transformers/models/code_llama/tokenization_code_llama.py index ed12b737b2..5a114542df 100644 --- a/src/transformers/models/code_llama/tokenization_code_llama.py +++ b/src/transformers/models/code_llama/tokenization_code_llama.py @@ -456,13 +456,6 @@ class CodeLlamaTokenizer(PreTrainedTokenizer): snippet](https://github.com/facebookresearch/llama/blob/556949fdfb72da27c2f4a40b7f0e4cf0b8153a28/llama/generation.py#L320-L362) in the original repository. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) template = ( "{% if messages[0]['role'] == 'system' %}" "{% set loop_messages = messages[1:] %}" # Extract system message if it's present diff --git a/src/transformers/models/code_llama/tokenization_code_llama_fast.py b/src/transformers/models/code_llama/tokenization_code_llama_fast.py index 845ce94ad9..d1cde882ff 100644 --- a/src/transformers/models/code_llama/tokenization_code_llama_fast.py +++ b/src/transformers/models/code_llama/tokenization_code_llama_fast.py @@ -369,13 +369,6 @@ class CodeLlamaTokenizerFast(PreTrainedTokenizerFast): snippet](https://github.com/facebookresearch/llama/blob/556949fdfb72da27c2f4a40b7f0e4cf0b8153a28/llama/generation.py#L320-L362) in the original repository. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) template = ( "{% if messages[0]['role'] == 'system' %}" "{% set loop_messages = messages[1:] %}" # Extract system message if it's present diff --git a/src/transformers/models/cohere/tokenization_cohere_fast.py b/src/transformers/models/cohere/tokenization_cohere_fast.py index 1fd38e555f..96db4d4d11 100644 --- a/src/transformers/models/cohere/tokenization_cohere_fast.py +++ b/src/transformers/models/cohere/tokenization_cohere_fast.py @@ -247,13 +247,6 @@ class CohereTokenizerFast(PreTrainedTokenizerFast): '<|START_OF_TURN_TOKEN|><|USER_TOKEN|>Hello, how are you?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>' """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) default_template = ( "{{ bos_token }}" "{% if messages[0]['role'] == 'system' %}" diff --git a/src/transformers/models/gpt2/tokenization_gpt2.py b/src/transformers/models/gpt2/tokenization_gpt2.py index 3d5281008a..9e81b4473e 100644 --- a/src/transformers/models/gpt2/tokenization_gpt2.py +++ b/src/transformers/models/gpt2/tokenization_gpt2.py @@ -336,11 +336,4 @@ class GPT2Tokenizer(PreTrainedTokenizer): """ A simple chat template that ignores role information and just concatenates messages with EOS tokens. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return "{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}" diff --git a/src/transformers/models/gpt2/tokenization_gpt2_fast.py b/src/transformers/models/gpt2/tokenization_gpt2_fast.py index 498ca69832..39e59298c8 100644 --- a/src/transformers/models/gpt2/tokenization_gpt2_fast.py +++ b/src/transformers/models/gpt2/tokenization_gpt2_fast.py @@ -147,11 +147,5 @@ class GPT2TokenizerFast(PreTrainedTokenizerFast): """ A simple chat template that ignores role information and just concatenates messages with EOS tokens. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) + return "{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}" diff --git a/src/transformers/models/gpt_neox/tokenization_gpt_neox_fast.py b/src/transformers/models/gpt_neox/tokenization_gpt_neox_fast.py index 2ee18c05ab..ba54a1dc7d 100644 --- a/src/transformers/models/gpt_neox/tokenization_gpt_neox_fast.py +++ b/src/transformers/models/gpt_neox/tokenization_gpt_neox_fast.py @@ -234,11 +234,4 @@ class GPTNeoXTokenizerFast(PreTrainedTokenizerFast): """ A simple chat template that ignores role information and just concatenates messages with EOS tokens. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return "{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}" diff --git a/src/transformers/models/gpt_neox_japanese/tokenization_gpt_neox_japanese.py b/src/transformers/models/gpt_neox_japanese/tokenization_gpt_neox_japanese.py index 83ae777985..b761e539a5 100644 --- a/src/transformers/models/gpt_neox_japanese/tokenization_gpt_neox_japanese.py +++ b/src/transformers/models/gpt_neox_japanese/tokenization_gpt_neox_japanese.py @@ -165,13 +165,6 @@ class GPTNeoXJapaneseTokenizer(PreTrainedTokenizer): """ A simple chat template that just adds BOS/EOS tokens around messages while discarding role information. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return ( "{% for message in messages %}" "{{ bos_token + eos_token + message.content + eos_token }}" diff --git a/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py b/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py index 83fbd4bd0b..1000bfd1b6 100644 --- a/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py +++ b/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py @@ -301,13 +301,6 @@ class GPTSw3Tokenizer(PreTrainedTokenizer): This chat template formats messages like an instant messenger chat log, with "User:" and "Bot:" strings preceding messages. BOS tokens are added between all messages. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return ( "{{ eos_token }}{{ bos_token }}" "{% for message in messages %}" diff --git a/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py b/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py index 7cb28acaeb..627f7a9b28 100644 --- a/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py +++ b/src/transformers/models/gptsan_japanese/tokenization_gptsan_japanese.py @@ -246,13 +246,6 @@ class GPTSanJapaneseTokenizer(PreTrainedTokenizer): A simple chat template that adds standard BOS, SEP and EOS tokens between messages while discarding role information. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return ( "{% for message in messages %}" "{% if not loop.first %}{{ bos_token}}{% endif %}" diff --git a/src/transformers/models/idefics2/processing_idefics2.py b/src/transformers/models/idefics2/processing_idefics2.py index 7b98519928..b20f69bd07 100644 --- a/src/transformers/models/idefics2/processing_idefics2.py +++ b/src/transformers/models/idefics2/processing_idefics2.py @@ -284,8 +284,14 @@ class Idefics2Processor(ProcessorMixin): if self.chat_template is not None: chat_template = self.chat_template else: + logger.warning_once( + "No chat template is set for this processor, falling back to a default class-level template. This is " + "very error-prone, because models are often trained with templates different from the class default! " + "Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " + "point any code depending on them will stop working. We recommend setting a valid chat template before " + "then to ensure that this model continues working without issues." + ) chat_template = self.default_chat_template - return self.tokenizer.apply_chat_template( conversation, chat_template=chat_template, tokenize=tokenize, **kwargs ) diff --git a/src/transformers/models/llama/tokenization_llama.py b/src/transformers/models/llama/tokenization_llama.py index def5e8ecba..2b4920c668 100644 --- a/src/transformers/models/llama/tokenization_llama.py +++ b/src/transformers/models/llama/tokenization_llama.py @@ -429,13 +429,6 @@ class LlamaTokenizer(PreTrainedTokenizer): snippet](https://github.com/facebookresearch/llama/blob/556949fdfb72da27c2f4a40b7f0e4cf0b8153a28/llama/generation.py#L320-L362) in the original repository. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) template = ( "{% if messages[0]['role'] == 'system' %}" "{% set loop_messages = messages[1:] %}" # Extract system message if it's present diff --git a/src/transformers/models/llama/tokenization_llama_fast.py b/src/transformers/models/llama/tokenization_llama_fast.py index ccc01cd619..4a7ef126d4 100644 --- a/src/transformers/models/llama/tokenization_llama_fast.py +++ b/src/transformers/models/llama/tokenization_llama_fast.py @@ -261,13 +261,6 @@ class LlamaTokenizerFast(PreTrainedTokenizerFast): snippet](https://github.com/facebookresearch/llama/blob/556949fdfb72da27c2f4a40b7f0e4cf0b8153a28/llama/generation.py#L320-L362) in the original repository. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) template = ( "{% if messages[0]['role'] == 'system' %}" "{% set loop_messages = messages[1:] %}" # Extract system message if it's present diff --git a/src/transformers/models/whisper/tokenization_whisper.py b/src/transformers/models/whisper/tokenization_whisper.py index 9eabef7e2d..0a6ad5be6e 100644 --- a/src/transformers/models/whisper/tokenization_whisper.py +++ b/src/transformers/models/whisper/tokenization_whisper.py @@ -815,13 +815,6 @@ class WhisperTokenizer(PreTrainedTokenizer): """ A simple chat template that ignores role information and just concatenates messages with EOS tokens. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return "{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}" def get_decoder_prompt_ids(self, task=None, language=None, no_timestamps=True): diff --git a/src/transformers/models/whisper/tokenization_whisper_fast.py b/src/transformers/models/whisper/tokenization_whisper_fast.py index ee54fca283..54aa60839c 100644 --- a/src/transformers/models/whisper/tokenization_whisper_fast.py +++ b/src/transformers/models/whisper/tokenization_whisper_fast.py @@ -544,13 +544,6 @@ class WhisperTokenizerFast(PreTrainedTokenizerFast): """ A simple chat template that ignores role information and just concatenates messages with EOS tokens. """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a default class-level template. " - "This is very error-prone, because models are often trained with templates different from the class " - "default! Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return "{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}" # Copied from transformers.models.whisper.tokenization_whisper.WhisperTokenizer.get_decoder_prompt_ids diff --git a/src/transformers/tokenization_utils_base.py b/src/transformers/tokenization_utils_base.py index a30daf5f7f..0a57345e39 100644 --- a/src/transformers/tokenization_utils_base.py +++ b/src/transformers/tokenization_utils_base.py @@ -1746,16 +1746,27 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): if tokenizer_kwargs is None: tokenizer_kwargs = {} + using_default_template = False + # First, handle the cases when the model has a dict of multiple templates if isinstance(self.chat_template, dict) or ( self.chat_template is None and isinstance(self.default_chat_template, dict) ): - template_dict = self.chat_template or self.default_chat_template + if self.chat_template is not None: + template_dict = self.chat_template + using_default_dict = False + else: + template_dict = self.default_chat_template + using_default_dict = True if chat_template is not None and chat_template in template_dict: # The user can pass the name of a template to the chat template argument instead of an entire template chat_template = template_dict[chat_template] + if using_default_dict: + using_default_template = True elif chat_template is None and "default" in template_dict: chat_template = template_dict["default"] + if using_default_dict: + using_default_template = True elif chat_template is None: raise ValueError( "This model has multiple chat templates with no default specified! Please either pass a chat " @@ -1769,6 +1780,16 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): chat_template = self.chat_template else: chat_template = self.default_chat_template + using_default_template = True + + if using_default_template: + logger.warning_once( + "No chat template is set for this tokenizer, falling back to a default class-level template. This is " + "very error-prone, because models are often trained with templates different from the class default! " + "Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " + "point any code depending on them will stop working. We recommend setting a valid chat template before " + "then to ensure that this model continues working without issues." + ) # Compilation function uses a cache to avoid recompiling the same template compiled_template = self._compile_jinja_template(chat_template) @@ -1840,13 +1861,6 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): This template formats inputs in the standard ChatML format. See https://github.com/openai/openai-python/blob/main/chatml.md """ - logger.warning_once( - "No chat template is set for this tokenizer, falling back to a ChatML template. " - "This is very error-prone, because most models are not trained with a ChatML template!" - "Default chat templates are a legacy feature and will be removed in Transformers v4.43, at which " - "point any code depending on them will stop working. We recommend setting a valid chat template before " - "then to ensure that this model continues working without issues." - ) return ( "{% for message in messages %}" "{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}"