Skip to content

Commit 675aa2e

Browse files
Isotr0pyywang96
andauthored
[Model] Upstream Deepseek-OCR model (#27247)
Signed-off-by: Isotr0py <mozf@mail2.sysu.edu.cn> Signed-off-by: Roger Wang <hey@rogerw.io> Co-authored-by: Roger Wang <hey@rogerw.io>
1 parent 3ae082c commit 675aa2e

10 files changed

Lines changed: 1821 additions & 40 deletions

File tree

‎docs/models/supported_models.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ These models primarily accept the [`LLM.generate`](./generative_models.md#llmgen
639639
| `ChameleonForConditionalGeneration` | Chameleon | T + I | `facebook/chameleon-7b`, etc. | | ✅︎ |
640640
| `Cohere2VisionForConditionalGeneration` | Command A Vision | T + I<sup>+</sup> | `CohereLabs/command-a-vision-07-2025`, etc. | | ✅︎ |
641641
| `DeepseekVLV2ForCausalLM`<sup>^</sup> | DeepSeek-VL2 | T + I<sup>+</sup> | `deepseek-ai/deepseek-vl2-tiny`, `deepseek-ai/deepseek-vl2-small`, `deepseek-ai/deepseek-vl2`, etc. | | ✅︎ |
642+
| `DeepseekOCRForCausalLM` | DeepSeek-OCR | T + I<sup>+</sup> | `deepseek-ai/DeepSeek-OCR`, etc. | | ✅︎ |
642643
| `Ernie4_5_VLMoeForConditionalGeneration` | Ernie4.5-VL | T + I<sup>+</sup>/ V<sup>+</sup> | `baidu/ERNIE-4.5-VL-28B-A3B-PT`, `baidu/ERNIE-4.5-VL-424B-A47B-PT` | | ✅︎ |
643644
| `FuyuForCausalLM` | Fuyu | T + I | `adept/fuyu-8b`, etc. | | ✅︎ |
644645
| `Gemma3nForConditionalGeneration` | Gemma 3n | T + I + A | `google/gemma-3n-E2B-it`, `google/gemma-3n-E4B-it`, etc. | | |

‎examples/offline_inference/vision_language.py‎

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ModelRequestData(NamedTuple):
3030
prompts: list[str]
3131
stop_token_ids: list[int] | None = None
3232
lora_requests: list[LoRARequest] | None = None
33+
sampling_params: list[SamplingParams] | None = None
3334

3435

3536
# NOTE: The default `max_num_seqs` and `max_model_len` may result in OOM on
@@ -153,23 +154,6 @@ def run_chameleon(questions: list[str], modality: str) -> ModelRequestData:
153154
)
154155

155156

156-
# Dots-OCR
157-
def run_dots_ocr(questions: list[str], modality: str) -> ModelRequestData:
158-
assert modality == "image"
159-
160-
prompts = [f"<|img|><|imgpad|><|endofimg|>{question}" for question in questions]
161-
engine_args = EngineArgs(
162-
model="rednote-hilab/dots.ocr",
163-
limit_mm_per_prompt={modality: 1},
164-
trust_remote_code=True,
165-
)
166-
167-
return ModelRequestData(
168-
engine_args=engine_args,
169-
prompts=prompts,
170-
)
171-
172-
173157
def run_command_a_vision(questions: list[str], modality: str) -> ModelRequestData:
174158
assert modality == "image"
175159

@@ -217,6 +201,66 @@ def run_deepseek_vl2(questions: list[str], modality: str) -> ModelRequestData:
217201
)
218202

219203

