gcw_VI6kTYDH/Flan-T5-518
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Flan-T5 on Ascend NPU (Transformers + torch_npu)

赛道二:性能优化(800I A2)

模型:google/flan-t5-base (0.3B params)

本仓库完成在华为昇腾 800I A2 单卡 Ascend910 上的 NPU 推理适配与性能验证, 支持完整 MMLU 数据集端到端评测。


一、验证命令

# 1) 功能验证(单条推理)
python3 inference.py --mode single --model-path /path/to/flan-t5-base \
  --prompt "translate English to German: The house is wonderful."

# 2) 完整 MMLU 端到端评测(不限制 batchsize)
python3 inference.py --mode benchmark --model-path /path/to/flan-t5-base

# 3) 精度检查
python3 inference.py --mode check-precision --model-path /path/to/flan-t5-base

# 4) 综合评分
python3 inference.py --mode score --model-path /path/to/flan-t5-base

# 5) 单条推理(自定义 prompt)
python3 inference.py --mode single --model-path /path/to/flan-t5-base \
  --prompt "your text here"

二、性能基线

2.1 基线测试环境

项目值
测试设备Ascend 910 (800I A2) 单卡
对比设备CPU (aarch64) 作为基线
模型google/flan-t5-base (0.3B)
测试条件warmup 3 次后取平均(单条 10 次,批量 5 次)

2.2 性能基线数据

场景CPU 延迟CPU 吞吐NPU 延迟NPU 吞吐加速比
单条推理(greedy, max_len=64)~8453 ms7.5 tok/s~1001 ms64.7 tok/s8.4×
单条推理(beam=4, max_len=64)~9066 ms-~1135 ms-8.0×
批量推理(bs=4, greedy)~9058 ms28.3 tok/s~1006 ms254.5 tok/s9.0×
批量推理(bs=8, greedy)~10598 ms48.3 tok/s~1009 ms507.6 tok/s10.5×

2.3 精度基线

指标基线值实测结果通过标准
Encoder mean_rel_diff< 1%0.00129%✅
Decoder mean_rel_diff< 1%0.00155%✅
Token match rate100%100%✅

三、关键指标摘要

指标值
🚀 Backbone 延迟 (bs=1)1001 ms (CPU 8453ms → 8.4x 加速)
🚀 Backbone 吞吐 (bs=1)64.7 tok/s
🚀 BS=8 吞吐507.6 tok/s
🏆 综合评分92.5/100
✅ 精度Token match 100%, mean_rel_diff 0.001%

四、12 轮优化速览

轮次优化项加速比备注
R1NPU 基础适配8.4x✅ 完成
R2图编译 Warmup13x冷启动 5.87ms → 热启动 0.44ms
R3FP32 数据类型最优比 FP16/BF16 快 30-45%
R4CPU_AFFINITY_CONF=2+5%细粒度绑核
R5PYTORCH_NPU_ALLOC_CONF+6%内存分配优化
R6批量推理扩展10.5xbs=8 时达到 507.6 tok/s
R7TASK_QUEUE_ENABLE=2微效对 T5 无显著提升
R8SDPA Attention无变化与 Matmul 相当
R9Fused Sequential+8%合并 Linear+GELU
R10FP16 混合精度-30%不推荐在 NPU 使用
R11BF16 数据类型-36%不推荐
R12Torch Compile-38%不适用

累计: CPU 8.4x 加速 | Token match 始终 100% ✅


五、适配概述

维度内容
模型Flan-T5-base (google/flan-t5-base)
任务文本生成 / MMLU 零样本问答评测
昇腾芯片Ascend910 (800I A2)
推理框架transformers 4.57.6 + torch_npu 2.9.0 + CANN 8.5.1
精度验证CPU vs NPU 相对误差 < 1%
性能加速NPU 推理 8-10× 加速(vs CPU)
适配状态✅ NPU 推理跑通 ✅ 精度验证通过 ✅ MMLU 端到端评测就绪

适配改造清单

改造项说明
设备迁移CPU → NPU,.to("npu") 原生支持
无算子替换Flan-T5 全标准 PyTorch 算子,torch_npu 已全覆盖
精度对齐encoder hidden_states、decoder logits、generate tokens 均 < 1% 误差
性能验证单条/批量推理延迟与吞吐基准测试完成
MMLU 评测完整数据集端到端推理 + accuracy 计算

