Skip to content

Commit 14f227c

Browse files
brokenSeqIO
authored andcommitted
Rename DatasetProviderBase to DatasetProvider
PiperOrigin-RevId: 479216470
1 parent 055abf0 commit 14f227c

3 files changed

Lines changed: 58 additions & 25 deletions

File tree

seqio/dataset_providers.py

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,27 @@ def splits(self) -> Sequence[str]:
8686
@abc.abstractmethod
8787
def get_dataset(
8888
self,
89-
sequence_length: int,
90-
split: str,
89+
sequence_length: Optional[Mapping[str, int]] = None,
90+
split: str = tfds.Split.TRAIN,
9191
use_cached: bool = False,
9292
shuffle: bool = True,
9393
seed: Optional[int] = None,
9494
shard_info: Optional[ShardInfo] = None,
95-
num_epochs: int = 1,
95+
num_epochs: Optional[int] = 1
9696
) -> tf.data.Dataset:
9797
"""Returns the requested tf.data.Dataset."""
9898
raise NotImplementedError
9999

100100
@abc.abstractmethod
101-
def num_input_examples(self, split: str) -> int:
101+
def num_input_examples(self, split: str) -> Optional[int]:
102102
raise NotImplementedError
103103

104104

105+
class DatasetProvider(DatasetProviderBase, metaclass=abc.ABCMeta):
106+
"""Synonym for DatasetProviderBase."""
107+
pass
108+
109+
105110
class DatasetProviderRegistry(object):
106111
"""Base for registry of data providers.
107112
@@ -110,8 +115,8 @@ class DatasetProviderRegistry(object):
110115
"""
111116

112117
# Class variables must be defined in subclasses.
113-
_REGISTRY: MutableMapping[str, DatasetProviderBase]
114-
_PROVIDER_TYPE: Type[DatasetProviderBase]
118+
_REGISTRY: MutableMapping[str, DatasetProvider]
119+
_PROVIDER_TYPE: Type[DatasetProvider]
115120

