Instructions to use Qwen/Qwen2.5-VL-3B-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Qwen/Qwen2.5-VL-3B-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Qwen/Qwen2.5-VL-3B-Instruct") 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 AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct") model = AutoModelForMultimodalLM.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct", device_map="auto") 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?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Qwen/Qwen2.5-VL-3B-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Qwen/Qwen2.5-VL-3B-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Qwen/Qwen2.5-VL-3B-Instruct", "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/Qwen/Qwen2.5-VL-3B-Instruct
- SGLang
How to use Qwen/Qwen2.5-VL-3B-Instruct 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 "Qwen/Qwen2.5-VL-3B-Instruct" \ --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": "Qwen/Qwen2.5-VL-3B-Instruct", "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 "Qwen/Qwen2.5-VL-3B-Instruct" \ --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": "Qwen/Qwen2.5-VL-3B-Instruct", "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 Qwen/Qwen2.5-VL-3B-Instruct with Docker Model Runner:
docker model run hf.co/Qwen/Qwen2.5-VL-3B-Instruct
Chat template of AutoTokenizer does not work on assistant mask tokens
Issue with Qwen2.5 Chat Template: Assistant Mask Always Zero
I'm encountering an issue when applying the chat template using AutoTokenizer for Qwen 2.5 models. The assistant mask always comes back full of zeros. The chat template seems to be looking for {{ "{% generation %}" }} token but it doesn't exist.
Code
from transformers import AutoTokenizer
model_name = "Qwen/Qwen2.5-7B-Instruct"
tokenizer2 = AutoTokenizer.from_pretrained(model_name)
msgs = [
{"role":"system", "content":"This is system prompt"},
{"role":"user", "content":"this is user prompt"},
{"role":"assistant","content":"This is assistant prompt"}
]
chat_without_tokenized = tokenizer2.apply_chat_template(msgs, tokenize=False)
max_length = 4000
rendered_text = tokenizer2.apply_chat_template(
msgs,
tokenize=True,
max_length=max_length,
add_generation_prompt=False,
return_assistant_tokens_mask=True,
return_dict=True
)
#print(rendered_text)
input_ids = rendered_text["input_ids"]
attention_mask = rendered_text["attention_mask"]
assistant_mask = rendered_text["assistant_masks"]
print(f"Length InputIDs = {len(input_ids)} Input IDs: {input_ids}")
print(f"Length Attention Mask = {len(attention_mask)} Attention Mask: {attention_mask}")
print(f"Length Assistant Mask = {len(assistant_mask)} Assistant Mask: {assistant_mask}")
Output
return_assistant_tokens_mask==True but chat template does not contain `{% generation %}` keyword.
Length InputIDs = 27 Input IDs: [151644, 8948, 198, 1986, 374, 1849, 9934, 151645, 198, 151644, 872, 198, 574, 374, 1196, 9934, 151645, 198, 151644, 77091, 198, 1986, 374, 17847, 9934, 151645, 198]
Length Attention Mask = 27 Attention Mask: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Length Assistant Mask = 27 Assistant Mask: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I also tried to decode the template with the add_generation_prompt True and False. Things get worse. The template seems to be completely wrong as it tries to add the generation prompt over again.
Decoded text (add_generation_prompt=True):
<|im_start|>system
This is system prompt<|im_end|>
<|im_start|>user
this is user prompt<|im_end|>
<|im_start|>assistant
This is assistant prompt<|im_end|>
<|im_start|>assistant
The original chat template of Qwen2.5 doesn't support assistant masks as there are no {{ "{% generation %}" }} and {{ "{% endgeneration %}" }} in it.
You can rewrite the template to support this feature:
qwen2_5_vl_template_add_generation = (
"{% set image_count = namespace(value=0) %}"
"{% set video_count = namespace(value=0) %}"
"{% for message in messages %}"
"{% if loop.first and message['role'] != 'system' %}"
"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n"
"{% endif %}"
"<|im_start|>{{ message['role'] }}\n"
"{% if message['content'] is string %}"
"{% if message['role'] == 'assistant' %}"
"{% generation %}"
"{{ message['content'] }}"
"{% endgeneration %}"
"{% else %}"
"{{ message['content'] }}"
"{% endif %}"
"<|im_end|>\n"
"{% else %}"
"{% for content in message['content'] %}"
"{% if content['type'] == 'image' or 'image' in content or 'image_url' in content %}"
"{% set image_count.value = image_count.value + 1 %}"
"{% if add_vision_id %}"
"Picture {{ image_count.value }}: "
"{% endif %}"
"<|vision_start|><|image_pad|><|vision_end|>"
"{% elif content['type'] == 'video' or 'video' in content %}"
"{% set video_count.value = video_count.value + 1 %}"
"{% if add_vision_id %}"
"Video {{ video_count.value }}: "
"{% endif %}"
"<|vision_start|><|video_pad|><|vision_end|>"
"{% elif 'text' in content %}"
"{% if message['role'] == 'assistant' %}"
"{% generation %}"
"{{ content['text'] }}"
"{% endgeneration %}"
"{% else %}"
"{{ content['text'] }}"
"{% endif %}"
"{% endif %}"
"{% endfor %}"
"<|im_end|>\n"
"{% endif %}"
"{% endfor %}"
"{% if add_generation_prompt %}"
"<|im_start|>assistant\n"
"{% endif %}")