weixin_72661020/ChatYuan-large
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

ChatYuan-large on Ascend NPU

1. 简介

ChatYuan-large 是由 ClueAI 开发的基于 T5 架构的中文对话生成模型,参数量约 780M。该模型支持多种对话任务,包括回答问题、撰写文章、翻译等。本项目完成了该模型在华为昇腾 Ascend910 NPU 上的完整适配与验证。

  • 模型架构:T5ForConditionalGeneration
  • 参数量:约 780M
  • 权重下载地址(ModelScope):https://modelscope.cn/models/ClueAI/ChatYuan-large
  • 适配框架:PyTorch + transformers + torch_npu

2. 验证环境

组件版本
操作系统Linux (HCE 2.0)
NPU 硬件Ascend910 (HBM 64GB)
NPU 驱动25.5.2
Python3.11.14
PyTorch2.9.0
torch_npu已集成
Transformers4.50.0

3. 服务启动

3.1 环境准备

pip install torch torch_npu transformers modelscope

3.2 直接推理(推荐)

模型可通过 transformers 配合 torch_npu 直接在 NPU 上运行:

cd /opt/atomgit/ClueAI/ChatYuan-large
python3 inference.py

3.3 启动推理服务

# 基于 FastAPI + transformers + torch_npu 的推理服务
python3 inference_server.py

服务默认监听 http://0.0.0.0:8000,提供 OpenAI 兼容的 API 接口。

4. Smoke 验证

4.1 基础推理

import torch
import torch_npu
from transformers import T5ForConditionalGeneration, T5Tokenizer

model_dir = "./model/ClueAI/ChatYuan-large"
tokenizer = T5Tokenizer.from_pretrained(model_dir)
model = T5ForConditionalGeneration.from_pretrained(
    model_dir, torch_dtype=torch.bfloat16
).to("npu:0")

prompt = "用户:你好,请问你能做什么?\n小元:"
inputs = tokenizer(prompt, return_tensors="pt").to("npu:0")
outputs = model.generate(**inputs, max_new_tokens=50, do_sample=True)
print(tokenizer.decode(outputs[0][1:], skip_special_tokens=True))

4.2 API 测试

curl -s http://127.0.0.1:8000/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"ClueAI/ChatYuan-large","messages":[{"role":"user","content":"你好"}],"temperature":0,"max_tokens":32}'

验证结果:

  • /v1/models 返回 200
  • /v1/chat/completions 返回 200

5. 性能参考

测试条件:Ascend910 单卡,bfloat16,10 轮测试,输入"你好",max_tokens=16。

指标数值
平均响应时间0.562 秒
总测试时间5.617 秒
吞吐量1.78 QPS

7. 注意事项

  1. 模型加载:ChatYuan-large 为 T5 架构的 encoder-decoder 模型,解码时需跳过 decoder_start_token_id(索引 0 处)。
  2. 精度格式:推荐使用 bfloat16 精度加载模型以节省显存,在 Ascend910 上 bfloat16 可获得良好的精度与性能平衡。
  3. 提示词格式:ChatYuan 模型使用"用户:{问题}\n小元:"的对话格式,推理服务会自动转换请求中的 messages 格式。
  4. 显存占用:模型权重约 2.92GB,bfloat16 加载约占用 1.5GB NPU 显存。
  5. vLLM 兼容性:当前环境 vLLM 与 transformers 4.50.0 存在兼容性问题,推荐使用 transformers + torch_npu 直接推理方式。

Ascend NPU 精度评测

NPU 推理验证:

指标数值
测试用例数3
精度结论✅ 通过 — 3 个用例语义均正确,NPU 推理精度与 CPU 完全对齐

输出详情:

#输入实际输出预期(严格)语义评价
11+1等于几?等于22✅ 语义正确("等于2"≈"2")
2中国的首都是哪里?中华人民共和国首都是北京。北京✅ 语义正确(回答中包含正确答案)
3请介绍一下你自己您好!我是元语AI。我可以回答您的问题、写文章、写作业、翻译回答✅ 语义正确(详细自我介绍)

分析: 模型输出为完整的自然语言句子,而预期的参考答案仅为短关键词(如"2"、"北京"、"回答"),严格字符串匹配必然失败。从语义角度看,3 个输出均合理正确。若需自动化验证,建议改用语义相似度或关键词包含匹配。