116121
@classmethod
117122
def add_provider(cls, name: str, provider):
@@ -225,10 +230,10 @@ def get_dataset(
225230
...
226231

227232

228-
class DataSource(DatasetProviderBase):
233+
class DataSource(DatasetProvider):
229234
"""A `DatasetProvider` that provides raw data from an input source.
230235
231-
Inherits all abstract methods and properties of `DatasetProviderBase` except
236+
Inherits all abstract methods and properties of `DatasetProvider` except
232237
those overidden below.
233238
"""
234239

@@ -264,7 +269,7 @@ def supports_arbitrary_sharding(self) -> bool:
264269

265270
@property
266271
def output_features(self) -> Mapping[str, Feature]:
267-
"""Override unused property of `DatasetProviderBase`."""
272+
"""Override unused property of `DatasetProvider`."""
268273
raise NotImplementedError
269274

270275
@abc.abstractmethod
@@ -275,10 +280,14 @@ def list_shards(self, split: str) -> Sequence[str]:
275280
@abc.abstractmethod
276281
def get_dataset(
277282
self, # pytype: disable=signature-mismatch # overriding-default-value-checks
278-
split: str,
283+
split: str = tfds.Split.TRAIN,
279284
shuffle: bool = True,
280285
seed: Optional[int] = None,
281286
shard_info: Optional[ShardInfo] = None,
287+
*, # remaining args are out of order from parent
288+
sequence_length: Optional[Mapping[str, int]] = None, # Unused
289+
use_cached: bool = False, # Unused
290+
num_epochs: Optional[int] = 1 # Unused
282291
) -> tf.data.Dataset:
283292
"""Overrides base class to add shard identifier and remove use_cached.
284293
@@ -287,6 +296,9 @@ def get_dataset(
287296
shuffle: bool, whether to shuffle the input source.
288297
seed: tf.int64 scalar tf.Tensor (or None) for shuffling input source.
289298
shard_info: optional specification for loading a shard of the split.
299+
sequence_length: Unused
300+
use_cached: Unused
301+
num_epochs: Unused
290302
"""
291303
raise NotImplementedError
292304

@@ -376,10 +388,14 @@ def supports_arbitrary_sharding(self) -> bool:
376388

377389
def get_dataset(
378390
self,
379-
split: str,
391+
split: str = tfds.Split.TRAIN,
380392
shuffle: bool = True,
381393
seed: Optional[int] = None,
382394
shard_info: Optional[ShardInfo] = None,
395+
*, # remaining args are out of order from parent
396+
sequence_length: Optional[Mapping[str, int]] = None, # Unused
397+
use_cached: bool = False, # Unused
398+
num_epochs: Optional[int] = 1 # Unused
383399
) -> tf.data.Dataset:
384400
if shard_info and shard_info.num_shards > 1:
385401
raise ValueError(
@@ -470,10 +486,14 @@ def supports_arbitrary_sharding(self) -> bool:
470486

471487
def get_dataset(
472488
self,
473-
split: str,
489+
split: str = tfds.Split.TRAIN,
474490
shuffle: bool = True,
475491
seed: Optional[int] = None,
476492
shard_info: Optional[ShardInfo] = None,
493+
*, # remaining args are out of order from parent
494+
sequence_length: Optional[Mapping[str, int]] = None, # Unused
495+
use_cached: bool = False, # Unused
496+
num_epochs: Optional[int] = 1 # Unused
477497
) -> tf.data.Dataset:
478498
return self.tfds_dataset.load(
479499
split, shuffle_files=shuffle, seed=seed, shard_info=shard_info
@@ -539,10 +559,14 @@ def supports_arbitrary_sharding(self) -> bool:
539559

540560
def get_dataset(
541561
self,
542-
split: str,
562+
split: str = tfds.Split.TRAIN,
543563
shuffle: bool = True,
544564
seed: Optional[int] = None,
545565
shard_info: Optional[ShardInfo] = None,
566+
*, # remaining args are out of order from parent
567+
sequence_length: Optional[Mapping[str, int]] = None, # Unused
568+
use_cached: bool = False, # Unused
569+
num_epochs: Optional[int] = 1 # Unused
546570
) -> tf.data.Dataset:
547571
files = self.list_shards(split)
548572

@@ -862,7 +886,7 @@ def __call__(self, dataset):
862886
MetricFnCallable = metrics_lib.MetricFnCallable
863887

864888

865-
class Task(DatasetProviderBase):
889+
class Task(DatasetProvider):
866890
"""A class to manage a dataset and its related metrics."""
867891

868892
def __init__(
@@ -1260,15 +1284,15 @@ def get_cached_stats(
12601284

12611285
def get_dataset(
12621286
self, # pytype: disable=signature-mismatch # overriding-default-value-checks
1263-
sequence_length: Optional[Mapping[str, int]],
1287+
sequence_length: Optional[Mapping[str, int]] = None,
12641288
split: str = tfds.Split.TRAIN,
12651289
use_cached: bool = False,
12661290
shuffle: bool = True,
1267-
shuffle_buffer_size: Optional[int] = None,
1291+
shuffle_buffer_size: Optional[int] = None, # Unique to Task
12681292
seed: Optional[int] = None,
12691293
shard_info: Optional[ShardInfo] = None,
12701294
num_epochs: Optional[int] = 1,
1271-
trim_output_features: bool = True,
1295+
trim_output_features: bool = True, # Unique to Task
12721296
) -> tf.data.Dataset:
12731297
"""Returns a tf.data.Dataset from cache or generated on the fly.
12741298
@@ -1465,7 +1489,7 @@ def get(cls, name) -> Task:
14651489
MixtureRate = Union[int, float, Callable[[Union[Task, "Mixture"]], float]]
14661490

14671491

1468-
class Mixture(DatasetProviderBase):
1492+
class Mixture(DatasetProvider):
14691493
"""Class for mixing multiple tasks."""
14701494

14711495
def __init__(
@@ -1607,14 +1631,14 @@ def _check_compatible_features(self) -> None:
16071631

16081632
def get_dataset( # pytype: disable=signature-mismatch # overriding-parameter-type-checks
16091633
self,
1610-
sequence_length: Optional[Mapping[str, int]],
1634+
sequence_length: Optional[Mapping[str, int]] = None,
16111635
split: str = tfds.Split.TRAIN,
16121636
use_cached: bool = False,
16131637
shuffle: bool = True,
16141638
seed: Optional[int] = None,
16151639
shard_info: Optional[ShardInfo] = None,
1616-
num_epochs: Optional[int] = None,
1617-
copy_pretokenized: bool = False,
1640+
num_epochs: Optional[int] = None, # Unique default for Mixture
1641+
copy_pretokenized: bool = False, # Unique (and all below) to Mixture
16181642
compute_stats_empirically: bool = False,
16191643
log_mixing_proportions: bool = True,
16201644
passthrough_features: Optional[Sequence[str]] = None,
@@ -2030,7 +2054,7 @@ def get_dataset(
20302054

20312055
mixture_or_task = (
20322056
get_mixture_or_task(mixture_or_task_name)
2033-
if not isinstance(mixture_or_task_name, DatasetProviderBase)
2057+
if not isinstance(mixture_or_task_name, DatasetProvider)
20342058
else mixture_or_task_name
20352059
)
20362060
is_grain_task = False

seqio/experimental.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from seqio import preprocessors as seqio_preprocessors
2323
from seqio import utils
2424
import tensorflow as tf
25-
25+
import tensorflow_datasets as tfds
2626

2727
CacheDatasetPlaceholder = dataset_providers.CacheDatasetPlaceholder
2828
Mixture = dataset_providers.Mixture
@@ -232,10 +232,14 @@ def list_shards(self, split: str) -> Sequence[str]:
232232

233233
def get_dataset(
234234
self,
235-
split: str,
235+
split: str = tfds.Split.TRAIN,
236236
shuffle: bool = True,
237237
seed: Optional[int] = None,
238238
shard_info: Optional[ShardInfo] = None,
239+
*, # remaining args are out of order from parent
240+
sequence_length: Optional[Mapping[str, int]] = None, # Unused
241+
use_cached: bool = False, # Unused
242+
num_epochs: Optional[int] = 1 # Unused
239243
) -> tf.data.Dataset:
240244
shard_info: ShardInfo = shard_info or ShardInfo(0, 1)
241245
if self._train_split not in self._original_source.splits:

seqio/helpers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from seqio import dataset_providers as dp
2424
from seqio import vocabularies as vc
2525
import tensorflow.compat.v2 as tf
26+
import tensorflow_datasets as tfds
2627

2728

2829
def mixture_or_task_with_new_vocab(
@@ -201,10 +202,14 @@ def list_shards(self, split: str) -> Sequence[str]:
201202

202203
def get_dataset(
203204
self,
204-
split: str,
205+
split: str = tfds.Split.TRAIN,
205206
shuffle: bool = True,
206207
seed: Optional[int] = None,
207208
shard_info: Optional[dp.ShardInfo] = None,
209+
*, # remaining args are out of order from parent
210+
sequence_length: Optional[Mapping[str, int]] = None, # Unused
211+
use_cached: bool = False, # Unused
212+
num_epochs: Optional[int] = 1 # Unused
208213
) -> tf.data.Dataset:
209214
"""See base class for documentation."""
210215
if split not in self.split_sizes:

0 commit comments

Comments
 (0)