-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data] Add support for shuffling input files (#40154)
This PR is to add support for shuffling input files ordering, for all file-based data sources. The interface for controlling the behavior is through `shuffle` argument in all read APIs for file-based data sources: ```py # Enable input files shuffling with default seed ds = ray.data.read_parquet(..., shuffle="files") ds = ray.data.read_images(..., shuffle="files") ``` Signed-off-by: Cheng Su <[email protected]>
- Loading branch information
Showing
7 changed files
with
208 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,33 @@ | ||
from typing import Any, Dict, List, Tuple | ||
import sys | ||
from typing import Any, List, Union | ||
|
||
import numpy as np | ||
|
||
class FileMetadataShuffler: | ||
"""Abstract class for file metadata shuffler. | ||
Shufflers live on the driver side of the Dataset only. | ||
""" | ||
if sys.version_info >= (3, 8): | ||
from typing import Literal | ||
else: | ||
from typing_extensions import Literal | ||
|
||
def __init__(self, reader_args: Dict[str, Any]): | ||
self._reader_args = reader_args | ||
|
||
def shuffle_files( | ||
self, | ||
paths_and_sizes: List[Tuple[str, int]], | ||
) -> List[Tuple[str, int]]: | ||
"""Shuffle files in the given paths and sizes. | ||
Args: | ||
paths_and_sizes: The file paths and file sizes to shuffle. | ||
Returns: | ||
The file paths and their sizes after shuffling. | ||
""" | ||
raise NotImplementedError | ||
class FileMetadataShuffler: | ||
"""Random shuffle file metadata when the `shuffle` parameter enables it. | ||
Otherwise returns file metadata in its original order. | ||
""" | ||
|
||
def __init__(self, shuffle: Union[Literal["files"], None]): | ||
self._is_shuffle_enabled = False | ||
if shuffle == "files": | ||
self._is_shuffle_enabled = True | ||
self._generator = np.random.default_rng() | ||
|
||
class SequentialFileMetadataShuffler(FileMetadataShuffler): | ||
def shuffle_files( | ||
self, | ||
paths_and_sizes: List[Tuple[str, int]], | ||
) -> List[Tuple[str, int]]: | ||
"""Return files in the given paths and sizes sequentially.""" | ||
return paths_and_sizes | ||
files_metadata: List[Any], | ||
) -> List[Any]: | ||
if self._is_shuffle_enabled: | ||
return [ | ||
files_metadata[i] | ||
for i in self._generator.permutation(len(files_metadata)) | ||
] | ||
else: | ||
return files_metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.