Skip to content

fix(vision): dynamic aspect-ratio image processing for Pixtral and Mistral3 with IMG_BREAK/IMG_END row tokens #779

Closed

Description

@inureyes

Summary

Pixtral and Mistral3 image inputs currently run through SigLipProcessor::new_rescale_only (src/loading/vlm_pixtral.rs:222, 295), which force-resizes every image to a fixed square (src/vision/processors/siglip.rs:56-93, resize_exact), and the prompt builder emits a fixed mm_tokens_per_image block (vlm_pixtral.rs:223-229, 299-304) with no [IMG_BREAK]/[IMG_END] structure. These models were trained on dynamically resized inputs that preserve aspect ratio, with image tokens laid out row by row, [IMG_BREAK] between rows and [IMG_END] at the end. The current fixed-square path distorts non-square images and omits the row structure, degrading VQA and OCR quality.

Target behavior

  • Resize: ratio = max(h / max_h, w / max_w); if the ratio exceeds 1, downscale both sides by it; round each side UP to a whole multiple of patch_size * spatial_merge_size. Never upscale.
  • Tokens per image: rows x cols patches after merge; emit patch tokens row by row, [IMG_BREAK] after each row except the last, [IMG_END] after the last row.
  • Per-image sizes flow to the encoder so its 2D position grid matches the actual rows and cols.

Implementation plan

  1. A new Pixtral processor in src/vision/processors/ implementing the resize and patchify rules above (resampling filter consistent with the existing processors), replacing SigLipProcessor::new_rescale_only in load_pixtral_vlm and load_mistral3_vlm (src/loading/vlm_pixtral.rs).
  2. Variable-length token expansion: replace the fixed MergeStrategy::LLaVA count with a per-image expansion carrying the break and end token ids (look up the [IMG_BREAK] and [IMG_END] ids from the tokenizer); follow the row-structured expansion patterns already in src/multimodal/ (the DeepSeek-OCR 2D newline mosaic is the closest precedent).
  3. Encoder: verify the Pixtral vision encoder consumes non-square patch grids (2D RoPE or position embedding over rows and cols); fix the grid plumbing if it assumes a square.
  4. Tests: resize math (downscale-only, round-up-to-factor), token layout for a 3x5 grid (break and end placement), and a square-image regression that matches current behavior modulo the added structure tokens.

Acceptance criteria

  • Real-checkpoint validation on Pixtral and Mistral3 with non-square images: descriptions reference spatial layout correctly, with a quality spot check against the fixed-square path.
  • Square images keep working; no regression in existing tests.

Effort: medium-high.

Activity

  1. added
    status:ready
    type:bug
    priority:medium
    status:in-progress
    status:review
    status:done
    and removed
    status:ready
    status:in-progress
    status:review
    on Jul 13, 2026
  2. added a commit that references this issue on Jul 30, 2026
    e7477c5
  3. self-assigned this
    on Aug 31, 2026
  4. added a commit that references this issue on Oct 27, 2026
    720bdce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority:mediumstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions