|
4 | 4 | # This source code is licensed under the BSD-style license found in the
|
5 | 5 | # LICENSE file in the root directory of this source tree.
|
6 | 6 |
|
7 |
| -from unittest import mock |
8 |
| - |
9 | 7 | import pytest
|
| 8 | +from tests.common import ASSETS |
10 | 9 | from tests.test_utils import DummyChatFormat, DummyTokenizer
|
11 |
| -from torchtune.data import Message |
| 10 | +from torchtune.data import get_sharegpt_messages |
12 | 11 | from torchtune.data._common import CROSS_ENTROPY_IGNORE_IDX
|
13 |
| -from torchtune.datasets import ChatDataset |
| 12 | +from torchtune.datasets import chat_dataset, ChatDataset |
14 | 13 |
|
15 | 14 |
|
16 | 15 | class TestChatDataset:
|
17 | 16 | @pytest.fixture
|
18 | 17 | def chat_format(self):
|
19 | 18 | return DummyChatFormat
|
20 | 19 |
|
21 |
| - @pytest.fixture |
22 |
| - def dialogue(self): |
23 |
| - return [ |
24 |
| - { |
25 |
| - "dialogue": [ |
26 |
| - Message.from_dict( |
27 |
| - { |
28 |
| - "role": "system", |
29 |
| - "content": "You are an AI assistant.", |
30 |
| - "masked": True, |
31 |
| - } |
32 |
| - ), |
33 |
| - Message.from_dict( |
34 |
| - { |
35 |
| - "role": "user", |
36 |
| - "content": "What is the meaning of life?", |
37 |
| - "masked": True, |
38 |
| - } |
39 |
| - ), |
40 |
| - Message.from_dict( |
41 |
| - { |
42 |
| - "role": "assistant", |
43 |
| - "content": "The meaning of life is 42.", |
44 |
| - "masked": False, |
45 |
| - } |
46 |
| - ), |
47 |
| - Message.from_dict( |
48 |
| - { |
49 |
| - "role": "user", |
50 |
| - "content": "That's ridiculous.", |
51 |
| - "masked": True, |
52 |
| - } |
53 |
| - ), |
54 |
| - Message.from_dict( |
55 |
| - {"role": "assistant", "content": "I agree.", "masked": False} |
56 |
| - ), |
57 |
| - ], |
58 |
| - }, |
59 |
| - ] |
60 |
| - |
61 |
| - @mock.patch("torchtune.datasets._chat.load_dataset") |
62 |
| - def test_get_item(self, mock_load_dataset, chat_format, dialogue): |
63 |
| - mock_load_dataset.return_value = dialogue |
| 20 | + def test_get_item(self, chat_format): |
64 | 21 | expected_tokenized_prompts = [
|
65 | 22 | [
|
66 | 23 | 0,
|
@@ -104,15 +61,68 @@ def test_get_item(self, mock_load_dataset, chat_format, dialogue):
|
104 | 61 | ]
|
105 | 62 | ds = ChatDataset(
|
106 | 63 | tokenizer=DummyTokenizer(),
|
107 |
| - source="iam/agoofy/goober", |
108 |
| - convert_to_messages=lambda x, y: x["dialogue"], |
| 64 | + source="json", |
| 65 | + convert_to_messages=get_sharegpt_messages, |
109 | 66 | chat_format=chat_format,
|
110 | 67 | max_seq_len=100,
|
111 | 68 | train_on_input=False,
|
| 69 | + data_files=str(ASSETS / "chat_tiny.json"), |
| 70 | + split="train", |
112 | 71 | )
|
113 | 72 | assert len(ds) == 1
|
114 |
| - mock_load_dataset.assert_called_once() |
| 73 | + prompt, label = ds[0]["tokens"], ds[0]["labels"] |
| 74 | + assert prompt == expected_tokenized_prompts[0] |
| 75 | + assert label == expected_labels[0] |
| 76 | + |
| 77 | + expected_tokenized_prompts = [ |
| 78 | + [ |
| 79 | + 0, |
| 80 | + 3, |
| 81 | + 3, |
| 82 | + 2, |
| 83 | + 2, |
| 84 | + 10, |
| 85 | + 4, |
| 86 | + 2, |
| 87 | + 3, |
| 88 | + 7, |
| 89 | + 2, |
| 90 | + 5, |
| 91 | + 3, |
| 92 | + 7, |
| 93 | + 2, |
| 94 | + 4, |
| 95 | + 2, |
| 96 | + 3, |
| 97 | + -1, |
| 98 | + 0, |
| 99 | + 6, |
| 100 | + 11, |
| 101 | + 1, |
| 102 | + 6, |
| 103 | + -1, |
| 104 | + ] |
| 105 | + ] |
| 106 | + prompt_lengths = (12, 3) |
| 107 | + expected_labels = [ |
| 108 | + [CROSS_ENTROPY_IGNORE_IDX] * prompt_lengths[0] |
| 109 | + + [3, 7, 2, 4, 2, 3, -1] |
| 110 | + + [CROSS_ENTROPY_IGNORE_IDX] * prompt_lengths[1] |
| 111 | + + [1, 6, -1] |
| 112 | + ] |
| 113 | + |
| 114 | + ds = chat_dataset( |
| 115 | + tokenizer=DummyTokenizer(), |
| 116 | + source="json", |
| 117 | + data_files=str(ASSETS / "chat_tiny.json"), |
| 118 | + conversation_column="conversations", |
| 119 | + conversation_style="sharegpt", |
| 120 | + train_on_input=False, |
| 121 | + packed=False, |
| 122 | + split="train", |
| 123 | + ) |
115 | 124 |
|
| 125 | + assert len(ds) == 1 |
116 | 126 | prompt, label = ds[0]["tokens"], ds[0]["labels"]
|
117 | 127 | assert prompt == expected_tokenized_prompts[0]
|
118 | 128 | assert label == expected_labels[0]
|
0 commit comments