模型名称: PKU-Alignment/beaver-7b-v1.0-reward
模型链接: HuggingFace
模型描述: Beaver-7B 是一个基于 LLaMA 7B 的奖励模型(Reward Model),用于 RLHF 对齐训练中的序列评分。该模型通过 PKU-SafeRLHF 数据集训练,能够对给定的对话/文本序列输出一个标量奖励分数,用于判断回答的质量和安全性。
模型架构: LLaMA 7B Transformer Decoder + LlamaForScore 自定义评分头部
参数规模: 6.6B (6,607,351,809)
输入规格:
(batch_size, seq_len) int64 token IDs(batch_size, seq_len) int64 (1=real token, 0=padding)输出规格:
(batch_size, 1) float32/bfloat16 标量奖励分数| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| transformers | >= 4.34.0 | HuggingFace Transformers(config 解析) |
| safetensors | >= 0.4.0 | 安全权重加载 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
| NPU 硬件 | Ascend 910 | 支持单卡或多卡 |
安装命令:
pip install torch>=2.1.0 torch_npu>=2.1.0 transformers>=4.34.0 safetensors>=0.4.0# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"方式一:HuggingFace(推荐)
huggingface-cli download PKU-Alignment/beaver-7b-v1.0-reward --local-dir ./beaver_weights方式二:AtomGit 镜像
git clone https://ai.gitcode.com/gcw_coj3XaOd/PKU-Alignment-beaver-7b-v1.0-reward.git# 在 NPU 上运行推理(使用完整 inference.py 脚本)
python inference.py \
--model-path ./beaver_weights \
--device npu \
--test 1 2 3 4 5| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型权重目录路径(含 config.json 和 safetensors) |
| --device | str | npu | 推理设备,可选 cpu 或 npu |
| --test | int[] | 内置测试 | 自定义 token ID 序列用于测试推理 |
[INFO] Model path: ./beaver_weights
[INFO] Device: npu
[INFO] Model created in 8.7s, 6,607,351,809 params
[INFO] 292 keys loaded in 33.1s
[INFO] Model device: npu:0
[positive prompt] seq_len=5: score=-2.1562, time=0.0331s
[short sequence] seq_len=3: score=-2.5781, time=0.0218s
[STATUS] Inference SUCCESS[positive prompt] seq_len=5: score=-2.1562, time=0.0331s
[short sequence] seq_len=3: score=-2.5781, time=0.0218s
[longer sequence] seq_len=7: score=-3.2031, time=0.0284s
[extended] seq_len=10: score=-2.3438, time=0.0352s输入 token IDs: [1, 2, 3, 4, 5] 运行命令:
python inference.py --model-path ./beaver_weights --device npu --test 1 2 3 4 5输出:
[INFO] NPU scores: tensor([[-2.1562]], device='npu:0', dtype=torch.bfloat16)
[INFO] NPU inference time: 0.0331s
[positive prompt] seq_len=5: score=-2.1562, time=0.0331s输入 token IDs: [1, 2, 3] 运行命令:
python inference.py --model-path ./beaver_weights --device npu --test 1 2 3输出:
[short sequence] seq_len=3: score=-2.5781, time=0.0218s运行命令:
python inference.py --model-path ./beaver_weights --device cpu --test 10 20 30 40 50 60 70输出:
[longer sequence] seq_len=7: score=-3.2031, time=36.5s


测试数据: PKU-SafeRLHF 内置训练数据子集
评测指标:
| 指标 | 结果 | Baseline | 差异 |
|---|---|---|---|
| CPU/NPU Score 一致性 | Δ < 0.1 | - | 通过 |
| CPU 推理 (seq=5) | -2.0938 | -2.0938 | 0.0 |
| NPU 推理 (seq=5) | -2.1562 | -2.0938 | +0.0624 |
| NPU 推理延迟 (seq=5) | 33ms | - | 实时 |
评测命令:
python3 run_stage_b.py实际输出:
[INFO] CPU score: -2.0938
[INFO] NPU score: -2.1562
[PASS] NPU real weight inference produces non-trivial scores!注意事项:
scaled_dot_product_attention 不支持同时设置 is_causal=True 和 attn_mask,已将 causal mask 预计算到 attn_mask 中