Repository navigation
Description
Summary
When an image's grid-rounded size falls outside [min_pixels, max_pixels], the reference smart_resize rescales the original height and width once (beta = sqrt(h * w / max_pixels), then a single floor or ceil). Qwen2VLProcessor, Ernie45VlProcessor and DotsOcrProcessor instead rescale the already grid-rounded size. Rounding to the grid first moves each edge by up to factor / 2, so the rescale often lands on a different multiple of factor: one row or column of merged patches more or fewer than the reference, and a different image-token count. This is distinct from the tie-rounding defect in #3463; the two do not overlap, and the reporter confirms the fixes apply in either order.
Background
Confirmed statically on main at db3a23141 (this environment cannot build or run; this is a source read). src/vision/processors/qwen2_vl.rs:299-317 contains the shared Qwen2VLProcessor::smart_resize clamp:
- over
max_pixels:scale = sqrt(max_pixels / (h * w))of the already roundedh/w, rounded half away from zero, with a secondfloorpass if still over; - then, as a separate
ifrather than anelse, themin_pixelsbranch rescales the rounded size andceils.
Ernie45VlProcessor::smart_resize (src/vision/processors/ernie4_5_vl.rs:64-73) and DotsOcrProcessor::smart_resize (src/vision/processors/dots_ocr.rs:62-73) have the same shape: they scale the rounded h_bar/w_bar rather than the original size, and in ERNIE the min_pixels check is again not an else.
Qwen2VLProcessor is shared, so its clamp sizes images and Qwen-VL video frames for Qwen2-VL, Qwen2.5-VL, Qwen3-VL and its MoE/Omni/4-Exp variants, GLM-4V and GLM-OCR, Mage-VL, MiMo-VL, Cohere Compass, ColQwen2.5, and the XLA Qwen host preprocessor. Only images whose rounded size is out of bounds take these branches; in-bounds images are unaffected.
The reporter checked each case against transformers 5.24.1 and mlx-vlm e8035ae3 (reference functions executed, not re-typed) and against real checkpoints on GB10 (CUDA). Over 45 common sizes under six bound sets (270 pairs), main differs from the reference on 55 of the 137 out-of-bounds (size, family) pairs and agrees on every in-bounds one. The ported branch matches the reference on all 137. The full survey, per-size token tables, perplexity and teacher-forced-trace evidence are preserved under Original Suggestion below.
Proposed Solution
In the three processors, port the reference branch: rescale the original size in the reference's operation order (beta = sqrt((height * width) / max_pixels), then floor(height / beta / factor) * factor over max_pixels and ceil(height * beta / factor) * factor under min_pixels), with elif between the two branches. In Qwen2VLProcessor, beyond a 200:1 aspect ratio (where the reference raises ValueError and mlxcel has no error path), the short edge can clamp to one factor and leave the area over max_pixels; trim the long edge there so max_image_tokens (used by the XLA context floor) still bounds every image. Below 200:1, return the reference's result.
Implementation Notes
| File | Function | Lines on db3a23141 |
|---|---|---|
src/vision/processors/qwen2_vl.rs |
Qwen2VLProcessor::smart_resize |
299-317 |
src/vision/processors/ernie4_5_vl.rs |
Ernie45VlProcessor::smart_resize |
64-73 |
src/vision/processors/dots_ocr.rs |
DotsOcrProcessor::smart_resize |
62-73 |
Deliberately left alone (reference differs in ways beyond this rule): GLM-4V and GLM-OCR (their reference multiplies area by num_frames = temporal_patch_size and upsizes tiny edges first, which mlxcel matches neither before nor after, so they need their own change); Hunyuan-VL, MiniMax-M3, LFM2-VL, PaddleOCR-VL, Falcon-OCR, Jina VLM, Mage-Flow and CLEF media (already scale the original size); Youtu-VL, Muse Glimmer, Kimi, Moondream 3, Phi-4 SigLIP, LocateAnything (different algorithms). The change does not touch the grid-rounding lines that #3463 changes.
Acceptance Criteria
- For each of the three processors, a unit test pins out-of-bounds cases whose expected values come from the reference function (fails on
main), plus in-bounds and agreeing out-of-bounds cases that must not move (pass onmainand after). Qwen2VLProcessorgives 728x1316 for 1372x772 undermax_pixels = 1003520, 1472x2656 (3818 tokens) for 3840x2160 under Mage-VL's bounds, and 192x352 for 320x180 under Qwen3-VL's bounds.- On a real checkpoint, an image that hits the condition gets the same image-token count as mlx-vlm, and an mlx-vlm teacher-forced trace that refuses
main's prompt runs.
Original Suggestion
Title: fix(vision): Qwen-VL, ERNIE 4.5 VL and dots.ocr rescale out-of-bounds images from the rounded size, where the reference scales the original size
Problem
When an image's rounded size is outside [min_pixels, max_pixels], transformers' Qwen2-VL smart_resize rescales the original height and width once:
h_bar = round(height / factor) * factor
w_bar = round(width / factor) * factor
if h_bar * w_bar > max_pixels:
beta = math.sqrt((height * width) / max_pixels)
h_bar = max(factor, math.floor(height / beta / factor) * factor)
w_bar = max(factor, math.floor(width / beta / factor) * factor)
elif h_bar * w_bar < min_pixels:
beta = math.sqrt(min_pixels / (height * width))
h_bar = math.ceil(height * beta / factor) * factor
w_bar = math.ceil(width * beta / factor) * factorQwen2VLProcessor::smart_resize, Ernie45VlProcessor::smart_resize and DotsOcrProcessor::smart_resize instead rescale the already rounded h_bar x w_bar. Qwen2VLProcessor also rounds that first rescale half away from zero, then floors a second time if the result is still over the cap, and runs the min_pixels branch after the max_pixels one instead of as an else. Rounding to the grid first moves each edge by up to factor / 2, so the rescale often lands on a different multiple: one row or column of merged patches more or fewer than the reference, and a different image-token count.
This is a separate defect from the tie rounding in #3463 / #3464, and half-even rounding does not change any out-of-bounds number below: with both changes applied, the function returns the reference's result for every size in the survey.
Reproduction
main at db3a23141. Tests that assert the value the reference function itself returns for the same input (the reference functions were lifted verbatim from transformers 5.24.1 and mlx-vlm e8035ae3 and executed; see "Reference behaviour"). On main:
vision::processors::qwen2_vl::tests::smart_resize_rescales_out_of_bounds_images_from_the_original_size 5120x2880 at 3136..12845056 left: (2660, 4760) right: (2688, 4760)
vision::processors::ernie4_5_vl::tests::smart_resize_rescales_out_of_bounds_images_from_the_original_size 1680x1050 at the defaults left: (784, 1232) right: (784, 1260)
vision::processors::dots_ocr::tests::smart_resize_rescales_out_of_bounds_images_from_the_original_size 6000x4000 at the defaults left: (2744, 4088) right: (2716, 4088)
More, from the same reference functions: Qwen2.5-VL under max_pixels = 1003520 (the 1280-token cap) gives 756x1316 for 1372x772 where the reference gives 728x1316, and 868x1148 for a 12 MP 4032x3024 photo where the reference gives 840x1148. Qwen3-VL gives 224x352 for a 320x180 thumbnail where the reference gives 192x352.
Real checkpoints (GB10, CUDA), mlxcel generate -m MODEL --image IMAGE -p "Describe the image." -n N --temp 0 on main, against mlx-vlm e8035ae3 preprocessing the same file:
| Checkpoint (revision) | Image | Branch | Image tokens main / mlx-vlm |
Prompt tokens main / mlx-vlm |
|---|---|---|---|---|
mlx-community/Mage-VL-8bit (40dc28d8), bounds 3136..4000000 |
3840x2160 | max | 3901 / 3818 | 3926 / 3843 |
mlx-community/Qwen3-VL-2B-Instruct-4bit (9c4f5209), 65536..16777216 |
320x180 | min | 77 / 66 | 91 / 80 |
mlx-community/Qwen2.5-VL-3B-Instruct-4bit (46d4cf06), 3136..12845056 |
40x30 | min | 4 / 6 | 29 / 31 |
| same | 96x24 | min | 8 / 4 | 33 / 29 |
The mlx-vlm teacher-forced trace (scripts/mlx_vlm_image_logit_trace.py --mlxcel-prompt) refuses main's prompt in all four cases, for example:
image-token count differs: mlx-vlm 3818, mlxcel 3901; the prompts cannot share pixel features
Scored on the same continuation against the mlx-vlm float32 forward (which preprocesses the image itself), main's distribution is further off than a prompt with the reference's grid: Qwen3-VL 320x180 perplexity +5.1% and logit delta on the reference's choice up to 1.60 (-0.7% and 0.74 with the reference's grid), Qwen2.5-VL 40x30 +7.4% and 3.93 (-3.3% and 1.63), 96x24 +5.8% and 1.36 (-0.7% and 0.34), Mage-VL 3840x2160 -0.9% and 0.66 (-0.1% and 0.22). A 640x480 image, inside the bounds of all three checkpoints, is unaffected.
Current behaviour
| File | Lines on db3a23141 |
What it does |
|---|---|---|
src/vision/processors/qwen2_vl.rs (Qwen2VLProcessor::smart_resize) |
299-318 | over max_pixels: scale = sqrt(max_pixels / (h_bar * w_bar)), round(h_bar * scale / factor) (half away from zero); if still over, a second floor pass on the result; then, not as an else, ceil(h_bar * scale / factor) under min_pixels with scale = sqrt(min_pixels / (h_bar * w_bar)) |
src/vision/processors/ernie4_5_vl.rs (Ernie45VlProcessor::smart_resize) |
64-73 | floor(h_bar * scale / factor) / ceil(h_bar * scale / factor) with scale from the rounded area; the min_pixels check is not an else |
src/vision/processors/dots_ocr.rs (DotsOcrProcessor::smart_resize) |
62-73 | floor(h_bar * scale / factor) / ceil(h_bar * scale / factor) with scale from the rounded area |
Qwen2VLProcessor is shared. Its smart_resize sizes images (and Qwen-VL video frames) for Qwen2-VL, Qwen2.5-VL, Qwen3-VL, Qwen3-VL-MoE, Qwen3.5-VL, Qwen3-Omni-MoE, Qwen4-Exp, GLM-4V, GLM-4V-MoE, GLM-OCR, Mage-VL, MiMo-V2 VL, Cohere Compass, ColQwen2.5 and the XLA Qwen host preprocessor.
Reference behaviour
| mlxcel processor | Reference | Out-of-bounds rule there |
|---|---|---|
Qwen2VLProcessor |
transformers qwen2_vl/image_processing_qwen2_vl.py and image_processing_pil_qwen2_vl.py smart_resize (Qwen2-VL, Qwen2.5-VL, Qwen3-VL images); mlx-vlm qwen3_vl/processing_qwen3_vl.py _smart_resize_image, which mlx-vlm's Mage-VL processor also uses; transformers cohere_compass smart_resize |
beta from the original height * width, one floor / ceil of height / beta / factor |
Ernie45VlProcessor |
transformers ernie4_5_vl_moe/image_processing_ernie4_5_vl_moe.py smart_resize; mlx-vlm ernie4_5_moe_vl/processing_ernie4_5_moe_vl.py smart_resize |
same; mlx-vlm writes it as floor_by_factor(int(height / beta), factor) |
DotsOcrProcessor |
mlx-vlm dots_ocr/processing_dots_ocr.py loads the checkpoint's image processor through AutoImageProcessor, which is transformers' Qwen2-VL one |
same as Qwen2-VL |
Checked against transformers 5.24.1 and mlx-vlm e8035ae3b9740d5e3fc6cb0de4894132d60c8682 on the test host. Over every size in the table below, transformers' PIL and torchvision Qwen2-VL functions, mlx-vlm's Qwen3-VL function and transformers' Cohere Compass function return the same result, and transformers' and mlx-vlm's ERNIE functions agree.
Which inputs differ
Only images whose rounded size is out of bounds take these branches. Inside the bounds main and the reference agree. Out of bounds they differ whenever rounding first moved an edge far enough to cross a multiple of factor after scaling, which is common. Image tokens below are merged tokens, (h / factor) * (w / factor), reference vs main:
| Family, bounds (factor) | Size | Branch | Reference (h, w) | main (h, w) |
Image tokens, reference / main |
|---|---|---|---|---|---|
| Qwen2.5-VL checkpoint, 3136..12845056 (28) | 5120x2880 (5K screen), also 6016x3384, 7680x4320 | max | 2688x4760 | 2660x4760 | 16320 / 16150 |
| 5184x3456 (18 MP) | max | 2912x4368 | 2912x4396 | 16224 / 16328 | |
Qwen2.5-VL with max_pixels = 1003520 (1280 tokens) |
1372x772 | max | 728x1316 | 756x1316 | 1222 / 1269 |
| 4032x3024 (12 MP), also 3264x2448, 4000x3000, 8064x6048 | max | 840x1148 | 868x1148 | 1230 / 1271 | |
| 6000x4000 (24 MP), also 5184x3456, 6240x4160 | max | 812x1204 | 812x1232 | 1247 / 1276 | |
| 3024x1964, 3456x2234 (MacBook Pro screens) | max | 784x1232 | 812x1232 | 1232 / 1276 | |
| Qwen3-VL checkpoint, 65536..16777216 (32) | 320x180, 256x144 (thumbnails) | min | 192x352 | 224x352 | 66 / 77 |
| 177x100 | min | 224x352 | 192x384 | 77 / 72 | |
| 160x120 | min | 224x320 | 256x288 | 70 / 72 | |
| 6000x4000 (24 MP) | max | 3328x4992 | 3328x5024 | 16224 / 16328 | |
| 7680x4320 (8K) | max | 3072x5440 | 3040x5440 | 16320 / 16150 | |
| Mage-VL checkpoint, 3136..4000000 (32) | 3840x2160 (4K screen), also 5120x2880, 6016x3384, 7680x4320 | max | 1472x2656 | 1504x2656 | 3818 / 3901 |
| 3024x1964, 3456x2234 | max | 1600x2464 | 1600x2496 | 3850 / 3900 | |
| 6000x4000 (24 MP), also 5184x3456, 6240x4160 | max | 1632x2432 | 1600x2432 | 3876 / 3800 | |
| ERNIE 4.5 VL default, 3136..1003520 (28) | 1366x768 | max | 728x1316 | 728x1344 | 1222 / 1248 |
| 1680x1050 | max | 784x1260 | 784x1232 | 1260 / 1232 | |
| 8160x6120, 9248x6936 | max | 840x1148 | 868x1148 | 1230 / 1271 | |
| dots.ocr default, 3136..11289600 (28) | 6000x4000 (24 MP), also 6240x4160 | max | 2716x4088 | 2744x4088 | 14162 / 14308 |
| 5120x2880 (5K) | max | 2520x4480 | 2520x4452 | 14400 / 14310 | |
| 4624x3472 (16 MP), also 8160x6120, 9248x6936 | max | 2884x3864 | 2912x3864 | 14214 / 14352 |
Over 45 common photo, screen and thumbnail sizes under the six bound sets above (270 pairs), main differs from the reference on 55 of the 137 (size, family) pairs that are out of bounds: Qwen2.5-VL checkpoint 5 / 14, Qwen2.5-VL at 1003520 15 / 30, Qwen3-VL 8 / 22, Mage-VL 11 / 24, ERNIE 5 / 30, dots.ocr 11 / 17. The ported branch matches the reference on all 137. main can land on either side of the reference: more tokens for some sizes, fewer for others. Sizes that are inside the bounds are unaffected (the only in-bounds differences in the survey are the x.5 ties of #3463).
The survey's main column comes from a Python transcription of the Rust code; a scratch test that prints the real main function's result for the same 270 pairs agreed with it on all 270.
Deliberately left alone
- GLM-4V and GLM-OCR go through
Qwen2VLProcessorand change with it, but their reference (transformersglm4vsmart_resize, mlx-vlmglm_ocr/processing.py) multiplies the area bynum_frames = temporal_patch_size = 2and upsizes images with an edge underfactorfirst. mlxcel matches neither before nor after this change (for example GLM-OCR at its bounds, 3840x2160:mainand the fix 2156x3836, reference 1624x2912). That needs its own change to how those loaders set the bounds. - Hunyuan-VL, MiniMax-M3, LFM2-VL, PaddleOCR-VL, Falcon-OCR, Jina VLM, Mage-Flow (
conditioner::smart_resize) and CLEF media already scale the original size. - Youtu-VL (
ceilloop, fix(youtu_vl): match the AutoProcessor patch grid (36864 cap, ceil rounding) #1611), Muse Glimmer, Kimi, Moondream 3, Phi-4 SigLIP, LocateAnything: different algorithms, not this rule. - Aspect ratio beyond 200:1: the reference raises
ValueError; mlxcel has no error path insmart_resize. See "Proposed fix". - Video:
Qwen2VLProcessorsizes video frames with the samesmart_resize. The fix moves out-of-bounds frames the same way, but the Qwen-VL video path was not checked against a video reference here.
Proposed fix
Port the reference's branch in the three processors, from the original size and in the reference's operation order (height / beta / factor, height * beta / factor, beta = sqrt((height * width) / max_pixels)), with elif between the two branches. In Qwen2VLProcessor, beyond 200:1 (where the reference raises) the short edge can clamp to one factor and leave the area over max_pixels; trim the long edge there so max_image_tokens (used by the XLA context floor) still bounds every image. Below 200:1 keep the reference's result.
The change does not touch the lines #3464 changes, and the tests avoid tie inputs, so it applies with or without #3464.
Acceptance criteria
- For each of the three processors, a unit test pins out-of-bounds cases whose expected values come from the reference function (fails on
main), and in-bounds and agreeing out-of-bounds cases that must not move (pass onmainand after). Qwen2VLProcessorgives 728x1316 for 1372x772 undermax_pixels = 1003520, 1472x2656 (3818 tokens) for 3840x2160 under Mage-VL's bounds, and 192x352 for 320x180 under Qwen3-VL's bounds.- On a real checkpoint, an image that hits the condition gets the same image-token count as mlx-vlm, and an mlx-vlm teacher-forced trace that refuses
main's prompt runs.
Activity
[-]fix(vision): Qwen-VL, ERNIE 4.5 VL and dots.ocr rescale out-of-bounds images from the rounded size, where the reference scales the original size[/-][+]fix(vision): Qwen-VL, ERNIE 4.5 VL and dots.ocr rescale out-of-bounds images from the rounded size instead of the original[/+]fix(vision): keep the original-size rescale bounded for a zero edge
fix(vision): rescale out-of-bounds images from the original size like…