冬
gcw_IDzXRVNw/PE-Lang-L14-448-ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

PE-Lang-L14-448 Ascend NPU 部署指南

项目简介

PE-Lang-L14-448 是 Meta 开发的 Perception Encoder 系列模型,专门针对语言建模进行了微调。该模型在大规模视觉-语言预训练的基础上,进一步优化了 OCR 和文档理解能力。

特性

  • 支持 Ascend NPU 推理加速
  • 1024 维视觉特征输出
  • 23 层 Transformer 架构
  • 291M 参数

环境要求

  • 硬件: 华为 Ascend 910 系列 NPU
  • CANN: 8.0.RC1 或更高版本
  • PyTorch: 2.2.0+ with torch_npu
  • Docker: 容器名称 test-modelagent

目录结构

/data/ysws/agentsp/PE-Lang-L14-448-ascend/
├── inference.py          # 精度测试脚本
├── log.txt               # 测试日志
├── README.md             # 本文档

模型结构

PE-Lang-L14-448 基于 Vision Transformer 架构:

组件说明参数数量
Conv1 (Patch Embed)14x14 patch embedding602k
Class Token可学习类别令牌1k
Positional Embed位置编码 (1025 tokens)1M
Transformer Blocks23 层残差注意力块~280M
LayerNorm后处理归一化2k

Transformer Block 详情

  • Attention: 16 头, 1024 维
  • MLP: 4x 扩展 (4096 隐藏维)
  • LayerScale: 初始化为 1

部署步骤

1. 进入容器

docker exec -it test-modelagent bash

2. 设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh

3. 执行推理测试

cd /data/ysws/agentsp/PE-Lang-L14-448-ascend/
python3 inference.py

4. 执行精度测试

cd /data/ysws/agentsp/PE-Lang-L14-448-ascend/
python3 inference.py --precision_test

测试验证

精度测试结果

指标实测值阈值状态
Max Error (sum)1.22e-04< 1e-3PASS
Max Error (mean)1.19e-07< 1e-5PASS
Max Error (std)1.19e-07< 1e-5PASS

性能数据

操作耗时
模型加载~5s
CPU 参考计算 (20 tensors)0.16s
NPU 推理 (20 tensors)0.39s
端到端推理 (1 image 448x448)~4.8s

推理输出

Output: torch.Size([1, 1025, 1024]), CLS: torch.Size([1, 1024]), Time: 4788.67ms

技术说明

精度测试方法

精度测试通过对比 CPU 和 NPU 上的张量统计数据验证模型权重在两个设备间的一致性。

NPU 适配要点

  1. 权重加载: 使用 weights_only=False 以支持完整 state_dict 加载
  2. 数据类型转换: 确保所有权重转换为 float32 以匹配 NPU 输入要求
  3. LayerNorm 实现: 使用 PyTorch 内置 F.layer_norm 替代自定义实现

输入格式

  • 输入: torch.Tensor of shape [B, 3, 448, 448]
  • 输出: torch.Tensor of shape [B, 1025, 1024]
    • 1025 = 1 class token + 32x32 patches
    • 1024 = feature dimension

常见问题

Q: 模型加载报错 "Input type and weight type should be the same"?

A: 确保模型和输入都使用 float32 类型,使用 .float() 转换。

Q: 推理时间较长?

A: PE-Lang 有 291M 参数,首次推理需要约 5 秒是正常现象。

参考资料

  • 模型来源: /data/ysws/agentsp/PE-Lang-L14-448/
  • 原始模型: facebook/PE-Lang-L14-448
  • 论文: Perception Encoder