Qwen3VL: apply the sRGB tone curve in the image preprocess path#411
Merged
davidkoski merged 1 commit intoJul 13, 2026
Merged
Conversation
CoreImage filters run in a linear-light working space and asMLXArray renders with a nil colorSpace, so the image path fed the model linearized values instead of the gamma-encoded sRGB bytes the HF reference preprocesses. Bright content survives that error; dark content does not — near-black text (luminance 23/255 on a 6/255 background) reached the ViT with ~12x less contrast than reference and was unreadable. The sibling Qwen25VL/Qwen2VL image paths and this file's own video path already apply the tone-curve step; the port dropped it in the image path. Measured on the incident screenshot: faint-band signal vs background was 0.011 without the step, 0.126 with it (reference: 0.133). Adds a regression test pinning the model-side extrema of a faint dark-theme fixture to the gamma-encoded reference values. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
davidkoski
reviewed
Jul 13, 2026
| let resampled = processed.map { | ||
| MediaProcessing.resampleBicubic( | ||
| MediaProcessing.inSRGBToneCurveSpace($0), to: targetSize) | ||
| } |
Collaborator
There was a problem hiding this comment.
I wonder if other models have the same issue? From a quick scan it looks like Idefics3, MistralV3, and Paligemma might.
Qwen25VL, Gemma3 and Gemma4 all do the resample in gamma space (incorrect, but probably matches the python code) so they won't have this issue.
davidkoski
approved these changes
Jul 13, 2026
davidkoski
left a comment
Collaborator
There was a problem hiding this comment.
Change looks good. See comment on other models. Thank you.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #410.
The Qwen3VL image path hands the model CoreImage's linear-light working-space values instead of the gamma-encoded sRGB values the HF reference preprocesses: CoreImage filters run in linear light and
MediaProcessing.asMLXArrayrenders with a nil colorSpace. The siblingQwen25VL/Qwen2VLimage paths and this file's own video path all apply the linear→sRGB tone-curve step; the Qwen3VL port dropped it in the image path.Bright content survives the error; dark content does not — near-black text (luminance 23/255 on a 6/255 background) reaches the ViT with ~12× less contrast than reference and is unreadable. Measured on the incident screenshot (text-vs-background signal after mean/std-0.5 normalization): 0.011 without the step, 0.126 with it, HF reference 0.133. Before/after screenshots in #410.
The change is the one-line
inSRGBToneCurveSpacewrap at the resample site, plus a regression test (Qwen3VLToneCurveTests) that runs a synthetic faint dark-theme fixture throughpreprocessand pins the model-side extrema to the gamma-encoded reference values — the linear-space failure mode lands far outside the tolerances.Note: textually adjacent to #398 (both touch the resample site); whichever lands second is a trivial rebase.