DeepSeek-OCR-2 ONNX
ONNX builds of DeepSeek-OCR-2, made by Moonbox for on-device use with ONNX Runtime (CPU, CUDA, DirectML). Each
precision is one folder holding the graphs and a small Python pipeline.py that does the rest (image preprocessing,
prompt, positions, decoding loop); what the graphs take and return is under How to run.
Files
The last column is the benchmark against the original model (details under How to run): character error rate of the recognised text against the page's text, per language, and against the original's own output.
| Precision | File | Size | sha256 | Benchmark |
|---|---|---|---|---|
| fp16 | fp16/decoder.onnx |
71.5 kB | 444314debeb8 |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/decoder.onnx.data |
5.54 GB | f4d0adc7501f |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/embed.onnx |
331.0 MB | 036acadf04dc |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/flow.onnx |
719.7 MB | e214bdb3ffc6 |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/pipeline.py |
7.3 kB | b4c6a12e41e1 |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/processor_config.json |
0.5 kB | 0fe7ba9aa6b9 |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/sam_1024.onnx |
190.5 MB | 5ae571093476 |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/sam_768.onnx |
187.9 MB | 981cb367f442 |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/special_tokens_map.json |
0.8 kB | ab4bd57ce17d |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/tokenizer.json |
10.0 MB | a02f8fd5228c |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| fp16 | fp16/tokenizer_config.json |
0.2 MB | a0cbe8464049 |
CER 0.0 % / 0.0 % (original 0.0 / 0.0, bf16), identical text; 2.01 s per page (CUDA) |
| int8 | int8/decoder.onnx |
92.9 kB | 5d54f98acffe |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/decoder.onnx.data |
2.95 GB | 08da5263fed9 |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/embed.onnx |
165.7 MB | e7c72797a44a |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/flow.onnx |
363.1 MB | c5cffd9e4035 |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/pipeline.py |
7.3 kB | b4c6a12e41e1 |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/processor_config.json |
0.5 kB | 0fe7ba9aa6b9 |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/sam_1024.onnx |
106.1 MB | 556272876f30 |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/sam_768.onnx |
100.6 MB | 485cbfa9567f |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/special_tokens_map.json |
0.8 kB | ab4bd57ce17d |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/tokenizer.json |
10.0 MB | a02f8fd5228c |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int8 | int8/tokenizer_config.json |
0.2 MB | a0cbe8464049 |
CER 0.0 % / 0.0 %, identical text; 2.39 s per page (CUDA) |
| int4 | int4/decoder.onnx |
93.5 kB | 614ca27c08cd |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/decoder.onnx.data |
1.73 GB | f1d659ce6656 |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/embed.onnx |
165.7 MB | e7c72797a44a |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/flow.onnx |
363.1 MB | c5cffd9e4035 |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/pipeline.py |
7.3 kB | b4c6a12e41e1 |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/processor_config.json |
0.5 kB | 0fe7ba9aa6b9 |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/sam_1024.onnx |
106.1 MB | 556272876f30 |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/sam_768.onnx |
100.6 MB | 485cbfa9567f |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/special_tokens_map.json |
0.8 kB | ab4bd57ce17d |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/tokenizer.json |
10.0 MB | a02f8fd5228c |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
| int4 | int4/tokenizer_config.json |
0.2 MB | a0cbe8464049 |
routed experts 4-bit, rest 8-bit: CER 0.0 % / 0.69 % (one wrong character); 2.29 s per page (CUDA) |
How to run
DeepSeek-OCR-2 (DeepSeek, a 3B document OCR model) as five ONNX graphs plus pipeline.py, a numpy runtime that
reproduces the repository's infer around them. The model has three parts. A SAM ViT-B image encoder. A Qwen2-0.5B
"visual causal flow" encoder: learned queries read the image features in a learned order. A DeepSeek-V2
mixture-of-experts decoder: 12 layers, 64 routed experts with 6 active per token, plus 2 shared experts.
sam_1024.onnx: the global view,image[1, 3, 1024, 1024] ->features[1, 256, 896].sam_768.onnx: the tiles,image[N, 3, 768, 768] ->features[N, 144, 896]. Two SAM graphs (about 0.2 GB each in fp16), because the position embedding is resized per view size.flow.onnx:features[N, n, 896] ->image_embeds[N, n, 1280] (the flow encoder and the projector; the query table is picked by n: 256 or 144) andview_separator[1280].embed.onnx:input_ids[1, S] ->inputs_embeds[1, S, 1280].decoder.onnx:inputs_embeds[1, S, 1280],past_key_values.{0..11}.key / .value[1, 10, P, 128] ->logitsof the last position [1, 129280] andpresent.*[1, 10, P + S, 128] (the whole cache). Positions are taken from the cache length.
pipeline.py does the rest in numpy, as the repository's infer does with crop mode on, base size 1024 and tile size
768:
- pages larger than 768 px are resized to the grid of 2-6 tiles of 768 px closest to their aspect ratio;
- the page is padded to 1024 px for the global view; mean / std are 0.5;
- the image tokens are laid out as the repository does: tiles first, then the global view, then the separator;
- greedy decoding with
no_repeat_ngram_size35 (the repository's eval mode) until the end token.
It needs numpy, onnxruntime (or onnxruntime-gpu), tokenizers and Pillow:
import sys
from PIL import Image
sys.path.insert(0, "int8")
import pipeline
ocr = pipeline.DeepSeekOCR2("int8", "cuda")
print(ocr(Image.open("page.png"))) # "<image>\nFree OCR."
print(ocr(Image.open("page.png"), "<image>\n<|grounding|>Convert the document to markdown."))
The prompts are the two from the model card. The grounding prompt returns markdown with layout boxes. The
repository's drawing of those boxes, and its PDF / vLLM tooling, are not included. On Windows, give CUDA sessions the
run option memory.enable_memory_arena_shrinkage = gpu:0, as pipeline.py does. Every input length differs, so
ONNX Runtime's GPU arena otherwise keeps growing.
The decoder graph. It is written directly as ONNX, not exported from PyTorch. The routed experts run densely: per layer, one matmul computes gate and up for all 64 experts and one matmul covers all 64 down projections. Each expert's output is weighted by its router weight, which is zero outside the token's top 6, so the sum is the same as the original's sparse dispatch. The cost is that every token reads every expert's weights. Decoding is memory-bound and slower than a sparse kernel would be. The residual stream, RMSNorms, router and attention softmax are in fp32.
Precisions.
fp16/: fp16 weights and math (fp32 inputs / outputs on the vision graphs).int8/: the decoder's matmuls 8-bit MatMulNBits (block 32); the vision graphs weight-only int8 (per output channel); the embedding int8 per row.int4/: the routed experts 4-bit (block 32) and the decoder's other matmuls 8-bit, the rest asint8/. With every decoder matmul in 4-bit, it misread Chinese characters (2 % CER, over the bar).- No bf16: fp16 passes.
Benchmark.
- Pages: the 12 synthetic pages of the lab's OCR benchmark: six English and six Chinese sentences of our own, rendered with Windows fonts, so the text is known exactly. Every page is 840 px wide, so the tile path (4 or 6 tiles) is used.
- Task: "
\nFree OCR.", decoded greedily as above.
- Original: the repository's own
infer(eval mode) under transformers 4.46.3, in bfloat16 on CUDA with eager attention (flash-attn isn't available here). Its inputs were recorded and fed to the ONNX pipeline as well. - Scores: character error rate (whitespace ignored) against the page's text, and against the original's output. Pass (the ASR rule): per language, CER at most 1 point above the original's.
- Limits: the set is easy. The original reads every page exactly, so this shows the conversion is faithful, not how the model does on hard documents.
- Timing: seconds per page on an RTX 3080 Laptop GPU (16 GB), whole pipeline. CPU wasn't benchmarked: loading the int4 build on the CPU took over 13 GB of RAM here.
| CER English / Chinese | CER vs the original's text | seconds per page | |
|---|---|---|---|
| original (PyTorch bf16, CUDA) | 0.0 % / 0.0 % | - | 2.12 |
fp16/ (CUDA) |
0.0 % / 0.0 % | 0.0 % / 0.0 % | 2.01 |
int8/ (CUDA) |
0.0 % / 0.0 % | 0.0 % / 0.0 % | 2.39 |
int4/ (CUDA) |
0.0 % / 0.69 % | 0.0 % / 0.69 % | 2.29 |
The int4 Chinese error is one wrong character on one page, but it changes the meaning: ζε ("suspended") is read
as ζζΆ ("temporarily"). Use int8/ where exact text matters. The int4 English output differs from the original
only in line breaks, which the score ignores.
Before the benchmark, each graph was checked against the original modules in float32 (largest absolute difference).
- SAM: 2.3e-6 of 0.64 (1024 px) and 2.6e-6 of 0.58 (768 px).
- Flow encoder: 3.6e-6 of 2.1 (both query tables).
- Embedding: exact.
- Decoder: checked on its first 4 layers against the original's first 4 layers: 1.7e-5 of 22, with and without the cache.
How these were made
From DeepSeek-OCR-2 at revision aaa02f3811. The graphs were exported from the original PyTorch modules with
torch.onnx (opset 17) and checked against them. Weight-only quantization: int8 is a per-output-channel int8 weight
behind DequantizeLinear, int4 is ONNX Runtime's MatMulNBits (block 32). Each card above says which precision every
graph uses. Every precision was benchmarked against the original before upload (pass: per language, character error
rate at most 1 point above the original's), and the sha256 column above is what the files must hash to.
License
apache-2.0. These files are a derivative of the original model and carry its license: it is copied in this repository, and it governs your use of these files. The original model card follows unchanged.
Original model card
π Github | π₯ Model Download | π Paper Link | π Arxiv Paper Link |
DeepSeek-OCR 2: Visual Causal Flow
Explore more human-like visual encoding.
Usage
Inference using Huggingface transformers on NVIDIA GPUs. Requirements tested on python 3.12.9 + CUDA11.8οΌ
torch==2.6.0
transformers==4.46.3
tokenizers==0.20.3
einops
addict
easydict
pip install flash-attn==2.7.3 --no-build-isolation
from transformers import AutoModel, AutoTokenizer
import torch
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
model_name = 'deepseek-ai/DeepSeek-OCR-2'
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModel.from_pretrained(model_name, _attn_implementation='flash_attention_2', trust_remote_code=True, use_safetensors=True)
model = model.eval().cuda().to(torch.bfloat16)
# prompt = "<image>\nFree OCR. "
prompt = "<image>\n<|grounding|>Convert the document to markdown. "
image_file = 'your_image.jpg'
output_path = 'your/output/dir'
res = model.infer(tokenizer, prompt=prompt, image_file=image_file, output_path = output_path, base_size = 1024, image_size = 768, crop_mode=True, save_results = True)
vLLM
Refer to πGitHub for guidance on model inference acceleration and PDF processing, etc.
Support-Modes
- Dynamic resolution
- Default: (0-6)Γ768Γ768 + 1Γ1024Γ1024 β (0-6)Γ144 + 256 visual tokens β
Main Prompts
# document: <image>\n<|grounding|>Convert the document to markdown.
# without layouts: <image>\nFree OCR.
Acknowledgement
We would like to thank DeepSeek-OCR, Vary, GOT-OCR2.0, MinerU, PaddleOCR for their valuable models and ideas.
We also appreciate the benchmark OmniDocBench.
Citation
@article{wei2025deepseek,
title={DeepSeek-OCR: Contexts Optical Compression},
author={Wei, Haoran and Sun, Yaofeng and Li, Yukun},
journal={arXiv preprint arXiv:2510.18234},
year={2025}
}
@article{wei2026deepseek,
title={DeepSeek-OCR 2: Visual Causal Flow},
author={Wei, Haoran and Sun, Yaofeng and Li, Yukun},
journal={arXiv preprint arXiv:2601.20552},
year={2026}
}
Model tree for moonbox-dev/DeepSeek-OCR-2-ONNX
Base model
deepseek-ai/DeepSeek-OCR-2