Views
No views yet
1from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
2from qwen_vl_utils import process_vision_info
3import torch
4
5# # default: Load the model on the available device(s)
6# model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
7# "", torch_dtype="auto", device_map="auto"
8# ) # replace with any of the ViGoRL models
9
10# We recommend enabling flash_attention_2 for better acceleration and memory saving.
11model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
12 "",
13 torch_dtype=torch.bfloat16,
14 attn_implementation="flash_attention_2",
15 device_map="auto",
16)
17
18# default processer
19processor = AutoProcessor.from_pretrained("")
20
21# The default range for the number of visual tokens per image in the model is 4-16384.
22# You can set min_pixels and max_pixels according to your needs, such as a token range of 256-1280, to balance performance and cost.
23# min_pixels = 256*28*28
24# max_pixels = 1280*28*28
25# processor = AutoProcessor.from_pretrained("", min_pixels=min_pixels, max_pixels=max_pixels)
26
27messages = [
28 {
29 "role": "user",
30 "content": [
31 {
32 "type": "image",
33 "image": "path/to/image.png",
34 },
35 {"type": "text", "text": "QUERY HERE"},
36 ],
37 }
38]
39
40# Preparation for inference
41text = processor.apply_chat_template(
42 messages, tokenize=False, add_generation_prompt=True
43)
44image_inputs, video_inputs = process_vision_info(messages)
45inputs = processor(
46 text=[text],
47 images=image_inputs,
48 videos=video_inputs,
49 padding=True,
50 return_tensors="pt",
51)
52inputs = inputs.to("cuda")
53
54# Inference: Generation of the output
55generated_ids = model.generate(**inputs, max_new_tokens=512)
56generated_ids_trimmed = [
57 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
58]
59output_text = processor.batch_decode(
60 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
61)
62print(output_text) # this will output a single tool call turn of the model if version is multiturn.1@article{sarch2025vigorl,
2 title={Grounded Reinforcement Learning for Visual Reasoning},
3 author={Sarch, Gabriel and Saha, Snigdha and Khandelwal, Naitik and Jain, Ayush and Tarr, Michael J and Kumar, Aviral and Fragkiadaki, Katerina},
4 year={2025}
5}