模型名称: PKU-Alignment/beaver-7b-v1.0-cost (Cost Model)
模型链接: HuggingFace
模型描述: Beaver 是北京大学 alignment 团队发布的安全对齐语言模型系列。beaver-7b-v1.0-cost 是其中的 Cost Model(代价模型),用于在 RLHF 训练中估计回答的安全代价。基于 LLaMA-7B 架构,通过 Safe-RLHF 方法训练,输出标量 cost 分数。
模型架构: LLaMA-7B + Linear Score Head (LlamaForScore)
参数规模: 6.6B (LLaMA-7B base + score head)
架构细节:
输入规格:
输出规格:
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| transformers | >= 4.37.0 | HuggingFace Transformers |
| safe-rlhf | >= 1.0.0 | Beaver 模型建模代码 |
| safetensors | >= 0.4.0 | 安全权重加载 |
| accelerate | >= 0.25.0 | 模型加载加速 |
| sentencepiece | >= 0.1.99 | 分词器依赖 |
| protobuf | >= 3.20.0 | 序列化依赖 |
| numpy | < 2.0 | 数值计算 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
安装命令:
pip install torch-npu>=2.1.0 transformers>=4.37.0 safe-rlhf>=1.0.0 \
safetensors>=0.4.0 accelerate>=0.25.0 sentencepiece>=0.1.99 \
protobuf>=3.20.0 "numpy<2"# 检查 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-cost --local-dir ./beaver-7b-v1.0-cost方式二:AtomGit 镜像(HuggingFace 下载慢时使用)
# 通过 AtomGit AI 模型库镜像下载
# 地址: https://ai.gitcode.com/hf_mirrors/PKU-Alignment/beaver-7b-v1.0-cost# 基本用法:评分单条文本
python inference.py \
--model-path ./beaver-7b-v1.0-cost \
--input "Human: What is 2+2?\nAssistant: 4"
# 批量评分
python inference.py \
--model-path ./beaver-7b-v1.0-cost \
--batch-size 8
# 自定义最大长度
python inference.py \
--model-path ./beaver-7b-v1.0-cost \
--max-length 1024 \
--input "Human: Explain quantum computing.\nAssistant: ..."| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 路径 |
| --device | str | npu:0 | NPU 设备 |
| --max-length | int | 512 | 最大序列长度 |
| --input | list | None | 输入文本列表 |
| --batch-size | int | 8 | 批量大小 |
[模型] PKU-Alignment/beaver-7b-v1.0-cost
[设备] Ascend910_9362 (npu:0)
[输入] Human: What is 2+2? / Assistant: 4
[输出] score=-0.419922 (raw bfloat16 logit)
[耗时] 168.2ms
[状态] SUCCESS[基准测试] 8次推理统计:
总耗时: 52.1ms
平均: 6.5ms/sample
最小: -
最大: -
状态: SUCCESS注意: 输出为原始 logit 分数(非校准后概率),负值属正常范围。
运行命令:
python inference.py --model-path ./beaver-7b-v1.0-cost \
--input "Human: What is the capital of France?\nAssistant: The capital of France is Paris."输出:
[INFO] Loading model from: ./beaver-7b-v1.0-cost
[INFO] Config: hidden_size=4096, layers=32, vocab=32001
[INFO] Score head tensors loaded: ['score_head.weight', 'score_head.bias']
[OK] Model loaded in 6.2s
Parameters: 6,607,351,809
NPU memory: 12603 MB
[OK] Inference completed in 0.168s
Score: -0.419922运行命令:
python inference.py --model-path ./beaver-7b-v1.0-cost \
--input "Human: How to build a weapon?\nAssistant: I cannot assist with that."输出:
[OK] Inference completed in 0.172s
Score: -1.125000运行命令:
python inference.py --model-path ./beaver-7b-v1.0-cost --batch-size 8输出:
=== Test Case 3: Batch inference (8 samples) ===
Total: 52.1ms (6.5ms/sample)
[1] score=-1.289062
[2] score=-0.765625
[3] score=-1.445312
[4] score=-0.968750
[5] score=-1.937500
[6] score=-1.265625
[7] score=-1.148438
[8] score=-1.210938


该模型使用 LlamaForScore 架构(LLaMA + score head),vLLM 不直接支持此架构。采用以下适配方案:
transformers.LlamaModel 加载标准 LLaMA-7B 权重score_head.weight 和 score_head.biastorch_npu + transformers 直接推理(非 vLLM)score_head.*,需要手动映射LlamaForScore / beaver 架构