text_summarization 是基于 T5-small 微调的文本摘要模型,能够将长文本压缩为简洁连贯的摘要。该模型在多样化的文档数据集上训练,能够捕获关键信息并生成有意义的摘要。
text_summarization-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
└── test_texts.txt # 测试文本source /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-15/text_summarization/ 目录下:
pip install transformers torch_npu运行推理脚本进行文本摘要:
cd /data/ysws/agentsp/5-15/text_summarization-ascend/
# 使用默认测试文本
python3 inference.py
# 使用指定设备
python3 inference.py --device npu:0运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-15/text_summarization-ascend/
# 运行完整精度测试
python3 inference.py --mode precision_test| 参数 | 说明 | 默认值 |
|---|---|---|
--mode | 测试模式: inference 或 precision_test | inference |
--device | 运行设备 | npu:0 (自动检测) |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 相对误差 | 0.0001% | < 1.00% | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 0.271s |
| NPU 推理时间 | 0.579s |
输入文本:
summarize: Hugging Face: Revolutionizing Natural Language Processing. In the rapidly evolving field of Natural Language Processing (NLP), Hugging Face has emerged as a prominent and innovative force...生成的摘要:
In the rapidly evolving field of Natural Language Processing (NLP), Hugging Face has emerged as a prominent and innovative force. This article will explore the story and significance of Hugging face, a company that has made remarkable contributions to NLP and AI.============================================================
text_summarization NPU 推理测试
============================================================
Model dir: /data/ysws/agentsp/5-15/text_summarization
Output dir: /data/ysws/agentsp/5-15/text_summarization-ascend
NPU available: True
NPU device count: 8
NPU 0: Ascend910B3, total_memory=61.0GB
============================================================
Precision Test: CPU vs NPU
============================================================
Loading tokenizer...
Loading model for CPU...
Loading model for NPU...
Running inference on CPU...
Running inference on NPU...
CPU inference time: 0.271s
NPU inference time: 0.579s
Max absolute error: 4.196167e-05
Max relative error: 9.850012e-07 (0.0001%)
PASS: True (threshold: 1.0%)
============================================================
PRECISION TEST RESULT
============================================================
Relative error: 9.850012e-07
CPU time: 0.271s
NPU time: 0.579s
PASS: True
============================================================
Test Complete!
============================================================import torch
from transformers import T5ForConditionalGeneration, T5Tokenizer
MODEL_DIR = "/data/ysws/agentsp/5-15/text_summarization"
tokenizer = T5Tokenizer.from_pretrained(MODEL_DIR, legacy=False)
model = T5ForConditionalGeneration.from_pretrained(MODEL_DIR)
model = model.to("npu:0")
model.eval()
input_text = "summarize: Your long text here..."
inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=512)
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
outputs = model.generate(
input_ids=inputs["input_ids"],
attention_mask=inputs["attention_mask"],
max_length=150,
min_length=30,
num_beams=4,
early_stopping=True
)
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Summary: {summary}")from transformers import pipeline
summarizer = pipeline("summarization", model="Falconsai/text_summarization")
summary = summarizer("Your long text to summarize...", max_length=100, min_length=30, do_sample=False)
print(summary)| 组件 | 说明 |
|---|---|
| encoder | Transformer 编码器,处理输入文本 |
| decoder | Transformer 解码器,生成摘要 |
| lm_head | 语言模型头部,输出词汇概率 |
从 config.json 提取的关键参数:
{
"d_model": 512,
"d_ff": 2048,
"num_heads": 8,
"num_layers": 6,
"num_decoder_layers": 6,
"vocab_size": 32128,
"n_positions": 512
}A: 正常现象。NPU 首次运行需要进行模型编译和算子加载,后续推理会快很多。
A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。0.1-0.2% 的数值误差是正常的。
A: 可以在 generate 时调整 max_length 和 min_length 参数。
A: 该模型支持 summarization、translation_en_to_de、translation_en_to_fr、translation_en_to_ro 等任务。
本项目遵循 Apache-2.0 许可证