Views
No views yet




1pip install torch==2.7.1 transformers==4.57.0 numpy==1.25.0 pillow==10.3.0 moviepy==1.0.3 accelerate==1.12.0
2pip install --no-build-isolation --no-cache-dir flash-attn==2.8.31import torch
2import requests
3from PIL import Image
4from transformers import AutoModelForCausalLM
5
6# Thinking mode & budget
7enable_thinking = True
8enable_thinking_budget = True # Only effective if enable_thinking is True.
9
10# Total tokens for thinking + answer. Ensure: max_new_tokens > thinking_budget + 25
11max_new_tokens = 2048
12thinking_budget = 1024
13
14model = AutoModelForCausalLM.from_pretrained(
15 "AIDC-AI/Ovis2.6-30B-A3B",
16 torch_dtype=torch.bfloat16,
17 trust_remote_code=True,
18 device_map="auto"
19)
20
21messages = [{
22 "role": "user",
23 "content": [
24 {"type": "image", "image": Image.open(requests.get("https://cdn-uploads.huggingface.co/production/uploads/658a8a837959448ef5500ce5/TIlymOb86R6_Mez3bpmcB.png", stream=True).raw)},
25 {"type": "text", "text": "Calculate the sum of the numbers in the middle box in figure (c)."},
26 ],
27}]
28
29input_ids, pixel_values, grid_thws = model.preprocess_inputs(
30 messages=messages,
31 add_generation_prompt=True,
32 enable_thinking=enable_thinking
33)
34input_ids = input_ids.cuda()
35pixel_values = pixel_values.cuda() if pixel_values is not None else None
36grid_thws = grid_thws.cuda() if grid_thws is not None else None
37
38outputs = model.generate(
39 inputs=input_ids,
40 pixel_values=pixel_values,
41 grid_thws=grid_thws,
42 enable_thinking=enable_thinking,
43 enable_thinking_budget=enable_thinking_budget,
44 max_new_tokens=max_new_tokens,
45 thinking_budget=thinking_budget,
46)
47
48response = model.text_tokenizer.decode(outputs[0], skip_special_tokens=True)
49print(response)End your response with 'Final answer: '.Calculate the sum of the numbers in the middle box in figure (c).
End your response with 'Final answer: '.generate method and the default TextIteratorStreamer is now incompatible. If you need to stream model output, be sure to use the helper class below.1# --- Budget-aware streamer helper ---
2from transformers import TextIteratorStreamer
3
4class BudgetAwareTextStreamer(TextIteratorStreamer):
5 """A streamer compatible with Ovis two-phase generation.
6
7 Call .manual_end() after generation to flush any remaining text.
8 """
9 def manual_end(self):
10 if len(self.token_cache) > 0:
11 text = self.tokenizer.decode(self.token_cache, **self.decode_kwargs)
12 printable_text = text[self.print_len:]
13 self.token_cache = []
14 self.print_len = 0
15 else:
16 printable_text = ""
17 self.next_tokens_are_prompt = True
18 self.on_finalized_text(printable_text, stream_end=True)
19
20 # Disable base class's end hook; we'll finalize via manual_end()
21 def end(self):
22 pass1streamer = BudgetAwareTextStreamer(
2 model.text_tokenizer,
3 skip_prompt=True,
4 skip_special_tokens=True
5)
6
7outputs = model.generate(
8 inputs=input_ids,
9 pixel_values=pixel_values,
10 grid_thws=grid_thws,
11 enable_thinking=enable_thinking,
12 enable_thinking_budget=enable_thinking_budget,
13 max_new_tokens=max_new_tokens,
14 thinking_budget=thinking_budget,
15 streamer=streamer
16)
171# Multi-image inference
2multi_image_files = [
3 "/path/to/image_1.jpg",
4 "/path/to/image_2.jpg",
5 "/path/to/image_3.jpg",
6]
7
8content = [{"type": "image", "image": Image.open(p).convert("RGB")} for p in multi_image_files]
9content.append({"type": "text", "text": "Describe the images."})
10messages = [{"role": "user", "content": content}]
11
12input_ids, pixel_values, grid_thws = model.preprocess_inputs(messages=messages, add_generation_prompt=True, max_pixels=896*896)
13input_ids = input_ids.cuda()
14pixel_values = pixel_values.cuda().to(model.dtype) if pixel_values is not None else None
15grid_thws = grid_thws.cuda() if grid_thws is not None else None
16
17with torch.no_grad():
18 outputs = model.generate(inputs=input_ids, pixel_values=pixel_values, grid_thws=grid_thws,
19 max_new_tokens=1024, do_sample=True,
20 eos_token_id=model.text_tokenizer.eos_token_id,
21 pad_token_id=model.text_tokenizer.pad_token_id)
22print(model.text_tokenizer.decode(outputs[0], skip_special_tokens=True))1# Video inference
2from moviepy.editor import VideoFileClip # pip install moviepy==1.0.3
3
4video_file = "/path/to/video_1.mp4"
5num_frames = 8
6
7with VideoFileClip(video_file) as clip:
8 total_frames = int(clip.fps * clip.duration)
9 indices = [int(i * total_frames / num_frames) for i in range(num_frames)]
10 frames = [Image.fromarray(clip.get_frame(t)) for t in (idx / clip.fps for idx in indices)]
11
12messages = [{"role": "user", "content": [
13 {"type": "video", "video": frames},
14 {"type": "text", "text": "Describe this video in detail."},
15]}]
16
17input_ids, pixel_values, grid_thws = model.preprocess_inputs(messages=messages, add_generation_prompt=True, max_pixels=896*896)
18input_ids = input_ids.cuda()
19pixel_values = pixel_values.cuda().to(model.dtype) if pixel_values is not None else None
20grid_thws = grid_thws.cuda() if grid_thws is not None else None
21
22with torch.no_grad():
23 outputs = model.generate(inputs=input_ids, pixel_values=pixel_values, grid_thws=grid_thws,
24 max_new_tokens=1024, do_sample=True,
25 eos_token_id=model.text_tokenizer.eos_token_id,
26 pad_token_id=model.text_tokenizer.pad_token_id)
27print(model.text_tokenizer.decode(outputs[0], skip_special_tokens=True))1# Text-only inference
2messages = [{"role": "user", "content": "Hi, please introduce Yellow Mountain."}]
3
4input_ids, _, _ = model.preprocess_inputs(messages=messages, add_generation_prompt=True)
5input_ids = input_ids.cuda()
6
7with torch.no_grad():
8 outputs = model.generate(inputs=input_ids, max_new_tokens=1024, do_sample=True,
9 eos_token_id=model.text_tokenizer.eos_token_id,
10 pad_token_id=model.text_tokenizer.pad_token_id)
11print(model.text_tokenizer.decode(outputs[0], skip_special_tokens=True))Please provide the bounding box coordinates. (for boxes) or Please provide the point coordinates. (for points). To target a specific object, wrap its description in <ref> tags, e.g.:Find the <ref>red apple</ref> in the image. Please provide the bounding box coordinates.[0,1) with the origin (0,0) at the top-left corner of the image.<point>(x,y)</point><box>(x1,y1),(x2,y2)</box> where (x1,y1) is top-left, (x2,y2) is bottom-right.[<box>(...)</box>,<box>(...)</box> ]1The image features a serene scene with <ref>three birds</ref>[
2 <box>(0.401,0.526),(0.430,0.557)</box>,
3 <box>(0.489,0.494),(0.516,0.526)</box>,
4 <box>(0.296,0.529),(0.324,0.576)</box>
5] flying in formation against a clear blue sky.1@article{lu2025ovis25technicalreport,
2 title={Ovis2.5 Technical Report},
3 author={Shiyin Lu and Yang Li and Yu Xia and Yuwei Hu and Shanshan Zhao and Yanqing Ma and Zhichao Wei and Yinglun Li and Lunhao Duan and Jianshan Zhao and Yuxuan Han and Haijun Li and Wanying Chen and Junke Tang and Chengkun Hou and Zhixing Du and Tianli Zhou and Wenjie Zhang and Huping Ding and Jiahe Li and Wen Li and Gui Hu and Yiliang Gu and Siran Yang and Jiamang Wang and Hailong Sun and Yibo Wang and Hui Sun and Jinlong Huang and Yuping He and Shengze Shi and Weihong Zhang and Guodong Zheng and Junpeng Jiang and Sensen Gao and Yi-Feng Wu and Sijia Chen and Yuhui Chen and Qing-Guo Chen and Zhao Xu and Weihua Luo and Kaifu Zhang},
4 year={2025},
5 journal={arXiv:2508.11737}
6}
7
8@article{lu2024ovis,
9 title={Ovis: Structural Embedding Alignment for Multimodal Large Language Model},
10 author={Shiyin Lu and Yang Li and Qing-Guo Chen and Zhao Xu and Weihua Luo and Kaifu Zhang and Han-Jia Ye},
11 year={2024},
12 journal={arXiv:2405.20797}
13}