|
前文PaddleOCR-VL-0.9B上手体验写了如何使用PaddleOCR-VL来做图片的识别过程,而真正生成环境部署时Paddle不推荐使用之前的方式。 因为Paddle的模型自成体系并做了优化,但PaddleOCR-VL-0.9B却使用的Hugging Face的模型格式,Paddle并未对其进行优化,所以推荐使用vllm和SGLang这样的专用大模型推理工具作为后端进行生成部署使用。 本文就分别介绍在用一台机器上使用vllm作为后端部署的场景和将整个OCR识别作为远端服务的场景

后端部署和服务部署的区别 PaddleOCR-VL实际的架构,大体描述如下: 要加快PaddleOCR-VL-0.9B的推理呢,需要分成两个独立的进程且部署在同一台设备中: 为了减少耦合性,进一步采用服务部署的方式,识别进程和服务可以分别部署于不同的设备节点上,架构如下:

Vllm后端部署 # 安装推理加速服务依赖
paddleocrinstall_genai_server_depsvllm 安装完成后,可通过paddleocr genai_server命令启动服务: paddleocrgenai_server--model_namePaddleOCR-VL-0.9B--backendvllm--port8118 该命令支持的参数如下: | |
|---|
--model_name | | --model_dir | | --host | | --port | | --backend | 后端名称,即使用的推理加速框架名称,可选vllm或sglang | --backend_config | |
客户端使用方法¶启动 VLM 推理服务后,客户端即可通过 PaddleOCR 调用该服务。 Python API 调用¶创建PaddleOCRVL对象时传入vl_rec_backend和vl_rec_server_url参数:
frompaddleocrimportPaddleOCRVLpipeline = PaddleOCRVL(vl_rec_backend="vllm-server", vl_rec_server_url="http://127.0.0.1:8118/v1")output = pipeline.predict("webwxgetmsgimg.jpeg")#print(output)forresinoutput: #res.print() res.save_to_json(save_path="output") res.save_to_markdown(save_path="output") print(res._to_json()) print(dir(res))
需要注意的地方¶ 1. 当前通过paddleocrinstall_genai_server_depsvllm安装的vllm版本为0.10.1,pytorch为2.8.0,如果你直接使用自己安装的版本时要做个参照,比之小的vllm版本如0.6.x时会因为不兼容报错。 2. 所需显存 vllm启动真实所需的显存大约为4.2GB,但实际情况下需要有更多的空闲显存才能启动,否则将报OOM。如果还有其它模型需要同时运行,一种取巧的方法是先启动vllm的,等启动成功后,再启动其它模型。 3. 配置修改 对于传入后端vllm的参数可以通过过/home/zhds/.local/lib/python3.10/site-packages/paddlex/inference/genai/configs/paddleocr_vl_09b.py文件进行修改 defget_config(backend):ifbackend=="fastdeploy":return{"gpu-memory-utilization":0.3,"max-model-len":16384,"max-num-batched-tokens":131072,"max-num-seqs":256,}elifbackend=="vllm":return{"trust-remote-code":True,"gpu-memory-utilization":0.3,"max-model-len":16384,"max-num-batched-tokens":131072,"api-server-count":4,"max-num-seqs":4,}elifbackend=="sglang":return{"trust-remote-code":True,"mem-fraction-static":0.5,"context-length":16384,"max-prefill-tokens":131072,}else:raiseValueError(f"Unsupportedbackend:{backend}")

SGLang后端部署 安装依赖 paddleocrinstall_genai_server_deps sglang 官方提供的sglang版本为0.5.2,可惜sglang的依赖flashinfer=0.3.1的版本太过复杂无法安装成功。手工安装flashinfer==0.5.2可以成功,但最后运行时会报参数个数错误,和sglang-0.5.2版本不匹配导致无法运行。
解决办法
最后通过单独安装sglang==0.5.5版本解决,有可能单独安装torchao, pytorch默认不带此组件。 启动命令: paddleocrgenai_server--model_namePaddleOCR-VL-0.9B--backendsglang--port8119
输出如下: [2025-11-22 15:02:40] INFO: Application startup complete.[2025-11-22 15:02:40] INFO: Uvicorn runningonhttp://localhost:8119 (Press CTRL+C to quit)[2025-11-22 15:02:40] INFO: 127.0.0.1:53766-"GET /health HTTP/1.1"503Service Unavailable[2025-11-22 15:02:41] INFO: 127.0.0.1:53774-"GET /get_model_info HTTP/1.1"200OK[2025-11-22 15:02:41] Prefill batch,#new-seq: 1, #new-token: 6, #cached-token: 0, token usage: 0.00, #running-req: 0, #queue-req: 0,[2025-11-22 15:02:41] INFO: 127.0.0.1:53790-"GET /health HTTP/1.1"503Service Unavailable[2025-11-22 15:02:42] INFO: 127.0.0.1:53802-"GET /health HTTP/1.1"503Service Unavailable[2025-11-22 15:02:43] INFO: 127.0.0.1:53784-" OST /generate HTTP/1.1"200OK[2025-11-22 15:02:43] The serverisfired upandready to roll![2025-11-22 15:02:43] Prefill batch,#new-seq: 1, #new-token: 1, #cached-token: 0, token usage: 0.00, #running-req: 0, #queue-req: 0,[2025-11-22 15:02:44] INFO: 127.0.0.1:53808-"GET /health HTTP/1.1"200OK[2025/11/22 15:02:44] paddleocr INFO: The PaddleOCR GenAI server has been started. You can either: 1.Set the server URLinthe moduleorpipeline configurationandcall the PaddleOCR CLIorPython API. For example: paddleocr doc_parser --input demo.png --vl_rec_backend sglang-server --vl_rec_server_url http://localhost:8119/v1 2.Make HTTP requests directly,orusingthe OpenAI client library.
占用显存: 4.5GB, 和vllm不同不会提前锁定显存,启动非常顺利 python代码: frompaddleocrimportPaddleOCRVLpipeline=PaddleOCRVL(vl_rec_backend="sglang-server",vl_rec_server_url="http://127.0.0.1:8119/v1")output=pipeline.predict("webwxgetmsgimg.jpeg")#print(output)forresinoutput:#res.print()res.save_to_json(save_path="output")res.save_to_markdown(save_path="output")print(res._to_json())print(dir(res))

服务部署 执行以下命令,通过 PaddleX CLI 安装服务化部署插件 然后,使用 PaddleX CLI 启动服务器: paddlex--serve--pipelinePaddleOCR-VL 启动后将看到类似如下输出,服务器默认监听8080端口: INFO:Startedserverprocess[63108]INFO:Waitingforapplicationstartup.INFO:Applicationstartupcomplete.INFO:Uvicornrunningonhttp://0.0.0.0:8080(PressCTRL+Ctoquit) 与服务化部署相关的命令行参数如下: | |
|---|
--pipeline | | --device | 产线部署设备。默认情况下,若 GPU 可用则使用 GPU,否则使用 CPU。 | --host | 服务器绑定的主机名或 IP 地址,默认为0.0.0.0。 | --port | | --use_hpip | 启用高性能推理模式。请参考高性能推理文档了解更多信息。 | --hpi_config | 高性能推理配置。请参考高性能推理文档了解更多信息。 |
上述服务启动使用的是transformer作为推理的,仅限测试和验证时使用。 客户端访问代码:
importbase64importrequestsimportpathlib
API_URL ="http://localhost:8080/layout-parsing"# 服务URL
image_path ="./demo.jpg"
# 对本地图像进行Base64编码withopen(image_path,"rb")asfile: image_bytes = file.read() image_data = base64.b64encode(image_bytes).decode("ascii")
payload = { "file": image_data,# Base64编码的文件内容或者文件URL "fileType":1,# 文件类型,1表示图像文件}
# 调用APIresponse = requests.post(API_URL, json=payload)
# 处理接口返回数据assertresponse.status_code ==200result = response.json()["result"]fori, resinenumerate(result["layoutParsingResults"]): print(res["prunedResult"]) md_dir = pathlib.Path(f"markdown_{i}") md_dir.mkdir(exist_ok=True) (md_dir /"doc.md").write_text(res["markdown"]["text"]) forimg_path, imginres["markdown"]["images"].items(): img_path = md_dir / img_path img_path.parent.mkdir(parents=True, exist_ok=True) img_path.write_bytes(base64.b64decode(img)) print(f"Markdown document saved at{md_dir /'doc.md'}") forimg_name, imginres["outputImages"].items(): img_path =f"{img_name}_{i}.jpg" pathlib.Path(img_path).parent.mkdir(exist_ok=True) withopen(img_path,"wb")asf: f.write(base64.b64decode(img)) print(f"Output image saved at{img_path}")

服务部署优化 调整服务化部署的 PaddleOCR-VL 配置只需以下三步:
5.1 生成配置文件¶paddlex--get_pipeline_configPaddleOCR-VL 默认在当前目录下生成PaddleOCR-VL.yaml文件,可以指定为其它目录
5.2 修改配置文件¶使用加速框架提升 VLM 推理性能 如需使用 vLLM 等加速框架提升 VLM 推理性能(第 2 节详细介绍如何启动 VLM 推理服务),可在产线配置文件中修改VLRecognition.genai_config.backend和VLRecognition.genai_config.server_url字段,例如: VLRecognition: ... genai_config: backend: vllm-server
server_url: http://127.0.0.1:8118/v1
启用文档图像预处理功能 默认配置启动的服务不支持文档预处理功能。若客户端调用该功能,将返回错误信息。如需启用文档预处理,请在产线配置文件中将use_doc_preprocessor设置为True,并使用修改后的配置文件启动服务。 禁用结果可视化功能 服务默认返回可视化结果,这会引入额外开销。如需禁用该功能,可在产线配置文件中添加如下配置: Serving: visualize:False
此外,也可在请求体中设置visualize字段为false,以针对单次请求禁用可视化。 5.3 应用配置文件¶paddlex--serve--pipelinePaddleOCR-VL.yaml 日志如下: Creatingmodel ' P-DocLayoutV2',None)Modelfilesalreadyexist.Usingcachedfiles.Toredownload,pleasedeletethedirectorymanually:`/home/zhds/.paddlex/official_models/PP-DocLayoutV2`./home/zhds/.local/lib/python3.10/site-packages/paddle/utils/cpp_extension/extension_utils.py:718:UserWarning:Noccachefound.Pleasebeawarethatrecompilingallsourcefilesmayberequired.Youcandownloadandinstallccachefrom:https://github.com/ccache/ccache/blob/master/doc/INSTALL.mdwarnings.warn(warning_message)Creatingmodel ' addleOCR-VL-0.9B',None)INFO:Startedserverprocess[1938346]INFO:Waitingforapplicationstartup.INFO:Applicationstartupcomplete.INFO:Uvicornrunningonhttp://0.0.0.0:8080(PressCTRL+Ctoquit)INFO:127.0.0.1:49176-" OST/layout-parsingHTTP/1.1"200OK |