Skip to content

Studio: show the model a phone photo the right way up - #12241

Merged
Lyxot merged 4 commits into
unslothai:mainfrom
NilayYadav:apply-exif-orientation-chat-images
Sep 29, 2026
Merged

Lyxot merged 4 commits into
unslothai:mainfrom
NilayYadav:apply-exif-orientation-chat-images

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

A photo taken on a phone is usually stored sideways with a tag that says how to turn it. The chat composer and the message bubble honour that tag, so the photo looks upright, but the model was given the stored pixels. Questions about text, layout or left and right in the photo came back wrong, and nothing showed that the model saw it rotated. This happened with GGUF models and with transformers models alike.

Chat images are now turned the way the tag says before they reach the model, the same way image editing already does since #10553. A tagged JPEG is saved again as a JPEG with the same quality settings as the original, so it stays about the same size. Images without a rotation tag are still sent byte for byte, unchanged.

NilayYadav added a commit to NilayYadav/unsloth that referenced this pull request Sep 28, 2026

@NilayYadav NilayYadav left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review + reproduction evidence

Pinned review: 520cdfe3ae0c51e75365d2925593ec9e9b9cad54. 1 finding, fixed in 520cdfe3.

I reproduced the chat flow on fresh upstream main (Studio installed with install.sh --local on Linux, NVIDIA L4, Chromium 1440x900). I logged in, loaded the model from the picker, attached a JPEG stored 800x600 with EXIF Orientation 6 through "Add photos & files", and asked "Is the red half at the top, bottom, left or right of the picture? Then read the word written on the red half." The composer and the message bubble show the photo upright (red TOP above blue BOTTOM) on both sides.

  • main 501b15fb, Qwen3-VL-2B-Instruct-GGUF Q4_K_M: "a red half on the left and a blue half on the right ... it reads "보통" ... the red half is at the left of the picture."
  • main 501b15fb, Qwen3-VL-2B-Instruct-unsloth-bnb-4bit: "The red half is on the left. The word written on the red half is "TOP"."
  • PR 520cdfe3, GGUF: "the red half is located at the top of the picture. The word written on the red half is TOP."
  • PR 520cdfe3, transformers: "The red half is at the top of the picture. The word written on the red half is "TOP"."

PR 12241 before/after

  • Unchanged paths: the same picture without the tag gives "top / TOP" on both sides and both backends. Untagged and Orientation 1 JPEG and PNG still reach llama-server byte for byte (checked across JPEG, grayscale, CMYK, RGBA, palette, 16-bit PNG, WebP and GIF). A follow-up turn in the same chat works, and the re-encode is deterministic, so the prompt cache still matches on history replay.
  • Tests: the new file fails on main (11 failed, 7 passed) and passes on the PR (18 passed). With the four related image test files, main has 11 failed and 499 passed; the PR has 510 passed.
  • Cost: on a 12 MP tagged JPEG the llama path takes about +29 ms per request (30.8 ms to 59.7 ms, median of 5), and the output is +0.6% in size. Untagged photos are unchanged (43.7 ms vs 43.6 ms).
  • Browser parity: I checked the rendered size in Chromium, Firefox and WebKit. A tagged JPEG or PNG is turned in all three; a tagged WebP is turned only in WebKit. That matches the WebP skip already used for image editing in #10553.

Fix pushed (520cdfe3): for a tagged PNG, or a JPEG that stb_image cannot read, _llama_image_data_url threw away the turned raster and decoded and turned the file a second time. On a 24 MP tagged PNG that cost 681 MB peak and 1.4 s. It now encodes the raster it already has: 578 MB and 1.17 s, with byte-identical output. A test checks that the file is decoded once.

Conclusion: the issue is real on main for both GGUF and transformers vision models and fixed on this branch. Remaining before merge: Actions A/B on fresh main (PR head, fix reverted, expected to fail) and upstream CI. All of those jobs were still queued when I posted this.

@NilayYadav NilayYadav left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex review coverage

Mirror: NilayYadav#155 (base is the PR's merge base d670959f, so the diff is this PR only). It covers upstream head 520cdfe3ae0c51e75365d2925593ec9e9b9cad54, and the mirror head has the same tree (f0cf85df).

Rounds: 2. Converged because Codex reported "Didn't find any major issues" on the mirror head 520cdfe3.

  • Round 1 (398012c5), P2, a tagged non-JPEG was decoded and turned twice while the first turned raster was still alive: fixed in 520cdfe3 (measured on a 24 MP tagged PNG: 681 MB peak and 1.4 s before, 578 MB and 1.17 s after, same output bytes; a test checks for a single decode).
  • Round 1 (398012c5), P2, a re-encoded JPEG could exceed the 10 MiB remote image limit: rejected. That limit mirrors llama-server's cap on its own http downloads, and llama-server does not cap base64 data URLs, which is what Studio sends. Main already forwards larger inline images unchanged.

Final Codex result on 520cdfe3: "Didn't find any major issues."

…at images

The chat decode carried a copy of the orientation rule image editing already used, without its step that drops the consumed tag, so a turned chat image still reported its old orientation. Both now call one exif_upright helper.
…id 1

Pillow keeps only the dict keys 0..n-1 of a qtables dict, so re-encoding such a tagged JPEG raised and the request failed with HTTP 400. The tables are now passed as a list in id order. The tests also check that the source's quantization tables survive the re-encode.
@Lyxot

Lyxot commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Pushed two small follow-up commits:

  • 082250fcef refactor: chat now uses the same EXIF orientation helper as image editing (core/inference/image_orientation.py) instead of a copy of it. The copy skipped the step that drops the consumed tag, so a turned chat image still reported its old orientation and a later re-save could turn it again. The model output does not change: the PNG hop to the worker already drops the tag.
  • b4b43bf590 fix: a tagged JPEG whose quantization tables are numbered from 1 failed with HTTP 400, because Pillow keeps only dict keys 0..n-1 when saving with qtables. The tables are now passed as a list in id order. The tests also check that the source tables survive the re-encode.

I also checked that the MLX path gets the upright image. MLX runs in the worker, which takes the image from _decode_and_resize_image like transformers does, and mlx_vlm does not re-apply the tag to PIL inputs.

@Lyxot

Lyxot commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Staging review and CI for head b4b43bf590, on staging PR Lyxot#111 (base is this PR's merge base, so the diff is exactly this PR):

The failures on this PR's own CI are not from this change:

  • Repo tests (CPU, studio) and workflow-trigger lint both fail test_playwright_suites_run_in_ci over tests/studio/playwright_user_message_time.py, which is on main.
  • Chat UI Tests (chat) failed one update-banner layout check, at one viewport, with an empty list. This PR changes only backend image decoding, and the job passes on main and on staging. It is being re-run.

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. It fixes a real problem, and GGUF, transformers and MLX all get the photo the right way up, turned once. Codex converged and staging CI is green on b4b43bf. The remaining CI failures on this PR are not from this change.

@Lyxot
Lyxot merged commit be52b45 into unslothai:main Sep 29, 2026
58 of 61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants