g
gcw_coj3XaOd/PKU-Alignment-beaver-7b-v1.0-reward
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PKU-Alignment/beaver-7b-v1.0-reward - 昇腾 NPU 推理部署

1. 模型简介

模型名称: 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)

输入规格:

  • input_ids: (batch_size, seq_len) int64 token IDs
  • attention_mask: (batch_size, seq_len) int64 (1=real token, 0=padding)
  • vocab_size: 32001
  • 支持序列长度: 1 ~ 2048

输出规格:

  • scores: (batch_size, 1) float32/bfloat16 标量奖励分数
  • 分数范围: 无固定范围(训练后通常在 [-5, 5] 区间)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.34.0HuggingFace 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

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一: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

3.3 运行推理

# 在 NPU 上运行推理(使用完整 inference.py 脚本)
python inference.py \
    --model-path ./beaver_weights \
    --device npu \
    --test 1 2 3 4 5

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型权重目录路径(含 config.json 和 safetensors)
--devicestrnpu推理设备,可选 cpu 或 npu
--testint[]内置测试自定义 token ID 序列用于测试推理

4. 推理成功日志

4.1 单条推理日志

[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

4.2 批量推理统计(同批次多条序列)

[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

5. 测试样例及输出结果

样例 1:正向序列评分

输入 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

样例 2:短序列评分

输入 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

样例 3:CPU 推理(无 NPU 环境)

运行命令:

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

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: PKU-SafeRLHF 内置训练数据子集

评测指标:

指标结果Baseline差异
CPU/NPU Score 一致性Δ < 0.1-通过
CPU 推理 (seq=5)-2.0938-2.09380.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!

8. NPU 配置说明

  • NPU 型号: Ascend 910
  • NPU 卡数: 1 (单卡)
  • Tensor Parallel: 不需要(单卡推理)
  • 显存占用: ~13GB (bfloat16 加载)
  • CANN 版本: 8.5.1
  • PyTorch 版本: 2.1.0
  • torch_npu 版本: 2.1.0

注意事项:

  • 奖励模型不支持 vLLM-Ascend 推理(非生成式模型),使用 torch_npu 原生推理
  • 权重使用 bfloat16 加载以节省 NPU HBM
  • NPU 上 scaled_dot_product_attention 不支持同时设置 is_causal=True 和 attn_mask,已将 causal mask 预计算到 attn_mask 中

9. 已知问题

  • CPU 推理速度较慢(~37s for seq=64),建议使用 NPU 推理(~33ms for seq=5)
  • NPU 上 bfloat16 的部分操作可能回退到 float32(出现 double dtype cast warning 属正常现象)