Skip to content

fix(youtu_vl): emit patches in merge-block-major order - #1619

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-1610-youtu-vl-patch-order
Sep 4, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-1610-youtu-vl-patch-order

Conversation

@inureyes

@inureyes inureyes commented Sep 4, 2026

Copy link
Copy Markdown
Member

Summary

YoutuVLProcessor::try_preprocess_with_spatial emitted one row per patch in plain raster order over the patch grid with the inner feature order (c, dy, dx). Two independent consumers expect merge-block-major rows with channel-last (dy, dx, c) features, so the vision tower was fed patches that do not correspond to the positions and groupings it assumes.

The checkpoint's own processor, convert_image_to_patches in https://huggingface.co/tencent/Youtu-VL-4B-Instruct/blob/main/image_processing_siglip2_fast.py, reshapes to (C, nh/m, m, ps, nw/m, m, ps) and permutes (1, 4, 2, 5, 3, 6, 0). Siglip2VisionEmbeddings.patch_embedding is an nn.Linear over those rows and remap_youtu_vl_weights only renames that weight, so raster rows fed the trained projection a permuted vector. Independently, YoutuVLVisionEncoder::rot_pos_emb builds position ids with reshape(&[h / merge, merge, w / merge, merge]) then transpose_axes(&[0, 2, 1, 3]), and forward_with_spatial gathers on [n_groups, spatial_merge_unit, dim], so both the rotary position and the 2x2 merge grouping were attached to the wrong patch. The mismatch was provable without an external oracle: the encoder's two inputs disagreed with each other.

What changed

  • src/vision/processors/youtu_vl.rs: the emission loop now runs (block_y, block_x, inner_y, inner_x) with (dy, dx, c) inner features. The stale comment claiming upstream uses unfold is replaced with one naming this checkpoint's actual construction and linking it. Qwen2-VL's loop shape is mirrored, but not its (c, dy, dx) inner order, which is correct for that family because it does unfold; src/vision/processors/qwen2_vl.rs is untouched.
  • src/vision/processors/youtu_vl.rs: a new YoutuVLPreprocessError::UnalignedPatchGrid makes the merge-divisibility precondition explicit. smart_resize rounds both edges to a multiple of patch_size * spatial_merge_size, so the grid is always divisible today, but the block loops would silently drop patches rather than fail if that resize policy ever changed.
  • src/vision/processors/youtu_vl_tests.rs: patches_are_emitted_merge_block_major_with_channel_last_features pins the row order and the inner feature order on a 4x4 patch grid, which is 2x2 merge blocks. The grid size is the point of the test: at spatial_merge_size=2 a 2x2 patch grid is a single block, where block-major and raster are the same sequence, so a smaller fixture could not fail on this defect. The three RGB channels carry three non-overlapping codes so one row also separates (dy, dx, c) from (c, dy, dx).
  • tests/youtu_vl_parity.rs (new): real-model content tests in the shape of tests/qwen2_5_vl_parity.rs, since a test that only counts tokens passes on a fluent invention.

Real-checkpoint validation

models/mlx/youtu-vl-4b-instruct (tencent/Youtu-VL-4B-Instruct), M1 Ultra, release build with metal,accelerate, greedy (-t 0 -n 48). Both columns were measured on this machine, before at 64f5d9b41 and after at this branch.

tests/fixtures/test_image.png, a 224x224 solid orange square (prompt: "Describe this image briefly.")

  • Before: "The image is completely black and contains no visible content, objects, text, or details. It is a solid black square or rectangle with no variation in color or texture."
  • After: "The image is a solid, uniform block of bright orange color. There are no visible objects, textures, patterns, or variations — it is a pure, flat field of orange with no discernible features or details."

This is the fixture that pins the defect. At 224x224 the merged grid is 7x7, a single 8x8 window, so get_window_index is the identity and the window inverse from #1600 / #1603 provably cannot affect the result either way.

tests/fixtures/test_image_shapes.png, 448x448, a red square, a blue circle and a green triangle on light grey (prompt: "What shapes and colors are in this image? Answer briefly.")

  • Before: "The image contains a single, solid black circle on a white background."
  • After: "The image contains a single, solid black circle on a plain white background."

tests/fixtures/test_image_shapes_336.png, the same scene at 336x336 (same prompt)

  • Before: "The image contains a single, solid black circle on a white background."
  • After: "The image contains a single white circle on a black background."

The part that is still wrong, on purpose

Both three-shape fixtures changed and are both still wrong. That is the measured, expected outcome of this fix, not a regression and not an incomplete fix of this defect: issue #1610 predicted exactly these two strings before the work started. At least one further defect remains in this family's vision path, it is unidentified, and it is out of scope here.

