liuhongwei-2026/beaver-7b-v1.0-cost
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Beaver 7B Cost Model (beaver-7b-v1.0-cost) on Ascend NPU

模型适配 · 强化学习 RLHF 成本/安全偏好模型 (Reward Model) · 昇腾 NPU · vLLM-Ascend 服务化推理

Beta 版本头信息 {hardware NPU}

模型简介

PKU-Alignment/beaver-7b-v1.0-cost 是 Beaver(Safe RLHF)系列的成本模型(Cost Model), 基于 LLaMA-2-7B 主干,使用 PKU-SafeRLHF 数据集训练,用于在 RLHF 流程中评估回复的"危害/成本":分数越低代表回复越安全。

  • 架构:LlamaForScore(safe-rlhf 自定义类)= LLaMA-2-7B 主干 + score_head 线性头
    • hidden_size=4096,num_hidden_layers=32,num_attention_heads=32,vocab_size=32001
    • score_dim=1,score_bias=true,score_type=reward,do_normalize=false
  • 参数量:~6.74B(bf16,7 个 safetensors 分片,约 13.2GB)
  • 权重键布局:model.*(标准 LLaMA 主干)+ score_head.weight/bias + normalizer.mean/var

部署方式(昇腾 NPU + vLLM-Ascend 服务化)

1. 环境依赖

  • Python 3.11 + torch 2.9.0 + torch_npu 2.9.0.post1 + CANN 8.5.1
  • vLLM 0.18.0 + vLLM-Ascend 0.18.0
  • transformers 4.57.6
pip install -r requirements.txt

2. 权重准备

huggingface.co 直连不通时使用镜像(ModelScope 优先,或 hf-mirror / gitcode LFS):

# ModelScope
python -c "from modelscope import snapshot_download; print(snapshot_download('PKU-Alignment/beaver-7b-v1.0-cost', cache_dir='/data/.cache/models'))"

3. 启动 vLLM-Ascend 服务

LlamaForScore 是 safe-rlhf 自定义架构,vLLM 未内置。本仓库附带 vLLM 适配器 (vllm/model_executor/models/llama_score.py + registry 注册 LlamaForScore), 复刻 safe-rlhf 前向语义:LLaMA 主干 → 末 token hidden state → score_head → 序列分数。

# 前台启动(默认 8000 端口)
vllm serve /tmp/models/beaver-7b-v1.0-cost \
  --task score \
  --max-model-len 2048 \
  --gpu-memory-utilization 0.9 \
  --port 8000

多卡环境注意:npu:0 可能被其他会话占用,可通过 ASCEND_RT_VISIBLE_DEVICES=1 指定 npu:1。

4. 服务化推理

python inference.py --url http://127.0.0.1:8000/v1/score --model beaver-7b-v1.0-cost

也可以直接 curl:

curl -s http://127.0.0.1:8000/v1/score \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "beaver-7b-v1.0-cost",
    "text_1": ["As a responsible citizen, how should you react if you find a lost wallet on the street?"],
    "text_2": ["I would take the money out and throw the wallet away. Finders keepers!"]
  }'

5. 离线验证(可选,torch_npu 直推)

python inference.py --local --model-path /tmp/models/beaver-7b-v1.0-cost --device npu:1

精度验证结果

  • 服务化 /v1/score 与离线 torch_npu 推理输出一致
  • cost 模型成功区分安全 / 不安全回复(安全回复分数更低)
  • NPU 推理延迟 / 吞吐见 assets/model_result.png

适配要点

  1. LlamaForScore 架构 vLLM 未注册 → 新增最小适配器(复用 LlamaModel + score_head 线性层)
  2. score 任务走 vLLM --task score,端点 /v1/score(cross-encoder 语义,与 Sentence-Transformers CrossEncoder 对齐)
  3. normalizer.mean/var 在 do_normalize=false 时推理不参与,权重加载时忽略
  4. 纯 LLM 服务化(vLLM-Ascend 0.18.0);非生成式模型,无 lm_head,不可按 causal LM 加载

截图素材(assets/)

文件内容
agent_workflow.png适配/部署流程截图
npu_device_call.pngNPU 设备调用(npu-smi / 服务日志)截图
model_result.png模型推理结果截图

相关链接

  • 模型源:https://huggingface.co/PKU-Alignment/beaver-7b-v1.0-cost
  • 模型镜像:https://ai.gitcode.com/hf_mirrors/PKU-Alignment/beaver-7b-v1.0-cost
  • 代码仓库:https://github.com/PKU-Alignment/beaver
  • 本项目仓库:https://ai.atomgit.com/liuhongwei-2026/beaver-7b-v1.0-cost