2020import numpy
2121import functools
2222import typing
23- from typing import TYPE_CHECKING , Optional , Any , Tuple , Union , Type , Dict , Sequence , List , Callable
23+ from typing import TYPE_CHECKING , Optional , Any , Set , Tuple , Union , Type , Dict , Sequence , List , Callable
2424
2525from returnn .log import log
2626from returnn .engine .batch import Batch , BatchSetGenerator
@@ -147,12 +147,10 @@ def __init__(
147147 :param shard_index: local shard index, when sharding is enabled
148148 """
149149 self .name = name or ("dataset_id%s" % id (self ))
150- self .lock = None # type : Optional[RLock] # Used when manipulating our data potentially from multiple threads.
151- self .rnd_seq_drop = None # type: typing. Optional[Random]
150+ self .lock : Optional [RLock ] = None # Used when manipulating our data potentially from multiple threads.
151+ self .rnd_seq_drop : Optional [Random ] = None
152152 self .num_inputs = 0 # usually not used, but num_outputs instead, which is more generic
153- self .num_outputs = (
154- None
155- ) # type: typing.Optional[typing.Dict[str,typing.Tuple[int,int]]] # tuple is num-classes, len(shape). # nopep8
153+ self .num_outputs : Optional [Dict [str , Tuple [int , int ]]] = None # tuple is num-classes, len(shape).
156154 self .window = window
157155 self .seq_ordering = seq_ordering # "default", "sorted" or "random". See self.get_seq_order_for_epoch().
158156 self .fixed_random_seed = fixed_random_seed
@@ -165,10 +163,10 @@ def __init__(
165163 self ._seq_order_seq_lens_file = seq_order_seq_lens_file
166164 self ._seq_order_seq_lens_by_idx = None
167165 # There is probably no use case for combining the two, so avoid potential misconfiguration.
168- assert (
169- self . partition_epoch == 1 or self . repeat_epoch == 1
170- ), "Combining partition_epoch and repeat_epoch is prohibited."
171- self .labels = {} # type: typing. Dict[str,typing. List[str]]
166+ assert self . partition_epoch == 1 or self . repeat_epoch == 1 , (
167+ "Combining partition_epoch and repeat_epoch is prohibited."
168+ )
169+ self .labels : Dict [str , List [str ]] = {}
172170 self .weights = {}
173171 self ._num_timesteps = 0
174172 self ._num_seqs = 0
@@ -219,8 +217,8 @@ def __repr__(self):
219217 getattr (self , "epoch" , "<unknown>" ),
220218 )
221219
222- _getnewargs_exclude_attrs = set () # type: typing. Set[str]
223- _getnewargs_remap = {} # type: typing. Dict[str,str]
220+ _getnewargs_exclude_attrs : Set [str ] = set ()
221+ _getnewargs_remap : Dict [str , str ] = {}
224222
225223 @staticmethod
226224 def _create_from_reduce (cls , kwargs , state ) -> Dataset :
@@ -697,12 +695,13 @@ def get_seq_order_for_epoch(
697695 )
698696 old_seq_index = seq_index
699697 seq_index = [i for i in seq_index if all_seq_tags [i ] in self .seq_tags_filter ]
700- assert (
701- seq_index
702- ), "%s: empty after applying seq_list_filter_file. Example filter tags: %r, used tags: %r" % (
703- self ,
704- sorted (self .seq_tags_filter )[:3 ],
705- [all_seq_tags [i ] for i in old_seq_index [:3 ]],
698+ assert seq_index , (
699+ "%s: empty after applying seq_list_filter_file. Example filter tags: %r, used tags: %r"
700+ % (
701+ self ,
702+ sorted (self .seq_tags_filter )[:3 ],
703+ [all_seq_tags [i ] for i in old_seq_index [:3 ]],
704+ )
706705 )
707706 return seq_index
708707
@@ -773,9 +772,9 @@ def init_seq_order(self, epoch=None, seq_list=None, seq_order=None):
773772 """
774773 self .epoch = epoch
775774 self .rnd_seq_drop = Random (self ._get_random_seed_for_epoch (epoch = epoch ))
776- assert (
777- self . num_shards == 1 or self .supports_sharding ()
778- ), f" { self } : does not support sharding, but got num_shards == { self . num_shards } "
775+ assert self . _num_shards == 1 or self . supports_sharding (), (
776+ f" { self } : does not support sharding, but got num_shards == { self ._num_shards } "
777+ )
779778 return False
780779
781780 def finish_epoch (self , * , free_resources : bool = False ):
@@ -1007,16 +1006,16 @@ def get_complete_frac(self, sorted_seq_idx: int, *, allow_only_lr_suitable: bool
10071006 except Exception : # also not always available
10081007 num_seqs = None # ignore
10091008
1010- if math .isinf (num_seqs ):
1009+ if num_seqs is not None and math .isinf (num_seqs ):
10111010 if allow_only_lr_suitable :
10121011 # cannot compute meaningful complete_frac for infinite num_seqs
10131012 return None
10141013 else :
10151014 num_seqs = None
10161015
1017- assert (
1018- num_seqs is None or 0 <= sorted_seq_idx < num_seqs
1019- ), f" { self } : invalid seq indices: 0 <= seq_idx ( { sorted_seq_idx } ) < num_seqs ( { num_seqs } ) violated"
1016+ assert num_seqs is None or 0 <= sorted_seq_idx < num_seqs , (
1017+ f" { self } : invalid seq indices: 0 <= seq_idx ( { sorted_seq_idx } ) < num_seqs ( { num_seqs } ) violated"
1018+ )
10201019 return self .generic_complete_frac (sorted_seq_idx , num_seqs )
10211020
10221021 @property
0 commit comments