qianchu commited on
Commit
51084af
·
verified ·
1 Parent(s): 3f77e0a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +190 -3
README.md CHANGED
@@ -1,3 +1,190 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ pipeline_tag: image-text-to-text
6
+ tags:
7
+ - multimodal
8
+ library_name: transformers
9
+ base_model:
10
+ - Qwen/Qwen2.5-VL-7B-Instruct
11
+ ---
12
+
13
+
14
+ ## Introduction
15
+
16
+ X-Reasoner, a vision-language model posttrained solely on general-domain text for generalizable reasoning, using a twostage approach: an initial supervised fine-tuning phase with distilled long chainof-thoughts, followed by reinforcement learning with verifiable rewards. Experiments show that X-Reasoner successfully transfers reasoning capabilities to both multimodal and out-of-domain settings, outperforming existing state-of-theart models trained with in-domain and multimodal data across various general and medical benchmarks. More details can be found in the paper: https://arxiv.org/abs/2505.03981
17
+
18
+ ## Requirements
19
+ We recommend installing the transformers version used in our experiments and other dependencies with this command:
20
+ ```
21
+ pip install transformers==4.57.1 accelerate==1.12.0 torchvision==0.24.1 qwen-vl-utils==0.0.14
22
+ ```
23
+
24
+ ## Quickstart
25
+
26
+ Below, we provide a some examples to show how to use X-Reasoner with 🤗 Transformers or vLLM.
27
+
28
+ <details>
29
+ <summary>Inference with HF Transformers 🤗</summary>
30
+ Here we show a code snippet to show you how chat with X-Reasoner using `transformers` and `qwen_vl_utils`:
31
+
32
+ ```python
33
+ import torch
34
+ from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
35
+ from qwen_vl_utils import process_vision_info
36
+
37
+ # default: Load the model on the available device(s)
38
+ model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
39
+ "microsoft/X-Reasoner-7B", dtype=torch.bfloat16, device_map="auto"
40
+ )
41
+
42
+ # We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
43
+ # model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
44
+ # "microsoft/X-Reasoner",
45
+ # dtype=torch.bfloat16,
46
+ # attn_implementation="flash_attention_2",
47
+ # device_map="auto",
48
+ # )
49
+
50
+ # You can set min_pixels and max_pixels according to your needs.
51
+ min_pixels = 262144
52
+ max_pixels = 262144
53
+ processor = AutoProcessor.from_pretrained("microsoft/X-Reasoner-7B", min_pixels=min_pixels, max_pixels=max_pixels)
54
+
55
+ # Multiple Choice Query
56
+ messages = [
57
+ {
58
+ "role": "user",
59
+ "content": [
60
+
61
+ {"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:"},
62
+ {
63
+ "type": "image",
64
+ "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
65
+ },
66
+ {"type": "text", "text": "Is there a dog in the image? A. Yes B. No"},
67
+ ],
68
+ }
69
+ ]
70
+
71
+ # Preparation for inference
72
+ text = processor.apply_chat_template(
73
+ messages, tokenize=False, add_generation_prompt=True
74
+ )
75
+ image_inputs, video_inputs = process_vision_info(messages)
76
+ inputs = processor(
77
+ text=[text],
78
+ images=image_inputs,
79
+ videos=video_inputs,
80
+ padding=True,
81
+ return_tensors="pt",
82
+ )
83
+
84
+
85
+ inputs = inputs.to(device="cuda")
86
+
87
+ # Inference: Generation of the output
88
+ generated_ids = model.generate(**inputs, max_new_tokens=4000)
89
+ generated_ids_trimmed = [
90
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
91
+ ]
92
+ output_text = processor.batch_decode(
93
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
94
+ )
95
+ print(output_text)
96
+
97
+ ```
98
+ </details>
99
+
100
+ <details>
101
+ <summary>Inference with vLLM</summary>
102
+
103
+ Here we show an example of how to use X-Reasoner-7B with vLLM (tested with vLLM==0.11.2 and transformers==4.57.1):
104
+
105
+ ```python
106
+ from vllm import LLM, SamplingParams
107
+ from transformers import AutoProcessor
108
+
109
+ min_pixels = 262144
110
+ max_pixels = 262144
111
+ processor = AutoProcessor.from_pretrained("microsoft/X-Reasoner-7B", min_pixels=min_pixels, max_pixels=max_pixels)
112
+
113
+ llm = LLM(
114
+ model="microsoft/X-Reasoner-7B",
115
+ trust_remote_code=True,
116
+ dtype="bfloat16",
117
+ max_model_len=8192,
118
+ tensor_parallel_size=4,
119
+ gpu_memory_utilization=0.8,
120
+ limit_mm_per_prompt={"image": 1}
121
+ )
122
+
123
+ # Set up sampling parameters
124
+ sampling_params = SamplingParams(
125
+ temperature=0.6,
126
+ max_tokens=4000,
127
+ )
128
+
129
+ image_data = []
130
+
131
+
132
+
133
+ # Multiple Choice Query
134
+ image_data = ['https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg']
135
+ messages = [
136
+ {
137
+ "role": "user",
138
+ "content": [
139
+ {
140
+ "type": "image",
141
+ "image": image_data[0],
142
+ },
143
+ {"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"},
144
+ ],
145
+ }
146
+ ]
147
+
148
+ prompt = processor.apply_chat_template(
149
+ messages, tokenize=False, add_generation_prompt=True)
150
+
151
+ if image_data:
152
+ mm_prompt = {
153
+ "prompt": prompt,
154
+ "multi_modal_data": {"image": image_data}
155
+ }
156
+ else:
157
+ mm_prompt = {"prompt": prompt}
158
+
159
+ # Generate response
160
+ outputs = llm.generate([mm_prompt], sampling_params)
161
+
162
+ # Print the generated response
163
+ for output in outputs:
164
+ prompt = output.prompt
165
+ generated_text = output.outputs[0].text
166
+ print(f"Prompt: {prompt}")
167
+ print(f"Generated text: {generated_text}")
168
+ print("-" * 50)
169
+ ```
170
+ </details>
171
+
172
+
173
+ ### Known Issues
174
+ * In case the model generates non-stopping reasoning trace, we add `</think>` as a stop token to the assistant output and re-run to generate the final answer.
175
+
176
+ ## Citation
177
+
178
+ If you find our work helpful, feel free to give us a cite.
179
+
180
+ ```
181
+ @misc{liu2025xreasonergeneralizablereasoningmodalities,
182
+ title={X-Reasoner: Towards Generalizable Reasoning Across Modalities and Domains},
183
+ 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},
184
+ year={2025},
185
+ eprint={2505.03981},
186
+ archivePrefix={arXiv},
187
+ primaryClass={cs.AI},
188
+ url={https://arxiv.org/abs/2505.03981},
189
+ }
190
+ ```