ggg_0963/Baichuan2-7B-Chat
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

Baichuan2-7B-Chat on vLLM-Ascend #+NPU

1. 简介

本文档记录 baichuan-inc/Baichuan2-7B-Chat 模型在 vLLM-Ascend 环境的快速部署与验证结果。

Baichuan2-7B-Chat 是百川智能推出的 70 亿参数大语言模型,支持中英双语对话,具备通用对话和推理能力。

相关获取地址:

  • 权重下载地址(ModelScope):https://modelscope.cn/models/baichuan-inc/Baichuan2-7B-Chat
  • 权重下载地址(HuggingFace):https://huggingface.co/baichuan-inc/Baichuan2-7B-Chat

2. 验证环境

组件版本
vllm-ascend0.18.0rc1
vllm0.18.0
transformers4.57.6
torch-npu2.9.0
CANN8.5.1
SOCascend910_9391
  • NPU:1 逻辑卡(Ascend 910B2,64GB HBM)
  • 模型路径:/home/openmind/.cache/modelscope/hub/models/baichuan-inc/Baichuan2-7B-Chat

3. 模型配置

参数值
架构BaichuanForCausalLM
参数量7B
层数32
隐藏维度4096
注意力头数32
词表大小125696
精度BF16(原始)/ Float16(vLLM 推理)

4. 模型下载

# 从 ModelScope 下载
modelscope download --model baichuan-inc/Baichuan2-7B-Chat

5. 基础推理验证

使用 vLLM-Ascend 加载模型进行推理:

from vllm import LLM, SamplingParams

MODEL_PATH = "baichuan-inc/Baichuan2-7B-Chat"

llm = LLM(
    model=MODEL_PATH,
    trust_remote_code=True,
    dtype="float16",
    tensor_parallel_size=1,
    max_model_len=2048,
    gpu_memory_utilization=0.9,
)

sampling = SamplingParams(max_tokens=64, temperature=0)
outputs = llm.generate(["The capital of France is"], sampling)
print(outputs[0].outputs[0].text)
# Paris.

6. Smoke 验证

6.1 冒烟测试脚本

运行 4 项冒烟测试验证模型功能:

python3 smoke_test.py

6.2 验证结果

=== Baichuan2-7B-Chat vLLM-Ascend Smoke Test ===
[1/4] Testing basic generation...
  PASS: Paris.
[2/4] Testing Chinese generation...
  PASS: 中国的首都是北京。
[3/4] Testing batch generation...
  [1] The answer to 2 + 2 is 4. This is a basic arithmetic operation...
  [2] Large language models (LLMs) are a type of artificial intelligence...
  [3] Silent branches sway, Leaves dance in golden sunlight, Nature whispers peace...
  PASS: 3 prompts processed
[4/4] Testing math reasoning (GSM8K style)...
  PASS: Shawn has five toys. He got two toys each from his mom and dad, so he received a...
=== All 4/4 tests PASSED (74.5s) ===

验证项说明:

测试项输入预期结果
英文基础生成The capital of France is包含 ParisPASS
中文生成中国的首都是哪里?包含 北京PASS
批量生成3 个 prompt3 条非空输出PASS
数学推理GSM8K 风格题目包含正确答案 9PASS

7. 精度评测

7.1 评测配置

  • 数据集:GSM8K(5-shot)
  • 数据来源:ModelScope AI-ModelScope/gsm8k
  • 评测框架:vLLM-Ascend offline inference
  • 设备:NPU (npu:0)
python3 eval_gsm8k_vllm.py --tensor_parallel 1 --batch_size 64 --output logs/gsm8k_vllm.json

7.2 评测结果

指标基线NPU 结果差异
GSM8K 5-shot28.00%28.05% (370/1319)+0.05%

GSM8K NPU 准确率 28.05%,与基线 28.00% 差异 +0.05%(< 1%),验证通过。

7.3 评测日志

=== Baichuan2-7B-Chat GSM8K 5-shot vLLM-Ascend (tp=1, batch=64) ===
Total test samples: 1319
Loading model with vLLM-Ascend...
Model loaded.
Generating (1319 samples, batch=64)...
============================================================
Baichuan2-7B-Chat GSM8K 5-shot Results (vLLM-Ascend)
============================================================
  Accuracy: 28.05% (370/1319)
  Total time: 91s
  Throughput: 14.44 samples/s
============================================================
Results saved to logs/gsm8k_vllm.json

7.4 PyTorch 推理对比

同时使用 transformers + PyTorch NPU 方案进行评测作为对比:

推理方案精度耗时
vLLM-Ascend (float16)28.05%91s
PyTorch NPU (float32, 4卡并行)28.51%12473s

8. 注意事项

  1. use_cache 兼容性:使用 transformers 直接加载时,Baichuan2 的自定义 modeling 代码与 DynamicCache 不兼容。需设置 model.config.use_cache = False 和 GenerationConfig(use_cache=False)。推荐使用 vLLM-Ascend 方案以避免此问题。

  2. vLLM dtype:vLLM 推理使用 float16(NPU 原生支持),无需使用 float32。

  3. tokenizer 提示:vLLM 会提示使用 slow tokenizer,不影响推理结果,可忽略。

  4. 显存占用:float16 推理时模型约占用 14 GB HBM,单卡 64GB 即可运行。

  5. 此为 Chat 版本:模型已进行指令微调,适用于对话和推理场景,支持中英双语。