Some checks failed
Self-hosted runner (nightly-past-ci-caller) / Get number (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.11 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.10 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.9 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.8 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.7 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.6 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.5 (push) Has been cancelled
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
Build documentation / build (push) Has been cancelled
Build documentation / build_other_lang (push) Has been cancelled
CodeQL Security Analysis / CodeQL Analysis (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
PR CI / pr-ci (push) Has been cancelled
Slow tests on important models (on Push - A10) / Get all modified files (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
Slow tests on important models (on Push - A10) / Model CI (push) Has been cancelled
Check Tiny Models / Check tiny models (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Model CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Pipeline CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Example CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / DeepSpeed CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI - Flash Attn / Setup (push) Has been cancelled
Nvidia CI - Flash Attn / Model CI (push) Has been cancelled
Nvidia CI / Setup (push) Has been cancelled
Nvidia CI / Model CI (push) Has been cancelled
Nvidia CI / Torch pipeline CI (push) Has been cancelled
Nvidia CI / Example CI (push) Has been cancelled
Nvidia CI / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI / DeepSpeed CI (push) Has been cancelled
Nvidia CI / Quantization CI (push) Has been cancelled
Nvidia CI / Kernels CI (push) Has been cancelled
Doctests / Setup (push) Has been cancelled
Doctests / Call doctest jobs (push) Has been cancelled
Doctests / Send results to webhook (push) Has been cancelled
Extras Smoke Test / Get supported Python versions (push) Has been cancelled
Extras Smoke Test / Test extras on Python ${{ matrix.python-version }} (push) Has been cancelled
Extras Smoke Test / Check Slack token availability (push) Has been cancelled
Extras Smoke Test / Notify failures to Slack (push) Has been cancelled
Self-hosted runner (AMD scheduled CI caller) / Trigger Scheduled AMD CI (push) Has been cancelled
Stale Bot / Close Stale Issues (push) Has been cancelled
248 lines
8.8 KiB
Markdown
248 lines
8.8 KiB
Markdown
<!--Copyright 2025 The HuggingFace Team. All rights reserved.
|
||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||
the License. You may obtain a copy of the License at
|
||
|
||
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||
specific language governing permissions and limitations under the License.
|
||
|
||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||
rendered properly in your Markdown viewer.
|
||
|
||
-->
|
||
*This model was contributed to Hugging Face Transformers on 2025-03-18.*
|
||
|
||
<div style="float: right;">
|
||
<div class="flex flex-wrap space-x-1">
|
||
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
|
||
</div>
|
||
</div>
|
||
|
||
# Mistral 3
|
||
|
||
[Mistral 3](https://mistral.ai/news/mistral-small-3) is a latency optimized model with a lot fewer layers to reduce the time per forward pass. This model adds vision understanding and supports long context lengths of up to 128K tokens without compromising performance.
|
||
|
||
You can find the original Mistral 3 checkpoints under the [Mistral AI](https://huggingface.co/mistralai/models?search=mistral-small-3) organization.
|
||
|
||
> [!TIP]
|
||
> This model was contributed by [cyrilvallez](https://huggingface.co/cyrilvallez) and [yonigozlan](https://huggingface.co/yonigozlan).
|
||
> Click on the Mistral3 models in the right sidebar for more examples of how to apply Mistral3 to different tasks.
|
||
|
||
The example below demonstrates how to generate text for an image with [`Pipeline`] and the [`AutoModel`] class.
|
||
|
||
<hfoptions id="usage">
|
||
<hfoption id="Pipeline">
|
||
|
||
```python
|
||
from transformers import pipeline
|
||
|
||
|
||
messages = [
|
||
{"role": "user",
|
||
"content":[
|
||
{"type": "image",
|
||
"image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg",},
|
||
{"type": "text", "text": "Describe this image."}
|
||
,]
|
||
,}
|
||
,]
|
||
|
||
pipeline = pipeline(
|
||
task="image-text-to-text",
|
||
model="mistralai/Mistral-Small-3.1-24B-Instruct-2503",
|
||
device=0
|
||
)
|
||
outputs = pipeline(text=messages, max_new_tokens=50, return_full_text=False)
|
||
|
||
outputs[0]["generated_text"]
|
||
'The image depicts a vibrant and lush garden scene featuring a variety of wildflowers and plants. The central focus is on a large, pinkish-purple flower, likely a Greater Celandine (Chelidonium majus), with a'
|
||
```
|
||
|
||
</hfoption>
|
||
<hfoption id="AutoModel">
|
||
|
||
```python
|
||
from transformers import AutoModelForImageTextToText, AutoProcessor
|
||
|
||
|
||
model_checkpoint = "mistralai/Mistral-Small-3.1-24B-Instruct-2503"
|
||
processor = AutoProcessor.from_pretrained(model_checkpoint)
|
||
model = AutoModelForImageTextToText.from_pretrained(
|
||
model_checkpoint,
|
||
device_map="auto",
|
||
)
|
||
|
||
messages = [
|
||
{"role": "user",
|
||
"content":[
|
||
{"type": "image",
|
||
"image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg",},
|
||
{"type": "text", "text": "Describe this image."}
|
||
,]
|
||
,}
|
||
,]
|
||
|
||
inputs = processor.apply_chat_template(
|
||
messages,
|
||
add_generation_prompt=True,
|
||
tokenize=True, return_dict=True,
|
||
return_tensors="pt").to(model.device)
|
||
|
||
generate_ids = model.generate(**inputs, max_new_tokens=20)
|
||
decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
|
||
|
||
decoded_output
|
||
'The image depicts a vibrant and lush garden scene featuring a variety of wildflowers and plants. The central focus is on a large, pinkish-purple flower, likely a Greater Celandine (Chelidonium majus), with a'
|
||
```
|
||
|
||
</hfoption>
|
||
</hfoptions>
|
||
|
||
## Notes
|
||
|
||
- Mistral 3 supports text-only generation.
|
||
|
||
```py
|
||
import torch
|
||
from transformers import AutoProcessor, AutoModelForImageTextToText
|
||
|
||
model_checkpoint = ".mistralai/Mistral-Small-3.1-24B-Instruct-2503"
|
||
processor = AutoProcessor.from_pretrained(model_checkpoint)
|
||
model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map="auto")
|
||
|
||
SYSTEM_PROMPT = "You are a conversational agent that always answers straight to the point, always end your accurate response with an ASCII drawing of a cat."
|
||
user_prompt = "Give me 5 non-formal ways to say 'See you later' in French."
|
||
|
||
messages = [
|
||
{"role": "system", "content": SYSTEM_PROMPT},
|
||
{"role": "user", "content": user_prompt},
|
||
]
|
||
|
||
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
||
inputs = processor(text=text, return_tensors="pt").to(0)
|
||
generate_ids = model.generate(**inputs, max_new_tokens=50, do_sample=False)
|
||
decoded_output = processor.batch_decode(generate_ids[:, inputs["input_ids"].shape[1] :], skip_special_tokens=True)[0]
|
||
|
||
print(decoded_output)
|
||
"1. À plus tard!
|
||
2. Salut, à plus!
|
||
3. À toute!
|
||
4. À la prochaine!
|
||
5. Je me casse, à plus!
|
||
|
||
```
|
||
|
||
/\_/\
|
||
( o.o )
|
||
> ^ <
|
||
|
||
```"
|
||
````
|
||
|
||
- Mistral 3 accepts batched image and text inputs.
|
||
|
||
```py
|
||
import torch
|
||
from transformers import AutoProcessor, AutoModelForImageTextToText
|
||
|
||
model_checkpoint = "mistralai/Mistral-Small-3.1-24B-Instruct-2503"
|
||
processor = AutoProcessor.from_pretrained(model_checkpoint)
|
||
model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map="auto")
|
||
|
||
messages = [
|
||
[
|
||
{
|
||
"role": "user",
|
||
"content": [
|
||
{"type": "image", "url": "https://llava-vl.github.io/static/images/view.jpg"},
|
||
{"type": "text", "text": "Write a haiku for this image"},
|
||
],
|
||
},
|
||
],
|
||
[
|
||
{
|
||
"role": "user",
|
||
"content": [
|
||
{"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"},
|
||
{"type": "text", "text": "Describe this image"},
|
||
],
|
||
},
|
||
],
|
||
]
|
||
|
||
|
||
inputs = processor.apply_chat_template(messages, padding=True, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device)
|
||
|
||
output = model.generate(**inputs, max_new_tokens=25)
|
||
|
||
decoded_outputs = processor.batch_decode(output, skip_special_tokens=True)
|
||
decoded_outputs
|
||
["Write a haiku for this imageCalm waters reflect\nWhispers of the forest's breath\nPeace on wooden path"
|
||
, "Describe this imageThe image depicts a vibrant street scene in what appears to be a Chinatown district. The focal point is a traditional Chinese"]
|
||
```
|
||
|
||
- Mistral 3 also supported batched image and text inputs with a different number of images for each text. The example below quantizes the model with bitsandbytes.
|
||
|
||
```py
|
||
import torch
|
||
from transformers import AutoProcessor, AutoModelForImageTextToText, BitsAndBytesConfig
|
||
|
||
model_checkpoint = "mistralai/Mistral-Small-3.1-24B-Instruct-2503"
|
||
processor = AutoProcessor.from_pretrained(model_checkpoint)
|
||
quantization_config = BitsAndBytesConfig(load_in_4bit=True)
|
||
model = AutoModelForImageTextToText.from_pretrained(
|
||
model_checkpoint, quantization_config=quantization_config
|
||
device_map="auto")
|
||
|
||
messages = [
|
||
[
|
||
{
|
||
"role": "user",
|
||
"content": [
|
||
{"type": "image", "url": "https://llava-vl.github.io/static/images/view.jpg"},
|
||
{"type": "text", "text": "Write a haiku for this image"},
|
||
],
|
||
},
|
||
],
|
||
[
|
||
{
|
||
"role": "user",
|
||
"content": [
|
||
{"type": "image", "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"},
|
||
{"type": "image", "url": "https://thumbs.dreamstime.com/b/golden-gate-bridge-san-francisco-purple-flowers-california-echium-candicans-36805947.jpg"},
|
||
{"type": "text", "text": "These images depict two different landmarks. Can you identify them?"},
|
||
],
|
||
},
|
||
],
|
||
]
|
||
|
||
inputs = processor.apply_chat_template(messages, padding=True, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device)
|
||
|
||
output = model.generate(**inputs, max_new_tokens=25)
|
||
|
||
decoded_outputs = processor.batch_decode(output, skip_special_tokens=True)
|
||
decoded_outputs
|
||
["Write a haiku for this imageSure, here is a haiku inspired by the image:\n\nCalm lake's wooden path\nSilent forest stands guard\n", "These images depict two different landmarks. Can you identify them? Certainly! The images depict two iconic landmarks:\n\n1. The first image shows the Statue of Liberty in New York City."]
|
||
```
|
||
|
||
## Mistral3Config
|
||
|
||
[[autodoc]] Mistral3Config
|
||
|
||
## MistralCommonBackend
|
||
|
||
[[autodoc]] MistralCommonBackend
|
||
|
||
## Mistral3Model
|
||
|
||
[[autodoc]] Mistral3Model
|
||
|
||
## Mistral3ForConditionalGeneration
|
||
|
||
[[autodoc]] Mistral3ForConditionalGeneration
|
||
- forward
|
||
- get_image_features
|