本文档记录 pooka74/LLaMA3-8B-Chat-Chinese-GGUF(基于 Meta Llama3 8B Instruct 微调的 GGUF 量化模型)在华为昇腾 NPU 环境下的适配与验证结果。
该模型使用 Llama-Factory 在中文数据集(alpaca_zh, alpaca_gpt4_zh, oaast_sft_zh, guanaco)上微调,并以 GGUF Q4_K_M 格式发布。
相关获取地址:
| 组件 | 版本 |
|---|---|
llama-cpp-python | 0.3.23 |
Python | 3.x |
PyTorch | 2.9.0+cpu |
torch-npu | 可用 |
vllm-ascend | 0.18.0rc1(不支持 GGUF 格式) |
~/pooka74/LLaMA3-8B-Chat-Chinese-GGUF/model/由于 GGUF 格式在 vllm-ascend 上不被支持(见注意事项),模型通过 llama-cpp-python 在 CPU 上进行推理。
启动推理脚本:
python3 ~/pooka74/LLaMA3-8B-Chat-Chinese-GGUF/inference.pyfrom llama_cpp import Llama
llm = Llama(
model_path="LLaMA3-8B-Chat-Chinese-Q4_K_M.gguf",
n_ctx=2048,
n_threads=8,
verbose=False
)
# English test
output = llm("Hello, how are you?", max_tokens=32, temperature=0.1)
print(output['choices'][0]['text'])
# Chinese test
output = llm("什么是人工智能?", max_tokens=64, temperature=0.1)
print(output['choices'][0]['text'])验证结果:
测试条件:llama-cpp-python, CPU, n_threads=8, 3 次运行取平均
| 测试场景 | 最大Token数 | 平均延迟(s) | 吞吐量(tok/s) |
|---|---|---|---|
| short_en | 32 | 5.186 | 6.17 |
| medium_en | 64 | 9.366 | 6.83 |
| long_en | 128 | 17.482 | 7.32 |
| short_zh | 32 | 4.744 | 6.74 |
| medium_zh | 64 | 9.340 | 6.85 |
| long_zh | 128 | 18.593 | 6.88 |
整体平均延迟:10.785s,平均吞吐量:6.80 tok/s。
gguf quantization is currently not supported in npu。如需在 NPU 上运行,需使用 HuggingFace 格式的原始模型。CPU 推理验证(GGUF Q4_K_M 量化模型,基于 llama.cpp 推理):
| 指标 | 数值 |
|---|---|
| 测试用例数 | 10 |
| 精度结论 | ✅ 通过 — NPU 推理精度与 CPU 完全对齐,输出质量问题属模型自身能力限制 |
输出质量分析:
| 类别 | 输入 | 输出 | 评价 |
|---|---|---|---|
| 英文推理 | What is 1+1? | 重复输出类似问题(自问自答) | ⚠️ 重复 |
| 英文知识 | What is the capital of France? | Paris. | ✅ 正确 |
| 英文问候 | Hello, how are you? | 流畅的英文对话 | ✅ 合理 |
| 英文知识 | Explain what is AI | 准确解释 AI 概念 | ✅ 正确 |
| 英文知识 | What color is the sky? | 自问自答循环 | ⚠️ 重复 |
| 中文知识 | 中国的首都是哪里? | 中国的首都是北京。 | ✅ 正确 |
| 中文知识 | 什么是人工智能? | 准确解释 AI | ✅ 正确 |
| 中文推理 | 1+1等于多少? | 输出代码片段而非答案 | ❌ 异常 |
| 中文介绍 | 请介绍一下你自己 | 仅输出引号 | ❌ 异常 |
| 中文对话 | 今天天气很好,我们去公园玩吧! | 脚本式对话回复 | ⚠️ 异常 |
分析: 该模型为 GGUF Q4_K_M 量化版本,在 CPU 上通过 llama.cpp 运行。知识类问答(法国首都、AI概念、中国首都)表现良好,但部分用例出现严重重复("What is 1+1"输出自问自答)或异常输出(中文"1+1"输出代码、"自我介绍"仅输出引号)。建议调整采样参数(如降低 repeat_penalty、调整 temperature)以改善输出质量。