From 2b596fb55ff689d2e488d9a9bbab98f70f356c3c Mon Sep 17 00:00:00 2001 From: hiyouga <467089858@qq.com> Date: Wed, 19 Jun 2024 20:03:50 +0800 Subject: [PATCH] fix jinja template --- src/llamafactory/data/template.py | 6 +++++- tests/data/test_supervised.py | 9 +++++--- tests/data/test_template.py | 35 +++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 tests/data/test_template.py diff --git a/src/llamafactory/data/template.py b/src/llamafactory/data/template.py index 3c4bc5ec40..393ebfeadb 100644 --- a/src/llamafactory/data/template.py +++ b/src/llamafactory/data/template.py @@ -338,7 +338,11 @@ def _convert_slots_to_jinja(slots: "SLOTS", tokenizer: "PreTrainedTokenizer", pl def _get_jinja_template(template: "Template", tokenizer: "PreTrainedTokenizer") -> str: - jinja_template = _convert_slots_to_jinja(template.format_prefix.apply(), tokenizer) + jinja_template = "" + + prefix = _convert_slots_to_jinja(template.format_prefix.apply(), tokenizer) + if prefix: + jinja_template += "{{ " + prefix + " }}" if template.default_system: jinja_template += "{% set system_message = '" + _jinja_escape(template.default_system) + "' %}" diff --git a/tests/data/test_supervised.py b/tests/data/test_supervised.py index 9f7b2dbf48..9cb49615e9 100644 --- a/tests/data/test_supervised.py +++ b/tests/data/test_supervised.py @@ -17,6 +17,7 @@ import pytest from datasets import load_dataset +from transformers import AutoTokenizer from llamafactory.data import get_dataset from llamafactory.hparams import get_train_args @@ -48,10 +49,11 @@ def test_supervised(num_samples: int): tokenizer = tokenizer_module["tokenizer"] tokenized_data = get_dataset(model_args, data_args, training_args, stage="sft", **tokenizer_module) + ref_tokenizer = AutoTokenizer.from_pretrained(TINY_LLAMA) + original_data = load_dataset(TRAIN_ARGS["dataset"], split="train") indexes = random.choices(range(len(original_data)), k=num_samples) for index in indexes: - decoded_result = tokenizer.decode(tokenized_data["input_ids"][index]) prompt = original_data[index]["instruction"] if original_data[index]["input"]: prompt += "\n" + original_data[index]["input"] @@ -60,5 +62,6 @@ def test_supervised(num_samples: int): {"role": "user", "content": prompt}, {"role": "assistant", "content": original_data[index]["output"]}, ] - templated_result = tokenizer.apply_chat_template(messages, tokenize=False) - assert decoded_result == templated_result + templated_result = ref_tokenizer.apply_chat_template(messages, tokenize=False) + decoded_result = tokenizer.decode(tokenized_data["input_ids"][index]) + assert templated_result == decoded_result diff --git a/tests/data/test_template.py b/tests/data/test_template.py new file mode 100644 index 0000000000..9d73c116ce --- /dev/null +++ b/tests/data/test_template.py @@ -0,0 +1,35 @@ +# Copyright 2024 the LlamaFactory team. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +from transformers import AutoTokenizer + +from llamafactory.data import get_template_and_fix_tokenizer + + +TINY_LLAMA = os.environ.get("TINY_LLAMA", "llamafactory/tiny-random-Llama-3") + + +def test_jinja_template(): + tokenizer = AutoTokenizer.from_pretrained(TINY_LLAMA) + ref_tokenizer = AutoTokenizer.from_pretrained(TINY_LLAMA) + get_template_and_fix_tokenizer(tokenizer, name="llama3") + assert tokenizer.chat_template != ref_tokenizer.chat_template + + messages = [ + {"role": "user", "content": "hi!"}, + {"role": "assistant", "content": "hello there"}, + ] + assert tokenizer.apply_chat_template(messages) == ref_tokenizer.apply_chat_template(messages)