模型架构: Qwen2ForCausalLM | 参数量: 494M (0.5B) | 精度: bfloat16
适配状态: ⭐ 零代码适配 · 开箱即用 | 推理框架: vLLM + vLLM-Ascend
Qwen2-0.5B 是阿里通义千问 Qwen2 系列的最小版本,采用标准的 Transformer 解码器架构,支持 分组查询注意力 (GQA) 和 SwiGLU 激活函数。
| 配置项 | 值 |
|---|---|
| hidden_size | 896 |
| intermediate_size | 4864 |
| num_hidden_layers | 24 |
| num_attention_heads | 14 |
| num_key_value_heads | 2 (GQA) |
| vocab_size | 151,936 |
| max_position_embeddings | 131,072 |
| rope_theta | 1,000,000 |
| rms_norm_eps | 1e-6 |
| tie_word_embeddings | true |
| hidden_act | silu (SwiGLU) |
⚠️ 说明: 该模型为 Base 模型(非 Chat/Instruct 版本),推理输出为续写/补全模式。如需对话能力,建议使用
Qwen2-0.5B-Instruct。
| 组件 | 版本 |
|---|---|
| 推理框架 | vLLM 0.18.0 |
| Ascend 插件 | vLLM-Ascend 0.18.0rc1 |
| PyTorch | 2.5.1 |
| torch_npu | 正式版 |
| CANN | 8.5.1 |
| 昇腾设备 | Ascend910 (61.27GB) |
| CPU | ARM (192核) |
| OS | Linux (aarch64) |
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)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 8000curl 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/s | 5,254.1 toks/s | +54% |
| 请求吞吐 | 68.23 req/s | 105.08 req/s | +54% |
| TTFT (首Token延迟) | 25.8ms | 15.0ms | -42% |
| 模型加载时间 | 12.92s | 24.48s (含编译) | - |
| 测试项 | 数值 |
|---|---|
| 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 模型对填空式知识问答应以完形填空格式输出(
____),属于模型预训练阶段的固有行为,非推理异常。
使用 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-1 | NPU Top-1 | 匹配 | Top-5 | 余弦相似度 |
|---|---|---|---|---|---|
| The capital of France is | Human (id=33975) | Human (id=33975) | ✅ | 100% | 1.0009 |
| The chemical symbol for water is | Human (id=33975) | Human (id=33975) | ✅ | 100% | 0.9995 |
| def hello(name):\n return | import (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 (importvsfrom) 均在对方的 Top-5 内(Top-5 重叠率 100%),且 logits 余弦相似度高达 0.9991。对于 0.5B 小模型,bf16 数值精度波动导致 Top-1 翻转属于预期现象。整体上,NPU (bf16) 输出与 CPU (fp32) 参考基线高度一致,精度对齐通过。
| 输入 | 输出 | 正确性 |
|---|---|---|
| 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 colors | Red, Blue, Green (列举RGB) | ✅ |
| 输入 | 输出质量 | 正确性 |
|---|---|---|
| Python: check if a number is prime | is_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 精度对比数据首次启动包含 Dynamo 编译(~2.65s)和 ACL 图捕获(~3.40s),总耗时约 15s。后续启动可使用编译缓存加速。如需快速启动,添加 --enforce-eager 参数。
这是小型 Base 模型的常见现象。建议:
temperature=0 减少随机性max_tokens 避免过长生成长frequency_penalty 或 repetition_penalty 参数Qwen2-0.5B-InstructVLLM_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设置环境变量以获得更好的通信性能:
export HCCL_OP_EXPANSION_MODE=AIV