huggingface/pytorch-image-models · warning

Final scale randomization ({scale_factor:.2f}) resulted in s

Error message

Final scale randomization ({scale_factor:.2f}) resulted in size {final_size} exceeding max_seq_len={max_seq_len} after rounding. Reverting to feasible size {feasible_size}.

What it means

In RandomResizedCropToSequence.get_params, after applying a random final-scale factor the recomputed patch grid can (due to rounding) exceed max_seq_len tokens. The transform warns and reverts to the previously computed feasible size, guaranteeing the sequence-length budget is respected.

Source

Thrown at timm/data/naflex_transforms.py:693

                target_h = patch_h * math.ceil(raw_h / patch_h)
                target_w = patch_w * math.ceil(raw_w / patch_w)
            else:
                target_h = int(round(raw_h))
                target_w = int(round(raw_w))

            # Ensure final size is at least one patch dimension
            target_h = max(target_h, patch_h)
            target_w = max(target_w, patch_w)
            final_size = (target_h, target_w)

             # Final check: Ensure this randomized size still fits max_seq_len
             # (It should, as we scaled down, but rounding might theoretically push it over)
            num_patches_h = final_size[0] // patch_h
            num_patches_w = final_size[1] // patch_w
            if (num_patches_h * num_patches_w) > max_seq_len:
                 # If it exceeds, revert to the original feasible_size (safest)
                 final_size = feasible_size
                 warnings.warn(f"Final scale randomization ({scale_factor:.2f}) resulted in size {final_size} exceeding max_seq_len={max_seq_len} after rounding. Reverting to feasible size {feasible_size}.")

        # Select interpolation mode
        if isinstance(interpolation, (tuple, list)):
            interpolation = random.choice(interpolation)
        else:
            interpolation = interpolation

        return (top, left, crop_h, crop_w), final_size, interpolation

    def forward(self, img: torch.Tensor) -> torch.Tensor:
        # Sample crop, resize, and interpolation parameters
        crop_params, final_size, interpolation = self.get_params(
            img,
            scale=self.scale,
            ratio=self.ratio,
            crop_attempts=self.attempts,
            patch_h=self.patch_h,
            patch_w=self.patch_w,

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. No fix required — behavior is a safe revert; ignore or silence the warning
  2. To reduce frequency, lower the top of final_scale_range (e.g. (0.7, 0.95)) or slightly raise max_seq_len
  3. Align patch size and max_seq_len so rounding margins exist

Example fix

# before
tfm = RandomResizedCropToSequence(..., final_scale_range=(0.9, 1.0), max_seq_len=256)
# after
tfm = RandomResizedCropToSequence(..., final_scale_range=(0.8, 0.95), max_seq_len=256)
Defensive patterns

Strategy: fallback

Prevention

When it happens

Trigger: final_scale_range close to 1.0 combined with max_seq_len exactly at the boundary; patch sizes where rounding up h*w//patch pushes num_patches one over max_seq_len.

Common situations: Aggressive NaFlex configs that pack sequences to the max token budget. The fallback is intentional and safe; the warning just notes the randomized scale was discarded for that sample.

Related errors


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