Beaver 7B Reward 是北京大学(PKU-Alignment)基于 LLaMA-7B 训练的奖励模型 (LlamaForScore,6.6B 参数),使用 PKU-SafeRLHF 数据训练, 用于评估文本的安全性与偏好(RLHF 奖励打分)。
本项目完成其在**华为昇腾 NPU(Ascend 910B)**上的推理适配与验证。
| 项目 | 说明 |
|---|---|
| 模型架构 | LlamaForScore(llama,7B) |
| 参数量 | 6.6B(hidden 4096,32 层) |
| 输入 | 文本序列 |
| 输出 | 奖励分数(越高越符合安全/偏好) |
| 权重格式 | safetensors(13GB,7 分片,BF16) |
| 支持任务 | 文本奖励打分、安全偏好评估 |
| 项目 | 要求 |
|---|---|
| 硬件 | Ascend 910B 系列(≥1 卡,HBM ≥ 32GB) |
| CANN | ≥ 8.0(source /usr/local/Ascend/ascend-toolkit/set_env.sh) |
| Python | 3.10 – 3.11 |
| torch / torch_npu | 2.9.0 / 2.9.0.post1 |
| transformers | ≥ 4.30 |
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install torch_npu==2.9.0.post1 transformers pillow safetensors \
-i https://repo.huaweicloud.com/repository/pypi/simple/权重通过 HuggingFace 镜像获取:
git clone --depth 1 https://hf-mirror.com/PKU-Alignment/beaver-7b-v1.0-reward model_src权重目录包含:model-00001~00007-of-00007.safetensors(13GB)/
tokenizer.json / config.json 等。
cd /workspace/beaver-reward
python3 inference.py --model_path ./model_src \
--text "The weather is nice today." --device npu:0| 参数 | 说明 | 默认值 |
|---|---|---|
--model_path | 模型权重目录 | ./model_src |
--text | 输入文本(真实文本,必选) | - |
--device | 推理设备(npu:0 / cpu) | npu:0 |
--output | 结果保存文件(可选) | stdout |
设备解析:NPU 优先,自动回退 CPU。
输入:真实文本 "The weather is nice today. It is a good day to go for a walk in the park."
[info] model loaded: llama (6.6B)
[info] text: The weather is nice today. It is a good day to go for a walk in the park.
[info] running inference on npu:0 ...
============================================================
[info] 奖励分数: 1.7031 (越高表示越符合安全/偏好)
============================================================6.6B 奖励模型在 NPU 上(BF16)对真实文本完成奖励打分, NPU 推理链路完整跑通。
LlamaForScore(trust_remote_code),但权重与 LLaMA 7B 完全对齐,
可用 transformers 标准 AutoModelForSequenceClassification 加载。logits[0, 0] 作为奖励分数。torch.no_grad()。resolve_device():NPU 可用性检测,失败自动回退 CPU。*.safetensors / *.bin 已加入 .gitignore,
仓库仅包含推理脚本、模型卡片与截图素材。.
├── README.md # 模型卡片(本文件)
├── inference.py # NPU 推理脚本
└── assets/ # 适配过程截图素材