赛事: 昇腾 Model Agent 模型适配大赛(第二季) 引擎: torch_npu + LLaMA(score head) 硬件: Ascend 910B 系列(实测 Ascend910B4) 标签:
npuascendpytorchtransformers
| 属性 | 值 |
|---|---|
| 原始模型 | PKU-Alignment/beaver-7b-v1.0-cost |
| 架构 | LLaMA 7B + score head(LlamaForScore) |
| 参数量 | 6.6B |
| 任务类型 | RLHF 成本打分(Cost Model) |
| 输入格式 | 文本 prompt + response |
| 输出格式 | 标量 Reward 得分 |
⚠️ 本仓库不包含模型权重(交付件仅
inference.py/readme.md/requirements.txt/assets/), 推理前必须先从原始模型仓单独下载权重。
依赖清单(requirements.txt):torch / torch_npu、transformers、safetensors
代码获取:inference.py 内置 LlamaForScore 等价实现(LlamaModel + score head),
从 checkpoint 分片 7 加载 score_head.weight 权重。
# 1. 安装依赖
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple/
# 2. 下载模型权重(仓库不含权重,必须单独下载)
git clone https://gitcode.com/hf_mirrors/PKU-Alignment/beaver-7b-v1.0-cost.git .
git lfs pull
# 3. 运行 NPU 推理(默认示例文本打分)
python3 inference.py --model_path . --device npu:0python3 inference.py --model_path . --device npu:0预期输出:
[Model Agent] ✅ NPU 设备就绪: Ascend910B4
[Model Agent] ✅ 模型加载完成 → npu:0 (LLaMA 7B reward)
[Model Agent] 📊 输入文本: 20 tokens
[Model Agent] ✅ 推理完成 → Reward 得分 -8.3750, 耗时 410 ms
[Model Agent] ✅ 结果已保存 → inference_result.json.
├── inference.py # NPU 推理脚本
├── readme.md # 本文件
├── requirements.txt # 依赖清单
└── assets/ # 截图素材本模型由 昇腾 Model Agent 自动完成 NPU 适配。
Beaver LLaMA 成本模型(LlamaForScore)迁移至昇腾 NPU:
inference.py 实现 LlamaModel + score_head 等价结构(LLaMA 底座 + 线性打分头),
从 checkpoint 分片加载 score_head.weight/bias;torch_npu 完成设备初始化,取最后一个有效 token 的 hidden state 经 score head 输出 reward。