| 12 | from braceexpand import braceexpand |
| 13 | |
| 14 | class JsonlIterableDataset(DataPipeline): |
| 15 | def __init__(self, path, process_fn, seed, *, shuffle_buffer=1000): |
| 16 | # set shuffle_buffer = 1 to disable it, model-parallel will be different due to shuffle |
| 17 | |
| 18 | # parse path, may mixed with dir |
| 19 | # if there is a comma not between {}, add one for expansion |
| 20 | path_wo_brace = re.sub(r"\{.*?\}", "", path) |
| 21 | if ',' in path_wo_brace: |
| 22 | path = '{' + path + '}' |
| 23 | expanded_path = [] |
| 24 | for p in braceexpand(path): |
| 25 | if p.endswith('.jsonl'): |
| 26 | expanded_path.append(p) |
| 27 | else: |
| 28 | # assert a existing folder |
| 29 | assert os.path.isdir(p), f"{p} is not a valid folder" |
| 30 | # find all jsonl files |
| 31 | for root, dirs, files in os.walk(p): |
| 32 | for file in files: |
| 33 | if file.endswith('.jsonl'): |
| 34 | file_path = os.path.join(root, file) |
| 35 | expanded_path.append(file_path) |
| 36 | path = expanded_path |
| 37 | |
| 38 | try: |
| 39 | from sat.mpu import get_model_parallel_world_size |
| 40 | if get_model_parallel_world_size() > 1: |
| 41 | shuffle_buffer = 1 |
| 42 | except Exception: |
| 43 | pass |
| 44 | super().__init__( |
| 45 | ConfiguredResampledShards(path, seed), # Lots of shards are recommended, or not evenly |
| 46 | jsonl_samples, |
| 47 | webdataset.shuffle(shuffle_buffer), |
| 48 | process_fn |
| 49 | ) |
| 50 | |
| 51 | def jsonl_expander(streams): |
| 52 | for source in streams: |