TryingHard commited on
Commit
43697be
·
verified ·
1 Parent(s): ce7de0b

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +48 -6
README.md CHANGED
@@ -39,7 +39,7 @@ Building on these advances, **Ovis2.5-9B** achieves an average score of 78.3 on
39
 
40
  **Key Features**
41
  * **Native-Resolution Perception** — NaViT vision encoder preserves fine details and global structure without lossy tiling.
42
- * **Deep-Reasoning Capability** — Optional *thinking mode* for self-checking and revision beyond linear CoT.
43
  * **Chart & Document OCR** — State-of-the-art at its scale for complex chart analysis, document understanding (including tables and forms), and OCR.
44
  * **Broad Task Coverage** — Demonstrates leading performance on image reasoning, video understanding, and grounding benchmarks, showcasing strong general multimodal capability.
45
 
@@ -55,15 +55,51 @@ First, install the required dependencies:
55
  pip install torch==2.4.0 transformers==4.51.3 numpy==1.25.0 pillow==10.3.0 moviepy==1.0.3
56
  pip install flash-attn==2.7.0.post2 --no-build-isolation
57
  ```
58
- Then, run the following code:
59
  ```python
60
  import torch
61
  import requests
62
  from PIL import Image
63
  from transformers import AutoModelForCausalLM
64
 
65
- MODEL_PATH = "AIDC-AI/Ovis2.5-2B"
66
- THINKING = True # Controls whether to enable thinking mode
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  model = AutoModelForCausalLM.from_pretrained(
69
  MODEL_PATH,
@@ -71,6 +107,8 @@ model = AutoModelForCausalLM.from_pretrained(
71
  trust_remote_code=True
72
  ).cuda()
73
 
 
 
74
  messages = [{
75
  "role": "user",
76
  "content": [
@@ -82,7 +120,7 @@ messages = [{
82
  input_ids, pixel_values, grid_thws = model.preprocess_inputs(
83
  messages=messages,
84
  add_generation_prompt=True,
85
- enable_thinking=THINKING
86
  )
87
  input_ids = input_ids.cuda()
88
  pixel_values = pixel_values.cuda() if pixel_values is not None else None
@@ -92,7 +130,11 @@ outputs = model.generate(
92
  inputs=input_ids,
93
  pixel_values=pixel_values,
94
  grid_thws=grid_thws,
95
- max_new_tokens=3072
 
 
 
 
96
  )
97
 
98
  response = model.text_tokenizer.decode(outputs[0], skip_special_tokens=True)
 
39
 
40
  **Key Features**
41
  * **Native-Resolution Perception** — NaViT vision encoder preserves fine details and global structure without lossy tiling.
42
+ * **Deep-Reasoning Capability** — Optional *thinking mode* for self-checking and revision beyond linear CoT. *Thinking budget* supported.
43
  * **Chart & Document OCR** — State-of-the-art at its scale for complex chart analysis, document understanding (including tables and forms), and OCR.
44
  * **Broad Task Coverage** — Demonstrates leading performance on image reasoning, video understanding, and grounding benchmarks, showcasing strong general multimodal capability.
45
 
 
55
  pip install torch==2.4.0 transformers==4.51.3 numpy==1.25.0 pillow==10.3.0 moviepy==1.0.3
56
  pip install flash-attn==2.7.0.post2 --no-build-isolation
57
  ```
58
+ Then, run the following code. The thinking and thinking budget logic can be applied in the same way for multi-image, video and pure text scenarios.
59
  ```python
60
  import torch
61
  import requests
62
  from PIL import Image
63
  from transformers import AutoModelForCausalLM
64
 
65
+ MODEL_PATH = "AIDC-AI/Ovis2.5-9B"
66
+ # Controls whether to enable thinking mode.
67
+ enable_thinking = True
68
+ # NOTE: The thinking budget mechanism is effective only when
69
+ # enable_thinking and enable_thinking_budget are both True.
70
+ # Setting enable_thinking=True and enable_thinking_budget=False
71
+ # enables thinking without budget. In such case the model might
72
+ # spend a lot of tokens in the thinking phase and could be slow.
73
+ enable_thinking_budget = True
74
+ # max_new_tokens is the upper limit for thinking and non-thinking tokens combined.
75
+ # MUST ensure that max_new_tokens > thinking_budget + 25
76
+ # when using the thinking budget mechanism.
77
+ max_new_tokens = 3072
78
+ thinking_budget = 2048
79
+
80
+ # The implementation of thinking budget involves two-phase generation,
81
+ # which is incompatible with the official transformers TextIteratorStreamer.
82
+ # Hence we modified the streaming class. Could comment this part out if
83
+ # not using thinking budget. See the commented lines below that involve
84
+ # "streamer" for usage.
85
+ from transformers import TextIteratorStreamer
86
+ class MyTextIteratorStreamer(TextIteratorStreamer):
87
+ def manual_end(self):
88
+ """Flushes any remaining cache and prints a newline to stdout."""
89
+ # Flush the cache, if it exists
90
+ if len(self.token_cache) > 0:
91
+ text = self.tokenizer.decode(self.token_cache, **self.decode_kwargs)
92
+ printable_text = text[self.print_len :]
93
+ self.token_cache = []
94
+ self.print_len = 0
95
+ else:
96
+ printable_text = ""
97
+
98
+ self.next_tokens_are_prompt = True
99
+ self.on_finalized_text(printable_text, stream_end=True)
100
+
101
+ def end(self):
102
+ pass
103
 
104
  model = AutoModelForCausalLM.from_pretrained(
105
  MODEL_PATH,
 
107
  trust_remote_code=True
108
  ).cuda()
109
 
110
+ # streamer = MyTextIteratorStreamer(model.text_tokenizer, skip_prompt=True, skip_special_tokens=True)
111
+
112
  messages = [{
113
  "role": "user",
114
  "content": [
 
120
  input_ids, pixel_values, grid_thws = model.preprocess_inputs(
121
  messages=messages,
122
  add_generation_prompt=True,
123
+ enable_thinking=enable_thinking
124
  )
125
  input_ids = input_ids.cuda()
126
  pixel_values = pixel_values.cuda() if pixel_values is not None else None
 
130
  inputs=input_ids,
131
  pixel_values=pixel_values,
132
  grid_thws=grid_thws,
133
+ enable_thinking=enable_thinking,
134
+ enable_thinking_budget=enable_thinking_budget,
135
+ max_new_tokens=max_new_tokens,
136
+ thinking_budget=thinking_budget,
137
+ # streamer=streamer
138
  )
139
 
140
  response = model.text_tokenizer.decode(outputs[0], skip_special_tokens=True)