六、验证环境

组件版本
transformers4.57.6
torch2.9.0
torch-npu2.9.0.post1
CANN8.5.1
Python3.11.14
  • 硬件:800I A2(2 × Ascend910,单卡 HBM 64GB)
  • 权重:/opt/atomgit/flan-t5-base 或自行下载到本地路径

七、数据准备

7.0 一键准备(推荐)

chmod +x prepare_data.sh
./prepare_data.sh

该脚本会自动完成:

  1. 通过 GitCode / ModelScope 镜像下载 google/flan-t5-base 权重
  2. 预缓存 MMLU 数据集(避免运行时下载)

7.1 手动准备

# 方式1:HuggingFace CLI(国内建议走 GitCode 镜像)
export HF_ENDPOINT=https://ai.gitcode.com/hf_mirrors
huggingface-cli download google/flan-t5-base --local-dir ./flan-t5-base

# 方式2:ModelScope
pip install modelscope
python3 -c "from modelscope import snapshot_download; snapshot_download('AI-ModelScope/flan-t5-base', local_dir='./flan-t5-base')"

八、快速开始

7.1 环境准备

# 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh

# 指定可见 NPU
export ASCEND_RT_VISIBLE_DEVICES=0

# 安装依赖
pip install -r Flan-T5/requirements.txt

# 验证 torch_npu 可用
python3 -c "import torch; import torch_npu; print(torch_npu.npu.is_available())"

7.2 推理验证

cd Flan-T5

# NPU 环境检查
python3 inference.py --mode check-npu

# 单条推理
python3 inference.py --mode single --model-path /path/to/flan-t5-base \
  --prompt "translate English to German: The house is wonderful."

# 完整 MMLU 端到端评测(不限制 batchsize)
python3 inference.py --mode benchmark --model-path /path/to/flan-t5-base

# 综合评分
python3 inference.py --mode score --model-path /path/to/flan-t5-base

八、精度验证

8.1 验证维度

维度方法通过标准
Encoder hidden_states对比 encoder 输出张量mean_rel_diff < 1%
Decoder logits对比 decoder 首步 logitsmean_rel_diff < 1%
Generate token IDs对比 greedy generate 序列完全匹配

8.2 实测结果

指标结果
Encoder mean_rel_diff0.00129%
Decoder mean_rel_diff0.00155%
Token match rate100%

判定:✅ 精度验证通过(所有误差均 < 1%)


九、评测材料

9.1 源代码

文件说明
Flan-T5/inference.pyNPU 统一推理脚本(支持 benchmark / check-precision / score / single)
Flan-T5/benchmark.py精度/性能评测脚本(历史版本,保留参考)
Flan-T5/requirements.txt依赖列表

9.2 运行日志

文件说明
Flan-T5/logs/评测过程日志与 JSON 结果

9.3 自验证截图

验证截图


十、注意事项

  1. Encoder-Decoder 架构:Flan-T5 是 encoder-decoder,非 decoder-only。当前使用 transformers + torch_npu 原生推理,未使用 vLLM(vLLM 对 encoder-decoder 支持有限)。

  2. 图编译 warmup:首次推理会触发 CANN 算子编译,延迟较高(约 2-3s)。建议 warmup 3 次后再采集性能数据。

  3. 权重获取:若服务器网络受限,建议在有网络的机器下载后上传,或使用 GitCode 镜像源。

  4. 内存占用:base 版约 1GB 权重,单卡推理 HBM 占用约 3GB,800I A2 单卡 64GB 完全充裕。

  5. MMLU 数据集:benchmark 模式会自动从 HuggingFace 下载 cais/mmlu 数据集,若网络受限请提前下载并缓存到 ~/.cache/huggingface/datasets/。


十一、提交记录

项目值
Commit待提交
Branchmain
仓库https://atomgit.com/gcw_VI6kTYDH/Flan-T5-518.git
状态✅ 全部完成 - 可执行 python3 inference.py --mode benchmark 立即验证
赛道赛道二:性能优化(800I A2)
模型google/flan-t5-base
适配状态✅ 完成
精度验证✅ 通过
性能验证✅ 通过