Skip to content

fix(vision): honour checkpoint pixel bounds in eager Qwen3-VL processor - #3316

Merged
inureyes merged 4 commits into
mainfrom
fix/issue-3290-qwen3-vl-pixel-bounds
Nov 23, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/issue-3290-qwen3-vl-pixel-bounds

Conversation

@inureyes

@inureyes inureyes commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Eager Qwen3-VL resized images against the Qwen2-VL family defaults (min_pixels 3136) with Lanczos3, while HF and the XLA host preprocessor resize against the checkpoint's size.shortest_edge / size.longest_edge (65536 / 16777216) with bicubic. On tests/fixtures/test_image.png (224x224) eager saw 49 image tokens where XLA and HF see 64.

  • New QwenImageResizePolicy (src/loading/vlm_qwen_image_policy.rs) selects one whole sidecar as HF 5.x does: the nested processor_config.json image_processor block when present, otherwise preprocessor_config.json; keys the chosen source omits take the class defaults (3136 / 12845056 / bicubic). Within the source, explicit min_pixels / max_pixels win over size. A checkpoint with no sidecar keeps the historical defaults. Over the 97 local checkpoints that ship a sidecar, whole-source selection and the earlier key-by-key merge resolve identically.
  • Checkpoint bounds above 2^28 pixels are rejected, and smart_resize (src/vision/processors/qwen2_vl.rs) now uses saturating u64 arithmetic, so a crafted sidecar or a huge image cannot wrap the grid and image-token count.
  • qwen_vl_processor_with_norm applies it, so every caller follows: Qwen3-VL, Qwen3-VL MoE, Qwen3.5 / 3.6 / 3.8, Qwen3-Omni MoE, Qwen4-Exp and Cohere Compass. Compass's private bound reader is folded into the shared one (it keeps 16384 / 3868706 and 49 tokens) and now resamples bicubic. Qwen3-Omni keeps 3136 / 12845056 and only changes resampler. Qwen3-VL-Embedding and the Qwen3-VL reranker drop their duplicate apply_pixel_bounds and take the same policy through load_qwen3_vl, so they now resize bicubic (embedding values and rerank scores shift slightly; bounds unchanged).
  • The XLA Qwen3-VL loader validates the same policy (still requiring explicit bounds and resample=3) instead of parsing its own.
  • Malformed or inverted bounds and unsupported resample codes now fail the load rather than falling back.
  • Qwen2-VL, Qwen2.5-VL and GLM-4V use qwen_vl_processor and are untouched.

Real-model results (GB10, -n 64 --temp 0, "Describe this image.")

