c
colour_hand/flan-t5-small
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

FLAN-T5-Small 文本编码 — 昇腾 NPU 适配

1. 简介

  • 模型: google/flan-t5-small
  • 任务类型: 文本编码(T5 encoder 特征)
  • 框架: PyTorch(transformers 原生 T5EncoderModel)
  • 参数量: 35.333M
  • 来源厂商: Google

2. 验证环境

组件版本
torch2.9.0
CANN8.5.1
transformers4.57.6
Python3.11.14
硬件Ascend 910B

3. 推理步骤

source /usr/local/Ascend/ascend-toolkit/set_env.sh
python3 inference.py

4. 测试样例及输出结果

============================================================
FLAN-T5-Small 文本编码  NPU  + CPU/NPU 精度对比
============================================================
| device   : npu:0  (x2)
| model    : FLAN-T5-Small 文本编码 (35.333M params)
| load     : 1.00s
| model    : FLAN-T5-Small 文本编码 (35.333M params)
| load     : 0.10s
| input    : [2, 32]  (2 条, L=32)
| out shape: [2, 32, 512]
| 输出 (第 0 个样本, 前 5 维):
|   [-0.1193 -0.0028 0.0485 -0.0128 -0.0711]
| cosine   : 1.00000048
| rel_err  : 0.000090%
| latency  : 8.28 ms
| result   : PASS ✅
============================================================

5. 性能参考

指标值
延迟8.28 ms(2 条 L=32 文本)
Cosine Similarity1.00000048

6. 精度评测

指标值
Cosine Similarity1.00000048
相对误差0.000090%

适配状态: PASS ✓

7. 注意事项

  1. 推理前需执行 source /usr/local/Ascend/ascend-toolkit/set_env.sh 设置 CANN 环境变量
  2. 模型权重存放于 /data/models/flan-t5-small/,不要移动或修改
  3. transformers 原生 T5EncoderModel 直接加载(无需手写架构)
  4. 关键坑:
    • T5-Small(6 层 / d_model=512 / 8 头),仅 encoder 35.3M 参数
    • 输入 input_ids [B,L],输出 last_hidden_state [B,L,512]
    • 权重 model.safetensors + pytorch_model.bin
  5. 首次在 NPU 上运行可能需要额外的编译时间(kernel 缓存)