本文档记录 google/flan-t5-small Flan-T5-small 指令微调模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
Flan-T5 是 Google 对 T5 进行 Flan(Finetuned LAnguage Net)指令微调的版本。相比原始 T5,Flan-T5 在 1800+ 种 NLP 任务上通过指令格式统一微调,因此单个模型可支持摘要、翻译、问答、分类等多种任务(通过不同 prompt 切换)。该模型为 small 版本(60M),是 Flan-T5 系列中最轻量的。
评测方式:比较 encoder last_hidden_state(与 T5 系列相同,decoder 自回归不参与精度比较)。
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
transformers | 5.8.1 |
CANN | 8.5.1 |
8 × Ascend 910B3conda create -n google_flan-t5-small python=3.11 -y
conda activate google_flan-t5-small
pip install torch==2.8.0 torch_npu==2.8.0.post4 \
-i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers numpy \
-i https://pypi.tuna.tsinghua.edu.cn/simplepython inference.py --device npu编程接口:
from inference import T5Summarizer
model = T5Summarizer(model_path="./google_flan-t5-small", device="npu")
# Flan-T5 支持多种任务通过 prompt 切换
result = model.summarize(["summarize: Long text..."]) # 摘要
result = model.summarize(["translate English to French: Hello"]) # 翻译python inference.py --device npu预期输出:根据默认 prompt 生成的文本,无运行时错误。
| 指标 | 数值 |
|---|---|
| NPU encoder 加速 | 1.0×(T5-small 小 batch 下与 CPU 相近) |
比较 encoder last_hidden_state 的展平余弦相似度。
| 指标 | 数值 |
|---|---|
| 平均余弦相似度 | 1.000000 |
| 精度误差率 | 0.0000% |
结论:精度误差率 0.0000%,encoder 输出完全一致,评测通过。
T5ForConditionalGeneration.from_pretrained() 加载(Flan-T5 与 T5 共用模型类)model.to("npu:0") 迁移model.encoder(**inputs).last_hidden_stateimport torch, torch_npu
from transformers import T5ForConditionalGeneration, AutoTokenizer
model = T5ForConditionalGeneration.from_pretrained(
"google/flan-t5-small"
).to("npu:0")
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
text = "translate English to German: How are you?"
inputs = tokenizer(text, return_tensors="pt")
enc_inputs = {k: v.to("npu:0") for k, v in inputs.items()
if k in ["input_ids", "attention_mask"]}
with torch.no_grad():
encoder_output = model.encoder(**enc_inputs).last_hidden_state"summarize: {text}"(摘要)、"translate English to French: {text}"(翻译)、"question: {q} context: {c}"(问答)。