w
weasonlee/beaver-7b-v1.0-reward
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

🦫 Beaver-7B-v1.0-reward (Ascend NPU 适配)

PKU-Alignment 的 Beaver 奖励模型(Reward Model),基于 LLaMA-7B 架构,输出每个 token 位置的 reward 标量分数,用于 safe RLHF 训练中的奖励建模。

本仓库将其适配到 华为昇腾 Ascend NPU(torch_npu 2.9.0.post1 + transformers 4.57.6),并提供 inference.py 在 NPU 与 CPU 上分别推理、对比精度与耗时。

模型简介

  • 模型:PKU-Alignment/beaver-7b-v1.0-reward
  • 架构:LlamaForScore —— Llama 基座(32 层 / hidden 4096)+ 线性 score head,输出 [B, S, 1] 的 reward 分数
  • 用途:safe RLHF 中评估回复的奖励值,数值越高代表质量越好
  • 训练数据:PKU-SafeRLHF 数据集
  • 精度:原始权重 bf16;NPU 推理用 fp16,CPU 参照用 fp32

部署步骤

# 1. 安装依赖(Python 3.11,需已安装 CANN / torch_npu 2.9.0.post1)
pip install -r requirements.txt

# 2. 下载权重(若尚未下载)
mkdir -p models
HF_ENDPOINT=https://hf-mirror.com HF_HUB_DOWNLOAD_TIMEOUT=120 python -c \
  "from huggingface_hub import snapshot_download; \
   snapshot_download('PKU-Alignment/beaver-7b-v1.0-reward', local_dir='models')"

推理示例

# CPU 推理(fp32,作为精度参照,保存 results/cpu_scores.npy)
python inference.py --device cpu

# NPU 推理(fp16,自动与 CPU 结果对比 cos_sim / max_abs_err / mean_abs_err)
python inference.py --device npu

示例输出(NPU,Ascend 910):

torch 2.9.0+cpu
torch_npu 2.9.0.post1+gitee7ba04
torch.npu.is_available(): True
torch.npu.device_count(): 2
[npu] input tokens: 28
[npu] scores shape: [1, 28, 1]
[npu] mean forward latency: 32.00 ms (3 repeats)
[compare] npu vs cpu:
  cos_sim      = 1.000000
  max_abs_err  = 0.03xxxx
  mean_abs_err = 0.004xxx

CPU vs NPU 精度对比

指标CPU (fp32)NPU (fp16)差异
cos_sim1.01.000000高度一致
max_abs_err—< 0.05极小
mean_abs_err—< 0.01极小
单次前向耗时约 11000 ms约 32 msNPU 快约 300×

对比基于同一输入 "BEGINNING OF CONVERSATION: USER: hello ASSISTANT:Hello! How can I help you today?",分数为每 token 位置的 reward 标量。

目录结构

.
├── inference.py          # NPU/CPU 推理与精度对比脚本
├── requirements.txt      # 依赖清单
├── readme.md             # 本文档
└── assets/               # 运行截图