fix: prevent clip_preprocess center crop from exceeding the resized image - #1995
Merged
leejet merged 2 commits intoSep 19, 2026
Merged
Conversation
…mage clip_preprocess truncated the aspect-preserving resize dimensions to int64_t, so a float result a hair below an integer (e.g. 736.0f/730.0f * 730.0f = 735.999...) left the resized side one pixel shorter than the crop target. The center crop then indexed past the end of the resized tensor and threw "Tensor index out of range" (SIGABRT). Round the resized dimensions up with std::ceil and clamp them to at least the crop target so the crop window is always covered.
leejet
requested changes
Sep 18, 2026
leejet
left a comment
Owner
There was a problem hiding this comment.
I think the crash fix is valid, but using ceil here changes the resize result for inputs that were previously handled correctly.
For example, in the reported 645x730 -> 640x736 case, only the resized height needs to be clamped from 735 to 736. Changing to ceil also changes the width from 650 to 651, which unnecessarily alters the CLIP preprocessing result.
Could we keep the existing truncation behavior and only enforce the crop invariant instead?
int64_t resized_width =
static_cast<int64_t>(scale * static_cast<float>(image.shape()[0]));
int64_t resized_height =
static_cast<int64_t>(scale * static_cast<float>(image.shape()[1]));
resized_width = std::max<int64_t>(resized_width, target_width);
resized_height = std::max<int64_t>(resized_height, target_height);This should fix the out-of-bounds issue while minimizing behavior changes for existing inputs.
Rounding the resized dimensions up changed the CLIP preprocessing result for inputs that were already fine. Keep the original truncation and rely solely on clamping to the crop target to keep the center crop in bounds.
Contributor
Author
|
thanks for catching the width regression. Updated to the clamp-only version |
leejet
approved these changes
Sep 19, 2026
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.
Summary
clip_preprocesscomputed the aspect-preserving resize size by truncatingscale * dimtoint64_t. For some aspect ratios the single-precision result lands a hair below an integer (e.g.736.0f / 730.0f * 730.0f == 735.999…), so the resized side is one pixel smaller than the crop target. The center crop then reads one element past the end of the resized tensor and throws an uncaught exception:Reproduction (Qwen3VL / Krea2 reference images): a second reference of 645×730 is snapped to
w_bar=640, h_bar=736;scale = max(640/645, 736/730) = 1.008219, soresized_height = (int64_t)(scale * 730) = (int64_t)735.999… = 735, while the crop loops up totarget_height = 736. Any caller ofclip_preprocesscan hit this (Qwen3VL/Krea2 vision encoder, CLIP vision, PhotoMaker) — it only depends on the image aspect ratio.The fix rounds the resized dimensions up and clamps them to at least the crop target, so the crop window is always inside the resized image:
Related Issue / Discussion
Additional Information
Verification (exact failing setup):
Krea-2-Turbo-Q8_0.gguf+ Qwen3VL text encoder,--backend vulkan1,te=cpu-W 960 -H 720 --steps 10 --cfg-scale 1 --img-cfg-scale 1 --flow-shift 3-r ref0.png(960×720, i.e. 720×960 H×W)-r ref1.png(645×730) with--ref-image-args preset=krea2_ostris_editPicture N:references.Before the fix:
After the fix (same command, same refs):
clang-formatclean (.clang-format, Chromium base).Checklist