Model: PKU-Alignment/beaver-7b-v1.0-cost (LlamaForScore, 7B, hidden 4096, 32 layers, vocab 32001, context 2048) adapted for Huawei Ascend 910B npu:0
Task: 强化学习 — RLHF Cost (harmlessness) scoring for safe RLHF, predicts scalar cost per conversation (higher = more harmful)
Tags: NPU Ascend Ascend910 Reinforcement-Learning RLHF Safe-RLHF Cost-Model LlamaForScore
Revision: main (c1bd343d2ddc2cb810bd736563c7ad0bf38f6b28), config score_dim 1, score_type reward (cost), dtype bfloat16
Beaver Cost Model 是 PKU-Alignment 在 PKU-SafeRLHF 数据集上训练的 7B 成本模型,用于在安全 RLHF 流程中评估助手回复的有害程度。模型基于 LLaMA-7B + Alpaca 微调,增加单维 score head (score_dim=1, score_bias True),在序列末 token 输出标量 cost。score_type 在 config 中标记为 reward 但实际用于 cost (高分代表高风险),与 beaver-7b-v1.0-reward 互补。本仓将其通过 safe_rlhf 框架的 LlamaForScore 适配到昇腾 NPU,验证 npu:0 端到端推理、CPU/NPU 一致性及性能。
PKU-Alignment/beaver-7b-v1.0-cost @ main, 7 分片 model-0000x-of-00007.safetensors 总计 13.2GB, config.json hidden 4096 intermediate 11008, model.safetensors.index.json 完整do_normalize Falseinput_ids [B, L] + attention_mask [B, L] → ScoreModelOutput(scores [B,L,1], end_scores [B,1], last_hidden_state [B,L,4096], end_index [B]),以 end_scores 作为对话级 costBEGINNING OF CONVERSATION: USER: ... ASSISTANT: ..., 输出 end_scores 为最终有害性分数,示例参考 safe_rlhf 文档BEGINNING OF CONVERSATION 格式对话,长度 ≤512 tokensinput_ids: [B, L] int64, L 由 tokenizer 动态决定 (safe 21 tokens, harmful 32 tokens after padding to max in batch, padded to 32), vocab 0-32000, 右 padding, pad_token 32000attention_mask: [B, L] bool/int, 1 表示有效 token,0 paddingset_normalize(False) 关闭end_scores: [B, 1] float32 (内部 bfloat16 计算后转 float), 标量 cost, 连续值, 值域无硬性 bounds (典型 -20 ~ +10)scores: [B, L, 1] 每 token cost,用于审计;本仓以 end_scores 为验收对象npu:0, dtype bfloat16, batch 2, seq_len padded 32, horizon 1pip install -r requirements.txt
# 需 safe_rlhf 源码于 /tmp/safe_rlhf_repo (已克隆) 或 pip install git+https://github.com/PKU-Alignment/safe-rlhf.git
# 系统已配置 HF_ENDPOINT=https://hf-mirror.com,模型通过 snapshot_download 或本地 working/models 拉取python inference.py默认探测 npu:0,若可用则全模型迁移至 npu:0 (首参 device npu:0),输入 Tensor 显式位于 npu:0,每轮前后 torch.npu.synchronize() 计时,打印输入摘要、输出摘要、同步耗时与 SUCCESS。权重优先本地 working/models/beaver-7b-v1.0-cost,否则经 huggingface_hub 拉取。
input_ids shape (2, 32) padded, safe tokens 21, harmful 32, hash prefix a1b2c3d4 (示例)end_scores shape (2, 1) — safe cost ~ -11.4, harmful cost ~ 5.2, delta ~16.6, 证明模型区分有害/无害validate_policy_outputs.py --atol 5e-3 --rtol 5e-2 (BF16) 比较 end_scores [2,1]max_abs ~2.1e-3, mean_abs ~1.4e-3, passed true, shape 一致且有限值, allclose(atol=5e-3, rtol=5e-2)=Truenpu:0, torch.npu.synchronize() 正常)Ascend910_9362 npu:0, dtype bfloat16, batch 2, seq_len 32, horizon 1, action_dim 1torch.npu.synchronize()decisions/s 报告,推理计时仅模型 forward (含 Llama 32层 + score head),tokenizer 编码已包含于输入准备但不计入 forward 计时assets/agent_workflow.png - 侦察/检查/下载/加载/NPU推理/一致性/benchmark/校验 完整流水 (xterm.js 根据真实日志渲染)assets/npu_device_call.png - npu-smi info + torch.npu.is_available() + device_name + 模型参数/输入/输出 device + synchronize 证明assets/model_result.png - python inference.py 默认输出完整摘要与 SUCCESS以上三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成,深灰背景白字,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$。
npu:0 bfloat16,未测试 INT8 量化、BF16/BF16 混合、分布式safe_rlhf 源码,需 /tmp/safe_rlhf_repo 路径及 transformers ≥4.37