huggingface/pytorch-image-models · warning

Calculated batch size <= 0 (seq_len={seq_len}, remaining={re

Error message

Calculated batch size <= 0 (seq_len={seq_len}, remaining={remaining_samples}). Stopping schedule generation early.

What it means

NaFlexDataset._create_canonical_schedule clamps computed batch sizes with max(1, ...) and min(batch_size, remaining), so batch_size <= 0 is theoretically unreachable; the warning is a defensive guard that fires only if the clamping logic is broken or inputs (min_batch_size/batch_divisor) are pathological, and it breaks schedule generation to avoid an infinite loop.

Source

Thrown at timm/data/naflex_dataset.py:370

            # Sample sequence length deterministically based on base seed
            seq_idx = torch.randint(0, len(self.seq_lens), (1,), generator=g).item()
            seq_len = self.seq_lens[seq_idx]

            # Calculate batch size
            batch_size = calculate_naflex_batch_size(
                tokens_per_batch=self.max_tokens_per_batch,
                seq_len=seq_len,
                # max_size should be remaining_samples to avoid overshooting
                max_size=remaining_samples,
                divisor=self.batch_divisor,
                rounding='floor',
            )
            # Ensure batch size is positive and doesn't exceed remaining samples
            batch_size = max(1, batch_size)
            batch_size = min(batch_size, remaining_samples)

            if batch_size <= 0:
                 warnings.warn(f"Calculated batch size <= 0 (seq_len={seq_len}, remaining={remaining_samples}). Stopping schedule generation early.")
                 break # Avoid infinite loop if something goes wrong

            current_schedule.append((seq_len, batch_size))
            remaining_samples -= batch_size
            total_scheduled_samples += batch_size

        # Sanity check: Ensure the schedule covers all samples for the rank
        if total_scheduled_samples != num_samples_per_rank:
            warnings.warn(
                f"Rank {self.rank}: Canonical schedule accounts for {total_scheduled_samples} samples, "
                f"but expected {num_samples_per_rank} samples per rank. "
                f"This might happen if min_batch_size or batch_divisor constraints prevent utilizing all samples. "
                f"Check parameters. Remaining samples: {remaining_samples}"
            )
            # Adjust if needed? Could add a final small batch, but might violate constraints.
            # Current behavior: some samples might be dropped if schedule logic fails.

        self._canonical_batch_schedule = current_schedule

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Sanity-check constructor args: min_batch_size >= 1 and batch_divisor >= 1
  2. Ensure seq_len targets are positive integers (no NaN/inf)
  3. Update timm — if the clamping logic itself is buggy, a newer release may fix it

Example fix

# before
ds = NaFlexDataset(..., min_batch_size=0, batch_divisor=0)
# after
ds = NaFlexDataset(..., min_batch_size=1, batch_divisor=8)
Defensive patterns

Strategy: validation

Validate before calling

assert min_batch_size >= 1 and batch_divisor >= 1, 'schedule constraints must be positive'

Prevention

When it happens

Trigger: Passing inconsistent constraints such as min_batch_size=0 or batch_divisor=0 (division anomalies), or NaN sequence-length targets that make computed batch_size NaN before clamping.

Common situations: Misconfigured batch schedule parameters; upgrading timm where schedule math changed. In practice with valid params this warning never fires.

Related errors


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