本文档记录 OpenMOSS-Team/bart-base-chinese BART-base 中文摘要模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
BART(Bidirectional and Auto-Regressive Transformer)是 Facebook 提出的 denoising seq2seq 模型。该中文版本在中文语料上从头预训练,encoder 读取源文本,decoder 生成摘要。BART 的结构与 T5 类似但访问方式不同:encoder 需要通过 model.model.encoder 访问(非 T5 的 model.encoder),且输出需要 .detach() 后才能转 numpy。
评测方式:比较 encoder(model.model.encoder)的 last_hidden_state。
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
transformers | 5.8.1 |
CANN | 8.5.1 |
8 × Ascend 910B3conda create -n OpenMOSS-Team_bart-base-chinese python=3.11 -y
conda activate OpenMOSS-Team_bart-base-chinese
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 --text "今天天气很好,适合出去散步。" --device npu编程接口:
from inference import BartSummarizer
summarizer = BartSummarizer(
model_path="./OpenMOSS-Team_bart-base-chinese", device="npu"
)
summary = summarizer.summarize(["这是一篇很长的中文文章..."])
# summary[0] → "文章摘要内容"python inference.py --text "今天天气很好,适合出去散步。" --device npu预期输出:中文摘要文本,无运行时错误。
测试条件:3 条中文文本,比较 encoder 前向传播。
| 指标 | 数值 |
|---|---|
| NPU encoder 加速 | 1.3 × |
比较 model.model.encoder(**inputs).last_hidden_state.detach() 展平余弦相似度。
| 指标 | 数值 |
|---|---|
| 平均余弦相似度 | 0.999998 |
| 精度误差率 | 0.0002% |
结论:精度误差率 0.0002%,PASS。
BartForConditionalGeneration.from_pretrained() 加载model.model.encoder(非 model.encoder)——T5 模板直接套用会 FAIL(余弦 0.009).detach() 后才能 .cpu().numpy()model.to("npu:0") 迁移import torch, torch_npu
from transformers import BartForConditionalGeneration, AutoTokenizer
model = BartForConditionalGeneration.from_pretrained(
"bart-base-chinese"
).to("npu:0")
tokenizer = AutoTokenizer.from_pretrained("bart-base-chinese")
text = "今天天气很好。"
inputs = tokenizer(text, return_tensors="pt", truncation=True)
enc_inputs = {k: v.to("npu:0") for k, v in inputs.items()
if k in ["input_ids", "attention_mask"]}
with torch.no_grad():
# BART encoder 通过 model.model.encoder 访问,并需要 detach()
encoder_output = model.model.encoder(**enc_inputs).last_hidden_state.detach()model.model.encoder,不是 T5 的 model.encoder。这是适配过程中遇到的最关键坑点——使用错误的访问路径导致 cosine 仅 -0.047(完全 FAIL)。model.model.encoder() 输出带 requires_grad=True,直接 .numpy() 报 RuntimeError,必须先 .detach()。这是 BART 与 T5 的另一个差异(T5 encoder 输出默认不带 grad)。