冬
gcw_IDzXRVNw/text_summarization-ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

text_summarization Ascend NPU 部署指南

项目简介

text_summarization 是基于 T5-small 微调的文本摘要模型,能够将长文本压缩为简洁连贯的摘要。该模型在多样化的文档数据集上训练,能够捕获关键信息并生成有意义的摘要。

特性

  • 支持 Ascend NPU 推理加速
  • CPU vs NPU 精度对比测试 (< 1% 误差)
  • 文本摘要生成
  • 支持多语言翻译任务
  • 兼容 HuggingFace transformers

环境要求

  • 硬件: 华为 Ascend 910 系列 NPU
  • CANN: 8.0.RC1 或更高版本
  • PyTorch: 2.0+ with torch_npu
  • transformers: 4.31.0+

目录结构

text_summarization-ascend/
├── inference.py          # 推理测试脚本
├── log.txt               # 测试日志
├── README.md             # 本文档
└── test_texts.txt       # 测试文本

部署步骤

1. 设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh

2. 准备模型文件

模型文件位于 /data/ysws/agentsp/5-15/text_summarization/ 目录下:

  • model.safetensors - 模型权重 (约 242MB)
  • config.json - 模型配置
  • tokenizer.json / spiece.model - 分词器文件
  • generation_config.json - 生成配置

3. 安装依赖

pip install transformers torch_npu

使用方式

方式一:普通推理模式

运行推理脚本进行文本摘要:

cd /data/ysws/agentsp/5-15/text_summarization-ascend/

# 使用默认测试文本
python3 inference.py

# 使用指定设备
python3 inference.py --device npu:0

方式二:精度测试模式 (CPU vs NPU)

运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:

cd /data/ysws/agentsp/5-15/text_summarization-ascend/

# 运行完整精度测试
python3 inference.py --mode precision_test

命令行参数说明

参数说明默认值
--mode测试模式: inference 或 precision_testinference
--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!
============================================================

Python API 使用示例

基本文本摘要

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}")

使用 pipeline

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)

模型结构

  • 架构类型: T5ForConditionalGeneration (T5)
  • 模型类型: encoder-decoder transformer
  • 隐藏层维度: 512
  • 注意力头数: 8
  • 编码器层数: 6
  • 解码器层数: 6
  • 前馈网络维度: 2048
  • 位置编码: 512
组件说明
encoderTransformer 编码器,处理输入文本
decoderTransformer 解码器,生成摘要
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
}

常见问题

Q: 首次推理很慢?

A: 正常现象。NPU 首次运行需要进行模型编译和算子加载,后续推理会快很多。

Q: 精度测试失败?

A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。0.1-0.2% 的数值误差是正常的。

Q: 如何调整摘要长度?

A: 可以在 generate 时调整 max_length 和 min_length 参数。

Q: 支持哪些任务?

A: 该模型支持 summarization、translation_en_to_de、translation_en_to_fr、translation_en_to_ro 等任务。

参考链接

  • 原始模型: https://huggingface.co/Falconsai/text_summarization
  • T5 论文: https://arxiv.org/abs/1910.10683
  • HuggingFace Transformers: https://huggingface.co/transformers

许可证

本项目遵循 Apache-2.0 许可证