204+
def run_deepseek_ocr(questions: list[str], modality: str) -> ModelRequestData:
205+
from vllm.model_executor.models.deepseek_ocr import NGramPerReqLogitsProcessor
206+
207+
assert modality == "image"
208+
209+
model_name = "deepseek-ai/DeepSeek-OCR"
210+
211+
engine_args = EngineArgs(
212+
model=model_name,
213+
limit_mm_per_prompt={modality: 1},
214+
logits_processors=[NGramPerReqLogitsProcessor],
215+
)
216+
217+
# deepseek-ocr use plain prompt template
218+
prompts = [f"<image>\n{question}" for question in questions]
219+
220+
# The following sampling params config is taken from
221+
# the official Deepseek-OCR inference example.
222+
# (IMPORTANT) Use the custom logits processor and avoid skipping
223+
# special tokens for this model for the optimal OCR performance.
224+
sampling_params = [
225+
SamplingParams(
226+
temperature=0.0,
227+
max_tokens=8192,
228+
# ngram logit processor args
229+
extra_args=dict(
230+
ngram_size=30,
231+
window_size=90,
232+
# whitelist: <td>, </td>
233+
whitelist_token_ids={128821, 128822},
234+
),
235+
skip_special_tokens=False,
236+
)
237+
for _ in questions
238+
]
239+
240+
return ModelRequestData(
241+
engine_args=engine_args,
242+
prompts=prompts,
243+
sampling_params=sampling_params,
244+
)
245+
246+
247+
# Dots-OCR
248+
def run_dots_ocr(questions: list[str], modality: str) -> ModelRequestData:
249+
assert modality == "image"
250+
251+
prompts = [f"<|img|><|imgpad|><|endofimg|>{question}" for question in questions]
252+
engine_args = EngineArgs(
253+
model="rednote-hilab/dots.ocr",
254+
limit_mm_per_prompt={modality: 1},
255+
trust_remote_code=True,
256+
)
257+
258+
return ModelRequestData(
259+
engine_args=engine_args,
260+
prompts=prompts,
261+
)
262+
263+
220264
# Ernie4.5-VL
221265
def run_ernie45_vl(questions: list[str], modality: str) -> ModelRequestData:
222266
model_name = "baidu/ERNIE-4.5-VL-28B-A3B-PT"
@@ -1738,9 +1782,10 @@ def run_tarsier2(questions: list[str], modality: str) -> ModelRequestData:
17381782
"bee": run_bee,
17391783
"blip-2": run_blip2,
17401784
"chameleon": run_chameleon,
1741-
"dots_ocr": run_dots_ocr,
17421785
"command_a_vision": run_command_a_vision,
17431786
"deepseek_vl_v2": run_deepseek_vl2,
1787+
"deepseek_ocr": run_deepseek_ocr,
1788+
"dots_ocr": run_dots_ocr,
17441789
"ernie45_vl": run_ernie45_vl,
17451790
"fuyu": run_fuyu,
17461791
"gemma3": run_gemma3,
@@ -2003,8 +2048,12 @@ def main(args):
20032048

20042049
# We set temperature to 0.2 so that outputs can be different
20052050
# even when all prompts are identical when running batch inference.
2006-
sampling_params = SamplingParams(
2007-
temperature=0.2, max_tokens=64, stop_token_ids=req_data.stop_token_ids
2051+
sampling_params = (
2052+
SamplingParams(
2053+
temperature=0.2, max_tokens=64, stop_token_ids=req_data.stop_token_ids
2054+
)
2055+
if req_data.sampling_params is None
2056+
else req_data.sampling_params
20082057
)
20092058

20102059
assert args.num_prompts > 0

‎tests/models/registry.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ def check_available_online(
585585
transformers_version_reason="HF model is not compatible.",
586586
hf_overrides={"architectures": ["DeepseekVLV2ForCausalLM"]},
587587
),
588+
"DeepseekOCRForCausalLM": _HfExamplesInfo(
589+
"deepseek-ai/DeepSeek-OCR",
590+
),
588591
"DotsOCRForCausalLM": _HfExamplesInfo(
589592
"rednote-hilab/dots.ocr", trust_remote_code=True
590593
),

0 commit comments

Comments
 (0)