docs/source/en/model_doc/mllama.md
This model was released on 2024-09-25 and added to Hugging Face Transformers on 2024-09-25.
The Llama 3.2-Vision collection of multimodal large language models (LLMs) is a collection of pretrained and instruction-tuned image reasoning generative models in 11B and 90B sizes (text + images in / text out). The Llama 3.2-Vision instruction-tuned models are optimized for visual recognition, image reasoning, captioning, and answering general questions about an image.
Model Architecture: Llama 3.2-Vision is built on top of Llama 3.1 text-only model, which is an auto-regressive language model that uses an optimized transformer architecture. The tuned versions use supervised fine-tuning (SFT) and reinforcement learning with human feedback (RLHF) to align with human preferences for helpfulness and safety. To support image recognition tasks, the Llama 3.2-Vision model uses a separately trained vision adapter that integrates with the pre-trained Llama 3.1 language model. The adapter consists of a series of cross-attention layers that feed image encoder representations into the core LLM.
MllamaForConditionalGeneration.MllamaForCausalLM for generation to avoid loading vision tower."<|image|>" tokens where the images should be inserted.apply_chat_template method to convert chat messages to text that can then be passed as text to the processor. If you're using transformers>=4.49.0, you can also get a vectorized output from apply_chat_template. See the Usage Examples below for more details on how to use it.Mllama has an extra token used as a placeholder for image positions in the text. It means that input ids and an input embedding layer will have an extra token. But since the weights for input and output embeddings are not tied, the lm_head layer has one less token and will fail if you want to calculate loss on image tokens or apply some logit processors. In case you are training, make sure to mask out special "<|image|>" tokens in the labels as the model should not be trained on predicting them.
Otherwise if you see CUDA-side index errors when generating, use the below code to expand the lm_head by one more token.
old_embeddings = model.get_output_embeddings()
num_tokens = model.vocab_size + 1
resized_embeddings = model._get_resized_lm_head(old_embeddings, new_num_tokens=num_tokens, mean_resizing=True)
resized_embeddings.requires_grad_(old_embeddings.weight.requires_grad)
model.set_output_embeddings(resized_embeddings)
from transformers import AutoProcessor, MllamaForConditionalGeneration
model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"
model = MllamaForConditionalGeneration.from_pretrained(model_id, device_map="auto")
processor = AutoProcessor.from_pretrained(model_id)
messages = [
[
{
"role": "user",
"content": [
{"type": "image", "url": "https://llava-vl.github.io/static/images/view.jpg"},
{"type": "text", "text": "What does the image show?"}
]
}
],
]
inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=25)
print(processor.decode(output[0]))
import requests
from PIL import Image
from transformers import AutoProcessor, MllamaForConditionalGeneration
model_id = "meta-llama/Llama-3.2-11B-Vision"
model = MllamaForConditionalGeneration.from_pretrained(model_id, device_map="auto")
processor = AutoProcessor.from_pretrained(model_id)
prompt = "<|image|>If I had to write a haiku for this one"
url = "https://llava-vl.github.io/static/images/view.jpg"
raw_image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(text=prompt, images=raw_image, return_tensors="pt").to(model.device)
output = model.generate(**inputs, do_sample=False, max_new_tokens=25)
print(processor.decode(output[0], skip_special_tokens=True))
[[autodoc]] MllamaConfig
[[autodoc]] MllamaTextConfig
[[autodoc]] MllamaVisionConfig
[[autodoc]] MllamaProcessor - call
[[autodoc]] MllamaImageProcessor - preprocess
[[autodoc]] MllamaImageProcessorPil - preprocess
[[autodoc]] MllamaForConditionalGeneration - forward
[[autodoc]] MllamaForCausalLM - forward
[[autodoc]] MllamaTextModel - forward
[[autodoc]] MllamaModel
[[autodoc]] MllamaVisionModel - forward