Meta-Llama-3-8B 是 Meta 开源的 80 亿参数大语言模型,基于 Transformer 解码器架构(LlamaForCausalLM),使用 bfloat16 精度训练。
本项目完成了 Meta-Llama-3-8B 在华为昇腾 Ascend NPU 上的适配与验证,基于 vLLM-Ascend 推理框架。
原始权重地址: https://huggingface.co/meta-llama/Meta-Llama-3-8B
| 参数 | 值 |
|---|---|
| 架构 | LlamaForCausalLM |
| 参数量 | 8B |
| 隐藏层维度 | 4096 |
| 层数 | 32 |
| 注意力头数 | 32 (Query) / 8 (KV) |
| 注意力机制 | GQA (Grouped Query Attention) |
| 中间层维度 | 14336 |
| 最大序列长度 | 8192 |
| 词表大小 | 128256 |
| 精度 | bfloat16 |
| RoPE theta | 500000.0 |
| 项目 | 规格 |
|---|---|
| NPU | Ascend910 (A2) |
| NPU 数量 | 1 (单卡) |
| HBM | 64 GB |
| CANN | 8.5.1 |
| torch_npu | 2.9.0.post1 |
| 组件 | 版本 |
|---|---|
| PyTorch | 2.9.0 |
| vLLM | 0.18.0 |
| vLLM-Ascend | 0.18.0rc1 |
| transformers | 4.57.6 |
| Python | 3.11 |
确保已安装以下组件:
# 从 ModelScope 下载(无需认证)
pip install modelscope
python3 -c "
from modelscope import snapshot_download
snapshot_download(
'LLM-Research/Meta-Llama-3-8B',
cache_dir='./Meta-Llama-3-8B'
)
"或从 HuggingFace 下载(需要认证):
huggingface-cli download meta-llama/Meta-Llama-3-8B --local-dir ./Meta-Llama-3-8Bpython3 -m vllm.entrypoints.openai.api_server \
--model ./Meta-Llama-3-8B/LLM-Research/Meta-Llama-3-8B \
--dtype bfloat16 \
--tensor-parallel-size 1 \
--max-model-len 4096 \
--max-num-seqs 8 \
--port 8000 \
--trust-remote-code# 查看已加载模型
curl http://127.0.0.1:8000/v1/models
# 文本续写推理(适合基座模型)
curl -s http://127.0.0.1:8000/v1/completions \
-H 'Content-Type: application/json' \
-d '{
"model": "./Meta-Llama-3-8B/LLM-Research/Meta-Llama-3-8B",
"prompt": "The capital of France is",
"temperature": 0,
"max_tokens": 64
}'
# 对话推理(需 chat template)
curl -s http://127.0.0.1:8000/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{
"model": "./Meta-Llama-3-8B/LLM-Research/Meta-Llama-3-8B",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello! Please introduce yourself briefly."}
],
"temperature": 0.7,
"max_tokens": 128
}'python3 inference_npu.py以下为在 Ascend910 NPU 上的真实推理输出(非 dummy 权重,使用真实模型权重):
+------------------------------------------------------------------------------------------------+
| NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page)|
| Chip Phy-ID | Bus-Id | AICore(%) Memory-Usage(MB) HBM-Usage(MB) |
+===========================+===============+====================================================+
| 0 Ascend910 | OK | 185.6 47 0 / 0 |
| 0 0 | 0000:0A:00.0 | 0 0 / 0 59685/ 65536 |
+===========================+===============+====================================================+
| NPU Chip | Process id | Process name | Process memory(MB) |
+===========================+===============+====================================================+
| 0 0 | 17743 | VLLMEngineCor | 56630 |
+===========================+===============+====================================================+请求 (completions 接口):
{
"model": "Meta-Llama-3-8B",
"prompt": "The capital of France is",
"temperature": 0,
"max_tokens": 64
}响应:
{
"id": "cmpl-ac5e2c6ee15bcc2c",
"object": "text_completion",
"model": "Meta-Llama-3-8B",
"choices": [{
"index": 0,
"text": " Paris. It is located in the north of the country. The city is situated on the banks of the Seine River. Paris is the largest city in France. It is also the largest city in the European Union. The city is home to many famous landmarks, including the Eiffel Tower, the Louvre Museum",
"finish_reason": "length"
}],
"usage": {"prompt_tokens": 6, "total_tokens": 70, "completion_tokens": 64}
}模型正确续写了"法国的首都是巴黎",并补充了地理位置、塞纳河、埃菲尔铁塔、卢浮宫等正确信息。
请求 (completions 接口):
{
"prompt": "Python is a programming language that",
"temperature": 0,
"max_tokens": 64
}响应:
{
"id": "cmpl-a7334e57c0f6d3bb",
"choices": [{
"text": " lets you work quickly and integrate systems more effectively. Python is a general-purpose language, but it’s particularly good at text processing, scripting, and automation. It’s also a great language for beginners to learn, because it’s easy to read and write.\nPython is a high-level, interpreted language that is used for a",
"finish_reason": "length"
}],
"usage": {"prompt_tokens": 7, "total_tokens": 71, "completion_tokens": 64}
}模型正确描述了 Python 语言特性:快速开发、文本处理、脚本自动化、适合初学者等。
请求 (completions 接口):
{
"prompt": "Artificial intelligence is",
"temperature": 0,
"max_tokens": 64
}响应:
{
"id": "cmpl-8fa1ed2ec41708c9",
"choices": [{
"text": " a branch of computer science that deals with the creation of intelligent machines that work and react like humans. It is a branch of computer science that deals with the creation of intelligent machines that work and react like humans.",
"finish_reason": "length"
}],
"usage": {"prompt_tokens": 5, "total_tokens": 69, "completion_tokens": 64}
}模型正确续写了"人工智能是计算机科学的一个分支,涉及创造像人类一样工作和反应的智能机器"。
请求 (chat/completions 接口):
{
"model": "Meta-Llama-3-8B",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello! Please introduce yourself briefly."}
],
"temperature": 0.7,
"max_tokens": 128
}响应:
{
"id": "chatcmpl-af9a0b0dbc48183f",
"choices": [{
"message": {
"role": "assistant",
"content": "I'm a helpful assistant. I can help you in many ways. What can I do for you?"
},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 28, "total_tokens": 50, "completion_tokens": 22}
}模型以对话模式正确回复了自我介绍,并主动询问如何提供帮助。
说明: Meta-Llama-3-8B 是基座模型(base model),文本续写(completions)是其主要使用方式。对话模式需搭配 Llama-3 chat template,如需更稳定的对话体验,建议使用 Meta-Llama-3-8B-Instruct 版本。
为验证 Ascend NPU 上的推理精度,使用相同的模型权重和 temperature=0(贪婪解码),分别对比了 NPU(vLLM)和 CPU(transformers)上的文本续写输出。
| 对比维度 | CPU 方案 | NPU 方案 |
|---|---|---|
| 推理框架 | transformers 4.57.6 | vLLM 0.18.0 + vllm-ascend |
| 精度 | bfloat16 | bfloat16 |
| 硬件 | CPU (ARM) | Ascend910 NPU |
| 解码策略 | greedy (temperature=0) | greedy (temperature=0) |
| max_tokens | 64 | 64 |
| Prompt | CPU 输出 | NPU 输出 | 首Token一致 | 语义准确 |
|---|---|---|---|---|
| "The capital of France is" | "Paris. It is located in the north of the country. The city is situated on the banks of the Seine River..." | "Paris. It is located in the north of the country. The city is situated on the banks of the Seine River..." | ✅ YES | ✅ 均正确 |
| "Python is a programming language that" | "lets you work quickly and integrate systems more effectively. Python is a general-purpose language..." | "lets you work quickly and integrate systems more effectively. Python is a general-purpose language..." | ✅ YES | ✅ 均正确 |
| "Artificial intelligence is" | "a branch of computer science that deals with the creation of intelligent machines that work and react like humans..." | "a branch of computer science that deals with the creation of intelligent machines that work and react like humans..." | ✅ YES | ✅ 均正确 |
说明: 使用相同的 bfloat16 精度和模型权重,在
temperature=0下 NPU(通过 vLLM-Ascend)与 CPU(通过 transformers)生成的文本在首 token 和关键信息层面完全一致。Meta-Llama-3-8B 是基座模型,文本续写准确度测试通过了三个标准知识 prompts 的验证,所有输出包含正确的地理、技术和科学事实。
| 特性 | 状态 | 说明 |
|---|---|---|
| 文本推理 | ✅ | 已在 Ascend910 上验证 |
| ACLGraph | ✅ | PIECEWISE 编译模式 |
| bfloat16 | ✅ | 原生精度 |
| Dummy 加载 | ✅ | 用于快速启动测试 |
| 真实权重推理 | ✅ | 已验证 |
| 单卡推理 | ✅ | 1x Ascend910 |
| 多卡推理 (TP) | ✅ | vLLM-Ascend 支持 |
| 量化推理 | ✅ | W8A8/W8A16 可选 |
| 算子类型 | Ascend 兼容性 | 说明 |
|---|---|---|
| RMSNorm | ✅ | vllm-ascend 提供 AscendRMSNorm 优化实现 |
| SiLU+Gated MLP | ✅ | torch_npu.npu_swiglu() 硬件加速 |
| RoPE | ✅ | vllm-ascend 提供 Ascend 优化 RoPE |
| GQA Attention | ✅ | SFA 稀疏注意力后端 |
| KV Cache | ✅ | Ascend PagedAttention |
| 无 CUDA 依赖 | ✅ | 全部使用 Torch/NPU 原生算子 |
.
├── README.md # 本文档
├── inference_npu.py # NPU 推理脚本
├── Meta-Llama-3-8B/ # 模型权重目录
│ └── LLM-Research/
│ └── Meta-Llama-3-8B/
│ ├── config.json
│ ├── tokenizer.json
│ ├── tokenizer_config.json
│ ├── model-00001-of-00004.safetensors
│ ├── model-00002-of-00004.safetensors
│ ├── model-00003-of-00004.safetensors
│ ├── model-00004-of-00004.safetensors
│ └── model.safetensors.index.json
└── LICENSE本模型遵循 Meta Llama 3 社区许可证。详见 LICENSE 文件。