Repository navigation
Description
Problem / Background
reverse_window_indices in src/vision/encoders/youtu_vl_window.rs:147-159 builds the wrong permutation. It sorts (value, index) pairs and writes reverse[orig_idx] = rank. For a permutation of 0..N, which is exactly what get_window_index in the same file returns, the element with value v always lands at rank v, so the loop degenerates to reverse[i] = window_index[i]: it reproduces window_index itself instead of argsort(window_index). The call site at src/vision/encoders/youtu_vl.rs:420-427 even documents the intent as "Equivalent to argsort(window_index) (Python upstream)", so the comment and the code disagree.
Applying take(h, window_index) twice restores the original order only when the permutation is an involution. That happens whenever every merged grid dimension is a multiple of the window width, and fails otherwise, leaving the merged vision tokens scrambled before they reach the language model.
This is the same construction that src/vision/encoders/qwen2_5_vl.rs carried until #1596 (PR to follow). It was deliberately left out of that PR because Youtu-VL is a separate family with its own encoder, checkpoint and validation path, and the fix should be validated on a Youtu-VL checkpoint rather than assumed from the Qwen2.5-VL result.
Evidence
Measured for Qwen2.5-VL, whose get_window_index has the same shape (4x4 merged-token windows from window_size / spatial_merge_size / patch_size):
- 448x448 image, 16x16 merged grid: the permutation is an involution, 0 of 256 merged tokens misplaced, and the defect is invisible.
- 336x336 image, 12x12 merged grid: 120 of 144 merged tokens misplaced. The mean absolute difference of the merger output against a transformers float32 oracle was 0.85 relative with the incorrect inverse and 3.2e-3 with
argsort, and the model described a three-shape image as "A red circle and a blue triangle" instead of naming all three shapes with their own colors. - 560x560: 360 of 400 misplaced. 448x672: 352 of 384 misplaced.
Whether the same grid sizes reach Youtu-VL depends on its own processor and merger_window_size, which is why this needs its own measurement.
Proposed Solution
- Reproduce on a Youtu-VL checkpoint. Determine
spatial_merge_size,window_sizeandpatch_sizefrom the config, computemerger_window_size, and find an image size whose merged grid is not a multiple of it. Confirm thatwindow_indexcomposed with itself is not the identity at that size. - Replace the body of
reverse_window_indiceswith the direct inverse,inverse[window_index[rank]] = rank. Guard the index so a malformed grid cannot panic, asinvert_window_indexinsrc/vision/encoders/qwen2_5_vl.rsdoes. - Correct the comment at the call site so it describes what the code does.
- Add checkpoint-free unit tests beside the module:
[2, 0, 1]must invert to[1, 2, 0]; a non-involution grid must round-trip to the identity; an involution grid must round-trip too, so the case that hid the defect stays documented. - Validate end to end on a Youtu-VL checkpoint at a non-involution size against the upstream reference, and record the before and after descriptions in the PR.
Scope
In scope: src/vision/encoders/youtu_vl_window.rs, the call-site comment in src/vision/encoders/youtu_vl.rs, and the unit tests for the corrected helper.
Out of scope: the Qwen2.5-VL copy, fixed in #1596. Any other window-attention family that turns out to share the construction gets its own issue.
Acceptance Criteria
-
reverse_window_indicesequalsargsort(window_index)for every input, and the call-site comment matches the code. - Checkpoint-free unit tests cover
[2, 0, 1], a non-involution grid, and an involution grid. - A Youtu-VL checkpoint is run at a non-involution merged-grid size before and after, and both outputs are recorded in the PR alongside the upstream reference output.
-
cargo test --workspace --profile test-fast --features metal,accelerate,cargo clippy --workspace --all-targets --features metal,accelerate -- -D warnings, andcargo fmt --all -- --checkare green.
Technical Considerations
Related: #1596 (the same defect in the Qwen2.5-VL encoder, with the measurement method and the corrected helper to copy).
Activity
fix(qwen2_5_vl): f32 tower norm and a correct window inverse (#1601)
fix(youtu_vl): build the window inverse as argsort, not the permutati…
fix(youtu_vl): build the window inverse as argsort (lablup#1603)
fix(youtu_vl): emit patches in merge-block-major order (#1619)