|
| 1 | +import os |
| 2 | +import json |
| 3 | +import unittest |
| 4 | + |
| 5 | +from datasets import config, load_dataset |
| 6 | + |
| 7 | +from data_juicer.core import NestedDataset |
| 8 | +from data_juicer.utils.compress import compress, decompress, cleanup_compressed_cache_files, CompressionOff |
| 9 | +from data_juicer.utils import cache_utils |
| 10 | +from data_juicer.utils.unittest_utils import DataJuicerTestCaseBase |
| 11 | + |
| 12 | +class CacheCompressTest(DataJuicerTestCaseBase): |
| 13 | + |
| 14 | + def setUp(self) -> None: |
| 15 | + self.temp_output_path = 'tmp/test_compress/' |
| 16 | + self.test_data_path = self.temp_output_path + 'test.json' |
| 17 | + os.makedirs(self.temp_output_path, exist_ok=True) |
| 18 | + with open(self.test_data_path, 'w') as fout: |
| 19 | + json.dump([{'test_key_1': 'test_val_1'}], fout) |
| 20 | + self.ori_cache_dir = config.HF_DATASETS_CACHE |
| 21 | + config.HF_DATASETS_CACHE = self.temp_output_path |
| 22 | + |
| 23 | + def tearDown(self): |
| 24 | + if os.path.exists(self.temp_output_path): |
| 25 | + os.system(f'rm -rf {self.temp_output_path}') |
| 26 | + config.HF_DATASETS_CACHE = self.ori_cache_dir |
| 27 | + |
| 28 | + def test_basic_func(self): |
| 29 | + cache_utils.CACHE_COMPRESS = 'zstd' |
| 30 | + ds = load_dataset('json', data_files=self.test_data_path, split='train') |
| 31 | + prev_ds = ds.map(lambda s: {'test_key_2': 'test_val_2', **s}) |
| 32 | + curr_ds = prev_ds.map(lambda s: {'test_key_3': 'test_val_3', **s}) |
| 33 | + for fn in ds.cache_files: |
| 34 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 35 | + for fn in prev_ds.cache_files: |
| 36 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 37 | + for fn in curr_ds.cache_files: |
| 38 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 39 | + |
| 40 | + # won't compress original dataset |
| 41 | + compress(ds, prev_ds) |
| 42 | + # cache files of the original dataset always exist |
| 43 | + for fn in ds.cache_files: |
| 44 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 45 | + self.assertFalse(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 46 | + # cache files of the previous dataset are deleted |
| 47 | + for fn in prev_ds.cache_files: |
| 48 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 49 | + # cache files of the current dataset are kept |
| 50 | + for fn in curr_ds.cache_files: |
| 51 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 52 | + |
| 53 | + # compress previous dataset |
| 54 | + compress(prev_ds, curr_ds) |
| 55 | + # cache files of the original dataset always exist |
| 56 | + for fn in ds.cache_files: |
| 57 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 58 | + # cache files of the previous dataset are deleted |
| 59 | + for fn in prev_ds.cache_files: |
| 60 | + self.assertFalse(os.path.exists(fn['filename'])) |
| 61 | + self.assertTrue(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 62 | + # cache files of the current dataset are kept |
| 63 | + for fn in curr_ds.cache_files: |
| 64 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 65 | + |
| 66 | + # decompress the previous dataset |
| 67 | + decompress(prev_ds) |
| 68 | + for fn in prev_ds.cache_files: |
| 69 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 70 | + self.assertTrue(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 71 | + |
| 72 | + # clean up the compressed cache files of the previous dataset |
| 73 | + cleanup_compressed_cache_files(prev_ds) |
| 74 | + for fn in prev_ds.cache_files: |
| 75 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 76 | + self.assertFalse(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 77 | + |
| 78 | + def test_dif_compress_method(self): |
| 79 | + cache_utils.CACHE_COMPRESS = 'gzip' |
| 80 | + ds = load_dataset('json', data_files=self.test_data_path, split='train') |
| 81 | + prev_ds = ds.map(lambda s: {'test_key_2': 'test_val_2', **s}) |
| 82 | + curr_ds = prev_ds.map(lambda s: {'test_key_3': 'test_val_3', **s}) |
| 83 | + for fn in ds.cache_files: |
| 84 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 85 | + for fn in prev_ds.cache_files: |
| 86 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 87 | + for fn in curr_ds.cache_files: |
| 88 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 89 | + |
| 90 | + # won't compress original dataset |
| 91 | + compress(ds, prev_ds) |
| 92 | + # cache files of the original dataset always exist |
| 93 | + for fn in ds.cache_files: |
| 94 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 95 | + self.assertFalse(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 96 | + # cache files of the previous dataset are deleted |
| 97 | + for fn in prev_ds.cache_files: |
| 98 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 99 | + # cache files of the current dataset are kept |
| 100 | + for fn in curr_ds.cache_files: |
| 101 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 102 | + |
| 103 | + # compress previous dataset |
| 104 | + compress(prev_ds, curr_ds) |
| 105 | + # cache files of the original dataset always exist |
| 106 | + for fn in ds.cache_files: |
| 107 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 108 | + # cache files of the previous dataset are deleted |
| 109 | + for fn in prev_ds.cache_files: |
| 110 | + self.assertFalse(os.path.exists(fn['filename'])) |
| 111 | + self.assertTrue(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 112 | + # cache files of the current dataset are kept |
| 113 | + for fn in curr_ds.cache_files: |
| 114 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 115 | + |
| 116 | + # decompress the previous dataset |
| 117 | + decompress(prev_ds) |
| 118 | + for fn in prev_ds.cache_files: |
| 119 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 120 | + self.assertTrue(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 121 | + |
| 122 | + # clean up the compressed cache files of the previous dataset |
| 123 | + cleanup_compressed_cache_files(prev_ds) |
| 124 | + for fn in prev_ds.cache_files: |
| 125 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 126 | + self.assertFalse(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 127 | + |
| 128 | + def test_multiprocessing(self): |
| 129 | + cache_utils.CACHE_COMPRESS = 'zstd' |
| 130 | + ds = load_dataset('json', data_files=self.test_data_path, split='train') |
| 131 | + prev_ds = ds.map(lambda s: {'test_key_2': 'test_val_2', **s}) |
| 132 | + curr_ds = prev_ds.map(lambda s: {'test_key_3': 'test_val_3', **s}) |
| 133 | + for fn in ds.cache_files: |
| 134 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 135 | + for fn in prev_ds.cache_files: |
| 136 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 137 | + for fn in curr_ds.cache_files: |
| 138 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 139 | + compress(prev_ds, curr_ds, num_proc=2) |
| 140 | + # cache files of the original dataset always exist |
| 141 | + for fn in ds.cache_files: |
| 142 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 143 | + # cache files of the previous dataset are deleted |
| 144 | + for fn in prev_ds.cache_files: |
| 145 | + self.assertFalse(os.path.exists(fn['filename'])) |
| 146 | + self.assertTrue(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 147 | + # cache files of the current dataset are kept |
| 148 | + for fn in curr_ds.cache_files: |
| 149 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 150 | + |
| 151 | + # decompress the previous dataset |
| 152 | + decompress(prev_ds, num_proc=2) |
| 153 | + for fn in prev_ds.cache_files: |
| 154 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 155 | + self.assertTrue(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 156 | + |
| 157 | + # clean up the compressed cache files of the previous dataset |
| 158 | + cleanup_compressed_cache_files(prev_ds) |
| 159 | + for fn in prev_ds.cache_files: |
| 160 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 161 | + self.assertFalse(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 162 | + |
| 163 | + def test_compression_off(self): |
| 164 | + cache_utils.CACHE_COMPRESS = 'lz4' |
| 165 | + ds = load_dataset('json', data_files=self.test_data_path, split='train') |
| 166 | + prev_ds = ds.map(lambda s: {'test_key_2': 'test_val_2', **s}) |
| 167 | + curr_ds = prev_ds.map(lambda s: {'test_key_3': 'test_val_3', **s}) |
| 168 | + for fn in ds.cache_files: |
| 169 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 170 | + for fn in prev_ds.cache_files: |
| 171 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 172 | + for fn in curr_ds.cache_files: |
| 173 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 174 | + |
| 175 | + # disable cache compression |
| 176 | + with CompressionOff(): |
| 177 | + compress(prev_ds, curr_ds) |
| 178 | + # cache files of the original dataset always exist |
| 179 | + for fn in ds.cache_files: |
| 180 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 181 | + # cache files of the previous dataset are deleted |
| 182 | + for fn in prev_ds.cache_files: |
| 183 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 184 | + self.assertFalse(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 185 | + # cache files of the current dataset are kept |
| 186 | + for fn in curr_ds.cache_files: |
| 187 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 188 | + |
| 189 | + # re-enable cache compression |
| 190 | + compress(prev_ds, curr_ds) |
| 191 | + # cache files of the original dataset always exist |
| 192 | + for fn in ds.cache_files: |
| 193 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 194 | + # cache files of the previous dataset are deleted |
| 195 | + for fn in prev_ds.cache_files: |
| 196 | + self.assertFalse(os.path.exists(fn['filename'])) |
| 197 | + self.assertTrue(os.path.exists(fn['filename'] + f'.{cache_utils.CACHE_COMPRESS}')) |
| 198 | + # cache files of the current dataset are kept |
| 199 | + for fn in curr_ds.cache_files: |
| 200 | + self.assertTrue(os.path.exists(fn['filename'])) |
| 201 | + |
| 202 | + def test_dataset_without_cache(self): |
| 203 | + prev_ds = NestedDataset.from_list([{'test_key': 'test_val'}]) |
| 204 | + curr_ds = prev_ds.map(lambda s: {'test_key_2': 'test_val_2', **s}) |
| 205 | + # dataset from list does not have cache files |
| 206 | + self.assertTrue(len(prev_ds.cache_files) == 0) |
| 207 | + self.assertTrue(len(curr_ds.cache_files) == 0) |
| 208 | + compress(prev_ds, curr_ds) |
| 209 | + decompress(prev_ds) |
| 210 | + cleanup_compressed_cache_files(prev_ds) |
| 211 | + self.assertTrue(len(prev_ds.cache_files) == 0) |
| 212 | + self.assertTrue(len(curr_ds.cache_files) == 0) |
| 213 | + |
| 214 | + |
| 215 | +if __name__ == '__main__': |
| 216 | + unittest.main() |
0 commit comments