mirror of
				https://github.com/hiyouga/LLaMA-Factory.git
				synced 2025-11-04 18:02:19 +08:00 
			
		
		
		
	fix jinja template
Former-commit-id: 0ebf2e2ee23918d28b0cbb20ba456732d6eedfbb
This commit is contained in:
		
							parent
							
								
									3e0fa4a8da
								
							
						
					
					
						commit
						c65f7e9bd5
					
				@ -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) + "' %}"
 | 
			
		||||
 | 
			
		||||
@ -17,6 +17,7 @@ import random
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										35
									
								
								tests/data/test_template.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								tests/data/test_template.py
									
									
									
									
									
										Normal file
									
								
							@ -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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user