Use AIUnderstand AIBuild with AI
Build with AI·Model deep dive·· 4 min read

Run Unlimited-OCR to Parse Multi-Page Documents in One Pass

Baidu's Unlimited-OCR parses single images, multi-page sets and PDFs. Here is how to run it with Transformers or SGLang, from the model card.

The code in this post comes from the model's docs and hasn't been run in our CI yet. If something breaks, let us know.

Unlimited-OCR is a vision-language OCR model from Baidu, released under the MIT license. The card pitches it as “one-shot long-horizon parsing” and describes it as an effort to push DeepSeek-OCR one step further. The notable part for developers: it has a dedicated multi-page mode, so you can hand it several page images (or a PDF converted to images) in a single call.

The card gives no benchmark table and doesn’t state the parameter count or a memory requirement, so this post sticks to what the card does show: how to run it and which settings it uses.

Key facts from the card

  • Task: image-text-to-text, multilingual, library transformers, with custom_code (you need trust_remote_code=True).
  • Inference options: Transformers on NVIDIA GPUs, vLLM, and SGLang.
  • Tested environment for the Transformers path: Python 3.12.3 with CUDA 12.9.
  • Two single-image modes: gundam and base. Multi-page and PDF input use base only.
  • Paper: arXiv 2606.23050. Training with ms-swift is also supported, per the card.

Install

The card lists these tested versions:

torch==2.10.0
torchvision==0.25.0
transformers==4.57.1
Pillow==12.1.1
matplotlib==3.10.8
einops==0.8.2
addict==2.4.0
easydict==1.13
pymupdf==1.27.2.2
psutil==7.2.2

Run it on one image

This is the card’s Transformers example for a single image. The gundam configuration is base_size=1024, image_size=640, crop_mode=True.

single_image.py
import torch
from transformers import AutoModel, AutoTokenizer
model_name = 'baidu/Unlimited-OCR'
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModel.from_pretrained(
model_name,
trust_remote_code=True,
use_safetensors=True,
torch_dtype=torch.bfloat16,
)
model = model.eval().cuda()
model.infer(
tokenizer,
prompt='<image>document parsing.',
image_file='your_image.jpg',
output_path='your/output/dir',
base_size=1024, image_size=640, crop_mode=True,
max_length=32768,
no_repeat_ngram_size=35, ngram_window=128,
save_results=True,
)

The other single-image option, base, uses base_size=1024, image_size=1024, crop_mode=False.

Parse several pages at once

For a set of page images, the card uses infer_multi with the Multi page parsing. prompt, image_size=1024 and a larger ngram_window of 1024. This block reuses tokenizer and model from the script above.

multi_page.py
model.infer_multi(
tokenizer,
prompt='<image>Multi page parsing.',
image_files=['page1.png', 'page2.png', 'page3.png'],
output_path='your/output/dir',
image_size=1024,
max_length=32768,
no_repeat_ngram_size=35, ngram_window=1024,
save_results=True,
)

Parse a PDF

The model takes images, so the card converts each PDF page to a PNG with PyMuPDF at 300 DPI, then calls infer_multi. It also reuses model and tokenizer from the first script.

pdf_parse.py
import os
import tempfile
import fitz # PyMuPDF
def pdf_to_images(pdf_path, dpi=300):
doc = fitz.open(pdf_path)
tmp_dir = tempfile.mkdtemp(prefix='pdf_ocr_')
mat = fitz.Matrix(dpi / 72, dpi / 72)
paths = []
for i, page in enumerate(doc):
out = os.path.join(tmp_dir, f'page_{i+1:04d}.png')
page.get_pixmap(matrix=mat).save(out)
paths.append(out)
doc.close()
return paths
model.infer_multi(
tokenizer,
prompt='<image>Multi page parsing.',
image_files=pdf_to_images('your_doc.pdf', dpi=300),
output_path='your/output/dir',
image_size=1024,
max_length=32768,
no_repeat_ngram_size=35, ngram_window=1024,
save_results=True,
)

Serve it with SGLang

The card also shows an SGLang server. It installs a specific SGLang wheel that the card refers to as a local file, pins kernels and PyMuPDF, then starts the server:

Terminal window
uv venv --python 3.12
source .venv/bin/activate
uv pip install wheel/sglang-0.0.0.dev11416+g92e8bb79e-py3-none-any.whl
uv pip install kernels==0.11.7
uv pip install pymupdf==1.27.2.2
python -m sglang.launch_server \
--model baidu/Unlimited-OCR \
--served-model-name Unlimited-OCR \
--attention-backend fa3 \
--page-size 1 \
--mem-fraction-static 0.8 \
--context-length 32768 \
--enable-custom-logit-processor \
--disable-overlap-schedule \
--skip-server-warmup \
--host 0.0.0.0 \
--port 10000

The card’s client sends streaming requests to the OpenAI-compatible /v1/chat/completions endpoint. The request sets temperature to 0, skip_special_tokens to false, an images_config with the image mode, and a custom_logit_processor (DeepseekOCRNoRepeatNGramLogitProcessor) with ngram_size 35 and a window_size. It uses window_size 128 for a single gundam image and 1024 for multi-image and PDF input. See the card for the full client script.

vLLM

The card says vLLM inference is supported and points to the official recipe at recipes.vllm.ai/baidu/Unlimited-OCR. It lists two Docker images:

Terminal window
# Default (CUDA 13.0)
docker pull vllm/vllm-openai:unlimited-ocr
# Hopper GPUs (CUDA 12.9)
docker pull vllm/vllm-openai:unlimited-ocr-cu129

Gotchas

  • Custom code. Loading needs trust_remote_code=True. Review the repository code before running it on a shared machine.
  • CUDA only in the examples. The Transformers example calls .cuda(). The card doesn’t show CPU or non-NVIDIA setups.
  • Mode rules. Multi-page and PDF input use only the base configuration, with image_size=1024.
  • Repetition control matters. Every example sets no_repeat_ngram_size=35 (or the equivalent ngram_size in SGLang), and the window differs between single-image (128) and multi-page (1024) calls. Keep these values unless you have a reason to change them.
  • Raw output has layout markers. For OmniDocBench evaluation, the card applies a post-processing step that strips <|det|>type [bbox]<|/det|> markers. Expect to clean the output yourself if you want plain text.
  • Pinned versions. The SGLang path depends on a specific wheel and pinned packages, so use a fresh virtual environment.

When to pick it

Pick Unlimited-OCR if you want an MIT-licensed OCR model that takes many pages in one call, and you have an NVIDIA GPU. It also gives you three serving paths (Transformers, vLLM and SGLang).

Look elsewhere if you need a published accuracy comparison. The card doesn’t include one, so test it on your own documents before relying on it. Also skip it if you can’t run custom code or don’t have a CUDA GPU.

Related