Repository navigation
Add Phi-3-vision - #51214
Open
lucamotz wants to merge 3 commits into
Open
Add Phi-3-vision#51214lucamotz wants to merge 3 commits into
lucamotz wants to merge 3 commits into
Conversation
Rename "su"/"yarn" to "longrope" before standardize_rope_params(), so original_max_position_embeddings is added for "su" configs.
Native phi3_v for microsoft/Phi-3-vision-128k-instruct and microsoft/Phi-3.5-vision-instruct (TIGER-Lab/VLM2Vec-Full loads with the same classes), built on LLaVA-NeXT, with the Hub weights loaded through the conversion mapping. Co-authored-by: Yaswanth Gali <82788246+yaswanth19@users.noreply.github.com> Co-authored-by: Dahlbomii <101373053+Dahlbomii@users.noreply.github.com>
Generation that starts at or below original_max_position_embeddings and continues past it recomputes the cache with the long factors, as Phi3ForCausalLM does, passing the images again.
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: auto, phi3, phi3_v, phi4_multimodal |
lucamotz
marked this pull request as ready for review
November 25, 2026 07:42
Contributor
Author
|
fyi @hmellor |
This branch has not been deployed
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.
What does this PR do?
Adds Phi-3-vision (
phi3_v), so microsoft/Phi-3-vision-128k-instruct and microsoft/Phi-3.5-vision-instruct load without remote code; TIGER-Lab/VLM2Vec-Full loads with the same classes. It is LLaVA-NeXT with a Phi-3 text model: the new code isget_image_features(2x2 patch merge, learned row newline and global separator, padded crops skipped), the HD-crop image processor, and 6WeightRenamings that load the Hub weights unchanged.Fixes #36166
Earlier attempts: #41977 (no push since 2025-12-18, the 2025-11-24 review unanswered) and #36036; their authors are co-authors.
Depends on #51200 (
Phi3Configrejects the"su"rope type these checkpoints use); its commit is the first one here until it merges.Decisions for reviewers
<s>, the layout of the remote processor (it tokenized the text after every image separately). Ids then match the remote processor exactly (with<|image|>for its<|image_N|>); an eval of the LLaVA-style alternative is below.Phi3ForCausalLMandPhi4MultimodalForCausalLMdo. I ported it, which needs aprepare_inputs_for_generationoverride that also passes the images again. Without it, generation that starts below 4096 tokens degenerates once it passes them.Phi3VConfigdrops the"_attn_implementation": "flash_attention_2"that the Microsoft configs force when it folds the flat Hub config; otherwiseAutoModel*ignoresattn_implementationand loading needs flash-attn.processor.apply_chat_templateandtokenizer.image_tokenneed small Hub PRs (chat template, image token), which I have prepared.Validation: input ids match the remote processor; against the remote code (transformers 4.48.3, same attention setting) the logits are bit-identical on CPU in bf16 (Phi-3.5-vision) and agree to 2e-4 at text positions in fp32 (TF32 off) on an NVIDIA GB10 (DGX Spark), with identical greedy text;
RUN_SLOWtests pass on the GB10.Parity with the remote code
Same weights and inputs and the remote code's attention setting (eager Phi-3, SDPA CLIP). Four prompts (one landscape image, one portrait image, two images, text only), plus one short prompt for the processor row.
TIGER-Lab/VLM2Vec-Full, last-token embeddings, bf16 on the GB10: cosine to the remote code 0.9995–1.0000.
Token layout eval (decision 1)
Phi-3.5-vision, bf16, greedy, first 500 test items:
<s>after each image (this PR)<s>(<|image|>withrstrip=False)<s>, and no\nafter the image (Hub tokenizer as is)4096-token cache reset (decision 2)
Phi-3.5-vision, bf16 on the GB10, prompt of 4040 tokens (with an image, and text only), 200 greedy tokens. The reference is a restart of
generateon the full sequence just past 4096 tokens, which is what the Phi-3.5-vision remote code's reset does. With the override, generation matches the reference token for token. Without it, 4.9% of the tokens past the boundary match, and the output turns into repeated newlines.Teacher-forced, text only, four 4000-token windows, 300 following tokens: NLL past the boundary is 7.1–8.1 without the reset and 0.05–2.5 with it.
With the override, the Phi-3.5-vision remote code and this PR generate identical tokens across the boundary (fp32, TF32 off; a 3975-token prompt with an image, 200 tokens, crossing at token 122). A continuation from a passed-in cache that crosses 4096 tokens recomputes the prefix without the image features, since
generategets no images on a continuation.I used Claude Code for parts of this implementation.