Views
No views yet



pip install git+https://github.com/huggingface/transformers
# pip install transformers==4.57.0 # currently, V4.57.0 is not releasedtransformers:1from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
2
3# default: Load the model on the available device(s)
4model = Qwen3VLForConditionalGeneration.from_pretrained(
5 "Qwen/Qwen3-VL-4B-Thinking", dtype="auto", device_map="auto"
6)
7
8# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
9# model = Qwen3VLForConditionalGeneration.from_pretrained(
10# "Qwen/Qwen3-VL-4B-Thinking",
11# dtype=torch.bfloat16,
12# attn_implementation="flash_attention_2",
13# device_map="auto",
14# )
15
16processor = AutoProcessor.from_pretrained("Qwen/Qwen/Qwen3-VL-4B-Thinking")
17
18messages = [
19 {
20 "role": "user",
21 "content": [
22 {
23 "type": "image",
24 "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
25 },
26 {"type": "text", "text": "Describe this image."},
27 ],
28 }
29]
30
31# Preparation for inference
32inputs = processor.apply_chat_template(
33 messages,
34 tokenize=True,
35 add_generation_prompt=True,
36 return_dict=True,
37 return_tensors="pt"
38)
39
40# Inference: Generation of the output
41generated_ids = model.generate(**inputs, max_new_tokens=128)
42generated_ids_trimmed = [
43 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
44]
45output_text = processor.batch_decode(
46 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
47)
48print(output_text)@misc{qwen3technicalreport,
title={Qwen3 Technical Report},
author={Qwen Team},
year={2025},
eprint={2505.09388},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2505.09388},
}
@article{Qwen2.5-VL,
title={Qwen2.5-VL Technical Report},
author={Bai, Shuai and Chen, Keqin and Liu, Xuejing and Wang, Jialin and Ge, Wenbin and Song, Sibo and Dang, Kai and Wang, Peng and Wang, Shijie and Tang, Jun and Zhong, Humen and Zhu, Yuanzhi and Yang, Mingkun and Li, Zhaohai and Wan, Jianqiang and Wang, Pengfei and Ding, Wei and Fu, Zheren and Xu, Yiheng and Ye, Jiabo and Zhang, Xi and Xie, Tianbao and Cheng, Zesen and Zhang, Hang and Yang, Zhibo and Xu, Haiyang and Lin, Junyang},
journal={arXiv preprint arXiv:2502.13923},
year={2025}
}
@article{Qwen2VL,
title={Qwen2-VL: Enhancing Vision-Language Model's Perception of the World at Any Resolution},
author={Wang, Peng and Bai, Shuai and Tan, Sinan and Wang, Shijie and Fan, Zhihao and Bai, Jinze and Chen, Keqin and Liu, Xuejing and Wang, Jialin and Ge, Wenbin and Fan, Yang and Dang, Kai and Du, Mengfei and Ren, Xuancheng and Men, Rui and Liu, Dayiheng and Zhou, Chang and Zhou, Jingren and Lin, Junyang},
journal={arXiv preprint arXiv:2409.12191},
year={2024}
}
@article{Qwen-VL,
title={Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond},
author={Bai, Jinze and Bai, Shuai and Yang, Shusheng and Wang, Shijie and Tan, Sinan and Wang, Peng and Lin, Junyang and Zhou, Chang and Zhou, Jingren},
journal={arXiv preprint arXiv:2308.12966},
year={2023}
}