模型适配 · 强化学习 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=32001score_dim=1,score_bias=true,score_type=reward,do_normalize=falsemodel.*(标准 LLaMA 主干)+ score_head.weight/bias + normalizer.mean/varpip install -r requirements.txthuggingface.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'))"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。
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!"]
}'python inference.py --local --model-path /tmp/models/beaver-7b-v1.0-cost --device npu:1/v1/score 与离线 torch_npu 推理输出一致LlamaForScore 架构 vLLM 未注册 → 新增最小适配器(复用 LlamaModel + score_head 线性层)--task score,端点 /v1/score(cross-encoder 语义,与 Sentence-Transformers CrossEncoder 对齐)normalizer.mean/var 在 do_normalize=false 时推理不参与,权重加载时忽略lm_head,不可按 causal LM 加载| 文件 | 内容 |
|---|---|
agent_workflow.png | 适配/部署流程截图 |
npu_device_call.png | NPU 设备调用(npu-smi / 服务日志)截图 |
model_result.png | 模型推理结果截图 |