Fast, accurate OCR with correctness assessment.
docs: default OCR deployments to DSpark and retain header/footer OCR
caowanxia1238e83c65414d8
diff --git a/docs/llamacpp_deploy.md b/docs/llamacpp_deploy.mdindex edc26db..2464136 100644--- a/docs/llamacpp_deploy.md+++ b/docs/llamacpp_deploy.md@@ -1,141 +1,103 @@# llama.cpp Deployment Guide+[中文](./llamacpp_deploy_zh.md)+[← Back to README](../README.md)-## Overview+Deploy MiniCPM-OCR 1.0 with the **Q8_0 main model, Q8_0 DSpark draft head, and F16 4× vision projector** together, with DSpark speculative decoding enabled. Run the commands below from the **root of this repository** in Bash.-llama.cpp enables CPU and local GPU inference with GGUF models, ideal for resource-constrained environments or offline deployment.+## 1. Install llama.cpp-## Requirements+Use the verified reference version **[b11901](https://github.com/ggml-org/llama.cpp/releases/tag/b11901)**. You need Git, CMake, and a C++ compiler to build it:-- **Hardware**: CPU with AVX2, or NVIDIA/AMD GPU-- **RAM**: ≥16GB system memory-- **Storage**: ~2.5GB for the F16 model + vision projector+```bash+mkdir -p ./sources+git clone --branch b11901 --depth 1 https://github.com/ggml-org/llama.cpp.git ./sources/llama.cpp+cmake -S ./sources/llama.cpp -B ./sources/llama.cpp/build -DCMAKE_BUILD_TYPE=Release+cmake --build ./sources/llama.cpp/build -j --target llama-server+./sources/llama.cpp/build/bin/llama-server --version+```-## GGUF Weights+On Apple Silicon, the configuration above enables Metal by default; on Linux, it builds the CPU backend. For NVIDIA GPUs, append `-DGGML_CUDA=ON` to the `cmake -S ...` configuration command before building, with the CUDA toolkit installed. See the official [build instructions](https://github.com/ggml-org/llama.cpp/blob/b11901/docs/build.md) for other backends.-Download from [openbmb/ MiniCPM-OCR-1.0-gguf](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf):+Alternatively, download and extract the package for your system from the same release page. Replace `./sources/llama.cpp/build/bin/llama-server` in the commands below with the extracted `llama-server` executable path. Keep the executable and its accompanying libraries together. The serving command below was verified with the official b11901 macOS ARM64 package.-| File | Size | Description |-|---|---|---|-| `MiniCPM-OCR-1_0-F16.gguf` | 1.4 GB | Language model (F16) |-| `mmproj-model-f16-4x.gguf` | 859 MB | Vision projector (4x downsample) |+## 2. Download the main model, projector, and DSpark draft-Both files are required: the language model for `--model`, the projector for `--mmproj`.+Download the main model and projector from the [main model repository](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf), and the matching quantized draft from the [DSpark GGUF repository](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark-gguf). All three files are required; use matched weights from the same 1.0 release:-Quantized versions (Q4_K_M, Q8_0) are coming soon. To quantize them yourself:+| File | Approximate size | Purpose |+|---|---|---|+| `MiniCPM-OCR-1_0-Q8_0.gguf` | 0.81 GB | Q8_0 main model |+| `mmproj-model-f16-4x.gguf` | 0.90 GB | F16 vision projector, required for image input |+| `dspark-MiniCPM-OCR-1_0-Q8_0.gguf` | 0.33 GB | Q8_0 DSpark draft model for speculative decoding |```bash-./build/bin/llama-quantize MiniCPM-OCR-1_0-F16.gguf MiniCPM-OCR-1.0-Q8_0.gguf Q8_0+mkdir -p ./models/MiniCPM-OCR-1.0-gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf/resolve/main/MiniCPM-OCR-1_0-Q8_0.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf/resolve/main/mmproj-model-f16-4x.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark-gguf/resolve/main/dspark-MiniCPM-OCR-1_0-Q8_0.gguf```-## Installation--### Build from source+If Hugging Face is unreachable, create the same directory and use the official ModelScope [main model repository](https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf) and [DSpark repository](https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-DSpark-gguf) instead:```bash-git clone https://github.com/ggml-org/llama.cpp-cd llama.cpp--# NVIDIA GPU-cmake -B build -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release-cmake --build build -j --target llama-server llama-quantize--# AMD GPU: cmake -B build -DGGML_HIP=ON ...-# CPU only: cmake -B build ...+mkdir -p ./models/MiniCPM-OCR-1.0-gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf/resolve/master/MiniCPM-OCR-1_0-Q8_0.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf/resolve/master/mmproj-model-f16-4x.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-DSpark-gguf/resolve/master/dspark-MiniCPM-OCR-1_0-Q8_0.gguf```-> Tips: on recent NVIDIA GPUs you can speed up the build by targeting one architecture,-> e.g. `-DCMAKE_CUDA_ARCHITECTURES=120` (RTX 5090), `90` (H100), or `80` (A100).-> See the official [build documentation](https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md)-> for the full list of options.+## 3. Start the service-### Using pre-built binaries--Download from [llama.cpp releases](https://github.com/ggml-org/llama.cpp/releases).--## Serving--### Start server+In a service terminal at the repository root:```bash-./build/bin/llama-server \- --model ./MiniCPM-OCR-1_0-F16.gguf \- --mmproj ./mmproj-model-f16-4x.gguf \- --host 0.0.0.0 \+./sources/llama.cpp/build/bin/llama-server \+ --model ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ --mmproj ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ --model-draft ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ --spec-type draft-dspark \+ --spec-draft-n-max 15 \+ --n-gpu-layers 99 \+ --gpu-layers-draft 99 \+ --flash-attn on \+ --ctx-size 8192 \+ --parallel 1 \+ --host 127.0.0.1 \--port 18499 \- --ctx-size 40960 \- --n-gpu-layers 999+ --alias minicpm-ocr \+ --jinja \+ --reasoning off```-Flags:-- `--mmproj FILE`: path to the multimodal projector file — required for image input-- `--n-gpu-layers 999`: offload all layers to GPU; `0` for CPU-only inference-- `--ctx-size`: context window size--The official GGUF works out of the box — the embedded chat template already disables-thinking mode, so no extra template flags are needed. (If you convert the GGUF weights-yourself, start the server with `--chat-template-kwargs '{"enable_thinking":false}'`.)+Keep this terminal running. The service is available at **`http://127.0.0.1:18499`**, with the OpenAI-compatible chat endpoint **`/v1/chat/completions`** and model name **`minicpm-ocr`**. Choose an unused port; stop an Ollama instance using `18499` or change the port in all commands.-### Verify service--```bash-curl http://127.0.0.1:18499/health-# {"status":"ok"}-```+- `--model-draft` loads the matching quantized DSpark head; `--spec-type draft-dspark` selects DSpark decoding. The draft head cannot serve OCR on its own.+- `--spec-draft-n-max 15` sets the maximum draft length per step to 15 tokens. `--flash-attn on` enables Flash Attention.+- `--n-gpu-layers 99` and `--gpu-layers-draft 99` offload the main and draft model layers to GPU. For CPU-only serving, set both to `0` and add `--no-mmproj-offload`.+- `--ctx-size 8192 --parallel 1` starts one request slot with an 8192-token context. Increase the context if the image tokens and output exceed this limit; memory usage also increases.+- `--jinja --reasoning off` uses the official GGUF chat template with thinking disabled.-## SDK Configuration+## 4. Check the service-Install the SDK and layout model as described in the [SDK tutorial](./minicpmvocr_tutorial_en.md#installation), using `.minicpm-ocr` for the SDK environment. For CPU layout detection as shown below, replace the tutorial's `paddlepaddle-gpu` installation command with:+Open another terminal:```bash-python -m pip install "paddlepaddle==3.2.1"+curl --noproxy 127.0.0.1 -f http://127.0.0.1:18499/health+curl --noproxy 127.0.0.1 -f http://127.0.0.1:18499/v1/models```-Then run from the root of this repository:--```bash-source .minicpm-ocr/bin/activate-export OCR_LAYOUT_MODEL_DIR="$PWD/PP-DocLayoutV3_infer"-export PADDLE_PDX_DISABLE_MODEL_SOURCE_CHECK=True--minicpmvocr parse --config sdk/minicpmvocr/configs/minicpmvocr.yaml \- --backend llamacpp --server-url http://127.0.0.1:18499 \- --model minicpm-ocr \- --set layout.device=cpu \- --set pipeline.queue_mode=serial \- --set pipeline.max_workers=1 \- --input doc.pdf --output doc.md-```--For GPU layout detection, install GPU PaddlePaddle as described in the SDK tutorial, activate `.minicpm-ocr`, and use `--set layout.device=cuda:0` instead.--> **Concurrency**: llama-server processes one request at a time by default. Keep-> `pipeline.queue_mode=serial` and `pipeline.max_workers=1` in the SDK options.--## Performance--Measured on a single RTX 5090 (32GB) with the F16 weights:--- Model + projector load: ~2.3s-- One table crop (1,312 prompt + 265 output tokens): ~1.0s-- VRAM: ~26GB with `--ctx-size 40960` — reduce the value on smaller GPUs-- CPU-only (48 threads): one table image end-to-end (layout + recognition + assembly) in ~21s--### Quantization trade-offs--| Quantization | Size | Table | Dense text / XML | Notes |-|---|---|---|---|---|-| F16 | 1.4 GB | baseline | baseline | highest fidelity |-| Q8_0 | 775 MB | lossless | ≈lossless | recommended balance |-| Q4_K_M | 505 MB | lossless | small degradation | closing-tag slashes may be dropped in dense code — prefer Q8_0 for code-heavy documents |--## Limitations--- Quantization introduces small accuracy loss (~1-2% on benchmarks, most visible on dense code/XML at Q4_K_M)-- No speculative decoding support (no DSpark)-- Slower than vLLM on high-end GPUs-------For more details, see the [llama.cpp documentation](https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md).+`/health` should return `{"status":"ok"}`, and `/v1/models` should include `minicpm-ocr`. During startup, the service log should show `adding speculative implementation 'draft-dspark'` and `n_max=15`. After the first inference request, look for `draft acceptance` with accepted and generated token counts to confirm that DSpark participated in decoding.diff --git a/docs/llamacpp_deploy_zh.md b/docs/llamacpp_deploy_zh.mdnew file mode 100644index 0000000..3d46a53--- /dev/null+++ b/docs/llamacpp_deploy_zh.md@@ -0,0 +1,103 @@+# llama.cpp 部署指南++[English](./llamacpp_deploy.md)++[← 返回首页](../README_zh.md)++使用 **Q8_0 主模型、Q8_0 DSpark 草稿头和 F16 4× 视觉投影**联合部署 MiniCPM-OCR 1.0,默认启用 DSpark 推测解码。以下命令均在**本仓库根目录**通过 Bash 执行。++## 1. 安装 llama.cpp++使用已验证的参考版本 **[b11901](https://github.com/ggml-org/llama.cpp/releases/tag/b11901)**。源码编译需要 Git、CMake 和 C++ 编译器:++```bash+mkdir -p ./sources+git clone --branch b11901 --depth 1 https://github.com/ggml-org/llama.cpp.git ./sources/llama.cpp+cmake -S ./sources/llama.cpp -B ./sources/llama.cpp/build -DCMAKE_BUILD_TYPE=Release+cmake --build ./sources/llama.cpp/build -j --target llama-server+./sources/llama.cpp/build/bin/llama-server --version+```++Apple Silicon 默认启用 Metal;Linux 上述配置构建 CPU 后端。使用 NVIDIA GPU 时,安装 CUDA toolkit,并在构建前为 `cmake -S ...` 配置命令追加 `-DGGML_CUDA=ON`。其他后端见官方[编译说明](https://github.com/ggml-org/llama.cpp/blob/b11901/docs/build.md)。++也可以从同一发布页下载并解压适合当前系统的预编译包,将下文的 `./sources/llama.cpp/build/bin/llama-server` 替换为解压后的 `llama-server` 可执行文件路径,并保留其配套动态库。下文服务命令已使用官方 b11901 macOS ARM64 预编译包验证。++## 2. 下载主模型、视觉投影和 DSpark 草稿头++从 [主模型仓库](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf) 下载主模型和视觉投影器,从 [DSpark GGUF 仓库](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark-gguf) 下载配套量化草稿模型。三个文件均需下载,并使用同一 1.0 发布版本的配套权重:++| 文件 | 大小约 | 用途 |+|---|---|---|+| `MiniCPM-OCR-1_0-Q8_0.gguf` | 0.81 GB | Q8_0 主模型 |+| `mmproj-model-f16-4x.gguf` | 0.90 GB | F16 视觉投影器,图片输入必需 |+| `dspark-MiniCPM-OCR-1_0-Q8_0.gguf` | 0.33 GB | Q8_0 DSpark 草稿模型,用于推测解码 |++```bash+mkdir -p ./models/MiniCPM-OCR-1.0-gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf/resolve/main/MiniCPM-OCR-1_0-Q8_0.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf/resolve/main/mmproj-model-f16-4x.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark-gguf/resolve/main/dspark-MiniCPM-OCR-1_0-Q8_0.gguf+```++如无法访问 Hugging Face,创建相同目录后,改从 ModelScope 的 [主模型仓库](https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf) 和 [DSpark 仓库](https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-DSpark-gguf) 下载:++```bash+mkdir -p ./models/MiniCPM-OCR-1.0-gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf/resolve/master/MiniCPM-OCR-1_0-Q8_0.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf/resolve/master/mmproj-model-f16-4x.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-DSpark-gguf/resolve/master/dspark-MiniCPM-OCR-1_0-Q8_0.gguf+```++## 3. 启动服务++在仓库根目录的服务终端中执行:++```bash+./sources/llama.cpp/build/bin/llama-server \+ --model ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ --mmproj ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ --model-draft ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ --spec-type draft-dspark \+ --spec-draft-n-max 15 \+ --n-gpu-layers 99 \+ --gpu-layers-draft 99 \+ --flash-attn on \+ --ctx-size 8192 \+ --parallel 1 \+ --host 127.0.0.1 \+ --port 18499 \+ --alias minicpm-ocr \+ --jinja \+ --reasoning off+```++保持该终端运行。服务地址为 **`http://127.0.0.1:18499`**,OpenAI 兼容聊天接口为 **`/v1/chat/completions`**,模型名为 **`minicpm-ocr`**。请选择空闲端口;若 Ollama 已占用 `18499`,先停止该实例,或同步修改全部命令中的端口。++- `--model-draft` 加载匹配的量化 DSpark 头,`--spec-type draft-dspark` 指定 DSpark 解码。草稿头不能单独提供 OCR 服务。+- `--spec-draft-n-max 15` 将每步最大草稿长度设为 15 个 token;`--flash-attn on` 启用 Flash Attention。+- `--n-gpu-layers 99` 和 `--gpu-layers-draft 99` 将主模型和草稿模型的层卸载到 GPU。纯 CPU 部署时,将两项均设为 `0`,并追加 `--no-mmproj-offload`。+- `--ctx-size 8192 --parallel 1` 启用单个请求槽位,上下文长度为 8192 token。若图像 token 与输出总量超过此限制,可提高上下文长度,内存占用也会增加。+- `--jinja --reasoning off` 使用官方 GGUF 内置聊天模板并关闭思考。++## 4. 检查服务++另开一个终端执行:++```bash+curl --noproxy 127.0.0.1 -f http://127.0.0.1:18499/health+curl --noproxy 127.0.0.1 -f http://127.0.0.1:18499/v1/models+```++`/health` 应返回 `{"status":"ok"}`,`/v1/models` 应包含 `minicpm-ocr`。服务启动日志应出现 `adding speculative implementation 'draft-dspark'` 和 `n_max=15`;首次推理后,检查 `draft acceptance` 以及接受、生成的 token 数,确认 DSpark 已参与解码。diff --git a/docs/minicpmvocr_tutorial.md b/docs/minicpmvocr_tutorial.mdindex a61ca1b..75aaccd 100644--- a/docs/minicpmvocr_tutorial.md+++ b/docs/minicpmvocr_tutorial.md@@ -39,7 +39,7 @@ python -m pip install "paddlepaddle-gpu==3.3.0" \### 2. 推理服务环境配置-推理服务任选其一,本文使用 vLLM;SGLang 和 llama.cpp 见 [SGLang 部署指南](./sglang_deploy.md)、[llama.cpp 部署指南](./llamacpp_deploy.md)(英文)。+推理服务任选其一,本文使用 vLLM;其他后端见 [Ollama 部署指南](./ollama_deploy_zh.md)、[SGLang 部署指南](./sglang_deploy.md)(英文)和 [llama.cpp 部署指南](./llamacpp_deploy_zh.md)。> **旧驱动**:驱动低于 580 时,先按 [FAQ](./faq_zh.md#cuda-driver-version-is-insufficient) 配置适用于所用 GPU 的 CUDA 兼容库。安装和启动 vLLM、运行 GPU 版 SDK 的终端都需要设置相应的环境变量。diff --git a/docs/minicpmvocr_tutorial_en.md b/docs/minicpmvocr_tutorial_en.mdindex 9c46d3a..e4a5410 100644--- a/docs/minicpmvocr_tutorial_en.md+++ b/docs/minicpmvocr_tutorial_en.md@@ -38,7 +38,7 @@ python -m pip install "paddlepaddle-gpu==3.3.0" \### 2. Set up the inference environment-Choose one inference service. This tutorial uses vLLM; for SGLang and llama.cpp, see the [SGLang deployment guide](./sglang_deploy.md) and [llama.cpp deployment guide](./llamacpp_deploy.md).+Choose one inference service. This tutorial uses vLLM; for other backends, see the [Ollama deployment guide](./ollama_deploy.md), [SGLang deployment guide](./sglang_deploy.md), and [llama.cpp deployment guide](./llamacpp_deploy.md).> **Older drivers**: For drivers below 580, first configure a CUDA compatibility library suitable for your GPU as described in the [FAQ](./faq_en.md#cuda-driver-version-is-insufficient). Set its environment variables in each terminal used to install or start vLLM, or run the GPU SDK.diff --git a/docs/ollama_deploy.md b/docs/ollama_deploy.mdnew file mode 100644index 0000000..2ca2a55--- /dev/null+++ b/docs/ollama_deploy.md@@ -0,0 +1,104 @@+# Ollama Deployment Guide++[中文](./ollama_deploy_zh.md)++[← Back to README](../README.md)++Deploy MiniCPM-OCR 1.0 with Ollama using the **Q8_0 main model, Q8_0 DSpark draft head, and F16 4× vision projector** together, with DSpark speculative decoding enabled. Run the commands below from the **root of this repository** in Bash.++## 1. Install Ollama++Use **Ollama v0.40.12 or later**. Install the reference version on Linux:++```bash+curl -fsSL https://ollama.com/install.sh | OLLAMA_VERSION=0.40.12 sh+ollama --version+```++For macOS and Windows, use the [official downloads](https://ollama.com/download). If Ollama is already installed, check its version and restart the service after upgrading. GPU and driver requirements are listed in the [Ollama GPU documentation](https://docs.ollama.com/gpu).++## 2. Download the main model, projector, and DSpark draft++Download the main model and projector from the [main model repository](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf), and the matching quantized draft from the [DSpark GGUF repository](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark-gguf). All three files are required; use matched weights from the same 1.0 release:++| File | Approximate size | Purpose |+|---|---|---|+| `MiniCPM-OCR-1_0-Q8_0.gguf` | 0.81 GB | Q8_0 main model |+| `mmproj-model-f16-4x.gguf` | 0.90 GB | F16 vision projector, required for image input |+| `dspark-MiniCPM-OCR-1_0-Q8_0.gguf` | 0.33 GB | Q8_0 DSpark draft model for speculative decoding |++```bash+mkdir -p ./models/MiniCPM-OCR-1.0-gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf/resolve/main/MiniCPM-OCR-1_0-Q8_0.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf/resolve/main/mmproj-model-f16-4x.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark-gguf/resolve/main/dspark-MiniCPM-OCR-1_0-Q8_0.gguf+```++If Hugging Face is unreachable, create the same directory and use the official ModelScope [main model repository](https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf) and [DSpark repository](https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-DSpark-gguf) instead:++```bash+mkdir -p ./models/MiniCPM-OCR-1.0-gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf/resolve/master/MiniCPM-OCR-1_0-Q8_0.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf/resolve/master/mmproj-model-f16-4x.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-DSpark-gguf/resolve/master/dspark-MiniCPM-OCR-1_0-Q8_0.gguf+```++## 3. Start the service++In a service terminal at the repository root:++```bash+export OLLAMA_HOST=127.0.0.1:18499+export OLLAMA_MODELS="$PWD/models/ollama"+export OLLAMA_NUM_PARALLEL=1+export OLLAMA_MAX_LOADED_MODELS=1++ollama serve+```++Keep this terminal running. Ollama selects the inference backend automatically. On Linux with NVIDIA GPUs, use `CUDA_VISIBLE_DEVICES=0 ollama serve` to select GPU 0.++This instance uses port `18499` and stores imported models in `models/ollama`. The default Ollama port is `11434`; use the same host and port in the registration commands below. If you change the port, update all addresses accordingly.++## 4. Register the model and check the service++Open another terminal at the repository root:++```bash+export OLLAMA_HOST=127.0.0.1:18499++cat > ./models/MiniCPM-OCR-1.0-gguf/Modelfile <<'EOF'+FROM ./MiniCPM-OCR-1_0-Q8_0.gguf+FROM ./mmproj-model-f16-4x.gguf+DRAFT ./dspark-MiniCPM-OCR-1_0-Q8_0.gguf+REQUIRES 0.40.12+PARAMETER num_ctx 8192+PARAMETER temperature 0+PARAMETER draft_num_predict 15+EOF++ollama create minicpm-ocr -f ./models/MiniCPM-OCR-1.0-gguf/Modelfile+ollama show minicpm-ocr+ollama show minicpm-ocr --modelfile++curl --noproxy 127.0.0.1 -f http://127.0.0.1:18499/api/version+curl --noproxy 127.0.0.1 -f http://127.0.0.1:18499/api/tags+```++The two `FROM` lines load the main model and vision projector; `DRAFT` loads the matching DSpark draft model. All paths are relative to the Modelfile. `PARAMETER draft_num_predict 15` sets the maximum number of draft tokens per step to 15. Keep the official GGUF's embedded chat template.++`ollama show --modelfile` should include `DRAFT` and `PARAMETER draft_num_predict 15`, `ollama show` should report the `vision` capability, `/api/version` should return version `0.40.12` or later, and `/api/tags` should list `minicpm-ocr:latest`. The service is available at **`http://127.0.0.1:18499`**, with the local model name **`minicpm-ocr`**. Ollama loads the main and draft models on the first inference request; the underlying llama.cpp automatically selects `draft-dspark` from the draft file metadata.++On the first inference request, the service log should show `adding speculative implementation 'draft-dspark'`. After generation, look for `draft acceptance` or `statistics ... draft-dspark` to confirm that DSpark participated in decoding.diff --git a/docs/ollama_deploy_zh.md b/docs/ollama_deploy_zh.mdnew file mode 100644index 0000000..4116bdd--- /dev/null+++ b/docs/ollama_deploy_zh.md@@ -0,0 +1,104 @@+# Ollama 部署指南++[English](./ollama_deploy.md)++[← 返回首页](../README_zh.md)++使用 Ollama 部署 MiniCPM-OCR 1.0,联合加载 **Q8_0 主模型、Q8_0 DSpark 草稿模型(加速头)和 F16 4× 视觉投影器**,启用 DSpark 投机解码。以下命令均在 **本仓库根目录** 的 Bash 终端执行。++## 1. 安装 Ollama++使用 **Ollama v0.40.12 或更新版本**。在 Linux 上安装本文参考版本:++```bash+curl -fsSL https://ollama.com/install.sh | OLLAMA_VERSION=0.40.12 sh+ollama --version+```++macOS 和 Windows 安装方式见 [官方下载页](https://ollama.com/download)。已安装 Ollama 时先检查版本,升级后重启服务。GPU 和驱动要求见 [Ollama GPU 文档](https://docs.ollama.com/gpu)。++## 2. 下载主模型、视觉投影器和 DSpark 草稿模型++从 [主模型仓库](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf) 下载主模型和视觉投影器,从 [DSpark GGUF 仓库](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark-gguf) 下载配套量化草稿模型。三个文件均需下载,并使用同一 1.0 发布版本的配套权重:++| 文件 | 大小约 | 用途 |+|---|---|---|+| `MiniCPM-OCR-1_0-Q8_0.gguf` | 0.81 GB | Q8_0 主模型 |+| `mmproj-model-f16-4x.gguf` | 0.90 GB | F16 视觉投影器,图片输入必需 |+| `dspark-MiniCPM-OCR-1_0-Q8_0.gguf` | 0.33 GB | Q8_0 DSpark 草稿模型,用于投机解码 |++```bash+mkdir -p ./models/MiniCPM-OCR-1.0-gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf/resolve/main/MiniCPM-OCR-1_0-Q8_0.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-gguf/resolve/main/mmproj-model-f16-4x.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark-gguf/resolve/main/dspark-MiniCPM-OCR-1_0-Q8_0.gguf+```++如无法访问 Hugging Face,创建相同目录后,改从 ModelScope 的 [主模型仓库](https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf) 和 [DSpark 仓库](https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-DSpark-gguf) 下载:++```bash+mkdir -p ./models/MiniCPM-OCR-1.0-gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/MiniCPM-OCR-1_0-Q8_0.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf/resolve/master/MiniCPM-OCR-1_0-Q8_0.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/mmproj-model-f16-4x.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-gguf/resolve/master/mmproj-model-f16-4x.gguf+curl -fL --retry 3 \+ -o ./models/MiniCPM-OCR-1.0-gguf/dspark-MiniCPM-OCR-1_0-Q8_0.gguf \+ https://modelscope.cn/models/OpenBMB/MiniCPM-OCR-1.0-DSpark-gguf/resolve/master/dspark-MiniCPM-OCR-1_0-Q8_0.gguf+```++## 3. 启动服务++在服务终端中进入仓库根目录:++```bash+export OLLAMA_HOST=127.0.0.1:18499+export OLLAMA_MODELS="$PWD/models/ollama"+export OLLAMA_NUM_PARALLEL=1+export OLLAMA_MAX_LOADED_MODELS=1++ollama serve+```++保持此终端运行。Ollama 自动选择推理后端。在使用 NVIDIA GPU 的 Linux 系统上,可通过 `CUDA_VISIBLE_DEVICES=0 ollama serve` 选择 GPU 0。++此实例使用 `18499` 端口,将导入后的模型保存在 `models/ollama`。Ollama 默认端口为 `11434`,下文注册模型时应连接同一个地址和端口;更换端口时同步修改所有命令中的地址。++## 4. 注册模型并检查服务++新开一个终端,进入仓库根目录:++```bash+export OLLAMA_HOST=127.0.0.1:18499++cat > ./models/MiniCPM-OCR-1.0-gguf/Modelfile <<'EOF'+FROM ./MiniCPM-OCR-1_0-Q8_0.gguf+FROM ./mmproj-model-f16-4x.gguf+DRAFT ./dspark-MiniCPM-OCR-1_0-Q8_0.gguf+REQUIRES 0.40.12+PARAMETER num_ctx 8192+PARAMETER temperature 0+PARAMETER draft_num_predict 15+EOF++ollama create minicpm-ocr -f ./models/MiniCPM-OCR-1.0-gguf/Modelfile+ollama show minicpm-ocr+ollama show minicpm-ocr --modelfile++curl --noproxy 127.0.0.1 -f http://127.0.0.1:18499/api/version+curl --noproxy 127.0.0.1 -f http://127.0.0.1:18499/api/tags+```++两个 `FROM` 分别加载主模型与视觉投影器,`DRAFT` 加载配套的 DSpark 草稿模型;所有路径均相对于 Modelfile 所在目录。`PARAMETER draft_num_predict 15` 将每轮草稿 token 上限设为 15。保留官方 GGUF 内嵌的对话模板。++`ollama show --modelfile` 应包含 `DRAFT` 和 `PARAMETER draft_num_predict 15`,`ollama show` 应包含 `vision` 能力,`/api/version` 应返回 `0.40.12` 或更新版本,`/api/tags` 应列出 `minicpm-ocr:latest`。服务地址为 **`http://127.0.0.1:18499`**,本地模型名为 **`minicpm-ocr`**。Ollama 会在首次推理请求时加载主模型和草稿模型,底层 llama.cpp 根据草稿文件元数据自动选择 `draft-dspark`。++首次推理时,服务日志应出现 `adding speculative implementation 'draft-dspark'`,生成后应有 `draft acceptance` 或 `statistics ... draft-dspark` 统计,用于确认 DSpark 实际参与了解码。diff --git a/docs/vllm_deploy.md b/docs/vllm_deploy.mdindex 1111a43..3401a79 100644--- a/docs/vllm_deploy.md+++ b/docs/vllm_deploy.md@@ -2,15 +2,16 @@[English](./vllm_deploy_en.md)-本文介绍如何使用 vLLM 部署 MiniCPM-OCR 1.0,包括 OpenAI 兼容 API 服务、图片识别、离线推理,以及 SDK 文档解析。+[← 返回首页](../README_zh.md)++本文参照 [SDK 使用教程](./minicpmvocr_tutorial.md#3-启动服务)中的服务部署方式,使用 vLLM 部署 MiniCPM-OCR 1.0,**默认启用 DSpark 推测解码**,包括 OpenAI 兼容 API 服务、图片识别、离线推理,以及 SDK 文档解析。以下 Shell 命令均在**本仓库根目录**通过 Bash 执行。- 主模型:[MiniCPM-OCR-1.0](https://huggingface.co/openbmb/MiniCPM-OCR-1.0)-- DSpark 草稿模型:[MiniCPM-OCR-1.0-DSpark](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark),加速部署见 [SDK 使用教程](./minicpmvocr_tutorial.md#3-启动服务)+- DSpark 草稿模型:[MiniCPM-OCR-1.0-DSpark](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark),需要与配套主模型一起使用。- vLLM 版本:`xxx`- Transformers 版本:`xxx`- Python 版本:`xxx`-## 1. 环境准备为 vLLM 创建独立环境,安装指定版本:@@ -28,36 +29,53 @@ pip install 'vllm==xxx' 'transformers==xxx' 'openai==xxx' pillowpython -c "import vllm, transformers; print('vllm', vllm.__version__, '| transformers', transformers.__version__)"```-从 [openbmb/MiniCPM-OCR-1.0](https://huggingface.co/openbmb/MiniCPM-OCR-1.0) 将完整主模型下载到本地目录 `/path/to/MiniCPM-OCR`。后续示例统一使用该路径。+后续示例使用 `openbmb/MiniCPM-OCR-1.0` 和 `openbmb/MiniCPM-OCR-1.0-DSpark`。如需离线部署,请提前下载两个完整模型仓库,将这两个 ID 分别替换为匹配的本地主模型和草稿模型目录,并保留 tokenizer、processor、配置和聊天模板文件。## 2. API 服务部署### 2.1 启动 API 服务+在仓库根目录新开一个终端,激活上文创建的 Conda 环境。以下命令沿用教程的 DSpark 配置,并为后续客户端示例添加示例 API 密钥:+```bash-vllm serve /path/to/MiniCPM-OCR \+conda activate minicpm-ocr-vllm++vllm serve openbmb/MiniCPM-OCR-1.0 \--served-model-name minicpm-ocr \--host 127.0.0.1 \--port 18499 \- --dtype auto \- --max-model-len 16384 \- --gpu-memory-utilization 0.8 \- --max-num-batched-tokens 8192 \- --limit-mm-per-prompt '{"image":1,"video":0}' \+ --max-model-len 40960 \+ --max-num-batched-tokens 40960 \+ --max-num-seqs 32 \+ --limit-mm-per-prompt '{"image":36,"video":0}' \+ --attention-backend FLASH_ATTN \+ --mm-encoder-attn-backend FLASH_ATTN \+ --mm-processor-kwargs '{"downsample_mode":"4x"}' \+ --gpu-memory-utilization 0.85 \--trust-request-mm-kwargs \- --mm-processor-kwargs '{"downsample_mode":"4x","max_slice_nums":9}' \+ --speculative-config '{+ "method": "dspark",+ "model": "openbmb/MiniCPM-OCR-1.0-DSpark",+ "num_speculative_tokens": 15,+ "enable_adaptive_verification": false+ }' \+ --default-chat-template-kwargs '{"enable_thinking":false}' \--api-key token-abc123```-- `/path/to/MiniCPM-OCR`:本地主模型目录,也可替换为模型仓库 ID `openbmb/MiniCPM-OCR-1.0`。+- `openbmb/MiniCPM-OCR-1.0`:主模型;`--speculative-config` 中的 `model` 指向配套 DSpark 草稿模型。+- `--speculative-config`:启用 DSpark,每步最多生成 15 个草稿 token,并关闭自适应验证,与教程保持一致。删除该参数及其完整 JSON 配置即可使用普通解码,此时无需草稿模型。- `--served-model-name`:客户端请求使用的模型名称。- `--max-model-len`:最大上下文长度,包含输入与输出 token。- `--gpu-memory-utilization`:服务可使用的 GPU 显存比例。- `--max-num-batched-tokens`:批处理 token 预算。-- `--limit-mm-per-prompt`:本例每次请求允许一张图片,并关闭视频输入及其启动预热;PDF 由 SDK 转成图片后识别。+- `--max-num-seqs`:同时处理的最大序列数。+- `--limit-mm-per-prompt`:每次请求最多允许 36 张图片,并关闭视频输入及其启动预热;PDF 由 SDK 转成图片后识别。+- `--attention-backend` 和 `--mm-encoder-attn-backend`:为解码和多模态编码器选择 Flash Attention。+- `--default-chat-template-kwargs`:默认关闭思考。- `--api-key`:示例访问密钥,实际部署时替换为自己的密钥。-上下文长度、显存比例和批处理预算为示例值,需根据模型配置及设备调整。SDK 会逐请求发送 `mm_processor_kwargs`,因此这里显式允许该参数。固定 vLLM 版本必须支持 `downsample_mode`、`max_slice_nums` 及 SDK 的采样字段;若不支持,不能仅凭服务能启动就认定可接入 SDK。`--trust-remote-code` 及相关补丁以最终模型适配要求为准。+上下文长度、显存比例、并发和批处理预算与教程一致,需根据可用显存调整,同时考虑主模型和草稿模型的占用。SDK 会逐请求发送 `mm_processor_kwargs`,因此服务显式允许该参数。安装的 vLLM 和 Transformers 版本需支持 MiniCPM-OCR 与 DSpark。服务启动后,在另一终端检查模型列表:@@ -107,7 +125,7 @@ response = client.chat.completions.create("repetition_penalty": 1.0,"no_repeat_ngram_size": 100,"chat_template_kwargs": {"enable_thinking": False},- "mm_processor_kwargs": {"downsample_mode": "4x", "max_slice_nums": 9},+ "mm_processor_kwargs": {"downsample_mode": "4x"},},)@@ -139,22 +157,34 @@ print("finish_reason:", response.choices[0].finish_reason)## 3. 离线推理-不启动 HTTP 服务时,可在 vLLM 环境中直接调用模型:+不启动 HTTP 服务时,可在 vLLM 环境中直接调用主模型,并使用相同的 DSpark 草稿配置。使用仓库 ID 时,首次运行会下载文件;如需断网运行,请使用上文说明的本地路径。```pythonfrom PIL import Imagefrom transformers import AutoProcessorfrom vllm import LLM, SamplingParams-model_path = "/path/to/MiniCPM-OCR"+model_path = "openbmb/MiniCPM-OCR-1.0"+draft_model_path = "openbmb/MiniCPM-OCR-1.0-DSpark"image = Image.open("sample.png").convert("RGB")processor = AutoProcessor.from_pretrained(model_path)llm = LLM(model=model_path,- max_model_len=16384,- gpu_memory_utilization=0.8,- limit_mm_per_prompt={"image": 1, "video": 0},+ max_model_len=40960,+ max_num_batched_tokens=40960,+ max_num_seqs=32,+ gpu_memory_utilization=0.85,+ limit_mm_per_prompt={"image": 36, "video": 0},+ attention_backend="FLASH_ATTN",+ mm_encoder_attn_backend="FLASH_ATTN",+ mm_processor_kwargs={"downsample_mode": "4x"},+ speculative_config={+ "method": "dspark",+ "model": draft_model_path,+ "num_speculative_tokens": 15,+ "enable_adaptive_verification": False,+ },)messages = [@@ -170,6 +200,7 @@ prompt = processor.apply_chat_template(messages,tokenize=False,add_generation_prompt=True,+ enable_thinking=False,)outputs = llm.generate(@@ -186,7 +217,7 @@ print(outputs[0].outputs[0].text)print("finish_reason:", outputs[0].outputs[0].finish_reason)```-该示例以模型提供兼容的 `AutoProcessor` 和聊天模板为前提;自定义处理器加载方式、thinking 开关与停止 token 应随正式模型版本补齐,不能直接套用其他 MiniCPM-V 版本的设置。离线推理会单独加载模型,不要在显存不足时与 API 服务同时运行。+该示例使用模型自身的处理器和聊天模板,并关闭思考。离线推理会单独加载一组主模型和 DSpark 草稿模型,不要在显存不足时与 API 服务同时运行。## 4. 使用 SDK 解析文档@@ -198,7 +229,7 @@ API 图片识别适合单个图像块;PDF 或整页文档可使用 MiniCPM-OCRminicpmvocr parse --config sdk/minicpmvocr/configs/minicpmvocr.yaml \--backend vllm --server-url http://127.0.0.1:18499 --model minicpm-ocr \--set layout.model_dir=/path/to/PP-DocLayoutV3 --set layout.device=cpu \- --set engine.max_model_len=16384 \+ --set engine.max_model_len=40960 \--set engine.api_key=token-abc123 \--input document.pdf --output document.md```@@ -207,7 +238,7 @@ minicpmvocr parse --config sdk/minicpmvocr/configs/minicpmvocr.yaml \## 注意事项-1. **版本匹配**:使用发布指定的 vLLM 与 Transformers 版本,不使用浮动版本替代。+1. **版本匹配**:使用支持 MiniCPM-OCR 与 DSpark 的配套 vLLM 和 Transformers 版本。`xxx` 版本占位符沿用当前主分支的环境准备说明,安装前需要填入具体版本。2. **模型配置**:保留完整 tokenizer、processor 和聊天模板文件;停止 token 与特殊模板参数使用模型自身配置。3. **显存调整**:优先根据显存调整上下文、并发和批处理预算,不将示例设置视为硬件支持承诺。4. **接口地址**:OpenAI Python 客户端使用带 `/v1` 的地址,MiniCPM-OCR SDK 配置使用不带 `/v1` 的服务根地址。diff --git a/docs/vllm_deploy_en.md b/docs/vllm_deploy_en.mdindex 9fcb240..a614834 100644--- a/docs/vllm_deploy_en.md+++ b/docs/vllm_deploy_en.md@@ -2,10 +2,12 @@[中文](./vllm_deploy.md)-This guide covers deploying MiniCPM-OCR 1.0 with vLLM, including an OpenAI-compatible API service, image recognition, offline inference, and document parsing with the SDK.+[← Back to README](../README.md)++This guide deploys MiniCPM-OCR 1.0 with **DSpark speculative decoding enabled by default**, following the service setup in the [SDK tutorial](./minicpmvocr_tutorial_en.md#3-start-the-service). It includes an OpenAI-compatible API service, image recognition, offline inference, and document parsing with the SDK. Run shell commands from the **root of this repository** in Bash.- Main model: [MiniCPM-OCR-1.0](https://huggingface.co/openbmb/MiniCPM-OCR-1.0)-- DSpark draft model: [MiniCPM-OCR-1.0-DSpark](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark). For accelerated deployment, see the [SDK tutorial](./minicpmvocr_tutorial_en.md#3-start-the-service).+- DSpark draft model: [MiniCPM-OCR-1.0-DSpark](https://huggingface.co/openbmb/MiniCPM-OCR-1.0-DSpark), required alongside the matching main model.- vLLM version: `xxx`- Transformers version: `xxx`- Python version: `xxx`@@ -27,36 +29,53 @@ Check the installed versions:python -c "import vllm, transformers; print('vllm', vllm.__version__, '| transformers', transformers.__version__)"```-Download the complete main model from [openbmb/MiniCPM-OCR-1.0](https://huggingface.co/openbmb/MiniCPM-OCR-1.0) to the local directory `/path/to/MiniCPM-OCR`. The examples below consistently use this path.+The examples below use `openbmb/MiniCPM-OCR-1.0` and `openbmb/MiniCPM-OCR-1.0-DSpark`. For offline deployment, download both complete model repositories and replace these IDs with the matching local main-model and draft-model directories. Keep the tokenizer, processor, configuration, and chat template files.## 2. API Service Deployment### 2.1 Start the API Service+Open a new terminal at the repository root and activate the Conda environment created above. The command follows the tutorial's DSpark configuration and adds an example API key for the client examples below:+```bash-vllm serve /path/to/MiniCPM-OCR \+conda activate minicpm-ocr-vllm++vllm serve openbmb/MiniCPM-OCR-1.0 \--served-model-name minicpm-ocr \--host 127.0.0.1 \--port 18499 \- --dtype auto \- --max-model-len 16384 \- --gpu-memory-utilization 0.8 \- --max-num-batched-tokens 8192 \- --limit-mm-per-prompt '{"image":1,"video":0}' \+ --max-model-len 40960 \+ --max-num-batched-tokens 40960 \+ --max-num-seqs 32 \+ --limit-mm-per-prompt '{"image":36,"video":0}' \+ --attention-backend FLASH_ATTN \+ --mm-encoder-attn-backend FLASH_ATTN \+ --mm-processor-kwargs '{"downsample_mode":"4x"}' \+ --gpu-memory-utilization 0.85 \--trust-request-mm-kwargs \- --mm-processor-kwargs '{"downsample_mode":"4x","max_slice_nums":9}' \+ --speculative-config '{+ "method": "dspark",+ "model": "openbmb/MiniCPM-OCR-1.0-DSpark",+ "num_speculative_tokens": 15,+ "enable_adaptive_verification": false+ }' \+ --default-chat-template-kwargs '{"enable_thinking":false}' \--api-key token-abc123```-- `/path/to/MiniCPM-OCR`: Local main-model directory; you can also use the model repository ID `openbmb/MiniCPM-OCR-1.0`.+- `openbmb/MiniCPM-OCR-1.0`: Main model; the `model` field in `--speculative-config` points to the matching DSpark draft model.+- `--speculative-config`: Enables DSpark with up to 15 speculative tokens per step and adaptive verification disabled, matching the tutorial. Remove this argument and its entire JSON configuration to use standard decoding without a draft model.- `--served-model-name`: Model name used in client requests.- `--max-model-len`: Maximum context length, including input and output tokens.- `--gpu-memory-utilization`: Fraction of GPU memory available to the service.- `--max-num-batched-tokens`: Batched token budget.-- `--limit-mm-per-prompt`: This example allows one image per request and disables video input and its startup warmup; the SDK converts PDFs into images for recognition.+- `--max-num-seqs`: Maximum number of sequences processed concurrently.+- `--limit-mm-per-prompt`: Allows up to 36 images per request and disables video input and its startup warmup; the SDK converts PDFs into images for recognition.+- `--attention-backend` and `--mm-encoder-attn-backend`: Select Flash Attention for decoding and the multimodal encoder.+- `--default-chat-template-kwargs`: Disables thinking by default.- `--api-key`: Example access key; replace it with your own key for deployment.-Context length, GPU memory fraction, and batched token budget are example values; adjust them to the model configuration and device. The SDK sends `mm_processor_kwargs` in each request, so the service explicitly permits that parameter. The pinned vLLM version must support `downsample_mode`, `max_slice_nums`, and the SDK's sampling fields. Successful startup alone does not establish SDK compatibility. Follow the final model integration requirements for `--trust-remote-code` and any patches.+Context length, GPU memory fraction, concurrency, and batched token budget follow the tutorial; adjust them to available memory, accounting for both the main and draft models. The SDK sends `mm_processor_kwargs` in each request, so the service explicitly permits that parameter. The installed vLLM and Transformers versions must support MiniCPM-OCR and DSpark.After startup, check the model list from another terminal:@@ -106,7 +125,7 @@ response = client.chat.completions.create("repetition_penalty": 1.0,"no_repeat_ngram_size": 100,"chat_template_kwargs": {"enable_thinking": False},- "mm_processor_kwargs": {"downsample_mode": "4x", "max_slice_nums": 9},+ "mm_processor_kwargs": {"downsample_mode": "4x"},},)@@ -138,22 +157,34 @@ A successful health check only confirms that the service is reachable; it does n## 3. Offline Inference-To run without an HTTP service, call the model directly in the vLLM environment:+To run without an HTTP service, call the main model with the same DSpark draft configuration in the vLLM environment. Repository IDs download files on the first run; use the local paths described above to run without network access.```pythonfrom PIL import Imagefrom transformers import AutoProcessorfrom vllm import LLM, SamplingParams-model_path = "/path/to/MiniCPM-OCR"+model_path = "openbmb/MiniCPM-OCR-1.0"+draft_model_path = "openbmb/MiniCPM-OCR-1.0-DSpark"image = Image.open("sample.png").convert("RGB")processor = AutoProcessor.from_pretrained(model_path)llm = LLM(model=model_path,- max_model_len=16384,- gpu_memory_utilization=0.8,- limit_mm_per_prompt={"image": 1, "video": 0},+ max_model_len=40960,+ max_num_batched_tokens=40960,+ max_num_seqs=32,+ gpu_memory_utilization=0.85,+ limit_mm_per_prompt={"image": 36, "video": 0},+ attention_backend="FLASH_ATTN",+ mm_encoder_attn_backend="FLASH_ATTN",+ mm_processor_kwargs={"downsample_mode": "4x"},+ speculative_config={+ "method": "dspark",+ "model": draft_model_path,+ "num_speculative_tokens": 15,+ "enable_adaptive_verification": False,+ },)messages = [@@ -169,6 +200,7 @@ prompt = processor.apply_chat_template(messages,tokenize=False,add_generation_prompt=True,+ enable_thinking=False,)outputs = llm.generate(@@ -185,7 +217,7 @@ print(outputs[0].outputs[0].text)print("finish_reason:", outputs[0].outputs[0].finish_reason)```-This example assumes that the model provides a compatible `AutoProcessor` and chat template. Custom processor loading, the thinking switch, and stop tokens need to be completed for the final model release; do not directly reuse settings from other MiniCPM-V versions. Offline inference loads a separate model instance, so avoid running it alongside the API service when GPU memory is insufficient.+This example uses the model's own processor and chat template with thinking disabled. Offline inference loads a separate main model and DSpark draft instance, so avoid running it alongside the API service when GPU memory is insufficient.## 4. Parse Documents with the SDK@@ -197,7 +229,7 @@ Follow the [SDK tutorial](./minicpmvocr_tutorial_en.md#installation) to installminicpmvocr parse --config sdk/minicpmvocr/configs/minicpmvocr.yaml \--backend vllm --server-url http://127.0.0.1:18499 --model minicpm-ocr \--set layout.model_dir=/path/to/PP-DocLayoutV3 --set layout.device=cpu \- --set engine.max_model_len=16384 \+ --set engine.max_model_len=40960 \--set engine.api_key=token-abc123 \--input document.pdf --output document.md```@@ -206,7 +238,7 @@ The command should produce `document.md` with no failed pages or recognition reg## Notes-1. **Version compatibility**: Use the vLLM and Transformers versions specified for the release; do not substitute unpinned versions.+1. **Version compatibility**: Use release-specific vLLM and Transformers versions with MiniCPM-OCR and DSpark support. The `xxx` version placeholders follow the current main-branch environment setup and must be filled in before installation.2. **Model configuration**: Keep the complete tokenizer, processor, and chat template files. Use the model's own stop tokens and special template parameters.3. **GPU memory**: Adjust context length, concurrency, and batched token budget to available memory. Example settings are not a guarantee of hardware support.4. **Endpoint URLs**: The OpenAI Python client uses a URL with `/v1`; the MiniCPM-OCR SDK uses the service root URL without `/v1`.diff --git a/sdk/minicpmvocr/postprocess/markdown.py b/sdk/minicpmvocr/postprocess/markdown.pyindex 6f5fc42..b5115db 100644--- a/sdk/minicpmvocr/postprocess/markdown.py+++ b/sdk/minicpmvocr/postprocess/markdown.py@@ -286,6 +286,8 @@ def assemble_markdown(blocks: list, prompt_cfg: dict) -> str:parts.append(formula_fmt[label].format(content=content))elif label == "table":parts.append(table_fmt.format(content=content))+ elif label in ("header_image", "footer_image") and content:+ parts.append(content)elif label in ("image", "header_image", "footer_image"):parts.append(image_fmt)elif label == "chart":diff --git a/sdk/tests/test_skip_vlm_labels.py b/sdk/tests/test_skip_vlm_labels.pyindex 3caa8c7..c876e91 100644--- a/sdk/tests/test_skip_vlm_labels.py+++ b/sdk/tests/test_skip_vlm_labels.py@@ -164,3 +164,36 @@ def test_chart_otsl_is_converted_to_html():out = _text._normalize_crop_text(otsl, "chart")assert "<table" in out and "<fcel>" not in outassert "Japan" in out and "7%" in out++[email protected]("label", ["header_image", "footer_image"])+def test_recognized_header_footer_images_are_kept_in_markdown(label):+ from minicpmvocr.postprocess.markdown import assemble_markdown++ prompt_map = _prompt_map({"skip_vlm_labels": ["image"]})+ assert prompts.get_element_cfg(prompt_map, label) is not None+ blocks = [+ {"order": 0, "label": label, "content": "COURT OFFICE / Page 12"},+ {"order": 1, "label": "text", "content": "Body text"},+ ]+ assert assemble_markdown(blocks, prompt_map) == "COURT OFFICE / Page 12\n\nBody text"++[email protected]("label", ["header_image", "footer_image"])+def test_header_footer_images_without_text_keep_the_placeholder(label):+ from minicpmvocr.postprocess.markdown import assemble_markdown++ assert assemble_markdown(+ [{"label": label, "content": ""}], _prompt_map({})+ ) == "![image]()"++[email protected]("label", ["header_image", "footer_image"])+def test_explicitly_ignored_header_footer_images_stay_hidden(label):+ from minicpmvocr.postprocess.markdown import assemble_markdown++ prompt_map = _prompt_map({"skip_vlm_labels": ["image"]})+ prompt_map["markdown_assembly"]["ignore_labels"] = [label]+ assert assemble_markdown(+ [{"label": label, "content": "COURT OFFICE"}], prompt_map+ ) == ""