Image-Text-to-Text
Transformers
Safetensors
English
Chinese
feature-extraction
conversational
custom_code
Instructions to use FlashVL/FlashVL-2B-Dynamic with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use FlashVL/FlashVL-2B-Dynamic with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="FlashVL/FlashVL-2B-Dynamic", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("FlashVL/FlashVL-2B-Dynamic", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use FlashVL/FlashVL-2B-Dynamic with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "FlashVL/FlashVL-2B-Dynamic" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "FlashVL/FlashVL-2B-Dynamic", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/FlashVL/FlashVL-2B-Dynamic
- SGLang
How to use FlashVL/FlashVL-2B-Dynamic with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "FlashVL/FlashVL-2B-Dynamic" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "FlashVL/FlashVL-2B-Dynamic", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "FlashVL/FlashVL-2B-Dynamic" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "FlashVL/FlashVL-2B-Dynamic", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use FlashVL/FlashVL-2B-Dynamic with Docker Model Runner:
docker model run hf.co/FlashVL/FlashVL-2B-Dynamic
| import os | |
| import math | |
| import torch | |
| from torch import nn | |
| from functools import partial | |
| import torch.nn.functional as F | |
| class Adapter_Template(nn.Module): | |
| def __init__(self, config): | |
| super().__init__() | |
| self.gradient_checkpointing = False | |
| def freeze_module(self, module): | |
| for p in module.parameters(): | |
| p.requires_grad = False | |
| def forward(self, inputs, add_start_end=True): | |
| input_ids, hidden_states, targets, attn_mask, loss_mask = inputs | |
| image_features = self.forward_adapter_modules(hidden_states) | |
| return (input_ids, image_features, targets, attn_mask, loss_mask) | |
| class Adapter_AIM(Adapter_Template): | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.p0 = nn.Sequential( | |
| nn.LayerNorm(config.vision_config.hidden_size*4), | |
| nn.Linear(config.vision_config.hidden_size*4, config.intermediate_size), | |
| nn.GELU(), | |
| nn.Linear(config.intermediate_size, config.intermediate_size), | |
| nn.GELU(), | |
| ) | |
| self.proj = nn.Linear(config.intermediate_size, config.vision_config.proj_output_dim) | |
| def freeze(self): | |
| self.freeze_module(self.p0) | |
| self.freeze_module(self.proj) | |
| def pixel_shuffle(self, x, scale_factor=0.5): | |
| n, w, h, c = x.size() | |
| # N, W, H, C --> N, W, H * scale, C // scale | |
| x = x.reshape(n, w, int(h * scale_factor), int(c / scale_factor)) | |
| # N, W, H * scale, C // scale --> N, H * scale, W, C // scale | |
| x = x.permute(0, 2, 1, 3).contiguous() | |
| # N, H * scale, W, C // scale --> N, H * scale, W * scale, C // (scale ** 2) | |
| x = x.view(n, int(h * scale_factor), int(w * scale_factor), | |
| int(c / (scale_factor * scale_factor))) | |
| return x | |
| def forward_adapter_modules(self, hidden_states): | |
| h = w = int(hidden_states.shape[1] ** 0.5) | |
| hidden_states = hidden_states.reshape(hidden_states.shape[0], h, w, -1) | |
| hidden_states = self.pixel_shuffle(hidden_states, scale_factor=0.5) | |
| hidden_states = hidden_states.reshape(hidden_states.shape[0], -1, hidden_states.shape[-1]) | |
| hidden_states = self.proj(self.p0(hidden_states)) | |
| return hidden_states |