Skip to content

fix(youtu_vl): reverse_window_indices returns the permutation, not its inverse #1600

Closed

Description

@inureyes

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

  1. Reproduce on a Youtu-VL checkpoint. Determine spatial_merge_size, window_size and patch_size from the config, compute merger_window_size, and find an image size whose merged grid is not a multiple of it. Confirm that window_index composed with itself is not the identity at that size.
  2. Replace the body of reverse_window_indices with the direct inverse, inverse[window_index[rank]] = rank. Guard the index so a malformed grid cannot panic, as invert_window_index in src/vision/encoders/qwen2_5_vl.rs does.
  3. Correct the comment at the call site so it describes what the code does.
  4. 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.
  5. 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_indices equals argsort(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, and cargo fmt --all -- --check are 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

  1. added
    status:ready
    type:bug
    priority:medium
    area:models
    modeltype:vlm
    on Sep 2, 2026
  2. added
    status:done
    and removed
    status:ready
    on Sep 3, 2026
  3. added a commit that references this issue on Sep 3, 2026
    b2ff1ee
  4. added this to the 0.7 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:modelsmodeltype:vlmpriority:mediumstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions