PP-DocBee 是飞桨团队自研的一款专注于文档理解的多模态大模型,在中文文档理解任务上具有卓越表现。该模型通过近 500 万条文档理解类多模态数据集进行微调优化,各种数据集包括了通用VQA类、OCR类、图表类、text-rich文档类、数学和复杂推理类、合成数据类、纯文本数据等,并设置了不同训练数据配比。在学术界权威的几个英文文档理解评测榜单上,PP-DocBee基本都达到了同参数量级别模型的SOTA。在内部业务中文场景类的指标上,PP-DocBee也高于目前的热门开源和闭源模型。
| 组件 | 版本/状态 |
|---|---|
| 操作系统 | Ubuntu 22.04.5 LTS(aarch64) |
| Python | 3.11.15 |
| 驱动 | 25.2.0 |
| CANN | 8.5.2 |
| NPU | 910B3 |
docker pull swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.2-910b-ubuntu22.04-py3.11
export IMAGE=swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.2-910b-ubuntu22.04-py3.11
docker run --privileged \
--name PP-DocBee \
--net=host \
--shm-size=1g \
--device /dev/davinci_manager \
--device /dev/devmm_svm \
--device /dev/hisi_hdc \
-v /usr/local/dcmi:/usr/local/dcmi \
-v /usr/local/Ascend/driver/tools/hccn_tool:/usr/local/Ascend/driver/tools/hccn_tool \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
-v /etc/ascend_install.info:/etc/ascend_install.info \
-v /mnt:/mnt \
-w /workspace \
-itd $IMAGE /bin/bash| 包名 | 版本 | 用途 | 安装命令 |
|---|---|---|---|
paddlepaddle | 3.0.0.dev20250527 (CPU) | 仅需CPU版本,用于加载和转换 .pdparams 权重 | pip3 install paddlepaddle==3.0.0.dev20250527 |
torch | 2.9.0 (+cpu) | PyTorch 框架 | pip3 install torch==2.9.0 |
torch-npu | 2.9.0 | Ascend NPU 后端 | pip3 install torch-npu==2.9.0 |
torchvision | 0.24.0 | 图像处理(兼容 torch 2.9.0) | pip3 install torchvision==0.24.0 |
transformers | 5.9.0 | HF 模型加载 + Qwen2-VL 架构 | pip3 install transformers==5.9.0 |
safetensors | 0.7.0 | safetensors 权重格式 | pip3 install safetensors==0.7.0 |
# torch
python3 -c "import torch; print('torch:', torch.__version__)"
# torch_npu
python3 -c "
import torch, torch_npu
x = torch.randn(100, 100).to('npu:0')
y = torch.matmul(x, x.T)
print('NPU matmul OK, device:', y.device)
"
# paddle
python3 -c "import paddle; print('PaddlePaddle:', paddle.__version__)"
# transformers
python3 -c "from transformers import Qwen2VLConfig; print('transformers: OK')"
# safetensors
python3 -c "from safetensors.torch import load_file; print('safetensors: OK')"PP-DocBee-2B 是 PaddleX 官方模型,可直接通过如下链接下载
# 模型权重默认会存在 /root/.paddlex/official_models/ 目录,此案例均以此目录为例
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/official_inference_model/paddle3.0.0/PP-DocBee-2B_infer.tar
tar -xf PP-DocBee-2B_infer.tar下载后的文件结构
/root/.paddlex/official_models/PP-DocBee-2B/
├── model_state.pdparams (4.2GB, 核心权重文件)
├── config.json (4.0KB)
├── generation_config.json (241B)
├── tokenizer_config.json (5.0KB)
├── special_tokens_map.json (958B)
├── added_tokens.json (419B)
├── vocab.json (3.6MB)
├── merges.txt (1.6MB)
└── inference.yml (47B)模型权重转换脚本执行
python3 convert_bf16.py转换过程说明
| 步骤 | 说明 |
|---|---|
| 1. 加载 | paddle.load() 读取 model_state.pdparams(729 个 tensor),输入目录:/root/.paddlex/official_models/PP-DocBee-2B/ |
| 2. 格式转换 | Paddle 张量 → float32 numpy → torch 张量 |
| 3. 名称映射 | visual.* → model.visual.*, model.layers.* → model.language_model.layers.* |
| 4. 转置处理 | Paddle 线性层 [in, out] → PyTorch [out, in],嵌入层不转置 |
| 5. 精度转换 | float32 → bfloat16 |
| 6. 保存 | safetensors 格式,~4.42GB,输出目录:/root/.paddlex/official_models/PP-DocBee-2B-hf/ |
权重名称映射说明
| Paddle 命名 | HF Transformers 命名 |
|---|---|
visual.patch_embed.proj.weight | model.visual.patch_embed.proj.weight |
visual.blocks.0.mlp.fc1.weight | model.visual.blocks.0.mlp.fc1.weight |
model.layers.0.self_attn.q_proj.weight | model.language_model.layers.0.self_attn.q_proj.weight |
model.embed_tokens.weight | model.language_model.embed_tokens.weight |
model.norm.weight | model.language_model.norm.weight |
此外,HF Transformers 还需要生成图像处理器配置文件 preprocessor_config.json
python3 -c "
from transformers import Qwen2VLImageProcessor
Qwen2VLImageProcessor().save_pretrained('/root/.paddlex/official_models/PP-DocBee-2B-hf')
"生成的 preprocessor_config.json 内容:
{
"size": {"longest_edge": 1003520, "shortest_edge": 3136},
"do_convert_rgb": true,
"do_resize": true,
"resample": 3,
"do_rescale": true,
"rescale_factor": 0.00392156862745098,
"do_normalize": true,
"patch_size": 14,
"temporal_patch_size": 2,
"merge_size": 2,
"image_mean": [0.48145466, 0.4578275, 0.40821073],
"image_std": [0.26862954, 0.26130258, 0.27577711],
"image_processor_type": "Qwen2VLImageProcessor"
}/root/.paddlex/official_models/PP-DocBee-2B-hf/
├── model.safetensors (4.42GB, 转换后的 bfloat16 权重)
├── config.json (从原始模型复制)
├── generation_config.json (从原始模型复制)
├── tokenizer_config.json (从原始模型复制)
├── special_tokens_map.json (从原始模型复制)
├── added_tokens.json (从原始模型复制)
├── vocab.json (从原始模型复制)
├── merges.txt (从原始模型复制)
└── preprocessor_config.json (新生成,图像处理器配置)# 使用示例
python infer.py --device npu:2 --image-path ./medal_table.png
# 参数说明
--model-path: 模型名称,默认值:"~/.paddlex/official_models/PP-DocBee-2B-hf"
--image-path: 推理图片路径,默认值:"~/.paddlex/predict_input/medal_table.png"
--device: 设备id,默认值:"npu:0"
--prompt: 模型提示词,默认值:"识别这份表格的内容, 以markdown格式输出"
--max-new-tokens: 最大生成token数,默认值:1024
--ngram: N-gram 投机解码配置,设置为 0 时不使用,默认值:2测试图片 如下:

推理结果如下:
# python test_bf16.py --device npu:2 --image-path ./medal_table.png
Loading config...
Initializing model...
Loading bfloat16 weights...
Re-tied lm_head.weight to embed_tokens.weight
Moving to npu:2...
=== Text-only test (greedy) ===
[transformers] Setting `pad_token_id` to `eos_token_id`:151645 for open-end generation.
Input: The quick brown fox jumps over
Output: The quick brown fox jumps over the lazy dog. The quick brown fox
Time: 1.20s, Steps: 8
=== Text + Image test (greedy, max_new_tokens=1024 (n-gram=2)) ===
Warmup (64 tokens)...
[transformers] Setting `pad_token_id` to `eos_token_id`:151645 for open-end generation.
[transformers] Setting `pad_token_id` to `eos_token_id`:151645 for open-end generation.
Output:
system
You are a helpful assistant.
user
Please list all countries and their medal counts in a table format.
assistant
| 名次 | 国家/地区 | 金牌 | 银牌 | 铜牌 | 奖牌总数 |
|---|---|---|---|---|---|
| 1 | 中国(CHN) | 48 | 22 | 30 | 100 |
| 2 | 美国(USA) | 36 | 39 | 37 | 112 |
| 3 | 俄罗斯(RUS) | 24 | 13 | 23 | 60 |
| 4 | 英国(GBR) | 19 | 13 | 19 | 51 |
| 5 | 德国(GER) | 16 | 11 | 14 | 41 |
| 6 | 澳大利亚(AUS) | 14 | 15 | 17 | 46 |
| 7 | 韩国(KOR) | 13 | 11 | 8 | 32 |
| 8 | 日本(JPN) | 9 | 8 | 8 | 25 |
| 9 | 意大利(ITA) | 8 | 9 | 10 | 27 |
| 10 | 法国(FRA) | 7 | 16 | 20 | 43 |
| 11 | 荷兰(NED) | 7 | 5 | 4 | 16 |
| 12 | 乌克兰(UKR) | 7 | 4 | 11 | 22 |
| 13 | 肯尼亚(KEN) | 6 | 4 | 6 | 16 |
| 14 | 西班牙(ESP) | 5 | 11 | 3 | 19 |
| 15 | 牙买加(JAM) | 5 | 4 | 2 | 11 |
Generation time: 19.02s
Decode steps: 452
Per-step time: 42.1ms文本测试通过:正确补全了经典句子 "the lazy dog"
图文测试通过:正确识别了测试图片中的表格数据
| 策略 | 平均耗时 | 每步耗时 | 相对提升 |
|---|---|---|---|
| 标准(N-gram=0) | 22.28s | 49.3ms | — |
| N-gram=2 | 19.86s | 43.9ms | +11% |