Views
No views yet
pip install transformers==4.57.1 accelerate==1.12.0 torchvision==0.24.1 qwen-vl-utils==0.0.141import torch
2from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
3from qwen_vl_utils import process_vision_info
4
5# default: Load the model on the available device(s)
6model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
7 "microsoft/X-Reasoner-7B", dtype=torch.bfloat16, device_map="auto"
8)
9
10# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
11# model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
12# "microsoft/X-Reasoner",
13# dtype=torch.bfloat16,
14# attn_implementation="flash_attention_2",
15# device_map="auto",
16# )
17
18# You can set min_pixels and max_pixels according to your needs.
19min_pixels = 262144
20max_pixels = 262144
21processor = AutoProcessor.from_pretrained("microsoft/X-Reasoner-7B", min_pixels=min_pixels, max_pixels=max_pixels)
22
23# Multiple Choice Query
24messages = [
25 {
26 "role": "user",
27 "content": [
28
29 {"type": "text", "text": "You should provide your thoughts within <think> </think> tags, then answer with just one of the options below within <answer> </answer> tags (For example, if the question is \n'Is the earth flat?\n A: Yes \nB: No', you should answer with <think>...</think> <answer>B: No</answer>). \nHere is the question:"},
30 {
31 "type": "image",
32 "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
33 },
34 {"type": "text", "text": "Is there a dog in the image? A. Yes B. No"},
35 ],
36 }
37]
38
39# Preparation for inference
40text = processor.apply_chat_template(
41 messages, tokenize=False, add_generation_prompt=True
42)
43image_inputs, video_inputs = process_vision_info(messages)
44inputs = processor(
45 text=[text],
46 images=image_inputs,
47 videos=video_inputs,
48 padding=True,
49 return_tensors="pt",
50)
51
52
53inputs = inputs.to(device="cuda")
54
55# Inference: Generation of the output
56generated_ids = model.generate(**inputs, max_new_tokens=4000)
57generated_ids_trimmed = [
58 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
59]
60output_text = processor.batch_decode(
61 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
62)
63print(output_text)
641from vllm import LLM, SamplingParams
2from transformers import AutoProcessor
3
4min_pixels = 262144
5max_pixels = 262144
6processor = AutoProcessor.from_pretrained("microsoft/X-Reasoner-7B", min_pixels=min_pixels, max_pixels=max_pixels)
7
8llm = LLM(
9 model="microsoft/X-Reasoner-7B",
10 trust_remote_code=True,
11 dtype="bfloat16",
12 max_model_len=8192,
13 tensor_parallel_size=4,
14 gpu_memory_utilization=0.8,
15 limit_mm_per_prompt={"image": 1}
16)
17
18# Set up sampling parameters
19sampling_params = SamplingParams(
20 temperature=0.6,
21 max_tokens=4000,
22)
23
24image_data = []
25
26
27
28# Multiple Choice Query
29image_data = ['https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg']
30messages = [
31 {
32 "role": "user",
33 "content": [
34 {
35 "type": "image",
36 "image": image_data[0],
37 },
38 {"type": "text", "text": "You should provide your thoughts within <think> </think> tags, then answer with just one of the options below within <answer> </answer> tags (For example, if the question is \n'Is the earth flat?\n A: Yes \nB: No', you should answer with <think>...</think> <answer>B: No</answer>). \nHere is the question: Is there a dog in the picture? A: Yes B: No"},
39 ],
40 }
41]
42
43prompt = processor.apply_chat_template(
44 messages, tokenize=False, add_generation_prompt=True)
45
46if image_data:
47 mm_prompt = {
48 "prompt": prompt,
49 "multi_modal_data": {"image": image_data}
50 }
51else:
52 mm_prompt = {"prompt": prompt}
53
54# Generate response
55outputs = llm.generate([mm_prompt], sampling_params)
56
57# Print the generated response
58for output in outputs:
59 prompt = output.prompt
60 generated_text = output.outputs[0].text
61 print(f"Prompt: {prompt}")
62 print(f"Generated text: {generated_text}")
63 print("-" * 50)</think> as a stop token to the assistant output and re-run to generate the final answer.@misc{liu2025xreasonergeneralizablereasoningmodalities,
title={X-Reasoner: Towards Generalizable Reasoning Across Modalities and Domains},
author={Qianchu Liu and Sheng Zhang and Guanghui Qin and Timothy Ossowski and Yu Gu and Ying Jin and Sid Kiblawi and Sam Preston and Mu Wei and Paul Vozila and Tristan Naumann and Hoifung Poon},
year={2025},
eprint={2505.03981},
archivePrefix={arXiv},
primaryClass={cs.AI},
url={https://arxiv.org/abs/2505.03981},
}