|
| 1 | +<!--Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +
|
| 12 | +⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 | +rendered properly in your Markdown viewer. |
| 14 | +
|
| 15 | +--> |
| 16 | + |
| 17 | +*This model was released on 2025-05-06 and added to Hugging Face Transformers on 2025-10-07.* |
| 18 | + |
| 19 | +# FastVLM |
| 20 | + |
| 21 | +<div class="flex flex-wrap space-x-1"> |
| 22 | +<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 23 | +<img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat"> |
| 24 | +<img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 25 | +</div> |
| 26 | + |
| 27 | +## Overview |
| 28 | + |
| 29 | +FastVLM is an open-source vision-language model featuring a novel hybrid vision encoder, FastViTHD. Leveraging reparameterizable convolutional layers, scaled input resolution, and a reduced number of visual tokens, FastVLM delivers high accuracy with exceptional efficiency. Its optimized architecture enables deployment even on edge devices, achieving ultra-low TTFT (time to first token) without sacrificing performance. |
| 30 | + |
| 31 | +The model was proposed in [FastVLM: Efficient Vision Encoding for Vision Language Models](https://huggingface.co/papers/2412.13303) by Pavan Kumar Anasosalu Vasu, Fartash Faghri, Chun-Liang Li, Cem Koc, Nate True, Albert Antony, Gokul Santhanam, James Gabriel, Peter Grasch, Oncel Tuzel and Hadi Pouransari. |
| 32 | + |
| 33 | +The abstract from the paper is the following: |
| 34 | + |
| 35 | +*Scaling the input image resolution is essential for enhancing the performance of Vision Language Models (VLMs), particularly in text-rich image understanding tasks. However, popular visual encoders such as ViTs become inefficient at high resolutions due to the large number of tokens and high encoding latency. At different operational resolutions, the vision encoder of a VLM can be optimized along two axes: reducing encoding latency and minimizing the number of visual tokens passed to the LLM, thereby lowering overall latency. Based on a comprehensive efficiency analysis of the interplay between image resolution, vision latency, token count, and LLM size, we introduce FastVLM—a model that achieves an optimized trade-off between resolution, latency, and accuracy. FastVLM incorporates FastViTHD, a novel hybrid vision encoder designed to output fewer tokens and significantly reduce encoding time for high-resolution images. Unlike previous methods, FastVLM achieves the optimal balance between visual token count and image resolution solely by scaling the input image, eliminating the need for additional token pruning and simplifying the model design. In the LLaVA-1.5 setup, FastVLM achieves 3.2× improvement in time-to-first-token (TTFT) while maintaining similar performance on VLM benchmarks compared to prior works. Compared to LLaVa-OneVision at the highest resolution (1152×1152), FastVLM achieves better performance on key benchmarks like SeedBench, MMMU and DocVQA, using the same 0.5B LLM, but with 85× faster TTFT and a vision encoder that is 3.4× smaller.* |
| 36 | + |
| 37 | +This model was contributed by [Kamila](https://github.com/kamila-chay). |
| 38 | +The original code can be found [here](https://github.com/apple/ml-fastvlm). |
| 39 | + |
| 40 | +## Usage tips |
| 41 | + |
| 42 | +- We advise users to use `padding_side="left"` when computing batched generation as it leads to more accurate results. Simply make sure to call `processor.tokenizer.padding_side = "left"` before generating. |
| 43 | + |
| 44 | +- Note the model has not been explicitly trained to process multiple images in the same prompt, although this is technically possible, you may experience inaccurate results. |
| 45 | + |
| 46 | +**Important: ** |
| 47 | + |
| 48 | +Hugging Face models use SDPA by default; however, this model’s visual backbone supports only eager attention, so it automatically falls back to `"eager"`. |
| 49 | + |
| 50 | +If you want to use a different attention implementation in the language decoder, make sure to set it explicitly, for example: |
| 51 | + |
| 52 | +`model = FastVlmForConditionalGeneration.from_pretrained("KamilaMila/FastVLM-0.5B", attn_implementation={"text_config": "flash_attention_2"})` |
| 53 | + |
| 54 | +Setting it for the entire model, e.g. |
| 55 | + |
| 56 | +`model = FastVlmForConditionalGeneration.from_pretrained("KamilaMila/FastVLM-0.5B", attn_implementation="flash_attention_2")` |
| 57 | + |
| 58 | +will result in an error. |
| 59 | + |
| 60 | +### Formatting Prompts with Chat Templates |
| 61 | + |
| 62 | +Each **checkpoint** is trained with a specific prompt format, depending on the underlying large language model backbone. To ensure correct formatting, use the processor’s `apply_chat_template` method. |
| 63 | + |
| 64 | +**Important:** |
| 65 | +- You must construct a conversation history — passing a plain string won't work. |
| 66 | +- Each message should be a dictionary with `"role"` and `"content"` keys. |
| 67 | +- The `"content"` should be a list of dictionaries for different modalities like `"text"` and `"image"`. |
| 68 | + |
| 69 | +## Usage examples |
| 70 | + |
| 71 | +### Single input inference |
| 72 | + |
| 73 | + |
| 74 | +```python |
| 75 | +import torch |
| 76 | +from transformers import AutoProcessor, FastVlmForConditionalGeneration |
| 77 | + |
| 78 | +# Load the model in half-precision |
| 79 | +model = FastVlmForConditionalGeneration.from_pretrained("KamilaMila/FastVLM-0.5B", dtype=torch.bfloat16, device_map="auto") |
| 80 | +processor = AutoProcessor.from_pretrained("KamilaMila/FastVLM-0.5B") |
| 81 | + |
| 82 | +conversation = [ |
| 83 | + { |
| 84 | + "role": "user", |
| 85 | + "content": [ |
| 86 | + {"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"}, |
| 87 | + {"type": "text", "text": "What is shown in this image?"}, |
| 88 | + ], |
| 89 | + }, |
| 90 | +] |
| 91 | + |
| 92 | +inputs = processor.apply_chat_template( |
| 93 | + conversation, |
| 94 | + add_generation_prompt=True, |
| 95 | + tokenize=True, |
| 96 | + return_dict=True, |
| 97 | + return_tensors="pt" |
| 98 | +).to(model.device, torch.bfloat16) |
| 99 | + |
| 100 | +# Generate |
| 101 | +generate_ids = model.generate(**inputs, max_new_tokens=30) |
| 102 | +processor.batch_decode(generate_ids, skip_special_tokens=True) |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +### Batched inference |
| 107 | + |
| 108 | +FastVLM also supports batched inference. Here is how you can do it: |
| 109 | + |
| 110 | +```python |
| 111 | +import torch |
| 112 | +from transformers import AutoProcessor, FastVlmForConditionalGeneration |
| 113 | + |
| 114 | +# Load the model in half-precision |
| 115 | +model = FastVlmForConditionalGeneration.from_pretrained("KamilaMila/FastVLM-0.5B", dtype=torch.bfloat16, device_map="auto") |
| 116 | +processor = AutoProcessor.from_pretrained("KamilaMila/FastVLM-0.5B") |
| 117 | + |
| 118 | + |
| 119 | +# Prepare a batch of two prompts |
| 120 | +conversation_1 = [ |
| 121 | + { |
| 122 | + "role": "user", |
| 123 | + "content": [ |
| 124 | + {"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"}, |
| 125 | + {"type": "text", "text": "What is shown in this image?"}, |
| 126 | + ], |
| 127 | + }, |
| 128 | +] |
| 129 | + |
| 130 | +conversation_2 = [ |
| 131 | + { |
| 132 | + "role": "user", |
| 133 | + "content": [ |
| 134 | + {"type": "image", "url": "http://images.cocodataset.org/val2017/000000039769.jpg"}, |
| 135 | + {"type": "text", "text": "What is shown in this image?"}, |
| 136 | + ], |
| 137 | + }, |
| 138 | +] |
| 139 | + |
| 140 | +inputs = processor.apply_chat_template( |
| 141 | + [conversation_1, conversation_2], |
| 142 | + add_generation_prompt=True, |
| 143 | + tokenize=True, |
| 144 | + return_dict=True, |
| 145 | + padding=True, |
| 146 | + return_tensors="pt" |
| 147 | +).to(model.device, torch.bfloat16) |
| 148 | + |
| 149 | + |
| 150 | +# Generate |
| 151 | +generate_ids = model.generate(**inputs, max_new_tokens=30) |
| 152 | +processor.batch_decode(generate_ids, skip_special_tokens=True) |
| 153 | +``` |
| 154 | + |
| 155 | + |
| 156 | +## Note regarding reproducing original implementation |
| 157 | + |
| 158 | +In order to match the logits of the [original implementation](https://github.com/apple/ml-fastvlm), one needs to use float32. In half precision the logit difference is higher due to tiny differences in how some ops are implemented in timm. |
| 159 | + |
| 160 | +### Using Flash Attention 2 |
| 161 | + |
| 162 | +Flash Attention 2 is an even faster, optimized version of the previous optimization, please refer to the [Flash Attention 2 section of performance docs](https://huggingface.co/docs/transformers/perf_infer_gpu_one). |
| 163 | + |
| 164 | +## FastVlmConfig |
| 165 | + |
| 166 | +[[autodoc]] FastVlmConfig |
| 167 | + |
| 168 | +## FastVlmModel |
| 169 | + |
| 170 | +[[autodoc]] FastVlmModel |
| 171 | + |
| 172 | +## FastVlmForConditionalGeneration |
| 173 | + |
| 174 | +[[autodoc]] FastVlmForConditionalGeneration |
| 175 | + - forward |
0 commit comments