Skip to content

Add Phi-3-vision - #51214

Open
lucamotz wants to merge 3 commits into
huggingface:mainfrom
lucamotz:add-phi3-v
Open

lucamotz wants to merge 3 commits into
huggingface:mainfrom
lucamotz:add-phi3-v

Conversation

@lucamotz

@lucamotz lucamotz commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

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 is get_image_features (2x2 patch merge, learned row newline and global separator, padded crops skipped), the HD-crop image processor, and 6 WeightRenamings 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 (Phi3Config rejects the "su" rope type these checkpoints use); its commit is the first one here until it merges.

Decisions for reviewers

  1. Each image's placeholder run ends with <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.
  2. The Phi-3.5-vision remote code recomputes the cache when generation first crosses 4096 tokens, as Phi3ForCausalLM and Phi4MultimodalForCausalLM do. I ported it, which needs a prepare_inputs_for_generation override that also passes the images again. Without it, generation that starts below 4096 tokens degenerates once it passes them.
  3. Phi3VConfig drops the "_attn_implementation": "flash_attention_2" that the Microsoft configs force when it folds the flat Hub config; otherwise AutoModel* ignores attn_implementation and loading needs flash-attn.
  4. processor.apply_chat_template and tokenizer.image_token need 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_SLOW tests 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.

Phi-3.5-vision Phi-3-vision-128k
Input ids (native processor vs remote processor) identical, 5/5 prompts identical, 5/5 prompts
Pixel values max abs 0.030 (PIL vs torch resize) max abs 0.034
CPU, bf16: logits, image features, greedy 40 tokens all identical (bit-exact), 4/4 prompts (run on the GB10 only)
GB10, fp32 (TF32 off): image features identical identical
GB10, fp32 (TF32 off): logits at text positions max abs ≤ 7.0e-5 ≤ 1.6e-4
GB10, fp32 (TF32 off): greedy 40 tokens identical, 4/4 identical, 4/4
GB10, bf16: logits at text positions max abs ≤ 0.50 (logits ~30), argmax 100% ≤ 0.38, argmax 100% (94% on one prompt)
GB10, bf16: greedy 40 tokens identical on 4/4 end-to-end, 3/4 with the remote inputs (late near-tie) identical on 4/4 with the remote inputs, 3/4 end-to-end (late near-tie)

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) no <s> (<|image|> with rstrip=False) no <s>, and no \n after the image (Hub tokenizer as is)
AI2D 80.2% 80.2% (McNemar p 1.0) 80.4% (p 1.0)
ChartQA (relaxed accuracy) 64.6% 62.4% (p 0.027) 61.6% (p 0.004)
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 generate on 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 generate gets no images on a continuation.

I used Claude Code for parts of this implementation.

lucamotz and others added 3 commits November 24, 2026 22:18
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.
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: auto, phi3, phi3_v, phi4_multimodal

@lucamotz
lucamotz marked this pull request as ready for review November 25, 2026 07:42
@lucamotz

Copy link
Copy Markdown
Contributor Author

fyi @hmellor

This branch has not been deployed

No deployments
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.

Add phi3 Vision

1 participant