Comfy-Org/ComfyUI · error · ValueError

Resizing [{height}x{width}] to [{target_height}x{target_widt

Error message

Resizing [{height}x{width}] to [{target_height}x{target_width}] exceeds the patch budget.

What it means

The companion check in _get_aspect_ratio_preserving_size: after fixing zero sides to side_mult and clamping to max_side_length, the final target area must not exceed target_px = max_patches * patch_size^2. Very elongated images (e.g. panoramas) can still produce target_height * target_width above the budget after the clamping arithmetic, and this ValueError reports source and target sizes.

Source

Thrown at comfy/text_encoders/gemma4.py:1401

    target_px = max_patches * patch_size ** 2
    factor = math.sqrt(target_px / (height * width))
    side_mult = pooling_kernel_size * patch_size
    target_height = math.floor(factor * height / side_mult) * side_mult
    target_width = math.floor(factor * width / side_mult) * side_mult

    if target_height == 0 and target_width == 0:
        raise ValueError(f"Attempting to resize to a 0 x 0 image. Resized height should be divisible by {side_mult}.")

    max_side_length = (max_patches // pooling_kernel_size ** 2) * side_mult
    if target_height == 0:
        target_height = side_mult
        target_width = min(math.floor(width / height) * side_mult, max_side_length)
    elif target_width == 0:
        target_width = side_mult
        target_height = min(math.floor(height / width) * side_mult, max_side_length)

    if target_height * target_width > target_px:
        raise ValueError(f"Resizing [{height}x{width}] to [{target_height}x{target_width}] exceeds the patch budget.")

    return target_height, target_width


class Gemma4_Tokenizer():
    tokenizer_json_data = None
    prime_empty_thought = False

    def state_dict(self):
        if self.tokenizer_json_data is not None:
            return {"tokenizer_json": self.tokenizer_json_data}
        return {}

    def _audio_token_count(self, num_samples):
        # Default (E2B/E4B): mel frames after two stride-2 conv subsamples.
        _fl = 320  # int(round(16000 * 20.0 / 1000.0))
        _hl = 160  # int(round(16000 * 10.0 / 1000.0))
        _nmel = (num_samples + _fl // 2 - (_fl + 1)) // _hl + 1

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Crop or letterbox the image to a moderate aspect ratio (near square) before encoding.
  2. Increase max_patches so the budget covers the computed target resolution.
  3. Downscale the long side so that (long/short) ratio times side_mult stays within the budget.

Example fix

# before
emb = tokenizer.encode_image(panorama)  # 10000x500 -> exceeds patch budget

# after
h, w = panorama.height, panorama.width
if w / h > 4:
    w = h * 4  # crop to 4:1
    panorama = panorama.crop((0, 0, w, h))
emb = tokenizer.encode_image(panorama)
Defensive patterns

Strategy: validation

Validate before calling

target_px = max_patches * patch_size ** 2
assert height * width <= target_px * 4, f'image {height}x{width} too large/elongated for patch budget {target_px}'

Type guard

def fits_patch_budget(h: int, w: int, max_patches: int, patch: int) -> bool:
    target_px = max_patches * patch * patch
    max_side = (max_patches // (2 * 2)) * 2 * patch  # pooling=2 example
    return max(h, w) * min(h, w) <= target_px and max(h, w) <= max_side

Try / catch

try:
    h, w = _get_aspect_ratio_preserving_size(height, width, patch_size, max_patches, pooling)
except ValueError as e:
    if 'patch budget' in str(e):
        h, w = _get_aspect_ratio_preserving_size(height, height, patch_size, max_patches, pooling)  # crop to square
    else:
        raise

Prevention

When it happens

Trigger: Feeding extreme aspect-ratio images (wide panoramas, thin strips) to the Gemma4 vision encoder, where the long side capped at max_side_length times the forced short side still exceeds the patch budget; configs with small max_patches relative to the image.

Common situations: Panoramic or strip images in multimodal prompts; screenshot-style tall images; users lowering max_patches; defaults tuned for roughly square photos hitting unusual inputs.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/4f423953efa665c6. Report an issue: GitHub.