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

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

1. 模型简介

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

架构细节:

  • Base model: LLaMA (hidden_size=4096, 32 layers, 32 attention heads)
  • Score head: Linear(4096, 1) with bias
  • Score type: reward (cost)
  • Activation: SiLU (Swish)
  • Normalization: RMSNorm

输入规格:

  • Type: text (prompt + response pairs)
  • Max length: 2048 tokens (config), recommended 512 for inference
  • Format: "Human: ...\nAssistant: ..."

输出规格:

  • Type: scalar float32 (reward/cost score)
  • Shape: (batch_size, 1)
  • Range: unbounded (raw logits, not calibrated)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.37.0HuggingFace Transformers
safe-rlhf>= 1.0.0Beaver 模型建模代码
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"

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

3.3 运行推理

# 基本用法:评分单条文本
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: ..."

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--devicestrnpu:0NPU 设备
--max-lengthint512最大序列长度
--inputlistNone输入文本列表
--batch-sizeint8批量大小

4. 推理成功日志

4.1 单条推理日志

[模型] 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

4.2 批量推理日志

[基准测试] 8次推理统计:
  总耗时: 52.1ms
  平均: 6.5ms/sample
  最小: -
  最大: -
  状态: SUCCESS

注意: 输出为原始 logit 分数(非校准后概率),负值属正常范围。


5. 测试样例及输出结果

样例 1:安全问答

运行命令:

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

样例 2:拒绝有害请求

运行命令:

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

样例 3:批量推理(8 条)

运行命令:

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

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 硬件设备调用日志

NPU 设备调用

6.3 模型最终适配结果

模型适配结果


7. 适配说明

7.1 架构适配策略

该模型使用 LlamaForScore 架构(LLaMA + score head),vLLM 不直接支持此架构。采用以下适配方案:

  1. 基础模型:使用 transformers.LlamaModel 加载标准 LLaMA-7B 权重
  2. Score Head:手动从 safetensors 中加载 score_head.weight 和 score_head.bias
  3. 推理引擎:使用 torch_npu + transformers 直接推理(非 vLLM)
  4. Key Remapping:checkpoint 中 score head 的 key 为 score_head.*,需要手动映射

7.2 vLLM 兼容性

  • vLLM registry 中无 LlamaForScore / beaver 架构
  • 由于 score head 输出为标量而非 logits,不适合直接用于 text generation
  • 当前方案使用 transformers + torch_npu 原生推理,吞吐量 6.5ms/sample

8. NPU 配置说明

  • NPU 型号:Ascend910_9362
  • NPU 数量:1 卡 (NPU:0)
  • 显存占用:~12,603 MB / 65,536 MB (HBM)
  • 精度:bfloat16
  • CANN 版本:8.5.1
  • PyTorch 版本:2.9.0+cpu (with torch_npu)
  • transformers 版本:4.57.6

9. 已知问题

  1. Score 值为负:输出为原始 bfloat16 logit,未经过温度校准。不同输入的分数可比性受校准影响。
  2. vLLM 不直接支持:因架构为 LlamaForScore(非标准 Causal LM),暂不支持 vLLM 推理引擎。
  3. 最大序列长度:config 中 max_position_embeddings=2048,超长文本需截断。