|
| 1 | +""" |
| 2 | +tests for chat_template prompt strategy |
| 3 | +""" |
| 4 | +import unittest |
| 5 | + |
| 6 | +import pytest |
| 7 | +from datasets import Dataset |
| 8 | +from transformers import AutoTokenizer |
| 9 | + |
| 10 | +from axolotl.prompt_strategies.chat_template import ( |
| 11 | + ChatTemplatePrompter, |
| 12 | + ChatTemplateStrategy, |
| 13 | +) |
| 14 | +from axolotl.utils.chat_templates import chat_templates |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(name="sharegpt_dataset") |
| 18 | +def fixture_sharegpt_dataset(): |
| 19 | + # pylint: disable=duplicate-code |
| 20 | + return Dataset.from_list( |
| 21 | + [ |
| 22 | + { |
| 23 | + "conversations": [ |
| 24 | + { |
| 25 | + "from": "human", |
| 26 | + "value": "hello", |
| 27 | + }, |
| 28 | + { |
| 29 | + "from": "gpt", |
| 30 | + "value": "hello", |
| 31 | + }, |
| 32 | + { |
| 33 | + "from": "human", |
| 34 | + "value": "goodbye", |
| 35 | + }, |
| 36 | + { |
| 37 | + "from": "gpt", |
| 38 | + "value": "goodbye", |
| 39 | + }, |
| 40 | + ] |
| 41 | + } |
| 42 | + ] |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(name="llama3_tokenizer") |
| 47 | +def fixture_llama3_tokenizer(): |
| 48 | + tokenizer = AutoTokenizer.from_pretrained("NousResearch/Meta-Llama-3-8B") |
| 49 | + tokenizer.eos_token = "<|eot_id|>" |
| 50 | + |
| 51 | + return tokenizer |
| 52 | + |
| 53 | + |
| 54 | +class TestSharegptChatTemplateLlama3: |
| 55 | + """ |
| 56 | + Test class for ShareGPT style datasets with llama-3 prompts using the chat_template strategy. |
| 57 | + """ |
| 58 | + |
| 59 | + def test_llama3(self, llama3_tokenizer, sharegpt_dataset): |
| 60 | + # pylint: disable=duplicate-code |
| 61 | + strategy = ChatTemplateStrategy( |
| 62 | + ChatTemplatePrompter(llama3_tokenizer, chat_templates("llama3")), |
| 63 | + llama3_tokenizer, |
| 64 | + False, |
| 65 | + 512, |
| 66 | + ) |
| 67 | + res = strategy.tokenize_prompt(sharegpt_dataset[0]) |
| 68 | + input_ids = res["input_ids"] |
| 69 | + # fmt: off |
| 70 | + assert input_ids == [ |
| 71 | + 128000, # bos |
| 72 | + 128006, 882, 128007, # user header |
| 73 | + 271, 15339, 128009, # user prompt eot |
| 74 | + 128006, 78191, 128007, # assistant header |
| 75 | + 271, 15339, 128009, # assistant response eot |
| 76 | + 128006, 882, 128007, |
| 77 | + 271, 19045, 29474, 128009, |
| 78 | + 128006, 78191, 128007, |
| 79 | + 271, 19045, 29474, 128009, |
| 80 | + ] |
| 81 | + # fmt: on |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + unittest.main() |
0 commit comments