The evidence pattern is worth naming: the only fixture that reads correctly is the only one whose merged grid fits inside a single attention window (7x7). Both fixtures that need more than one window (11x11 after smart_resize lifts 336 to 352, and 14x14) are wrong, and they are wrong differently from each other, which is not what a size-independent defect in the embedding or the merger would produce. That is filed as #1618 with the measurements and the candidate areas, and it is deliberately not chased here.

Accordingly the two shape tests in tests/youtu_vl_parity.rs are written at the full correct answer and gated #[ignore = "known failing, blocked on #1618 ..."] rather than weakened to something this build can satisfy. Fixing #1618 is what turns them green.

Test plan

  • cargo test --profile test-fast --features metal,accelerate --lib vision::processors::youtu_vl (8 passed, including the new order test)
  • cargo test --profile test-fast --features metal,accelerate --test youtu_vl_parity -- --ignored youtu_vl_describes_the_solid_color_fixture (1 passed against the local checkpoint)
  • cargo clippy --lib --tests --features metal,accelerate -- -D warnings
  • cargo fmt --all -- --check
  • python3 scripts/ci/check_cross_repo_refs.py
  • Real-checkpoint runs on all three fixtures, before and after, recorded above
  • The full --workspace test and clippy gate, left to CI

Closes #1610

`YoutuVLProcessor::try_preprocess_with_spatial` emitted one row per patch in plain raster order over the patch grid with the inner feature order `(c, dy, dx)`. Two independent consumers expect something else, so the vision tower was fed patches that do not correspond to the positions and groupings it assumes.

The checkpoint's own processor, `convert_image_to_patches` in https://huggingface.co/tencent/Youtu-VL-4B-Instruct/blob/main/image_processing_siglip2_fast.py, reshapes to `(C, nh/m, m, ps, nw/m, m, ps)` and permutes `(1, 4, 2, 5, 3, 6, 0)`, which is merge-block-major rows with channel-last `(dy, dx, c)` features. `Siglip2VisionEmbeddings.patch_embedding` is an `nn.Linear` over those rows and `remap_youtu_vl_weights` only renames that weight, so raster rows feed the trained projection a permuted vector. Separately, `YoutuVLVisionEncoder::rot_pos_emb` builds position ids with `reshape(&[h / merge, merge, w / merge, merge])` then `transpose_axes(&[0, 2, 1, 3])`, and `forward_with_spatial` gathers on `[n_groups, spatial_merge_unit, dim]`, so both the rotary position and the 2x2 merge grouping were attached to the wrong patch.

The emission loop now runs `(block_y, block_x, inner_y, inner_x)` with `(dy, dx, c)` inner features, mirroring `src/vision/processors/qwen2_vl.rs` in loop shape only. Qwen2-VL's `(c, dy, dx)` inner order is correct for that family, which unfolds, and is left alone. `smart_resize` rounds both edges to a multiple of `patch_size * spatial_merge_size`, so the grid is always divisible by the merge factor, but the loop now checks that rather than relying on it silently, returning a new `UnalignedPatchGrid` error instead of dropping patches if the resize policy ever changes.

Validated on `models/mlx/youtu-vl-4b-instruct` (M1 Ultra, release with `metal,accelerate`, greedy `-t 0 -n 48`). The 224x224 solid-orange fixture went from "The image is completely black and contains no visible content, objects, text, or details. It is a solid black square or rectangle with no variation in color or texture." to "The image is a solid, uniform block of bright orange color. There are no visible objects, textures, patterns, or variations — it is a pure, flat field of orange with no discernible features or details." That fixture is the one that pins this defect: at 224 the merged grid is 7x7, a single 8x8 window, so the window permutation is the identity and cannot be responsible.

The two three-shape fixtures changed but are still described wrong, which is the expected result here, not a regression. At least one further defect remains in this family's vision path and is filed as #1618 with the measurements; both shape fixtures are carried in the new `tests/youtu_vl_parity.rs` as `#[ignore]`d assertions written at the full correct answer and labelled as known failing against that issue.

Tests: a checkpoint-free unit test pins the row order and the inner feature order on a 4x4 patch grid, which is 2x2 merge blocks and the minimum that separates block-major from raster. `tests/youtu_vl_parity.rs` adds the real-model content tests in the shape of `tests/qwen2_5_vl_parity.rs`; its 224 control passes against the local checkpoint.

Closes #1610
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:models Model architectures, weights, loading, metadata modeltype:vlm Vision-language model labels Sep 4, 2026
Project convention forbids em dashes in prose, code comments included. Comment text only; the assertion and the expected row order are untouched.

Refs #1610
@inureyes inureyes added status:done Completed and removed status:review Under review labels Sep 4, 2026
@inureyes
inureyes merged commit e9f84c9 into main Sep 4, 2026
12 checks passed
@inureyes
inureyes deleted the fix/issue-1610-youtu-vl-patch-order branch September 4, 2026 02:24
@inureyes inureyes added this to the 0.7 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata modeltype:vlm Vision-language model 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(youtu_vl): vision processor emits patches in the wrong order

1 participant