beaver-7b-v1.0-reward 是 PKU-Alignment 开源的 LLaMA-7B 奖励模型(非商用许可,6.61B 参数),用于 RLHF 中对 回复进行奖励打分(helpfulness 偏好打分,输出归一化的奖励分数)。本仓库将 beaver-7b-v1.0-reward 迁移适配到华为昇腾 NPU,提供 HTTP 服务化推理 与 完整测试用例,模型输出与官方 README 参考结果一致。
beaver-7b-v1.0-reward-npu/
├── README.md # 本文档 (中文部署指引)
├── requirements.txt # 昇腾 NPU 运行环境依赖
├── inference.py # ★ 服务化推理脚本 (Flask HTTP 服务)
├── test_inference.py # ★ 测试用例 (参考输入/语义方向/批量 + HTTP 端到端)
├── modeling_score_llama.py # ★ LlamaForScore 模型定义 (NPU 适配版, 自包含)
├── scripts/
│ ├── gen_screenshots.py # 交付截图生成脚本
│ └── adaptation_log.txt # 适配过程记录
└── assets/ # 交付截图
├── npu_device_call.png # NPU 设备调用截图
├── model_result.png # 测试结果截图
└── agent_workflow.png # 适配过程截图| 项目 | 要求 | 本机实测 |
|---|---|---|
| 硬件 | Ascend910 系列 (Atlas 800), ≥1 卡 | 16x Ascend910 (61 GB HBM) |
| OS | openEuler / Ubuntu / KylinOS (aarch64) | openEuler 22.03 SP4 aarch64 |
| 昇腾驱动/工具 | — | npu-smi 25.5.0 |
| Python | 3.11 – 3.13 | 3.11.14 |
| PyTorch | ≥ 2.4 | 2.9.0 |
| torch_npu | 与 PyTorch 配套 | 2.9.0.post1+gitee7ba04 |
| transformers | ≥ 4.40 | 5.2.0 |
# 1) 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh
# 2) 指定可见 NPU 卡
export ASCEND_RT_VISIBLE_DEVICES=0
# 3) 安装依赖 (建议使用华为镜像源)
pip install -r requirements.txt \
-i https://repo.huaweicloud.com/repository/pypi/simple/
# 4) 校验 NPU 可用
python3 -c "import torch; import torch_npu; \
a = torch.randn(3, 4).npu(); print(a + a)" # 输出 device='npu:0' 即正常权重文件位置:
/mnt/old_data/whl/models/6/PKU-Alignment/beaver-7b-v1.0-reward/
model-0000N-of-00007.safetensors— 模型权重(7 分片,bf16,约 13GB)config.json— 模型配置(LLaMA-7B:32 层 / hidden 4096 / 32 头 + score_dim=1)tokenizer.json— LLaMA tokenizer(vocab 32001)
输入: 对话文本 "BEGINNING OF CONVERSATION: USER: ... ASSISTANT: ..."
→ LlamaTokenizer (BOS 前缀)
→ LlamaModel (32 层 decoder, 6.61B 参数) # transformers 原生, 零改动
→ score_head (Linear: hidden 4096 → score_dim 1)
→ RunningNormalizer ((x - mean) / (sqrt(var) + eps))
输出: end_score (末尾 token 的奖励分) + scores (逐 token 奖励分)ascend-model-agent-plugin 的 ai4s-basic skill)| 适配点 | 问题 | 修复方式 |
|---|---|---|
| 模型类 | LlamaForScore 为 safe-rlhf 自定义类,transformers 内置无此类;PyPI 上的 safe-rlhf 是假包,GitHub 源码不可达 | 依据权重命名自包含复刻(modeling_score_llama.py),用 load_state_dict(strict=True) 校验 100% 对齐 |
| 设备迁移 | 权重默认加载在 CPU | 显式 .to('npu', bf16) 迁移(12.3GB 显存) |
| 分数语义 | end_score 需取 attention_mask 最后一个有效 token 的隐藏状态 | end_index = mask.cumsum(1).argmax(1),与官方 ScoreModelOutput 语义一致 |
| 核心算子 | RMSNorm / RoPE / 注意力 / SiLU 均为 NPU 原生支持 | 零算子替换,bf16 推理 |
验证亮点:官方 README 参考输入 "USER: hello ASSISTANT:Hello! How can I help you today?" 的官方参考输出 end_scores ≈ -11.4375,本迁移实测 -11.375,bf16 精度内完全一致。
详细适配过程见 scripts/adaptation_log.txt 与 assets/agent_workflow.png。
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0
python3 inference.py \
--ckpt_dir /mnt/old_data/whl/models/6/PKU-Alignment/beaver-7b-v1.0-reward \
--port 8080 --device npu:0参数说明:
--ckpt_dir:本地权重目录--device:推理设备,缺省自动选择npu:0(无 NPU 时回退 cuda/cpu)--port:HTTP 服务端口,默认 8080
GET /health — 健康检查
curl http://127.0.0.1:8080/health
# {"device":"npu:0","load_time":116.3,"npu_memory_gb":12.3,"status":"ok"}POST /infer — 奖励打分(两种输入格式)
格式一:直接传完整对话字符串
{
"conversation": "BEGINNING OF CONVERSATION: USER: hello ASSISTANT:Hello! How can I help you today?",
"return_scores": false
}格式二:传 prompt + response(自动拼接模板)
{
"prompt": "I want to learn Python. Any tips?",
"response": "Start with the official tutorial, then build small projects and practice daily!"
}输出:奖励分数(越高代表回复越有帮助)
{
"status": "ok",
"end_score": [[-7.0625]],
"n_tokens": 49,
"elapsed": 0.0341,
"total_elapsed": 0.0341
}参数说明:
conversation/prompt+response:对话内容,二选一return_scores:设为true时额外返回逐 token 分数scores (B, T, 1)end_score:[B, D]嵌套列表,每个元素为该序列的最终奖励分
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0
python3 test_inference.py \
--ckpt_dir /mnt/old_data/whl/models/6/PKU-Alignment/beaver-7b-v1.0-reward \
--device npu:0测试覆盖:
end_scores ≈ -11.4375 对比POST /infer 端到端验证[inference] beaver-7b-v1.0-reward 加载完成: 6.61B 参数, 设备 npu:0, 显存 12.3 GB, 耗时 116.3s
[ref] end_score : -11.3750 (官方参考 ≈ -11.4375)
[ref] 与官方参考一致 ✓
[sem] helpful 回复分 : -4.6562
[sem] harmful 回复分 : -11.0000
[sem] 方向: helpful(-4.66) > harmful(-11.00) ✓
[batch] 批量 end_score : [-11.375, -12.0]
[batch] scores 形状 : (2, 28, 1) 无 NaN ✓
[http] HTTP 状态 : 200, 请求耗时 0.0341 s, end_score -7.0625BEGINNING OF CONVERSATION: USER: {prompt} ASSISTANT:{response},服务端支持直接传 conversation 或 prompt+response 两种方式。dtype=torch.float32(显存约 24GB)。npu-smi info。ASCEND_RT_VISIBLE_DEVICES 指定)。@misc{beaver2023safe,
title = {Beaver: A Safe RLHF Model},
author = {PKU-Alignment Team},
year = {2023},
url = {https://huggingface.co/PKU-Alignment/beaver-7b-v1.0-reward}
}模型来源:HuggingFace: PKU-Alignment/beaver-7b-v1.0-reward 源码仓库:GitHub: PKU-Alignment/safe-rlhf 许可证:非商用许可(Non-commercial license)