Image-Text-to-Text
Transformers
Safetensors
multilingual
English
Chinese
hunyuan_vl
ocr
vision-language-model
document-parsing
text-spotting
information-extraction
text-image-translation
conversational
Eval Results
Instructions to use tencent/HunyuanOCR with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tencent/HunyuanOCR with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="tencent/HunyuanOCR") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("tencent/HunyuanOCR") model = AutoModelForMultimodalLM.from_pretrained("tencent/HunyuanOCR", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use tencent/HunyuanOCR with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "tencent/HunyuanOCR" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tencent/HunyuanOCR", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/tencent/HunyuanOCR
- SGLang
How to use tencent/HunyuanOCR with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "tencent/HunyuanOCR" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tencent/HunyuanOCR", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "tencent/HunyuanOCR" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tencent/HunyuanOCR", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use tencent/HunyuanOCR with Docker Model Runner:
docker model run hf.co/tencent/HunyuanOCR
Update README.md
Browse files
README.md
CHANGED
|
@@ -17,96 +17,81 @@ pipeline_tag: image-text-to-text
|
|
| 17 |
library_name: transformers
|
| 18 |
---
|
| 19 |
|
| 20 |
-
# HunyuanOCR-1.5
|
| 21 |
|
| 22 |
-
<
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
>
|
| 29 |
-
>
|
| 30 |
-
>
|
| 31 |
-
>
|
| 32 |
-
> [`v1.0/`](https://huggingface.co/tencent/HunyuanOCR/tree/main/v1.0).
|
| 33 |
-
> To load HunyuanOCR-1.0, use `subfolder="v1.0"` (or download the `v1.0/`
|
| 34 |
-
> directory directly).
|
| 35 |
|
| 36 |
---
|
| 37 |
|
| 38 |
## 📖 Introduction
|
| 39 |
|
| 40 |
-
**HunyuanOCR-1.5** is a lightweight, end-to-end OCR-specialized vision-language
|
| 41 |
-
|
| 42 |
-
**
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
HunyuanOCR-1.5
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
- ⚡ **Faster — DFlash inference acceleration.**
|
| 50 |
-
A lightweight block-diffusion draft model drafts multiple candidate tokens in
|
| 51 |
-
parallel, verified by the target model in a single pass, significantly
|
| 52 |
-
reducing decoding latency of long structured OCR outputs (dense documents,
|
| 53 |
-
tables, formulas) while **preserving the target model's output distribution**.
|
| 54 |
-
Draft weights: [`tencent/HunyuanOCR/dflash`](https://huggingface.co/tencent/HunyuanOCR/tree/main/dflash).
|
| 55 |
-
|
| 56 |
-
- 💻 **PC-side deployment via llama.cpp.**
|
| 57 |
-
Beyond server-grade vLLM, HunyuanOCR-1.5 also supports **CPU / consumer-GPU /
|
| 58 |
-
laptop** deployment via [`llama.cpp`](https://github.com/ggml-org/llama.cpp)
|
| 59 |
-
with an OpenAI-compatible `llama-server`. A DFlash-adapted `llama.cpp` fork is
|
| 60 |
-
also provided so the same speculative-decoding acceleration is available on
|
| 61 |
-
PC.
|
| 62 |
-
|
| 63 |
-
- 🧠 **Better — Agentic Data Flow + upgraded training recipe.**
|
| 64 |
-
An agent-driven data-construction system (**Agentic Data Flow**) translates
|
| 65 |
-
model weaknesses into executable data requirements, targeting long-tail
|
| 66 |
-
capabilities such as **low-resource OCR, ancient-script OCR, and multi-image
|
| 67 |
-
text-centric QA**. Pretraining Stage-3 is re-planned with **4K resolution** and
|
| 68 |
-
a **128K context window**; post-training refines SFT data and further explores
|
| 69 |
-
RL across different OCR tasks.
|
| 70 |
-
|
| 71 |
-
Together, HunyuanOCR-1.5 achieves both faster inference and broader OCR
|
| 72 |
-
capability coverage while retaining the deployment advantages of a lightweight
|
| 73 |
-
end-to-end model.
|
| 74 |
|
| 75 |
---
|
| 76 |
|
| 77 |
## ⚙️ Environment
|
| 78 |
|
| 79 |
-
-
|
| 80 |
-
- PyTorch 2.1+ (CUDA 12.1+)
|
| 81 |
-
- **transformers** (ships `HunYuanVLForConditionalGeneration` + `AutoProcessor` for the HunyuanOCR-1.5 series)
|
| 82 |
-
- **vLLM nightly** — for serving and DFlash speculative decoding
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
```bash
|
| 87 |
-
pip install
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
```
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
```bash
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
--extra-index-url https://wheels.vllm.ai/nightly
|
| 100 |
-
uv pip install runai-model-streamer
|
| 101 |
```
|
| 102 |
|
| 103 |
-
|
| 104 |
-
> (e.g. `cu121`, `cu124`).
|
| 105 |
-
---
|
| 106 |
-
|
| 107 |
-
## 🚀 Quick start
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
|
| 111 |
```python
|
| 112 |
import torch
|
|
@@ -124,7 +109,6 @@ prompt = (
|
|
| 124 |
"提取文档图片中正文的所有信息用markdown格式表示,其中页眉、页脚部分忽略,"
|
| 125 |
"表格用html格式表达,文档中公式用latex格式表示,按照阅读顺序组织进行解析。"
|
| 126 |
)
|
| 127 |
-
|
| 128 |
messages = [{
|
| 129 |
"role": "user",
|
| 130 |
"content": [
|
|
@@ -132,66 +116,161 @@ messages = [{
|
|
| 132 |
{"type": "text", "text": prompt},
|
| 133 |
],
|
| 134 |
}]
|
| 135 |
-
|
| 136 |
inputs = processor.apply_chat_template(
|
| 137 |
messages, add_generation_prompt=True, tokenize=True,
|
| 138 |
return_dict=True, return_tensors="pt",
|
| 139 |
).to(model.device)
|
| 140 |
-
|
| 141 |
with torch.inference_mode():
|
| 142 |
out = model.generate(**inputs, max_new_tokens=8000, do_sample=False)
|
| 143 |
-
|
| 144 |
gen = out[:, inputs["input_ids"].shape[1]:]
|
| 145 |
print(processor.batch_decode(gen, skip_special_tokens=True)[0])
|
| 146 |
```
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
```bash
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
```
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
|
| 162 |
```bash
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
# DFlash speculative decoding
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
# unsupported by vLLM's speculative-config):
|
| 172 |
-
# python -c "from huggingface_hub import snapshot_download; import shutil, os; \
|
| 173 |
-
# d=snapshot_download('tencent/HunyuanOCR', allow_patterns=['dflash/*']); \
|
| 174 |
-
# shutil.copytree(os.path.join(d,'dflash'), './hunyuanocr_dflash', dirs_exist_ok=True)"
|
| 175 |
-
MODEL_PATH=tencent/HunyuanOCR \
|
| 176 |
-
DFLASH_PATH=./hunyuanocr_dflash \
|
| 177 |
-
GPU=0 PORT=8001 GPU_MEM_UTIL=0.9 NUM_SPEC_TOKENS=15 \
|
| 178 |
-
bash inference/serve_dflash.sh
|
| 179 |
```
|
| 180 |
|
| 181 |
-
Send one image with the shipped client
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
|
| 184 |
```bash
|
| 185 |
-
|
|
|
|
| 186 |
--host 127.0.0.1 --port 8000 \
|
| 187 |
--model tencent/HunyuanOCR \
|
| 188 |
-
--image /path/to/document.png
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
```
|
| 190 |
|
| 191 |
### C. PC-side deployment via llama.cpp
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
---
|
| 197 |
|
|
@@ -202,23 +281,26 @@ See `docs/llama_cpp.md` in the GitHub repo for GGUF conversion, community
|
|
| 202 |
表格用html格式表达,文档中公式用latex格式表示,按照阅读顺序组织进行解析。
|
| 203 |
```
|
| 204 |
|
| 205 |
-
The model also handles text spotting, information extraction, and text-image
|
| 206 |
-
translation — pass a task-specific instruction as the text prompt.
|
| 207 |
|
| 208 |
---
|
| 209 |
|
| 210 |
-
## 🔗 Related
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
|
| 213 |
-
<https://github.com/Tencent-Hunyuan/HunyuanOCR>
|
| 214 |
-
- **DFlash draft weights** (required for speculative-decoding acceleration):
|
| 215 |
-
[`tencent/HunyuanOCR/dflash`](https://huggingface.co/tencent/HunyuanOCR/tree/main/dflash)
|
| 216 |
-
- **HunyuanOCR-1.0** (previous generation, archived under `v1.0/`):
|
| 217 |
-
[`tencent/HunyuanOCR/v1.0`](https://huggingface.co/tencent/HunyuanOCR/tree/main/v1.0)
|
| 218 |
|
| 219 |
---
|
| 220 |
|
| 221 |
## 📜 License
|
| 222 |
|
| 223 |
-
HunyuanOCR-1.5 is released under the same license as HunyuanOCR 1.0 — the
|
| 224 |
-
**Tencent Hunyuan Community License Agreement**.
|
|
|
|
| 17 |
library_name: transformers
|
| 18 |
---
|
| 19 |
|
| 20 |
+
# HunyuanOCR-1.5: Making Lightweight OCR VLMs Faster and Better
|
| 21 |
|
| 22 |
+
<p align="center">
|
| 23 |
+
<img src="https://raw.githubusercontent.com/Tencent-Hunyuan/HunyuanOCR/main/assets/hyocr-1.5-head-img.png" width="90%"/>
|
| 24 |
+
</p>
|
| 25 |
|
| 26 |
+
🤗 [Model](https://huggingface.co/tencent/HunyuanOCR) | 💻 [GitHub](https://github.com/Tencent-Hunyuan/HunyuanOCR) | 📄 [Paper](https://arxiv.org/pdf/2607.04884)
|
| 27 |
|
| 28 |
+
> 📦 **Model layout.** This repository hosts **HunyuanOCR-1.5** at the root
|
| 29 |
+
> (target base weights). The **DFlash speculative-decoding draft** lives under
|
| 30 |
+
> [`dflash/`](https://huggingface.co/tencent/HunyuanOCR/tree/main/dflash), and
|
| 31 |
+
> the previous **HunyuanOCR-1.0** is archived under
|
| 32 |
+
> [`v1.0/`](https://huggingface.co/tencent/HunyuanOCR/tree/main/v1.0)
|
| 33 |
+
> (load it with `subfolder="v1.0"`, or download the `v1.0/` directory directly).
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
---
|
| 36 |
|
| 37 |
## 📖 Introduction
|
| 38 |
|
| 39 |
+
**HunyuanOCR-1.5** is a lightweight, end-to-end OCR-specialized vision-language model. It targets a broad range of text-centric visual tasks and unifies **document parsing, text spotting, information extraction, text-image translation** within a single end-to-end VLM.
|
| 40 |
+
|
| 41 |
+
Building upon the validated lightweight architecture of HunyuanOCR-1.0, HunyuanOCR-1.5 does **not** redesign the model backbone. Instead, it performs a systematic upgrade around two goals — **making the model faster and better**:
|
| 42 |
+
|
| 43 |
+
- ⚡ **Faster — DFlash inference acceleration.** End-to-end OCR is often accompanied by long autoregressive decoding, which becomes the major bottleneck for dense documents, tables, formulas, and other long structured outputs. HunyuanOCR-1.5 adapts a speculative-decoding framework based on **DFlash**: a lightweight block-diffusion draft model drafts multiple candidate tokens in parallel, which are then verified by the target model in a single pass. This significantly reduces the decoding latency of long structured outputs while **preserving the output distribution** of the target model.
|
| 44 |
+
- 💻 **PC-side deployment via llama.cpp.** Beyond server-grade vLLM, HunyuanOCR-1.5 also supports **CPU / consumer-GPU / laptop** deployment through [`llama.cpp`](https://github.com/ggml-org/llama.cpp) with a GGUF-converted checkpoint and an OpenAI-compatible `llama-server`. A DFlash-adapted `llama.cpp` fork is provided as well, so the same speculative-decoding acceleration is available on PC.
|
| 45 |
+
- 🧠 **Better — Agentic Data Flow + upgraded training recipe.** On the data side, we propose **Agentic Data Flow**, an agent-driven data-construction system that translates model weaknesses into executable data requirements. Agents deeply participate in material search, tool-based verification, sample cleaning, and data-pipeline development, and iterate in a closed loop with algorithm engineers. In HunyuanOCR-1.5, this system is used for targeted long-tail capabilities such as **low-resource OCR, ancient-script OCR, and multi-image text-centric QA**. On the training side, we systematically upgrade the recipe: pretraining Stage-3 is re-planned to incorporate the newly produced capability data, multi-image data, and historical OCR data, with maximum image resolution extended to **4K** and context window extended to **128K**; post-training refines the SFT data and further explores RL across different OCR tasks to amplify the gains from reinforcement learning.
|
| 46 |
+
|
| 47 |
+
Together, HunyuanOCR-1.5 achieves **both faster inference and broader OCR capability coverage** while retaining the deployment advantages of a lightweight end-to-end model. The full SFT / DFlash training pipeline and the transformers / vLLM / llama.cpp inference stack are open-sourced in the [GitHub repo](https://github.com/Tencent-Hunyuan/HunyuanOCR).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
---
|
| 50 |
|
| 51 |
## ⚙️ Environment
|
| 52 |
|
| 53 |
+
Inference is split into **three self-contained, mutually exclusive setups** in the [GitHub repo](https://github.com/Tencent-Hunyuan/HunyuanOCR) under [`inference/`](https://github.com/Tencent-Hunyuan/HunyuanOCR/tree/main/inference). vLLM (AR / DFlash) and native transformers inference require different, incompatible `transformers` versions and **cannot share one environment** — this is a validated constraint, not a preference:
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
| Setup | vLLM | DFlash accel. | transformers | CUDA | Best for |
|
| 56 |
+
|---|:-:|:-:|:-:|---|---|
|
| 57 |
+
| [`inference/vllm_0_18_1`](https://github.com/Tencent-Hunyuan/HunyuanOCR/tree/main/inference/vllm_0_18_1) | 0.18.1 (release) | ❌ | ❌ | 12.x | simplest setup, AR only |
|
| 58 |
+
| [`inference/nightly`](https://github.com/Tencent-Hunyuan/HunyuanOCR/tree/main/inference/nightly) | nightly | ✅ | ❌ | 13 | AR + DFlash acceleration |
|
| 59 |
+
| [`inference/transformers`](https://github.com/Tencent-Hunyuan/HunyuanOCR/tree/main/inference/transformers) | — | — | ✅ 5.13.0 | host driver | native HF inference |
|
| 60 |
+
|
| 61 |
+
Each subfolder ships its own README and `requirements.txt`. See
|
| 62 |
+
[`inference/README.md`](https://github.com/Tencent-Hunyuan/HunyuanOCR/blob/main/inference/README.md)
|
| 63 |
+
for the selection guide and the full rationale.
|
| 64 |
+
|
| 65 |
+
**Common prerequisites:** Python 3.10+ (3.12 tested), an NVIDIA GPU, and
|
| 66 |
+
`huggingface_hub` for downloading the weights:
|
| 67 |
|
| 68 |
```bash
|
| 69 |
+
pip install -U "huggingface_hub[cli]"
|
| 70 |
+
# target base (1.5) — skip the archived 1.0 to save space
|
| 71 |
+
huggingface-cli download tencent/HunyuanOCR --local-dir ./HunyuanOCR --exclude "v1.0/*"
|
| 72 |
```
|
| 73 |
|
| 74 |
+
The download contains both the base model and the `dflash/` draft model.
|
| 75 |
+
|
| 76 |
+
---
|
| 77 |
+
|
| 78 |
+
## 🧪 Inference
|
| 79 |
|
| 80 |
+
All setups share the same weights and the same task-type prompts + sampling
|
| 81 |
+
(`temperature=0.0`, `top_p=1.0`, `top_k=-1`, `repetition_penalty=1.08`) +
|
| 82 |
+
post-processing, so their outputs are directly comparable. Grab the toolkit from
|
| 83 |
+
GitHub first:
|
| 84 |
|
| 85 |
```bash
|
| 86 |
+
git clone https://github.com/Tencent-Hunyuan/HunyuanOCR.git
|
| 87 |
+
cd HunyuanOCR
|
|
|
|
|
|
|
| 88 |
```
|
| 89 |
|
| 90 |
+
### A. HuggingFace transformers (native)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
The model ships the official `HunYuanVLForConditionalGeneration` + `AutoProcessor`
|
| 93 |
+
integration (transformers **≥ 5.13.0**). The simplest path — weights are pulled
|
| 94 |
+
from the Hub automatically:
|
| 95 |
|
| 96 |
```python
|
| 97 |
import torch
|
|
|
|
| 109 |
"提取文档图片中正文的所有信息用markdown格式表示,其中页眉、页脚部分忽略,"
|
| 110 |
"表格用html格式表达,文档中公式用latex格式表示,按照阅读顺序组织进行解析。"
|
| 111 |
)
|
|
|
|
| 112 |
messages = [{
|
| 113 |
"role": "user",
|
| 114 |
"content": [
|
|
|
|
| 116 |
{"type": "text", "text": prompt},
|
| 117 |
],
|
| 118 |
}]
|
|
|
|
| 119 |
inputs = processor.apply_chat_template(
|
| 120 |
messages, add_generation_prompt=True, tokenize=True,
|
| 121 |
return_dict=True, return_tensors="pt",
|
| 122 |
).to(model.device)
|
|
|
|
| 123 |
with torch.inference_mode():
|
| 124 |
out = model.generate(**inputs, max_new_tokens=8000, do_sample=False)
|
|
|
|
| 125 |
gen = out[:, inputs["input_ids"].shape[1]:]
|
| 126 |
print(processor.batch_decode(gen, skip_special_tokens=True)[0])
|
| 127 |
```
|
| 128 |
|
| 129 |
+
For **multi-GPU batch inference** with sampling / early-stop / doc-parse
|
| 130 |
+
normalization strictly aligned to the vLLM client, use the shipped script in a
|
| 131 |
+
dedicated `transformers==5.13.0` environment (see
|
| 132 |
+
[`inference/transformers/README.md`](https://github.com/Tencent-Hunyuan/HunyuanOCR/blob/main/inference/transformers/README.md)):
|
| 133 |
|
| 134 |
```bash
|
| 135 |
+
# install per inference/transformers/requirements.txt, then:
|
| 136 |
+
python inference/transformers/infer_hf_8gpu_hyocr15.py \
|
| 137 |
+
--model ./HunyuanOCR \
|
| 138 |
+
--input /path/to/bench.jsonl \
|
| 139 |
+
--output ./results/hf_out \
|
| 140 |
+
--gpu-ids 0,1,2,3,4,5,6,7 \
|
| 141 |
+
--max-new-tokens 8192 \
|
| 142 |
+
--merge
|
| 143 |
+
```
|
| 144 |
|
| 145 |
+
### B. vLLM (OpenAI-compatible)
|
| 146 |
+
|
| 147 |
+
Two mutually-exclusive vLLM setups. Both serve the model as `tencent/HunyuanOCR`
|
| 148 |
+
with `-tp 1` and `--max-model-len 131072`.
|
| 149 |
+
|
| 150 |
+
**B1 — vLLM 0.18.1 (release, AR only, simplest).** The release build natively
|
| 151 |
+
supports `HunYuanVLForConditionalGeneration`; no nightly or patch required.
|
| 152 |
+
Install per
|
| 153 |
+
[`inference/vllm_0_18_1/requirements.txt`](https://github.com/Tencent-Hunyuan/HunyuanOCR/blob/main/inference/vllm_0_18_1/requirements.txt)
|
| 154 |
+
(core: `pip install "vllm==0.18.1"`), then:
|
| 155 |
+
|
| 156 |
+
```bash
|
| 157 |
+
MODEL_PATH=./HunyuanOCR GPU=0 PORT=8000 bash inference/vllm_0_18_1/serve.sh
|
| 158 |
+
curl -sf http://127.0.0.1:8000/v1/models # readiness check
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
**B2 — vLLM nightly (AR + DFlash speculative decoding).** Required for the real
|
| 162 |
+
DFlash speedup. Install per
|
| 163 |
+
[`inference/nightly/requirements.txt`](https://github.com/Tencent-Hunyuan/HunyuanOCR/blob/main/inference/nightly/requirements.txt):
|
| 164 |
+
|
| 165 |
+
```bash
|
| 166 |
+
uv pip install -U vllm --torch-backend=cu130 --extra-index-url https://wheels.vllm.ai/nightly
|
| 167 |
+
uv pip install runai-model-streamer
|
| 168 |
```
|
| 169 |
|
| 170 |
+
The DFlash draft lives under the `dflash/` subfolder of `tencent/HunyuanOCR`.
|
| 171 |
+
vLLM's `--speculative-config` does not accept an HF subfolder, so download the
|
| 172 |
+
draft weight into a flat local dir first:
|
| 173 |
|
| 174 |
```bash
|
| 175 |
+
huggingface-cli download tencent/HunyuanOCR dflash/model.safetensors --local-dir ./HunyuanOCR
|
| 176 |
+
cp -r ./HunyuanOCR/dflash ./hyocr_dflash
|
| 177 |
+
```
|
| 178 |
+
|
| 179 |
+
Then launch AR or DFlash:
|
| 180 |
+
|
| 181 |
+
```bash
|
| 182 |
+
# AR (autoregressive) baseline
|
| 183 |
+
MODEL_PATH=./HunyuanOCR GPU=0 PORT=8000 bash inference/nightly/serve_ar.sh
|
| 184 |
|
| 185 |
# DFlash speculative decoding
|
| 186 |
+
MODEL_PATH=./HunyuanOCR DFLASH_PATH=./hyocr_dflash \
|
| 187 |
+
GPU=0 PORT=8001 NUM_SPEC_TOKENS=15 bash inference/nightly/serve_dflash.sh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
```
|
| 189 |
|
| 190 |
+
**Client (either vLLM setup).** Send one image with the shipped client. The
|
| 191 |
+
prompt is locked to an official task type via `--task-type` (run `--list-tasks`
|
| 192 |
+
to see all); sampling and streaming tail-repetition early-stop / cleanup are
|
| 193 |
+
built in:
|
| 194 |
|
| 195 |
```bash
|
| 196 |
+
# use the client from the same setup folder, e.g. inference/vllm_0_18_1/ or inference/nightly/
|
| 197 |
+
python inference/vllm_0_18_1/infer_vllm_client.py \
|
| 198 |
--host 127.0.0.1 --port 8000 \
|
| 199 |
--model tencent/HunyuanOCR \
|
| 200 |
+
--image /path/to/document.png \
|
| 201 |
+
--task-type doc_parse \
|
| 202 |
+
--max-tokens 32768
|
| 203 |
+
# add --no-stream to disable streaming + early-stop
|
| 204 |
+
# add --no-doc-postprocess to disable doc_parse markdown normalization
|
| 205 |
+
```
|
| 206 |
+
|
| 207 |
+
Available task types (`--task-type`): `doc_parse` (default), `structured_parse`, `spotting_json`, `spotting_hunyuan`, `layout`, `layout_parse`, `chart_parse`, `formula`, `table`, `doc_trans_en2zh`, `trans_other2en`, `trans_other2zh`.
|
| 208 |
+
|
| 209 |
+
For **batch** inference over a directory (same task types, multi-endpoint
|
| 210 |
+
concurrency, resumable):
|
| 211 |
+
|
| 212 |
+
```bash
|
| 213 |
+
python inference/vllm_0_18_1/batch_infer.py \
|
| 214 |
+
--image-dir /path/to/images \
|
| 215 |
+
--out-dir /path/to/output \
|
| 216 |
+
--ports 8000 \
|
| 217 |
+
--task-type doc_parse \
|
| 218 |
+
--max-tokens 32768 \
|
| 219 |
+
--concurrency 16
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
Or hand-written with the OpenAI SDK:
|
| 223 |
+
|
| 224 |
+
```python
|
| 225 |
+
import base64
|
| 226 |
+
from openai import OpenAI
|
| 227 |
+
|
| 228 |
+
def data_url(p): # Mime is fixed to image/jpeg
|
| 229 |
+
return f"data:image/jpeg;base64,{base64.b64encode(open(p,'rb').read()).decode()}"
|
| 230 |
+
|
| 231 |
+
client = OpenAI(api_key="EMPTY", base_url="http://127.0.0.1:8000/v1")
|
| 232 |
+
resp = client.chat.completions.create(
|
| 233 |
+
model="tencent/HunyuanOCR",
|
| 234 |
+
messages=[
|
| 235 |
+
{"role": "system", "content": ""},
|
| 236 |
+
{"role": "user", "content": [
|
| 237 |
+
{"type": "image_url", "image_url": {"url": data_url("/path/to/document.png")}},
|
| 238 |
+
{"type": "text", "text": "请提取图片中的文字内容。"},
|
| 239 |
+
]},
|
| 240 |
+
],
|
| 241 |
+
max_tokens=32768,
|
| 242 |
+
temperature=0.0, top_p=1.0,
|
| 243 |
+
extra_body={"top_k": -1, "repetition_penalty": 1.08, "skip_special_tokens": True},
|
| 244 |
+
)
|
| 245 |
+
print(resp.choices[0].message.content)
|
| 246 |
```
|
| 247 |
|
| 248 |
### C. PC-side deployment via llama.cpp
|
| 249 |
|
| 250 |
+
For **CPU / consumer-GPU / laptop** environments, HunyuanOCR-1.5 can also be deployed through [`llama.cpp`](https://github.com/ggml-org/llama.cpp) after converting the checkpoint to GGUF. Both the community `llama.cpp` (HunyuanOCR base only) and a DFlash-adapted fork ([`wendadawen/llama.cpp @ dflash-adapt-hunyuanocr-hunyuanstyle`](https://github.com/wendadawen/llama.cpp/tree/dflash-adapt-hunyuanocr-hunyuanstyle)) are supported.
|
| 251 |
+
|
| 252 |
+
Minimal build & serve (community, no DFlash):
|
| 253 |
+
|
| 254 |
+
```bash
|
| 255 |
+
# 1. Build
|
| 256 |
+
git clone https://github.com/ggml-org/llama.cpp.git && cd llama.cpp
|
| 257 |
+
cmake -B build -DLLAMA_BUILD_EXAMPLES=ON # add -DGGML_CUDA=ON for NVIDIA GPU
|
| 258 |
+
cmake --build ./build --config Release -j
|
| 259 |
+
|
| 260 |
+
# 2. Convert HunyuanOCR to GGUF (base + mmproj)
|
| 261 |
+
hf download tencent/HunyuanOCR --local-dir ./HunyuanOCR --exclude "v1.0/*"
|
| 262 |
+
python3 convert_hf_to_gguf.py --outfile ./HunyuanOCR/hyocr-f16.gguf --outtype f16 ./HunyuanOCR
|
| 263 |
+
python3 convert_hf_to_gguf.py --outfile ./HunyuanOCR/mmproj-hyocr-f16.gguf --outtype f16 --mmproj ./HunyuanOCR
|
| 264 |
+
|
| 265 |
+
# 3. Serve (OpenAI-compatible)
|
| 266 |
+
build/bin/llama-server \
|
| 267 |
+
--model ./HunyuanOCR/hyocr-f16.gguf \
|
| 268 |
+
--mmproj ./HunyuanOCR/mmproj-hyocr-f16.gguf \
|
| 269 |
+
--host 0.0.0.0 --port 8080 --alias HYVL \
|
| 270 |
+
--ctx-size 10240 --n-predict 4096
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
The DFlash-adapted variant and the full guide are in [`docs/llama_cpp.md`](https://github.com/Tencent-Hunyuan/HunyuanOCR/blob/main/docs/llama_cpp.md) in the GitHub repo.
|
| 274 |
|
| 275 |
---
|
| 276 |
|
|
|
|
| 281 |
表格用html格式表达,文档中公式用latex格式表示,按照阅读顺序组织进行解析。
|
| 282 |
```
|
| 283 |
|
| 284 |
+
The model also handles text spotting, information extraction, and text-image translation — pass a task-specific instruction as the text prompt (or use `--task-type` with the shipped client).
|
|
|
|
| 285 |
|
| 286 |
---
|
| 287 |
|
| 288 |
+
## 🔗 Related resources
|
| 289 |
+
|
| 290 |
+
- **GitHub — training & inference toolkit**: <https://github.com/Tencent-Hunyuan/HunyuanOCR>
|
| 291 |
+
- **DFlash draft weights**: [`tencent/HunyuanOCR/dflash`](https://huggingface.co/tencent/HunyuanOCR/tree/main/dflash)
|
| 292 |
+
- **HunyuanOCR-1.0** (previous generation, archived under `v1.0/`): [`tencent/HunyuanOCR/v1.0`](https://huggingface.co/tencent/HunyuanOCR/tree/main/v1.0)
|
| 293 |
+
|
| 294 |
+
---
|
| 295 |
+
|
| 296 |
+
## 🙏 Acknowledgements
|
| 297 |
+
|
| 298 |
+
We would like to thank [Qwen](https://github.com/QwenLM/Qwen3.6) and [DFlash](https://github.com/z-lab/dflash) for their valuable models and ideas.
|
| 299 |
|
| 300 |
+
Special thanks to the Hugging Face community for their Day-0 support.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
---
|
| 303 |
|
| 304 |
## 📜 License
|
| 305 |
|
| 306 |
+
HunyuanOCR-1.5 is released under the same license as HunyuanOCR 1.0 — the **Tencent Hunyuan Community License Agreement**. See [`LICENSE`](https://huggingface.co/tencent/HunyuanOCR/blob/main/LICENSE) for the full terms.
|
|
|