|
18 | 18 |
|
19 | 19 | import pytest |
20 | 20 | import transformers |
21 | | -from datasets import Dataset, DatasetDict |
| 21 | +from datasets import Dataset, DatasetDict, IterableDataset, IterableDatasetDict |
22 | 22 | from packaging.version import Version |
23 | 23 | from transformers import AutoProcessor, AutoTokenizer, is_vision_available |
24 | 24 |
|
@@ -1082,6 +1082,36 @@ def test_unpair_preference_dataset_iterable_extra_columns(self): |
1082 | 1082 | dict(zip(expected.keys(), vals, strict=False)) for vals in zip(*expected.values(), strict=False) |
1083 | 1083 | ] |
1084 | 1084 |
|
| 1085 | + def test_unpair_preference_dataset_iterable_dict(self): |
| 1086 | + # Test that an IterableDatasetDict is correctly unpaired |
| 1087 | + paired_dataset_dict = IterableDatasetDict({"abc": self.paired_dataset.to_iterable_dataset()}) |
| 1088 | + unpaired_dataset_dict = unpair_preference_dataset(paired_dataset_dict) |
| 1089 | + assert list(unpaired_dataset_dict["abc"]) == [ |
| 1090 | + dict(zip(self.unpaired_dataset.column_names, vals, strict=False)) |
| 1091 | + for vals in zip(*self.unpaired_dataset.to_dict().values(), strict=False) |
| 1092 | + ] |
| 1093 | + |
| 1094 | + def test_unpair_preference_dataset_iterable_dict_without_column_names(self): |
| 1095 | + # Test that an IterableDatasetDict without schema metadata is correctly unpaired |
| 1096 | + def generate_examples(): |
| 1097 | + for prompt, chosen, rejected in zip( |
| 1098 | + self.paired_dataset["prompt"], |
| 1099 | + self.paired_dataset["chosen"], |
| 1100 | + self.paired_dataset["rejected"], |
| 1101 | + strict=True, |
| 1102 | + ): |
| 1103 | + yield {"prompt": prompt, "chosen": chosen, "rejected": rejected} |
| 1104 | + |
| 1105 | + paired_dataset_dict = IterableDatasetDict({"abc": IterableDataset.from_generator(generate_examples)}) |
| 1106 | + assert paired_dataset_dict["abc"].column_names is None |
| 1107 | + |
| 1108 | + unpaired_dataset_dict = unpair_preference_dataset(paired_dataset_dict) |
| 1109 | + |
| 1110 | + assert list(unpaired_dataset_dict["abc"]) == [ |
| 1111 | + dict(zip(self.unpaired_dataset.column_names, vals, strict=False)) |
| 1112 | + for vals in zip(*self.unpaired_dataset.to_dict().values(), strict=False) |
| 1113 | + ] |
| 1114 | + |
1085 | 1115 | def test_unpair_preference_dataset_dict(self): |
1086 | 1116 | # Test that a paired dataset dict is correctly converted to unpaired |
1087 | 1117 | paired_dataset_dict = DatasetDict({"abc": self.paired_dataset}) |
|
0 commit comments