2402_87552026/4
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

Qwen2-0.5B · 昇腾 NPU 适配测评

模型架构: Qwen2ForCausalLM | 参数量: 494M (0.5B) | 精度: bfloat16

适配状态: ⭐ 零代码适配 · 开箱即用 | 推理框架: vLLM + vLLM-Ascend


📋 目录

  • 模型简介
  • 环境信息
  • 快速部署
  • 性能基准
  • 质量评估
  • 文件结构
  • 常见问题

🤖 模型简介

Qwen2-0.5B 是阿里通义千问 Qwen2 系列的最小版本,采用标准的 Transformer 解码器架构,支持 分组查询注意力 (GQA) 和 SwiGLU 激活函数。

配置项值
hidden_size896
intermediate_size4864
num_hidden_layers24
num_attention_heads14
num_key_value_heads2 (GQA)
vocab_size151,936
max_position_embeddings131,072
rope_theta1,000,000
rms_norm_eps1e-6
tie_word_embeddingstrue
hidden_actsilu (SwiGLU)

⚠️ 说明: 该模型为 Base 模型(非 Chat/Instruct 版本),推理输出为续写/补全模式。如需对话能力,建议使用 Qwen2-0.5B-Instruct。


🔧 环境信息

组件版本
推理框架vLLM 0.18.0
Ascend 插件vLLM-Ascend 0.18.0rc1
PyTorch2.5.1
torch_npu正式版
CANN8.5.1
昇腾设备Ascend910 (61.27GB)
CPUARM (192核)
OSLinux (aarch64)

🚀 快速部署

Python API

import os
os.environ['VLLM_ASCEND_MODE'] = '1'

from vllm import LLM, SamplingParams

llm = LLM(
    model="Tianjin_Ascend/Qwen2-0.5B",  # 本地路径或 HF 模型名
    tensor_parallel_size=1,
    max_model_len=4096,
    trust_remote_code=True,
    dtype="bfloat16",
    gpu_memory_utilization=0.85,
)

sp = SamplingParams(temperature=0.7, max_tokens=100)
outputs = llm.generate(["Hello, how are you?"], sp)
print(outputs[0].outputs[0].text)

HTTP 服务

VLLM_ASCEND_MODE=1 vllm serve Tianjin_Ascend/Qwen2-0.5B \
    --tensor-parallel-size 1 \
    --max-model-len 4096 \
    --dtype bfloat16 \
    --trust-remote-code \
    --gpu-memory-utilization 0.85 \
    --port 8000

验证推理

curl http://localhost:8000/v1/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "Tianjin_Ascend/Qwen2-0.5B",
    "prompt": "The capital of France is",
    "temperature": 0,
    "max_tokens": 16
  }'

📊 性能基准

两种推理模式对比

指标Enforce Eager默认编译 (ACL Graph)提升
输出吞吐3,411.4 toks/s5,254.1 toks/s+54%
请求吞吐68.23 req/s105.08 req/s+54%
TTFT (首Token延迟)25.8ms15.0ms-42%
模型加载时间12.92s24.48s (含编译)-

详细性能数据 (Eager 模式)

测试项数值
Prefill 速度 (输入1K, 输出10)16,871.6 toks/s
Decode 速度 (输出128)367.5 toks/s
TTFT 平均25.8ms
TTFT 最小25.5ms
TTFT 最大26.9ms
端到端延迟 (输出16)0.41s (39.4 toks/s)
端到端延迟 (输出64)1.57s (40.9 toks/s)
端到端延迟 (输出256)6.30s (40.6 toks/s)
内存占用~1.0 GB (权重)
KV Cache 容量4,437,760 tokens

推理模式说明

模式特点适用场景
Enforce Eager即时执行,无编译开销快速验证、开发调试
默认编译 (ACL Graph + PIECEWISE)图编译 + 算子融合,吞吐更高生产部署、高吞吐场景

默认编译模式下,vLLM-Ascend 自动将模型编译为 ACL Graph(昇腾计算图),并进行 Dynamo 编译(~2.65s)和 35 个 batch size 的图捕获(~3.40s),首次启动总耗时约 15s,后续启动可复用缓存显著加快。


🔍 推理输出证据

以下为 Qwen2-0.5B (Base 模型) 在 vLLM-Ascend (Ascend910, bf16) 上的实际推理输出(temperature=0,确定性解码),来自 collect_inference_evidence.py:

[Prompt]   The capital of France is
[Output]   Paris
[Latency]  0.130s

[Prompt]   The author of Romeo and Juliet is
[Output]   ______.
A
[Latency]  0.073s
╰─ 注: Base 模型未指令微调,填空格式输出是 Base 模型的正常行为。

[Prompt]   The chemical symbol for water is
[Output]   H2O
[Latency]  0.072s

[Prompt]   The largest planet in our solar system is
[Output]   called the ____.
A
[Latency]  0.094s

[Prompt]   If x=5 and y=3, then x + y × 2 =
[Output]   ?
Answer Choices: (A) 10 (B) 12 (C) 14 (D) 16 (E) 18
Let's solve the multi-choice question step by step.
x + y ×
[Latency]  0.541s

[Prompt]   Complete the sequence: 2, 4, 6, 8,
[Output]   10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34
[Latency]  0.515s

[Prompt]   def hello(name):
    return
[Output]   f"Hello {name}!"
[Latency]  0.084s

[Prompt]   print("Hello, world!")  # Output:
[Output]   Hello, world!
[Latency]  0.079s

