You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(vision): dynamic aspect-ratio image processing for Pixtral and Mistral3 with IMG_BREAK/IMG_END row tokens #779
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
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).
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).
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.
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.
Activity
fix(vision): aspect-ratio image processing for Pixtral and Mistral3 (l…
fix(vision): aspect-ratio image processing for Pixtral and Mistral3 (l…