|
| 1 | +# Third Party |
| 2 | +from datasets import Dataset |
| 3 | +from datasets.exceptions import DatasetGenerationError |
| 4 | +from transformers import AutoTokenizer, DataCollatorForSeq2Seq |
| 5 | +from trl import DataCollatorForCompletionOnlyLM |
| 6 | +import pytest |
| 7 | + |
| 8 | +# First Party |
| 9 | +from tests.data import ( |
| 10 | + MALFORMATTED_DATA, |
| 11 | + TWITTER_COMPLAINTS_DATA, |
| 12 | + TWITTER_COMPLAINTS_DATA_INPUT_OUTPUT, |
| 13 | +) |
| 14 | + |
| 15 | +# Local |
| 16 | +from tuning.utils.preprocessing_utils import ( |
| 17 | + combine_sequence, |
| 18 | + get_data_trainer_kwargs, |
| 19 | + get_preprocessed_dataset, |
| 20 | + load_hf_dataset_from_jsonl_file, |
| 21 | + validate_data_args, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize( |
| 26 | + "input_element,output_element,expected_res", |
| 27 | + [ |
| 28 | + ("foo ", "bar", "foo bar"), |
| 29 | + ("foo\n", "bar", "foo\nbar"), |
| 30 | + ("foo\t", "bar", "foo\tbar"), |
| 31 | + ("foo", "bar", "foo bar"), |
| 32 | + ], |
| 33 | +) |
| 34 | +def test_combine_sequence(input_element, output_element, expected_res): |
| 35 | + """Ensure that input / output elements are combined with correct whitespace handling.""" |
| 36 | + comb_seq = combine_sequence(input_element, output_element) |
| 37 | + assert isinstance(comb_seq, str) |
| 38 | + assert comb_seq == expected_res |
| 39 | + |
| 40 | + |
| 41 | +# Tests for loading the dataset from disk |
| 42 | +def test_load_hf_dataset_from_jsonl_file(): |
| 43 | + input_field_name = "Tweet text" |
| 44 | + output_field_name = "text_label" |
| 45 | + data = load_hf_dataset_from_jsonl_file( |
| 46 | + TWITTER_COMPLAINTS_DATA, |
| 47 | + input_field_name=input_field_name, |
| 48 | + output_field_name=output_field_name, |
| 49 | + ) |
| 50 | + # Our dataset should contain dicts that contain the input / output field name types |
| 51 | + next_data = next(iter(data)) |
| 52 | + assert input_field_name in next_data |
| 53 | + assert output_field_name in next_data |
| 54 | + |
| 55 | + |
| 56 | +def test_load_hf_dataset_from_jsonl_file_wrong_keys(): |
| 57 | + """Ensure that we explode if the keys are not in the jsonl file.""" |
| 58 | + with pytest.raises(DatasetGenerationError): |
| 59 | + load_hf_dataset_from_jsonl_file( |
| 60 | + TWITTER_COMPLAINTS_DATA, input_field_name="foo", output_field_name="bar" |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def test_load_hf_dataset_from_malformatted_data(): |
| 65 | + """Ensure that we explode if the data is not properly formatted.""" |
| 66 | + # NOTE: The actual keys don't matter here |
| 67 | + with pytest.raises(DatasetGenerationError): |
| 68 | + load_hf_dataset_from_jsonl_file( |
| 69 | + MALFORMATTED_DATA, input_field_name="foo", output_field_name="bar" |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def test_load_hf_dataset_from_jsonl_file_duplicate_keys(): |
| 74 | + """Ensure we cannot have the same key for input / output.""" |
| 75 | + with pytest.raises(ValueError): |
| 76 | + load_hf_dataset_from_jsonl_file( |
| 77 | + TWITTER_COMPLAINTS_DATA, |
| 78 | + input_field_name="Tweet text", |
| 79 | + output_field_name="Tweet text", |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +# Tests for custom masking / preprocessing logic |
| 84 | +@pytest.mark.parametrize("max_sequence_length", [1, 10, 100, 1000]) |
| 85 | +def test_get_preprocessed_dataset(max_sequence_length): |
| 86 | + tokenizer = AutoTokenizer.from_pretrained("Maykeye/TinyLLama-v0") |
| 87 | + preprocessed_data = get_preprocessed_dataset( |
| 88 | + data_path=TWITTER_COMPLAINTS_DATA, |
| 89 | + tokenizer=tokenizer, |
| 90 | + max_sequence_length=max_sequence_length, |
| 91 | + input_field_name="Tweet text", |
| 92 | + output_field_name="text_label", |
| 93 | + ) |
| 94 | + for tok_res in preprocessed_data: |
| 95 | + # Since the padding is left to the collator, there should be no 0s in the attention mask yet |
| 96 | + assert sum(tok_res["attention_mask"]) == len(tok_res["attention_mask"]) |
| 97 | + # If the source text isn't empty, we start with masked inputs |
| 98 | + assert tok_res["labels"][0] == -100 |
| 99 | + # All keys in the produced record must be the same length |
| 100 | + key_lengths = {len(tok_res[k]) for k in tok_res.keys()} |
| 101 | + assert len(key_lengths) == 1 |
| 102 | + # And also that length should be less than or equal to the max length depending on if we |
| 103 | + # are going up to / over the max size and truncating - padding is handled separately |
| 104 | + assert key_lengths.pop() <= max_sequence_length |
| 105 | + |
| 106 | + |
| 107 | +# Tests for fetching train args |
| 108 | +@pytest.mark.parametrize( |
| 109 | + "use_validation_data, collator_type, packing", |
| 110 | + [ |
| 111 | + (True, None, True), |
| 112 | + (False, None, True), |
| 113 | + (True, DataCollatorForCompletionOnlyLM, False), |
| 114 | + (False, DataCollatorForCompletionOnlyLM, False), |
| 115 | + ], |
| 116 | +) |
| 117 | +def test_get_trainer_kwargs_with_response_template_and_text_field( |
| 118 | + use_validation_data, collator_type, packing |
| 119 | +): |
| 120 | + training_data_path = TWITTER_COMPLAINTS_DATA |
| 121 | + validation_data_path = training_data_path if use_validation_data else None |
| 122 | + # Expected columns in the raw loaded dataset for the twitter data |
| 123 | + column_names = set(["Tweet text", "ID", "Label", "text_label", "output"]) |
| 124 | + trainer_kwargs = get_data_trainer_kwargs( |
| 125 | + training_data_path=training_data_path, |
| 126 | + validation_data_path=validation_data_path, |
| 127 | + packing=packing, |
| 128 | + response_template="\n### Label:", |
| 129 | + max_sequence_length=100, |
| 130 | + tokenizer=AutoTokenizer.from_pretrained("Maykeye/TinyLLama-v0"), |
| 131 | + dataset_text_field="output", |
| 132 | + ) |
| 133 | + assert len(trainer_kwargs) == 3 |
| 134 | + # If we are packing, we should not have a data collator |
| 135 | + if collator_type is None: |
| 136 | + assert trainer_kwargs["data_collator"] is None |
| 137 | + else: |
| 138 | + assert isinstance(trainer_kwargs["data_collator"], collator_type) |
| 139 | + |
| 140 | + # We should only have a validation dataset if one is present |
| 141 | + if validation_data_path is None: |
| 142 | + assert trainer_kwargs["eval_dataset"] is None |
| 143 | + else: |
| 144 | + assert isinstance(trainer_kwargs["eval_dataset"], Dataset) |
| 145 | + assert set(trainer_kwargs["eval_dataset"].column_names) == column_names |
| 146 | + |
| 147 | + assert isinstance(trainer_kwargs["train_dataset"], Dataset) |
| 148 | + assert set(trainer_kwargs["train_dataset"].column_names) == column_names |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.parametrize("use_validation_data", [True, False]) |
| 152 | +def test_get_trainer_kwargs_with_custom_masking(use_validation_data): |
| 153 | + training_data_path = TWITTER_COMPLAINTS_DATA_INPUT_OUTPUT |
| 154 | + validation_data_path = training_data_path if use_validation_data else None |
| 155 | + # Expected columns in the raw loaded dataset for the twitter data |
| 156 | + column_names = set(["input_ids", "attention_mask", "labels"]) |
| 157 | + trainer_kwargs = get_data_trainer_kwargs( |
| 158 | + training_data_path=training_data_path, |
| 159 | + validation_data_path=validation_data_path, |
| 160 | + packing=False, |
| 161 | + response_template=None, |
| 162 | + max_sequence_length=100, |
| 163 | + tokenizer=AutoTokenizer.from_pretrained("Maykeye/TinyLLama-v0"), |
| 164 | + dataset_text_field=None, |
| 165 | + ) |
| 166 | + assert len(trainer_kwargs) == 4 |
| 167 | + # If we are packing, we should not have a data collator |
| 168 | + assert isinstance(trainer_kwargs["data_collator"], DataCollatorForSeq2Seq) |
| 169 | + |
| 170 | + # We should only have a validation dataset if one is present |
| 171 | + if validation_data_path is None: |
| 172 | + assert trainer_kwargs["eval_dataset"] is None |
| 173 | + else: |
| 174 | + assert isinstance(trainer_kwargs["eval_dataset"], Dataset) |
| 175 | + assert set(trainer_kwargs["eval_dataset"].column_names) == column_names |
| 176 | + |
| 177 | + assert isinstance(trainer_kwargs["train_dataset"], Dataset) |
| 178 | + assert set(trainer_kwargs["train_dataset"].column_names) == column_names |
| 179 | + # Needed to sidestep TRL validation |
| 180 | + assert trainer_kwargs["formatting_func"] is not None |
| 181 | + |
| 182 | + |
| 183 | +# Tests for fetching train args |
| 184 | +@pytest.mark.parametrize( |
| 185 | + "dataset_text_field, response_template", |
| 186 | + [ |
| 187 | + ("input", None), |
| 188 | + (None, "output"), |
| 189 | + ], |
| 190 | +) |
| 191 | +def test_validate_args(dataset_text_field, response_template): |
| 192 | + with pytest.raises(ValueError): |
| 193 | + validate_data_args(dataset_text_field, response_template) |
0 commit comments