fix jinja template
This commit is contained in:
parent
4cff6a4ad5
commit
2b596fb55f
|
@ -338,7 +338,11 @@ def _convert_slots_to_jinja(slots: "SLOTS", tokenizer: "PreTrainedTokenizer", pl
|
||||||
|
|
||||||
|
|
||||||
def _get_jinja_template(template: "Template", tokenizer: "PreTrainedTokenizer") -> str:
|
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:
|
if template.default_system:
|
||||||
jinja_template += "{% set system_message = '" + _jinja_escape(template.default_system) + "' %}"
|
jinja_template += "{% set system_message = '" + _jinja_escape(template.default_system) + "' %}"
|
||||||
|
|
|
@ -17,6 +17,7 @@ import random
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from datasets import load_dataset
|
from datasets import load_dataset
|
||||||
|
from transformers import AutoTokenizer
|
||||||
|
|
||||||
from llamafactory.data import get_dataset
|
from llamafactory.data import get_dataset
|
||||||
from llamafactory.hparams import get_train_args
|
from llamafactory.hparams import get_train_args
|
||||||
|
@ -48,10 +49,11 @@ def test_supervised(num_samples: int):
|
||||||
tokenizer = tokenizer_module["tokenizer"]
|
tokenizer = tokenizer_module["tokenizer"]
|
||||||
tokenized_data = get_dataset(model_args, data_args, training_args, stage="sft", **tokenizer_module)
|
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")
|
original_data = load_dataset(TRAIN_ARGS["dataset"], split="train")
|
||||||
indexes = random.choices(range(len(original_data)), k=num_samples)
|
indexes = random.choices(range(len(original_data)), k=num_samples)
|
||||||
for index in indexes:
|
for index in indexes:
|
||||||
decoded_result = tokenizer.decode(tokenized_data["input_ids"][index])
|
|
||||||
prompt = original_data[index]["instruction"]
|
prompt = original_data[index]["instruction"]
|
||||||
if original_data[index]["input"]:
|
if original_data[index]["input"]:
|
||||||
prompt += "\n" + 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": "user", "content": prompt},
|
||||||
{"role": "assistant", "content": original_data[index]["output"]},
|
{"role": "assistant", "content": original_data[index]["output"]},
|
||||||
]
|
]
|
||||||
templated_result = tokenizer.apply_chat_template(messages, tokenize=False)
|
templated_result = ref_tokenizer.apply_chat_template(messages, tokenize=False)
|
||||||
assert decoded_result == templated_result
|
decoded_result = tokenizer.decode(tokenized_data["input_ids"][index])
|
||||||
|
assert templated_result == decoded_result
|
||||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue