-
Notifications
You must be signed in to change notification settings - Fork 10
Add WebDataset reader #124
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
Open
Guilherme Penedo (guipenedo)
wants to merge
7
commits into
main
Choose a base branch
from
codex/webdataset-reader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
17d4755
Add WebDataset reader
guipenedo 78c5993
Fix WebDataset dotted field parsing
guipenedo c984421
Clarify WebDataset dotted basename behavior
guipenedo 22c9028
Normalize WebDataset tar member paths
guipenedo e9e5413
Preserve dotted WebDataset directories
guipenedo ec3d7de
Reject duplicate WebDataset fields
guipenedo e294057
Clarify WebDataset field parsing docs
guipenedo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterator, Mapping | ||
| import posixpath | ||
| import tarfile | ||
| from typing import Any | ||
|
|
||
| from fsspec import AbstractFileSystem | ||
| import orjson | ||
|
|
||
| from refiner.io import DataFile | ||
| from refiner.io.fileset import DataFileSetLike | ||
| from refiner.pipeline.data.datatype import DTypeMapping, dtype_to_plan | ||
| from refiner.pipeline.data.row import DictRow | ||
| from refiner.pipeline.data.shard import FilePartsDescriptor | ||
| from refiner.pipeline.sources.readers.base import BaseReader, Shard, SourceUnit | ||
| from refiner.pipeline.sources.readers.utils import DEFAULT_TARGET_SHARD_BYTES | ||
|
|
||
|
|
||
| class WebDatasetReader(BaseReader): | ||
| """WebDataset tar reader planned at archive granularity. | ||
|
|
||
| Each output row is one WebDataset sample. Members are grouped by the path | ||
| before the first dot in the basename, and the remaining suffix becomes the | ||
| output field name. JSON members are parsed to Python values by default; all | ||
| other member payloads are emitted as bytes. | ||
| """ | ||
|
|
||
| name = "read_webdataset" | ||
|
|
||
| def __init__( | ||
| self, | ||
| inputs: DataFileSetLike, | ||
| *, | ||
| fs: AbstractFileSystem | None = None, | ||
| storage_options: Mapping[str, Any] | None = None, | ||
| recursive: bool = False, | ||
| target_shard_bytes: int = DEFAULT_TARGET_SHARD_BYTES, | ||
| num_shards: int | None = None, | ||
| file_path_column: str | None = "file_path", | ||
| sample_key_column: str | None = "sample_key", | ||
| parse_json: bool = True, | ||
| dtypes: DTypeMapping | None = None, | ||
| ): | ||
| super().__init__( | ||
| inputs, | ||
| fs=fs, | ||
| storage_options=storage_options, | ||
| recursive=recursive, | ||
| extensions=(".tar", ".tar.gz", ".tgz"), | ||
| target_shard_bytes=target_shard_bytes, | ||
| num_shards=num_shards, | ||
| file_path_column=file_path_column, | ||
| split_by_bytes=False, | ||
| dtypes=dtypes, | ||
| ) | ||
| self.sample_key_column = sample_key_column | ||
| self.parse_json = parse_json | ||
| self._metadata_columns = frozenset( | ||
| name for name in (file_path_column, sample_key_column) if name is not None | ||
| ) | ||
| if ( | ||
| self.file_path_column is not None | ||
| and self.sample_key_column is not None | ||
| and self.file_path_column == self.sample_key_column | ||
| ): | ||
| raise ValueError("file_path_column and sample_key_column must be distinct") | ||
|
|
||
| def describe(self) -> dict[str, Any]: | ||
| description = super().describe() | ||
| description.update( | ||
| { | ||
| "sample_key_column": self.sample_key_column, | ||
| "parse_json": self.parse_json, | ||
| "dtypes": ( | ||
| {key: dtype_to_plan(dtype) for key, dtype in self.dtypes.items()} | ||
| if self.dtypes | ||
| else None | ||
| ), | ||
| } | ||
| ) | ||
| return description | ||
|
|
||
| def read_shard(self, shard: Shard) -> Iterator[SourceUnit]: | ||
| descriptor = shard.descriptor | ||
| assert isinstance(descriptor, FilePartsDescriptor) | ||
| for part in descriptor.parts: | ||
| source = self.fileset.resolve_file(part.source_index, part.path) | ||
| yield from self._read_archive(source) | ||
|
|
||
| def _read_archive(self, source: DataFile) -> Iterator[SourceUnit]: | ||
| current_key: str | None = None | ||
| current_row: dict[str, Any] = {} | ||
|
|
||
| def flush() -> Iterator[SourceUnit]: | ||
| if current_key is None: | ||
| return | ||
| row = dict(current_row) | ||
| if self.sample_key_column is not None: | ||
| row[self.sample_key_column] = current_key | ||
| yield DictRow(self._with_file_path(row, source)) | ||
|
|
||
| with ( | ||
| source.open(mode="rb") as raw, | ||
| tarfile.open(fileobj=raw, mode="r|*") as tar, | ||
| ): | ||
| for member in tar: | ||
| if not member.isfile(): | ||
| continue | ||
| member_path = posixpath.normpath(member.name).lstrip("/") | ||
| if not member_path or member_path == ".": | ||
| continue | ||
| directory, basename = posixpath.split(member_path) | ||
| sample_prefix, separator, field_name = basename.partition(".") | ||
| if not separator or not sample_prefix or not field_name: | ||
| continue | ||
| sample_key = ( | ||
| f"{directory}/{sample_prefix}" if directory else sample_prefix | ||
| ) | ||
| field_name = field_name.lower() | ||
| if current_key is not None and sample_key != current_key: | ||
| yield from flush() | ||
| current_row = {} | ||
| current_key = sample_key | ||
| if field_name in self._metadata_columns: | ||
| raise ValueError( | ||
| f"WebDataset member field {field_name!r} collides with a " | ||
| "metadata column; rename the metadata column or disable it" | ||
| ) | ||
| if field_name in current_row: | ||
| raise ValueError( | ||
| f"Duplicate WebDataset field {field_name!r} for sample " | ||
| f"{sample_key!r} in {source.abs_path()!r}" | ||
| ) | ||
| member_file = tar.extractfile(member) | ||
| if member_file is None: | ||
| current_row[field_name] = b"" | ||
| continue | ||
| with member_file: | ||
| payload = member_file.read() | ||
| if self.parse_json and ( | ||
| field_name == "json" or field_name.endswith(".json") | ||
| ): | ||
| try: | ||
| current_row[field_name] = orjson.loads(payload) | ||
| except orjson.JSONDecodeError as exc: | ||
| raise ValueError( | ||
| f"Invalid JSON member {member_path!r} in {source.abs_path()!r}" | ||
| ) from exc | ||
| continue | ||
| current_row[field_name] = payload | ||
|
|
||
| yield from flush() | ||
|
|
||
|
|
||
| __all__ = ["WebDatasetReader"] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.