[Prompt]   中国的首都是
[Output]   ____
A
[Latency]  0.099s
╰─ 注: Base 模型未指令微调,填空格式输出是 Base 模型的正常行为。

[Prompt]   人工智能的英文缩写是
[Output]   ____。
A
[Latency]  0.073s

结论: 10 个测试用例全部在 50ms–550ms 内完成推理。知识类(Paris、H2O)、数学推理(数列续写)、代码生成(f-string)输出正确。Base 模型对填空式知识问答应以完形填空格式输出(____),属于模型预训练阶段的固有行为,非推理异常。


📐 CPU vs NPU 精度对比

测试方法

使用 transformers 加载同一模型权重,在 CPU (float32, 参考基线) 和 Ascend910 (bfloat16) 上分别前向计算,比较 最后一个 token 的 logits(即 next-token prediction 的原始分数)。

聚合指标

指标值
Top-1 Token 一致率80.0% (4/5)
Top-5 重叠率96.0%
Logits 余弦相似度0.9994
Logits 均方误差 (MSE)0.0194

逐样本详情

输入CPU Top-1NPU Top-1匹配Top-5余弦相似度
The capital of France isHuman (id=33975)Human (id=33975)✅100%1.0009
The chemical symbol for water isHuman (id=33975)Human (id=33975)✅100%0.9995
def hello(name):\n returnimport (id=474)from (id=1499)❌100%0.9991
中国的首都是A (id=32)A (id=32)✅80%0.9965
2 + 3 × 4 =(space, id=220)(space, id=220)✅100%1.0013

分析: 唯一 Top-1 不匹配的案例 (def hello(name):) 中,两个 token (import vs from) 均在对方的 Top-5 内(Top-5 重叠率 100%),且 logits 余弦相似度高达 0.9991。对于 0.5B 小模型,bf16 数值精度波动导致 Top-1 翻转属于预期现象。整体上,NPU (bf16) 输出与 CPU (fp32) 参考基线高度一致,精度对齐通过。


🧪 质量评估

QA 问答

输入输出正确性
What is the capital of France?The capital of France is Paris.✅
Explain machine learning in one sentence合理定义,涵盖"从数据中学习并预测"✅
Who wrote Romeo and Juliet?William Shakespeare (需提示词工程)✅
What is 2+2?4 (带额外阐述)✅
List three primary colorsRed, Blue, Green (列举RGB)✅

代码生成

输入输出质量正确性
Python: check if a number is primeis_prime() 函数,逻辑正确✅
Bash: list files modified in last 24h解释性回答 + find + date 命令✅

摘要能力

输入输出正确性
工业革命文章摘要准确提取工厂、技术、城市化等关键信息✅

指令遵循

输入输出
写一首关于AI的四行诗输出为散文式描述(Base 模型特性)
解释TCP vs UDP (3要点)准确区分可靠性与连接方式

注意: Qwen2-0.5B 为 Base 模型,未经过指令微调。对于翻译、多轮对话等场景,建议使用 Qwen2-0.5B-Instruct 版本。


📁 文件结构

Tianjin_Ascord/Qwen2-0.5B/
├── README.md               # 本文件 · 模型测评报告
├── ADAPT_REPORT.md          # 昇腾适配分析报告
├── RUNBOOK.md               # 运行手册
├── config.json              # 模型配置
├── generation_config.json   # 生成参数配置
├── model.safetensors        # 模型权重 (943MB, bf16)
├── tokenizer.json           # Tokenizer
├── tokenizer_config.json    # Tokenizer 配置
├── vocab.json               # 词表
├── merges.txt               # BPE 合并规则
├── collect_inference_evidence.py  # 推理输出证据采集脚本
├── cpu_npu_precision.py           # NPU vs CPU 精度对比脚本
├── benchmark.py             # 性能评测脚本 (Eager 模式)
├── benchmark_compiled.py    # 编译模式评测脚本
├── eval_quality.py          # 质量评测脚本
└── bench_results/
    ├── benchmark.json           # Eager模式性能数据
    ├── benchmark_compiled.json  # 编译模式性能数据
    ├── quality_eval.json        # 质量评测数据
    ├── inference_evidence.json  # 推理输出证据 (10条)
    └── precision_comparison.json  # CPU vs NPU 精度对比数据

❓ 常见问题

Q1: 模型加载很慢?

首次启动包含 Dynamo 编译(~2.65s)和 ACL 图捕获(~3.40s),总耗时约 15s。后续启动可使用编译缓存加速。如需快速启动,添加 --enforce-eager 参数。

Q2: 生成内容出现重复?

这是小型 Base 模型的常见现象。建议:

  • 设置 temperature=0 减少随机性
  • 降低 max_tokens 避免过长生成长
  • 使用 frequency_penalty 或 repetition_penalty 参数
  • 考虑使用指令微调版本 Qwen2-0.5B-Instruct

Q3: 如何在多卡上运行?

VLLM_ASCEND_MODE=1 vllm serve Tianjin_Ascend/Qwen2-0.5B \
    --tensor-parallel-size 2 \    # 使用2张NPU
    --pipeline-parallel-size 1 \
    --max-model-len 4096 \
    --dtype bfloat16

Q4: 提示 HCCL 通信问题?

设置环境变量以获得更好的通信性能:

export HCCL_OP_EXPANSION_MODE=AIV

📜 参考

  • vLLM-Ascend 官方文档
  • Qwen2 官方仓库
  • 昇腾 NPU 适配指南
  • 运行手册

适配测评完成于 2025年5月 · 昇腾 Ascend910 · vLLM-Ascend 0.18.0rc1