huggingface/pytorch-image-models · error · ValueError

All scheduled batch sizes must be positive integers.

Error message

All scheduled batch sizes must be positive integers.

What it means

Every entry in batch_sizes must be a positive integer (floats with fractional parts, 0, or negatives are rejected) because the sampler yields integer batch counts and budgets computed from these values.

Source

Thrown at timm/data/scheduled_sampler.py:67

            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:
                raise ValueError('schedule_random_mix must be between 0 and 1.')

        self.sampler = sampler
        self.batch_sizes = tuple(int(batch_size) for batch_size in batch_sizes)
        self.choice_weights = self._normalize_choice_weights(choice_weights)
        self._active_choices = tuple(

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Round/normalize computed sizes: [max(1, int(round(b))) for b in sizes].
  2. Fix the literal values in the config to plain positive integers.
  3. Add an assert/validated schema in your config loader.

Example fix

# before
sizes = [total_samples / num_buckets]  # may be 170.67
sched = ScheduledBatchSampler(sampler, batch_sizes=sizes)

# after
sizes = [max(1, round(total_samples / num_buckets))]
sched = ScheduledBatchSampler(sampler, batch_sizes=sizes)
Defensive patterns

Strategy: validation

Validate before calling

batch_sizes = [int(b) for b in batch_sizes if int(b) == b and b > 0]
assert len(batch_sizes) == len(raw_sizes)

Type guard

def valid_batch_sizes(sizes) -> bool:
    return bool(sizes) and all(isinstance(b,(int,float)) and b > 0 and int(b) == b for b in sizes)

Prevention

When it happens

Trigger: batch_sizes=[256.5], [0], [-32], or numpy float entries like [np.float64(128)] that don't compare equal to their int() cast… specifically any value where int(b)!=b or b<=0.

Common situations: Config files parsed as floats (256.0 is fine, but 25e1 typos like 256.5); computed batch sizes from divisions (total/num_workers) producing fractions; passing booleans/None inside the list.

Related errors


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