huggingface/pytorch-image-models · error · TypeError
ScheduledBatchSampler requires a sampler with a length.
Error message
ScheduledBatchSampler requires a sampler with a length.
What it means
ScheduledBatchSampler must iterate a sized sampler to compute batch counts and budgets; the passed sampler lacks __len__ (e.g. an infinite or generator-backed sampler), so a TypeError is raised at construction.
Source
Thrown at timm/data/scheduled_sampler.py:61
choices mixed into the progressive choice probabilities.
"""
def __init__(
self,
sampler: Sampler,
batch_sizes: Sequence[int],
choice_weights: Optional[Sequence[float]] = None,
seed: int = 0,
drop_last: bool = True,
shuffle_schedule: bool = True,
num_batches: Optional[int] = None,
choice_schedule: str = 'constant',
schedule_epochs: Optional[int] = None,
schedule_spread: float = 0.65,
schedule_random_mix: float = 0.1,
) -> None:
if not hasattr(sampler, '__len__'):
raise TypeError('ScheduledBatchSampler requires a sampler with a length.')
if len(sampler) <= 0:
raise ValueError('ScheduledBatchSampler requires a non-empty sampler.')
if not batch_sizes:
raise ValueError('batch_sizes must contain at least one value.')
if any(int(batch_size) != batch_size or batch_size <= 0 for batch_size in batch_sizes):
raise ValueError('All scheduled batch sizes must be positive integers.')
if num_batches is not None and (int(num_batches) != num_batches or num_batches <= 0):
raise ValueError('num_batches must be a positive integer when specified.')
if choice_schedule not in ('constant', 'progressive'):
raise ValueError("choice_schedule must be 'constant' or 'progressive'.")
if choice_schedule == 'progressive':
if len(batch_sizes) < 2:
raise ValueError('A progressive schedule requires at least two choices.')
if schedule_epochs is None or int(schedule_epochs) != schedule_epochs or schedule_epochs <= 0:
raise ValueError('schedule_epochs must be a positive integer for a progressive schedule.')
if schedule_spread < 0:
raise ValueError('schedule_spread must be non-negative.')
if not 0 <= schedule_random_mix <= 1:View on GitHub (pinned to 9a5261e31b)
Solutions
- Implement __len__ on the custom sampler (usually returning the underlying dataset length).
- Use a sized sampler such as RandomSampler(dataset) or the dataset itself.
- For infinite streams, pre-materialize an index list and wrap it in a sized Sampler.
Example fix
# before
class MySampler(Sampler):
def __iter__(self):
while True:
yield random.randrange(n)
# after
class MySampler(Sampler):
def __init__(self, n): self.n = n
def __iter__(self):
for _ in range(self.n):
yield random.randrange(self.n)
def __len__(self):
return self.n Defensive patterns
Strategy: type-guard
Validate before calling
assert hasattr(sampler, '__len__') and callable(getattr(sampler, '__len__')), 'sampler must be sized'
Type guard
def is_sized_sampler(s) -> bool:
return hasattr(s, '__iter__') and hasattr(s, '__len__') and callable(s.__len__) Try / catch
try:
sched = ScheduledBatchSampler(sampler, batch_sizes=[128])
except TypeError as e:
if 'sampler with a length' in str(e):
sampler = RandomSampler(dataset) # sized fallback
sched = ScheduledBatchSampler(sampler, batch_sizes=[128])
else:
raise Prevention
- Implement __len__ on custom samplers.
- Use framework-provided sized samplers where possible.
- Unit-test custom samplers for len() support.
When it happens
Trigger: Passing sampler=iter(dataset), a custom Sampler without __len__, or a DistributedSamplerWrapper built over an unsized iterable to ScheduledBatchSampler.__init__.
Common situations: Wrapping streaming/infinite samplers; custom sampler subclasses that forgot to implement __len__; adapting example code that used itertools.cycle.
Related errors
- ScheduledBatchSampler requires a non-empty sampler.
- batch_sizes must contain at least one value.
- All scheduled batch sizes must be positive integers.
- num_batches must be a positive integer when specified.
- choice_schedule must be 'constant' or 'progressive'.
AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27).
Data as JSON: /api/errors/84c35b7fdd10dc3f.
Report an issue: GitHub.