eardoi/beaver-7b-reward-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Beaver 7B Reward(昇腾 NPU 适配)

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)
Python3.10 – 3.11
torch / torch_npu2.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。

验证结果(Ascend 910B, npu:0)

输入:真实文本 "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 形状为 (batch, 2),取 logits[0, 0] 作为奖励分数。
  • 权重以 BF16 加载(NPU 上),推理使用 torch.no_grad()。
  • 设备解析 resolve_device():NPU 可用性检测,失败自动回退 CPU。
  • 模型权重不推送到仓库:*.safetensors / *.bin 已加入 .gitignore, 仓库仅包含推理脚本、模型卡片与截图素材。

目录结构

.
├── README.md          # 模型卡片(本文件)
├── inference.py       # NPU 推理脚本
└── assets/            # 适配过程截图素材

引用

  • 模型:https://huggingface.co/PKU-Alignment/beaver-7b-v1.0-reward
  • 数据:https://huggingface.co/datasets/PKU-Alignment/PKU-SafeRLHF