huggingface/transformers · error · ValueError

Padding dimensions are negative. Please make sure that the `

Error message

Padding dimensions are negative. Please make sure that the `pad_size` is larger than the image size. Got pad_size={pad_size}, image_size={image_size}.

What it means

Error "Padding dimensions are negative. Please make sure that the `pad_size` is larger than the image size. Got pad_size={pad_size}, image_size={image_size}." thrown in huggingface/transformers.

Source

Thrown at src/transformers/image_processing_backends.py:184

        """Pad images using Torchvision with batched operations."""
        if pad_size is not None:
            if not (pad_size.height and pad_size.width):
                raise ValueError(f"Pad size must contain 'height' and 'width' keys only. Got pad_size={pad_size}.")
            pad_size = (pad_size.height, pad_size.width)
        else:
            pad_size = get_max_height_width(images)

        grouped_images, grouped_images_index = group_images_by_shape(
            images, disable_grouping=disable_grouping, is_nested=is_nested
        )
        processed_images_grouped = {}
        processed_masks_grouped = {}
        for shape, stacked_images in grouped_images.items():
            image_size = stacked_images.shape[-2:]
            padding_height = pad_size[0] - image_size[0]
            padding_width = pad_size[1] - image_size[1]
            if padding_height < 0 or padding_width < 0:
                raise ValueError(
                    f"Padding dimensions are negative. Please make sure that the `pad_size` is larger than the "
                    f"image size. Got pad_size={pad_size}, image_size={image_size}."
                )
            if image_size != pad_size:
                padding = (0, 0, padding_width, padding_height)
                stacked_images = tvF.pad(stacked_images, padding, fill=fill_value, padding_mode=padding_mode)
            processed_images_grouped[shape] = stacked_images

            if return_mask:
                stacked_masks = torch.zeros_like(stacked_images, dtype=torch.int64)[..., 0, :, :]
                stacked_masks[..., : image_size[0], : image_size[1]] = 1
                processed_masks_grouped[shape] = stacked_masks

        processed_images = reorder_images(processed_images_grouped, grouped_images_index, is_nested=is_nested)
        if return_mask:
            processed_masks = reorder_images(processed_masks_grouped, grouped_images_index, is_nested=is_nested)
            return processed_images, processed_masks

View on GitHub (pinned to a597f97485)

Solutions

  1. Ensure `pad_size` height/width are at least the image height/width.
  2. Compute pad_size as max(pad, image_size) before calling pad.

When it happens

Trigger: Raised in image padding when target pad dimensions are smaller than the image dimensions, yielding negative padding.

Common situations: Requesting a pad_size smaller than the actual image size in an image preprocessing pipeline.


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/286681c1691d9b70. Report an issue: GitHub.