Add Mistral3 vision-language model support (For Flux2 Migration)#3246
Open
SpenserCai wants to merge 13 commits intohuggingface:mainfrom
Open
Add Mistral3 vision-language model support (For Flux2 Migration)#3246SpenserCai wants to merge 13 commits intohuggingface:mainfrom
SpenserCai wants to merge 13 commits intohuggingface:mainfrom
Conversation
Contributor
Author
Contributor
Author
|
Fixed clippy and fmt. |
Member
|
Is 24B the smallest model for this schenario? It's pretty slow on metal. |
Contributor
Author
Yes, the quantitative version is out, which is the smallest model. |
Contributor
Author
@ivarflakstad Is there anything I need to change about this pr?🤔 |
Member
|
Not sure. Focusing on stabilizing the next release. |
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.

Summary
This PR adds support for the Mistral3 (Mistral-Small-3.x) vision-language model to candle-transformers. Mistral3 combines the Pixtral vision encoder with the Mistral language model, enabling multimodal image-text understanding.
Changes
New files in
candle-transformers/src/models/mistral3/:mod.rs- Module exports and documentationconfig.rs-Mistral3Configwith vision, text, and projector settingsmodel.rs-Mistral3ModelandMistral3ForConditionalGenerationpatch_merger.rs-PatchMergerfor reducing image tokensprojector.rs-MultiModalProjector(RMSNorm + PatchMerger + MLP)Modified files:
candle-transformers/src/models/mod.rs- Addedmistral3module exportcandle-transformers/src/models/pixtral/vision_model.rs- Addedforward_with_hidden_states()andVisionModelOutputstructcandle-transformers/src/models/mistral.rs- Addedforward_embeds_hidden()for multimodal integrationArchitecture
Key Implementation Details
PatchMerger: Uses
reshape + permuteto implement PyTorch'sunfoldoperation (kernel_size == stride, no overlap), merging 2x2 patches into one.Image Token Replacement: Implements
replace_image_tokens()as Candle equivalent of PyTorch'smasked_scatter.Vision Tower Integration: Uses
forward_with_hidden_states()to get batch-dimension-preserved output matching PyTorch Transformers behavior.Supported Models
Differences from Pixtral LLaVA
Usage
Verification
The implementation has been verified against PyTorch Transformers reference:
Checklist