Model Backend Image tokens Index 8
qwen3-vl-2b-instruct-4bit eager, main 49 48204
qwen3-vl-2b-instruct-4bit eager, this PR 64 (grid [1,16,16]) 4285
qwen3-vl-2b-instruct-4bit XLA, this PR 64 (unchanged; ids identical to main's XLA run) 6437 (bf16 near-tie, #3291)
qwen2.5-vl-3b-instruct-4bit eager, main and this PR 64 identical 8 tokens, "The image is a solid orange color."

Qwen2.5-VL XLA matches eager up to EOS (the missing stop is #3292). Smoke runs: Qwen3-VL 30B-A3B MoE 64 tokens, coherent; Qwen3.5-0.8B 49 to 64 tokens, same greedy ids; North-Micro-Vision 49 tokens, ids identical to main.

Test plan

  • cargo test --profile test-fast --features cuda,xla-iree --lib -- qwen_image_policy qwen3_vl_iree_processor_policy (12 passed, including the 224x224 to 64-token gate through the real loader helper)
  • lib test filters qwen (663 passed) and compass clef mage (1010 passed), --test-threads=1
  • after review: qwen_image_policy qwen3_vl_iree_processor_policy qwen2_vl (30 passed), qwen3_vl_embedding qwen3_vl_generative cohere_compass (44 passed), real-model rerun (64 image tokens, index 8 = 4285)
  • cargo check --features cuda,xla-iree --all-targets, cargo check --no-default-features --features xla-diagnostics --all-targets, cargo clippy -p mlxcel --features cuda,xla-iree --lib --tests -- -D warnings, cargo fmt --check

Closes #3290

Eager Qwen3-VL resized images against the Qwen2-VL family defaults (min_pixels 3136) with Lanczos3, while HF and the XLA host preprocessor use the checkpoint's size.shortest_edge / longest_edge (65536 / 16777216) and its bicubic resample. A 224x224 image became 49 image tokens on eager and 64 on XLA and HF, one cause of the eager vs XLA divergence found in #3212.

A new shared reader, QwenImageResizePolicy, resolves bounds and resampler from the nested processor_config.json image_processor block first and preprocessor_config.json second, with explicit min_pixels / max_pixels before size. qwen_vl_processor_with_norm applies it, so Qwen3-VL, Qwen3-VL MoE, Qwen3.5 / 3.6 / 3.8, Qwen3-Omni, Qwen4-Exp and Cohere Compass all resize as HF does; the XLA Qwen3-VL loader validates the same policy instead of parsing its own. Compass's private bound reader is folded in. Malformed or inverted bounds and unsupported resample codes now fail the load. Qwen2-VL, Qwen2.5-VL and GLM-4V keep their processor.

Validated with the new policy unit tests (including a 224x224 -> grid [1,16,16], 64 token gate through the real loader helper), the qwen, compass, clef and mage lib test filters, and eager vs XLA real-model runs.

Closes #3290
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 23, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Eager Qwen3-VL-generation loaders resize images against the checkpoint's own pixel bounds and resampler, as HF and the XLA host preprocessor do (#3290).

Findings Addressed

None needed fixing on the branch: no CRITICAL or HIGH findings.

Remaining Items

  • QwenImageResizePolicy::from_sources merges the nested processor_config.json image_processor block and preprocessor_config.json key by key. HF never merges: transformers 5.x takes the nested block wholesale when it exists (image_processing_base.get_image_processor_dict), and 4.57 takes preprocessor_config.json wholesale first. A nested block missing a bound or resample therefore gets HF's class defaults (3136 / 12845056 / bicubic), not the standalone file's value. No checkpoint under models/ hits this (all Qwen3-VL, 3.5, 3.6, 3.8, Omni and Compass sidecars resolve identically both ways), so it is a fidelity gap, not a live bug. (MEDIUM)
  • CHANGELOG does not mention that Qwen3-VL-Embedding and the Qwen3-VL reranker now resample bicubic too (they load through load_qwen3_vl), and its "more image tokens" sentence does not apply to Qwen3-Omni, whose sidecar keeps 3136 / 12845056 so only its filter changes. (MEDIUM)
  • The branch conflicts with main in CHANGELOG.md only (main is 3 commits ahead). It needs a rebase before merge. (MEDIUM)
  • apply_pixel_bounds in src/models/qwen3_vl_embedding.rs is now redundant with the loader policy, and its doc comment still says the generative loader uses the Qwen2-VL defaults. (LOW)
  • HF does not validate inverted bounds (smart_resize just clamps to max_pixels), so failing the load is stricter than the reference. That is acceptable since the XLA path already failed closed and no Qwen-family checkpoint under models/ trips it (the only inverted sidecars locally are Idefics3, Mistral3 and Pixtral, which never reach this helper). (LOW)

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow (every qwen_vl_processor_with_norm caller: Qwen3-VL, Qwen3-VL MoE, Qwen3.5/3.6/3.8 dense and MoE, Qwen3-Omni MoE, Qwen4-Exp, Cohere Compass, plus the XLA Qwen3-VL host preprocessor)
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass: qwen_image_policy and qwen3_vl_iree_processor_policy (12 tests), plus the real-checkpoint Qwen3-VL-Embedding image gate and the Qwen3-VL-Reranker text and image gate on local checkpoints, all on the CUDA box

…ze overflow-safe

A crafted processor_config.json or preprocessor_config.json could state min_pixels or max_pixels in the trillions. smart_resize then upscaled every image to an allocation-exhausting size, and its u32 `h * w` products wrapped in release builds (panicked in debug), producing a wrong grid and image-token count.

The policy reader now rejects either bound above 2^28 pixels (16384 x 16384; real checkpoints use 16777216 at most). smart_resize does its arithmetic in saturating u64 and clamps the result to u32 instead of multiplying u32 edges.

Validated with the new oversized-bound and huge-input tests plus the existing qwen_image_policy and qwen2_vl suites. The xla-iree feature could not be built here (IREE_DIST is not provisioned on this host), so the XLA loader path was reviewed but not compiled.

Refs #3290
@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review of the sidecar reader, the eager and XLA Qwen3-VL loaders, and qwen2_vl.rs. Fixed in 3d49704.

  • HIGH (fixed): sidecar min_pixels / max_pixels had no ceiling, so a crafted checkpoint could force every image to be upscaled to an allocation-exhausting size. QwenImageResizePolicy now rejects either bound above 2^28 pixels.
  • HIGH (fixed): smart_resize multiplied u32 edges (h * w), which wraps in release and panics in debug for very large images or bounds, yielding a wrong grid and token count. It now uses saturating u64 arithmetic and clamps to u32.
  • No issue found: zero, negative, fractional and string bounds are rejected, inverted bounds are rejected, and size of the wrong type errors. A malformed sidecar JSON is skipped by read_optional_model_json, falling back to defaults, so there is no panic path.
  • No issue found: the policy is read once at load time, so no per-request cost is added to image preprocessing.
  • LOW (not changed): read_optional_model_json silently ignores a sidecar that exists but fails to parse. This is pre-existing behavior shared by other loaders.

Tests: new oversized-bound and huge-input tests pass along with the existing qwen_image_policy and qwen2_vl suites (26 passed, CUDA, test-fast). The xla-iree feature could not be built on this host (IREE_DIST is not set), so qwen3_vl_iree_processor_policy was not run. The CHANGELOG conflict with main is left for the finalizer.

QwenImageResizePolicy merged the nested processor_config.json image_processor block and preprocessor_config.json key by key. HF transformers 5.x picks one whole source instead: the nested block when present, otherwise the standalone file, with missing keys taking the class defaults (3136 / 12845056 / bicubic). The policy now does the same, and a new test pins a nested block without bounds next to a standalone file that has them.

Qwen3-VL-Embedding and the Qwen3-VL reranker re-applied preprocessor_config.json min/max on top of the loader through a private apply_pixel_bounds whose comment still said the loader used Qwen2-VL defaults. load_qwen3_vl already applies the shared policy, so the duplicate is removed.

Validation: a scan of the 97 local checkpoints with processor sidecars resolves the same bounds, resampler and validation outcome under both rules.

Refs #3290
@inureyes inureyes added status:done Completed area:architecture Architecture and code structure changes and removed status:review Under review labels Nov 23, 2026
@inureyes
inureyes marked this pull request as ready for review November 23, 2026 09:10
@inureyes
inureyes merged commit 19a2aea into main Nov 23, 2026
33 checks passed
@inureyes
inureyes deleted the fix/issue-3290-qwen3-vl-pixel-bounds branch November 23, 2026 09:28
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:architecture Architecture and code structure changes area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: honour checkpoint pixel bounds in the eager Qwen3-VL image processor

1 participant