huggingface/pytorch-image-models · error · ValueError

No full scheduled batch fits the sampler; reduce the batch s

Error message

No full scheduled batch fits the sampler; reduce the batch sizes.

What it means

When num_batches is not given, ScheduledBatchSampler builds a per-batch sample budget schedule from batch_sizes; if even one full smallest scheduled batch doesn't fit within len(sampler), the schedule is empty and this ValueError is raised — effectively the sampler is smaller than its own batch size.

Source

Thrown at timm/data/scheduled_sampler.py:106

            if choice_weight > 0
        )
        self.seed = seed
        self.drop_last = drop_last
        self.shuffle_schedule = shuffle_schedule
        self.choice_schedule = choice_schedule
        self.schedule_epochs = int(schedule_epochs) if schedule_epochs is not None else None
        self.schedule_spread = schedule_spread
        self.schedule_random_mix = schedule_random_mix
        self.epoch = 0
        self.average_batch_size = self._calculate_average_batch_size()
        if choice_schedule == 'progressive' and num_batches is None:
            num_batches = self._infer_num_batches()
        self.num_batches = int(num_batches) if num_batches is not None else None
        self._sample_budget_schedule: Tuple[Tuple[int, int], ...] = ()
        if self.num_batches is None:
            self._sample_budget_schedule = self._create_sample_budget_schedule()
            if not self._sample_budget_schedule:
                raise ValueError(
                    'No full scheduled batch fits the sampler; reduce the batch sizes.'
                )

    def _normalize_choice_weights(
            self,
            choice_weights: Optional[Sequence[float]],
    ) -> torch.Tensor:
        if choice_weights is None:
            return torch.full((len(self.batch_sizes),), 1.0 / len(self.batch_sizes), dtype=torch.float64)
        if len(choice_weights) != len(self.batch_sizes):
            raise ValueError('choice_weights and batch_sizes must have the same length.')

        weights = torch.tensor(choice_weights, dtype=torch.float64)
        if not torch.isfinite(weights).all() or (weights < 0).any():
            raise ValueError('choice_weights must contain finite, non-negative values.')
        weight_sum = weights.sum()
        if weight_sum <= 0:
            raise ValueError('choice_weights must have a positive sum.')

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Reduce batch_sizes so the smallest is <= len(sampler) (and the largest if you need full progressive coverage).
  2. Or pass num_batches explicitly if you accept truncation semantics of your own loop.
  3. Scale batch size per rank: batch_size = base // world_size.

Example fix

# before
sched = ScheduledBatchSampler(sampler, batch_sizes=[256, 512])  # len(sampler)=100

# after
sched = ScheduledBatchSampler(sampler, batch_sizes=[32, 64])  # fits 100 samples
Defensive patterns

Strategy: validation

Validate before calling

assert min(batch_sizes) <= len(sampler) <= max(batch_sizes) * 1000 and min(batch_sizes) <= len(sampler), \
    'smallest batch exceeds sampler length'

Prevention

When it happens

Trigger: len(sampler) < min(batch_sizes), e.g. a 100-sample dataset with batch_sizes=[256], or an aggressively large progressive final batch size.

Common situations: Small debug datasets with production batch sizes; distributed sharding leaving each rank fewer samples than the batch size; last-stage batch sizes in a progressive schedule exceeding the dataset.

Related errors


AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27). Data as JSON: /api/errors/b0ee89223ccc911d. Report an issue: GitHub.