vLLM推理

自 vLLM 0.11.0 版本起,Dots OCR 已正式集成到 vLLM 中,性能已得到验证,可以直接使用 vLLM docker 镜像(例如vllm/vllm-openai:v0.11.0)部署模型服务器。

# Launch vLLM model server
vllm serve rednote-hilab/dots.ocr --trust-remote-code --async-scheduling --gpu-memory-utilization 0.95

# vLLM API Demo
# See dots_ocr/model/inference.py for details on parameter and prompt settings 
# that help achieve the best output quality.
python3 ./demo/demo_vllm.py --prompt_mode prompt_layout_all_en

文档解析

# Parse all layout info, both detection and recognition
# Parse a single image
python3 dots_ocr/parser.py demo/demo_image1.jpg
# Parse a single PDF
python3 dots_ocr/parser.py demo/demo_pdf1.pdf  --num_thread 64  # try bigger num_threads for pdf with a large number of pages

# Layout detection only
python3 dots_ocr/parser.py demo/demo_image1.jpg --prompt prompt_layout_only_en

# Parse text only, except Page-header and Page-footer
python3 dots_ocr/parser.py demo/demo_image1.jpg --prompt prompt_ocr

# Parse layout info by bbox
python3 dots_ocr/parser.py demo/demo_image1.jpg --prompt prompt_grounding_ocr --bbox 163 241 1536 705

源码编译

# 1. 环境准备
conda create -n dots_ocr python=3.12          # 创建名为dots_ocr的conda虚拟环境,使用Python 3.12
conda activate dots_ocr                       # 激活虚拟环境

# 2. 项目下载
git clone <https://github.com/rednote-hilab/dots.ocr.git>    # 克隆dots.ocr项目代码
cd dots.ocr                                   # 进入项目目录

# 3. 安装PyTorch
pip install torch==2.7.0 torchvision==0.22.0 torchaudio==2.7.0 --index-url <https://download.pytorch.org/whl/cu128>
                                             # 安装PyTorch 2.7.0版本(支持CUDA 12.8)

# 4. 安装CUDA工具链
conda install cuda-toolkit -c nvidia        # 安装CUDA编译工具包,解决flash-attn编译问题

# 5. 第一次尝试安装项目依赖(失败)
pip install -e .                             # 以开发模式安装项目,但因为缺少CUDA工具链而失败

# 7. 下载模型权重
python3 tools/download_model.py             # 下载dots.ocr预训练模型权重文件

# model downloaded to /home/Ubuntu/dots.ocr/weights/DotsOCR

# 8. vLLM部署准备(失败,因为vllm未安装)
export hf_model_path=./weights/DotsOCR       # 设置模型路径环境变量
export PYTHONPATH=$(dirname "$hf_model_path"):$PYTHONPATH  # 添加模型路径到Python路径
sed -i '/^from vllm\.entrypoints\.cli\.main import main$/a\from DotsOCR import modeling_dots_ocr_vllm' `which vllm`
                                             # 尝试修改vllm配置文件(失败,因为vllm未安装)

# 9. 尝试启动vLLM服务(失败)
CUDA_VISIBLE_DEVICES=0 vllm serve ${hf_model_path} \
    --tensor-parallel-size 1 \
    --gpu-memory-utilization 0.95 \
    --chat-template-content-format string \
    --served-model-name model \
    --trust-remote-code                      # 尝试启动vLLM服务,但因为vllm未安装而失败

# 10. 使用HuggingFace方式测试(成功)
python3 demo/demo_hf.py                     # 使用HuggingFace Transformers直接运行模型演示,成功

# 11. 尝试Gradio Web界面(有问题)
python demo/demo_gradio.py                  # 启动Gradio Web演示界面,但出现跨域和资源加载问题

# 12. 尝试Gradio标注界面(有问题)
python demo/demo_gradio_annotion.py         # 启动Gradio图像标注演示界面,同样出现问题