Skip to content

Read the length of the datasource from the FileInstructions to limit I/O. #11033

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tensorflow_datasets/core/data_sources/array_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ArrayRecordDataSource(base.BaseDataSource):
length: int = dataclasses.field(init=False)

def __post_init__(self):
file_instructions = base.file_instructions(self.dataset_info, self.split)
file_instructions = self.split_info.file_instructions
self.data_source = array_record_data_source.ArrayRecordDataSource(
file_instructions
)
19 changes: 8 additions & 11 deletions tensorflow_datasets/core/data_sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ def __getitems__(self, keys: Iterable[int]) -> T:
"""Returns the value for the given `keys`."""


def file_instructions(
dataset_info: dataset_info_lib.DatasetInfo,
split: splits_lib.Split | None = None,
) -> list[shard_utils.FileInstruction]:
"""Retrieves the file instructions from the DatasetInfo."""
split_infos = dataset_info.splits.values()
split_dict = splits_lib.SplitDict(split_infos=split_infos)
return split_dict[split].file_instructions


@dataclasses.dataclass
class BaseDataSource(MappingView, Sequence):
"""Base DataSource to override all dunder methods with the deserialization.
Expand Down Expand Up @@ -94,6 +84,13 @@ def _deserialize(self, record: Any) -> Any:
return features.deserialize_example_np(record, decoders=self.decoders) # pylint: disable=attribute-error
raise ValueError('No features set, cannot decode example!')

@property
def split_info(self) -> splits_lib.SplitInfo | splits_lib.SubSplitInfo:
"""Returns the SplitInfo for the split."""
split_infos = self.dataset_info.splits.values()
splits_dict = splits_lib.SplitDict(split_infos=split_infos)
return splits_dict[self.split] # will raise an error if split is not found

def __getitem__(self, key: SupportsIndex) -> Any:
record = self.data_source[key.__index__()]
return self._deserialize(record)
Expand Down Expand Up @@ -133,7 +130,7 @@ def __repr__(self) -> str:
)

def __len__(self) -> int:
return self.data_source.__len__()
return sum(fi.take for fi in self.split_info.file_instructions)

def __iter__(self):
for i in range(self.__len__()):
Expand Down
6 changes: 6 additions & 0 deletions tensorflow_datasets/core/data_sources/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def test_read_write(
for i, element in enumerate(data_source):
assert element == {'id': i}

# Also works on sliced splits.
data_source = builder.as_data_source(split='train[0:2]')
assert len(data_source) == 2
data_source = builder.as_data_source(split='train[:50%]')
assert len(data_source) == 2


_FILE_INSTRUCTIONS = [
shard_utils.FileInstruction(
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_datasets/core/data_sources/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ParquetDataSource(base.BaseDataSource):
"""ParquetDataSource to read from a ParquetDataset."""

def __post_init__(self):
file_instructions = base.file_instructions(self.dataset_info, self.split)
file_instructions = self.split_info.file_instructions
filenames = [
file_instruction.filename for file_instruction in file_instructions
]
Expand Down
Loading