Repository navigation
fix(youtu_vl): emit patches in merge-block-major order - #1619
Merged
Merged
Conversation
`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
Project convention forbids em dashes in prose, code comments included. Comment text only; the assertion and the expected row order are untouched. Refs #1610
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
YoutuVLProcessor::try_preprocess_with_spatialemitted 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_patchesin 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_embeddingis annn.Linearover those rows andremap_youtu_vl_weightsonly renames that weight, so raster rows fed the trained projection a permuted vector. Independently,YoutuVLVisionEncoder::rot_pos_embbuilds position ids withreshape(&[h / merge, merge, w / merge, merge])thentranspose_axes(&[0, 2, 1, 3]), andforward_with_spatialgathers 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 usesunfoldis 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.rsis untouched.src/vision/processors/youtu_vl.rs: a newYoutuVLPreprocessError::UnalignedPatchGridmakes the merge-divisibility precondition explicit.smart_resizerounds both edges to a multiple ofpatch_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_featurespins 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: atspatial_merge_size=2a 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 oftests/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 withmetal,accelerate, greedy (-t 0 -n 48). Both columns were measured on this machine, before at64f5d9b41and after at this branch.tests/fixtures/test_image.png, a 224x224 solid orange square (prompt: "Describe this image briefly.")This is the fixture that pins the defect. At 224x224 the merged grid is 7x7, a single 8x8 window, so
get_window_indexis 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.")tests/fixtures/test_image_shapes_336.png, the same scene at 336x336 (same prompt)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_resizelifts 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.rsare 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 warningscargo fmt --all -- --checkpython3 scripts/ci/check_cross_repo_refs.py--workspacetest and clippy gate, left to CICloses #1610