Mllama
This model was released on 2024-09-25 and added to Hugging Face Transformers on 2024-09-25.
Mllama
Section titled “Mllama”
Overview
Section titled “Overview”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.
Usage Tips
Section titled “Usage Tips”- For image+text and text inputs use
MllamaForConditionalGeneration. - For text-only inputs use
MllamaForCausalLMfor generation to avoid loading vision tower. - Each sample can contain multiple images, and the number of images can vary between samples. The processor will pad the inputs to the maximum number of images across samples and to a maximum number of tiles within each image.
- The text passed to the processor should have the
"<|image|>"tokens where the images should be inserted. - The processor has its own
apply_chat_templatemethod to convert chat messages to text that can then be passed as text to the processor. If you’re usingtransformers>=4.49.0, you can also get a vectorized output fromapply_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 + 1resized_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)Usage Example
Section titled “Usage Example”Instruct model
Section titled “Instruct model”import torchfrom transformers import MllamaForConditionalGeneration, AutoProcessor
model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"model = MllamaForConditionalGeneration.from_pretrained(model_id, device_map="auto", dtype=torch.bfloat16)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]))Base model
Section titled “Base model”import requestsimport torchfrom PIL import Imagefrom transformers import MllamaForConditionalGeneration, AutoProcessor
model_id = "meta-llama/Llama-3.2-11B-Vision"model = MllamaForConditionalGeneration.from_pretrained(model_id, device_map="auto", dtype=torch.bfloat16)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))MllamaConfig
Section titled “MllamaConfig”[[autodoc]] MllamaConfig
MllamaProcessor
Section titled “MllamaProcessor”[[autodoc]] MllamaProcessor
MllamaImageProcessor
Section titled “MllamaImageProcessor”[[autodoc]] MllamaImageProcessor
MllamaImageProcessorFast
Section titled “MllamaImageProcessorFast”[[autodoc]] MllamaImageProcessorFast
MllamaForConditionalGeneration
Section titled “MllamaForConditionalGeneration”[[autodoc]] MllamaForConditionalGeneration - forward
MllamaForCausalLM
Section titled “MllamaForCausalLM”[[autodoc]] MllamaForCausalLM - forward
MllamaTextModel
Section titled “MllamaTextModel”[[autodoc]] MllamaTextModel - forward
MllamaModel
Section titled “MllamaModel”[[autodoc]] MllamaModel
MllamaVisionModel
Section titled “MllamaVisionModel”[[autodoc]] MllamaVisionModel - forward