-
Notifications
You must be signed in to change notification settings - Fork 566
Add Bagz format support #4496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add Bagz format support #4496
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
|
|
||
| import grain.python as grain | ||
| from grain.experimental import ElasticIterator | ||
| import bagz | ||
|
|
||
| from maxtext.input_pipeline import data_processing_utils | ||
| from maxtext.input_pipeline import input_pipeline_utils | ||
|
|
@@ -180,6 +181,76 @@ def create_dataset_from_pattern(pattern): | |
| elastic=elastic, | ||
| ) | ||
| return dataset | ||
| elif data_file_type == "bagz": | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic looks exactly the same as arrayrecord besides create_dataset_from_pattern, should we combine them into one? |
||
| def create_dataset_from_pattern(pattern): | ||
| files = find_data_files(pattern) | ||
| source = input_pipeline_utils.BagzDataSource(files) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reasons not directly using |
||
| return grain.MapDataset.source(source) | ||
|
|
||
| if mixture_config_path: | ||
| with open(mixture_config_path, "r", encoding="utf-8") as f: | ||
| mixture_config = json.load(f) | ||
| paths = [config["path"] for config in mixture_config.values()] | ||
| weights = [float(config["weight"]) for config in mixture_config.values()] | ||
|
|
||
| executor = futures.ThreadPoolExecutor(max_workers=grain_data_source_max_workers) | ||
| dataset_list = list(executor.map(create_dataset_from_pattern, paths)) | ||
| executor.shutdown(wait=True) | ||
|
|
||
| datasets_dict = dict(zip(mixture_config.keys(), dataset_list)) | ||
| for name, ds in datasets_dict.items(): | ||
| datasets_dict[name] = _apply_mapdataset_transforms( | ||
| ds, | ||
| shuffle, | ||
| shuffle_seed, | ||
| num_epoch, | ||
| dataloading_host_index, | ||
| dataloading_host_count, | ||
| grain_num_threads, | ||
| grain_prefetch_buffer_size, | ||
| ) | ||
|
|
||
| total_weight = sum(weights) | ||
| weights_dict = {name: weight / total_weight for name, weight in zip(mixture_config.keys(), weights)} | ||
| dataset = grain.IterDataset.mix(datasets_dict, weights_dict) | ||
| return dataset | ||
| elif ";" in data_file_pattern: | ||
| data_file_patterns, weights = zip(*[pattern.split(",") for pattern in data_file_pattern.split(";")]) | ||
| assert len(data_file_patterns) == len(weights) | ||
| weights = [float(w) for w in weights] | ||
| weights = [round(w / sum(weights), 4) for w in weights] | ||
|
|
||
| executor = futures.ThreadPoolExecutor(max_workers=grain_data_source_max_workers) | ||
| dataset_list = list(executor.map(create_dataset_from_pattern, data_file_patterns)) | ||
| executor.shutdown(wait=True) | ||
|
|
||
| for d, _ in enumerate(dataset_list): | ||
| dataset_list[d] = _apply_mapdataset_transforms( | ||
| dataset_list[d], | ||
| shuffle, | ||
| shuffle_seed, | ||
| num_epoch, | ||
| dataloading_host_index, | ||
| dataloading_host_count, | ||
| grain_num_threads, | ||
| grain_prefetch_buffer_size, | ||
| ) | ||
| dataset = grain.IterDataset.mix(dataset_list, weights) | ||
| return dataset | ||
| else: | ||
| dataset = create_dataset_from_pattern(data_file_pattern) | ||
| dataset = _apply_mapdataset_transforms( | ||
| dataset, | ||
| shuffle, | ||
| shuffle_seed, | ||
| num_epoch, | ||
| dataloading_host_index, | ||
| dataloading_host_count, | ||
| grain_num_threads, | ||
| grain_prefetch_buffer_size, | ||
| elastic=elastic, | ||
| ) | ||
| return dataset | ||
| elif data_file_type in ("tfrecord", "parquet"): | ||
| data_files = find_data_files(data_file_pattern) | ||
| file_slice, files_per_host, row_shard = input_pipeline_utils.compute_file_sharding( | ||
|
|
@@ -215,7 +286,7 @@ def create_dataset_from_pattern(pattern): | |
| return dataset | ||
| else: | ||
| raise ValueError( | ||
| f"grain pipeline supports (arrayrecord, tfrecord, parquet) as grain_file_type, but got {data_file_type}" | ||
| f"grain pipeline supports (arrayrecord, tfrecord, parquet, bagz) as grain_file_type, but got {data_file_type}" | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1070,3 +1070,43 @@ def map(self, element: dict[str, np.ndarray]) -> dict[str, np.ndarray]: | |
| element[f"{self.data_column}_mrope_deltas"] = mrope_position_deltas | ||
|
|
||
| return element | ||
|
|
||
|
|
||
| import bagz | ||
|
|
||
| class BagzDataSource: | ||
| """A fully picklable and fork-safe RandomAccessDataSource for Bagz files.""" | ||
| def __init__(self, paths: list[str] | str): | ||
| if isinstance(paths, (list, tuple)): | ||
| self._path = ",".join(sorted(paths)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we have to sort the paths here? |
||
| else: | ||
| self._path = paths | ||
| self._len = None | ||
| self._reader = None | ||
|
|
||
| @property | ||
| def reader(self): | ||
| if self._reader is None: | ||
| self._reader = bagz.Reader(self._path) | ||
| return self._reader | ||
|
|
||
| def __len__(self) -> int: | ||
| if self._len is None: | ||
| self._len = len(bagz.Reader(self._path)) | ||
| return self._len | ||
|
|
||
| def __getitem__(self, record_key: int): | ||
| return self.reader[record_key] | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"BagzDataSource(path={self._path!r})" | ||
|
|
||
| def __getstate__(self): | ||
| state = self.__dict__.copy() | ||
| state["_reader"] = None | ||
| return state | ||
|
|
||
| def __setstate__(self, state): | ||
| self.__dict__.update(state) | ||
| self._reader = None | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we tested the elastic iterator with BagZ?