|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +from unittest.mock import patch |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from torchtune import datasets |
| 12 | +from torchtune.datasets.alpaca import CROSS_ENTROPY_IGNORE_IDX |
| 13 | +from torchtune.modules.tokenizer import Tokenizer |
| 14 | + |
| 15 | +from tests.test_utils import get_assets_path |
| 16 | + |
| 17 | + |
| 18 | +class TestAlpacaDataset: |
| 19 | + @pytest.fixture |
| 20 | + def tokenizer(self): |
| 21 | + # m.model is a pretrained Sentencepiece model using the following command: |
| 22 | + # spm.SentencePieceTrainer.train('--input=<TRAIN_FILE> --model_prefix=m --vocab_size=2000') |
| 23 | + return Tokenizer.from_file(str(get_assets_path() / "m.model")) |
| 24 | + |
| 25 | + @patch("torchtune.datasets.alpaca.load_dataset") |
| 26 | + def test_prompt_generation(self, load_dataset, tokenizer): |
| 27 | + """ |
| 28 | + Test the prompt generation based on the alpaca template is correct. |
| 29 | + """ |
| 30 | + |
| 31 | + # mock the call to HF datasets |
| 32 | + load_dataset.return_value = [ |
| 33 | + { |
| 34 | + "instruction": "Give three tips for staying healthy.", |
| 35 | + "input": "", |
| 36 | + "output": ( |
| 37 | + "1.Eat a balanced diet and make sure to include plenty of fruits and vegetables." |
| 38 | + "2. Exercise regularly to keep your body active and strong." |
| 39 | + "3. Get enough sleep and maintain a consistent sleep schedule." |
| 40 | + ), |
| 41 | + }, |
| 42 | + { |
| 43 | + "instruction": "Evaluate this sentence for spelling and grammar mistakes", |
| 44 | + "input": "He finnished his meal and left the resturant", |
| 45 | + "output": "He finished his meal and left the restaurant.", |
| 46 | + }, |
| 47 | + ] |
| 48 | + |
| 49 | + # Expected prompts are taken from the "output" field in |
| 50 | + # https://huggingface.co/datasets/tatsu-lab/alpaca |
| 51 | + expected_prompts = [ |
| 52 | + ( |
| 53 | + "Below is an instruction that describes a task. Write a response that appropriately " |
| 54 | + "completes the request.\n\n" |
| 55 | + "### Instruction:\nGive three tips for staying healthy.\n\n" |
| 56 | + "### Response:\n" |
| 57 | + ), |
| 58 | + ( |
| 59 | + "Below is an instruction that describes a task, paired with an input that provides further context. " |
| 60 | + "Write a response that appropriately completes the request.\n\n" |
| 61 | + "### Instruction:\nEvaluate this sentence for spelling and grammar mistakes\n\n" |
| 62 | + "### Input:\nHe finnished his meal and left the resturant\n\n" |
| 63 | + "### Response:\n" |
| 64 | + ), |
| 65 | + ] |
| 66 | + |
| 67 | + alpaca_dataset = datasets.get_dataset("alpaca", tokenizer=tokenizer) |
| 68 | + |
| 69 | + # alpaca_dataset._data contains the raw data loaded from HF's dataset. We need the raw data |
| 70 | + # to test the prompt generation since calling __getitem__ on the alpaca_dataset object will |
| 71 | + # return the encoded input and label |
| 72 | + for idx, sample in enumerate(alpaca_dataset._data): |
| 73 | + assert expected_prompts[idx] == alpaca_dataset._generate_prompt( |
| 74 | + sample["instruction"], sample["input"] |
| 75 | + ) |
| 76 | + |
| 77 | + @patch("torchtune.datasets.alpaca.load_dataset") |
| 78 | + def test_label_no_masking(self, load_dataset, tokenizer): |
| 79 | + """ |
| 80 | + Test whether the input and the labels are correctly created when the input is not masked. |
| 81 | + """ |
| 82 | + |
| 83 | + # mock the call to HF datasets |
| 84 | + load_dataset.return_value = [ |
| 85 | + { |
| 86 | + "instruction": "Give three tips for staying healthy.", |
| 87 | + "input": "", |
| 88 | + "output": ( |
| 89 | + "1.Eat a balanced diet and make sure to include plenty of fruits and vegetables." |
| 90 | + "2. Exercise regularly to keep your body active and strong." |
| 91 | + "3. Get enough sleep and maintain a consistent sleep schedule." |
| 92 | + ), |
| 93 | + } |
| 94 | + ] |
| 95 | + |
| 96 | + alpaca_dataset = datasets.get_dataset("alpaca", tokenizer=tokenizer) |
| 97 | + input, labels = alpaca_dataset[0] |
| 98 | + |
| 99 | + assert len(input) == len(labels) |
| 100 | + assert labels[-1] == tokenizer.eos_id |
| 101 | + assert input[0] == tokenizer.bos_id |
| 102 | + assert CROSS_ENTROPY_IGNORE_IDX not in labels |
| 103 | + |
| 104 | + @patch("torchtune.datasets.alpaca.load_dataset") |
| 105 | + def test_label_masking(self, load_dataset, tokenizer): |
| 106 | + """ |
| 107 | + Test whether the input and the labels are correctly created when the input is masked. |
| 108 | + """ |
| 109 | + |
| 110 | + # mock the call to HF datasets |
| 111 | + load_dataset.return_value = [ |
| 112 | + { |
| 113 | + "instruction": "Give three tips for staying healthy.", |
| 114 | + "input": "", |
| 115 | + "output": ( |
| 116 | + "1.Eat a balanced diet and make sure to include plenty of fruits and vegetables." |
| 117 | + "2. Exercise regularly to keep your body active and strong." |
| 118 | + "3. Get enough sleep and maintain a consistent sleep schedule." |
| 119 | + ), |
| 120 | + } |
| 121 | + ] |
| 122 | + |
| 123 | + alpaca_dataset = datasets.get_dataset( |
| 124 | + "alpaca", tokenizer=tokenizer, train_on_input=False |
| 125 | + ) |
| 126 | + |
| 127 | + # Extract the prompt and tokenize it; we'll need this to test whether we're masking the |
| 128 | + # input correctly |
| 129 | + sample = alpaca_dataset._data[0] |
| 130 | + prompt = alpaca_dataset._generate_prompt(sample["instruction"], sample["input"]) |
| 131 | + encoded_prompt = tokenizer.encode(text=prompt, add_bos=True, add_eos=False) |
| 132 | + |
| 133 | + # Generate the input and labels |
| 134 | + input, labels = alpaca_dataset[0] |
| 135 | + |
| 136 | + assert len(input) == len(labels) |
| 137 | + assert labels[-1] == tokenizer.eos_id |
| 138 | + assert input[0] == tokenizer.bos_id |
| 139 | + assert labels.count(CROSS_ENTROPY_IGNORE_IDX) == len(encoded_prompt) |
| 140 | + |
| 141 | + @patch("torchtune.datasets.alpaca.load_dataset") |
| 142 | + def test_alpaca_clean(self, load_dataset, tokenizer): |
| 143 | + """ |
| 144 | + Test whether the input and the labels are correctly created when the input is not masked. |
| 145 | + """ |
| 146 | + |
| 147 | + # mock the call to HF datasets |
| 148 | + load_dataset.return_value = [ |
| 149 | + { |
| 150 | + "instruction": "Give three tips for staying healthy.", |
| 151 | + "input": "", |
| 152 | + "output": ( |
| 153 | + "1.Eat a balanced diet and make sure to include plenty of fruits and vegetables." |
| 154 | + "2. Exercise regularly to keep your body active and strong." |
| 155 | + "3. Get enough sleep and maintain a consistent sleep schedule." |
| 156 | + ), |
| 157 | + } |
| 158 | + ] |
| 159 | + |
| 160 | + alpaca_dataset = datasets.get_dataset( |
| 161 | + "alpaca", tokenizer=tokenizer, use_clean=True |
| 162 | + ) |
| 163 | + input, labels = alpaca_dataset[0] |
| 164 | + |
| 165 | + assert len(input) == len(labels) |
| 166 | + assert labels[-1] == tokenizer.eos_id |
| 167 | + assert input[0] == tokenizer.bos_id |
| 168 | + assert CROSS_ENTROPY_IGNORE_IDX not in labels |
0 commit comments