-
Notifications
You must be signed in to change notification settings - Fork 87
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
Support Parquet files in ShardedDataSource #764
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import datasets | ||
import fsspec | ||
import numpy as np | ||
import pyarrow.parquet as pq | ||
|
||
from levanter.utils import fsspec_utils | ||
|
||
|
@@ -149,6 +150,10 @@ def datasource_from_json(urls_or_paths: Sequence[str]) -> ShardedDataSource[dict | |
return JsonDataSource(urls_or_paths) | ||
|
||
|
||
def datasource_from_parquet(urls_or_paths: Sequence[str]) -> ShardedDataSource[dict]: | ||
return ParquetDataSource(urls_or_paths) | ||
|
||
|
||
class WrappedHFDataSource(ShardedDataSource[dict]): | ||
""" | ||
This class is responsible for loading a dataset from HuggingFace Datasets and returning the shards. | ||
|
@@ -238,6 +243,11 @@ def open_shard_at_row(self, shard_name: str, row: int) -> Iterator[str]: | |
data = json.load(f) | ||
for doc in data[row:]: | ||
yield doc[self.text_key] | ||
case ".parquet": | ||
table = pq.read_table(f) | ||
sliced_table = table.slice(row) | ||
for record in sliced_table.to_pylist(): | ||
yield record[self.text_key] # assumes text_key is in record | ||
case _: | ||
raise ValueError(f"Unknown format {format}") | ||
|
||
|
@@ -313,7 +323,7 @@ def open_shard_at_row(self, shard_name: str, row: int) -> Iterator[Tuple[np.ndar | |
|
||
|
||
def _sniff_format_for_dataset(url): | ||
good_formats = [".jsonl", ".txt", ".json"] | ||
good_formats = [".jsonl", ".txt", ".json", ".parquet"] | ||
format_from_url = None | ||
# try both with and without compression (could be gz, bz2, etc, so look at the "first" extension) | ||
extensions = [os.path.splitext(url)[1], os.path.splitext(os.path.splitext(url)[0])[1]] | ||
|
@@ -417,6 +427,24 @@ def open_shard_at_row(self, shard_name: str, row: int) -> Iterator[dict]: | |
return iter(data[row:]) | ||
|
||
|
||
class ParquetDataSource(ShardedDataSource[dict]): | ||
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. ideally the TextUrlDataSource would also work with parquet files. That is what we use for training configs typically 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. Makes sense, I added a new test for this in #766 |
||
def __init__(self, urls): | ||
self.urls = urls | ||
self._shard_name_to_url_mapping = _mk_shard_name_mapping(urls) | ||
|
||
@property | ||
def shard_names(self) -> Sequence[str]: | ||
return list(self._shard_name_to_url_mapping.keys()) | ||
|
||
def open_shard_at_row(self, shard_name: str, row: int) -> Iterator[dict]: | ||
url = self._shard_name_to_url_mapping[shard_name] | ||
with fsspec.open(url, "rb", compression="infer") as f: | ||
table = pq.read_table(f) | ||
sliced_table = table.slice(row) # zero-copy slicing | ||
for record in sliced_table.to_pylist(): | ||
yield record | ||
|
||
|
||
def _mk_shard_name_mapping(urls): | ||
_shard_name_to_url_mapping = {} | ||
# remove common prefix | ||
|
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.
read_table is expensive in the general case and it would be better to look at the metadata to figure out which row group to start on and then use
read_row_group
I thinkThere 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.
Thanks for suggesting this, incorporated this in #766