Bound image-processor resize/crop output to prevent unbounded allocation#46486
Closed
LinZiyuu wants to merge 1 commit into
Closed
Bound image-processor resize/crop output to prevent unbounded allocation#46486LinZiyuu wants to merge 1 commit into
LinZiyuu wants to merge 1 commit into
Conversation
Image processors resize and center-crop every input image to the size read from preprocessor_config.json (size / crop_size), with no upper bound. A tiny config value (e.g. size=15000 or crop_size=50000) makes the processor materialize a huge (height x width x channels) array at preprocess time for any input image, exhausting memory and OOM-killing the worker. Add a shared output-size guard in image_transforms (validate_image_output_size) and call it in resize, center_crop and the Torchvision backend resize before allocating. Add a regression test.
zucchini-nlp
reviewed
Jun 8, 2026
zucchini-nlp
left a comment
Member
There was a problem hiding this comment.
Not sure it makes sense. We expect that users/authors to know what they are doing, and putting an arbitrary upper bound doesn't look reasonable to me
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Image processors resize and center-crop every input image to the
size/crop_sizeread frompreprocessor_config.json, with no upper bound.image_transforms.resizedoesimage.resize((width, height))andcenter_croppads/crops to(crop_height, crop_width), where those targets come straight from config. A model repo whosepreprocessor_config.jsonsets an oversized value (e.g.size=15000orcrop_size=50000) therefore makes anyprocessor(image)call materialize a huge(height × width × channels)array — for any input image, a 32×32 one suffices — exhausting memory and OOM-killing the worker.I verified this against the real library:
CLIPImageProcessor(size={"shortest_edge":15000}, crop_size=15000)(Image.new("RGB",(32,32)))produces a(1,3,15000,15000)= 2.70 GB array (≈12 s), and under a hard 128 MB limitsize=15000is OOM-killed. This is the same allocation class as the mel/chroma filter-bank guard and the positional-embedding guards.Add a shared output-size guard
validate_image_output_sizeinimage_transformsand call it before the allocation inresize,center_crop, and the Torchvision backendresize(the PIL backend resizes throughimage_transforms.resize, so it is covered too). The limit (1 << 27pixels) is far above any real model input. Adds a regression test.AI assistance was used while drafting this change; I reviewed every line and ran the tests below.
What does this PR do?
Bounds the image-processor resize/center-crop output so an oversized
size/crop_sizein an untrusted config cannot OOM the process at preprocess time.Tests run (
torch==2.12.0):Fixes # (security hardening; reported privately rather than via a public issue to avoid disclosing the vector)
Before submitting
Who can review?