Skip to content

Commit

Permalink
🚩 setup_chat_format: throw error if there is already a template in …
Browse files Browse the repository at this point in the history
…base model (#2252)

* setup_chat_format: throw error if there was already a template

* fix lint

* clarify in docs

* fix test?

---------

Co-authored-by: Kashif Rasul <[email protected]>
  • Loading branch information
ngxson and kashif authored Oct 22, 2024
1 parent f2349d2 commit 88be2c0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/test_dataset_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class SetupChatFormatTestCase(unittest.TestCase):
def setUp(self):
self.tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/llama-tokenizer")
self.model = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-MistralForCausalLM")
# remove built-in chat_template to simulate a model having no chat_template
self.tokenizer.chat_template = None

def test_setup_chat_format(self):
original_tokenizer_len = len(self.tokenizer)
Expand Down
8 changes: 8 additions & 0 deletions trl/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def setup_chat_format(
"""
Setup chat format by adding special tokens to the tokenizer, setting the correct format, and extending the embedding layer of the model based on the new special tokens.
If the model already has a chat template, this will throw an error. If you want to overwrite it, please set `tokenizer.chat_template` to `None`.
Args:
model (`~transformers.PreTrainedModel`): The model to be modified.
tokenizer (`~transformers.PreTrainedTokenizer`): The tokenizer to be modified.
Expand All @@ -94,6 +96,12 @@ def setup_chat_format(
model (`~transformers.PreTrainedModel`): The modified model.
tokenizer (`~transformers.PreTrainedTokenizer`): The modified tokenizer.
"""
# check if model already had a chat template
if tokenizer.chat_template is not None:
raise ValueError(
"Chat template is already added to the tokenizer. If you want to overwrite it, please set it to None"
)

# check if format available and retrieve
if format not in FORMAT_MAPPING:
raise ValueError(f"Format {format} not available. Please use one of {FORMAT_MAPPING.keys()}")
Expand Down

0 comments on commit 88be2c0

Please sign in to comment.