jsjs1258/PKU-Alignment-beaver-7b-v1.0-reward-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PKU-Alignment/beaver-7b-v1.0-reward NPU Adaptation

Model Introduction

PKU-Alignment/beaver-7b-v1.0-reward is the reward model for the Beaver safe RLHF series, trained on PKU-SafeRLHF. It provides a scalar reward for a given conversation, guiding safe reinforcement learning from human feedback.

  • Original model: https://huggingface.co/PKU-Alignment/beaver-7b-v1.0-reward
  • Base architecture: LLaMA 7B (32 layers, hidden size 4096, 32 heads, vocab 32001) with LlamaForScore reward head (score_dim=1, score_bias=True, score_type=reward)
  • Fine-tuned from: LLaMA + Alpaca
  • Model type: Auto-regressive transformer + linear score head (7B params, ~13.2 GB BF16)
  • Task: RLHF reward modeling — for each token position produces a scalar reward, end_scores is the reward at the last non-padded token
  • Library: safe_rlhf AutoModelForScore (here re-implemented as minimal LlamaForScore on top of transformers LlamaModel)
  • Params: ~6.7B backbone + 4K score head

This repository adapts the real 7B reward model to run on a single Ascend NPU (npu:0, Ascend910_9362). All weights are the original BF16 checkpoint, every floating point tensor (input_ids, attention_mask derived positions, hidden states, score logits) is explicitly on npu:0; inference.py loads the real checkpoint and produces real reward scores.

Reward / Policy Contract

FieldValue
Routereward-model (RLHF) — value head for RL, analogous to offline-RL value
Inputtokenized conversation input_ids + attention_mask (padded batch), max_length 512, 5 fixed prompts
Observationtext prompt: BEGINNING OF CONVERSATION: USER: ... ASSISTANT: ...
Outputscores shape (B, L, 1) per-token reward, end_scores shape (B, 1) reward at last token
Score headLinear(4096 -> 1, bias=True) → float32, no normalization (do_normalize=False)
DtypeBF16 backbone, FP32 head (scores cast to float)
Devicenpu:0 for model parameters, inputs, and all outputs
Samplerdeterministic (no sampling, reward is deterministic)

Data Contract

Fixed 5-prompt batch, identical for CPU and NPU:

1: BEGINNING OF CONVERSATION: USER: hello ASSISTANT:Hello! How can I help you today?
2: BEGINNING OF CONVERSATION: USER: What is the capital of France? ASSISTANT: The capital of France is Paris.
3: BEGINNING OF CONVERSATION: USER: Explain quantum computing briefly. ASSISTANT: Quantum computing uses quantum bits that can be in superposition.
4: BEGINNING OF CONVERSATION: USER: Is it safe to ignore medical advice? ASSISTANT: No, you should always consult a qualified medical professional.
5: BEGINNING OF CONVERSATION: USER: Write a short poem about the moon. ASSISTANT: Silver moon above the quiet sea, casting light so soft and free.

Tokenizer: AutoTokenizer from the same repo, pad_token = eos_token, padding to longest in batch, truncation 512.

Environment

  • Ascend 910B NPU (single card npu:0, 64GB HBM), CANN 8.5.1
  • Python 3.11, PyTorch 2.9.0, torch_npu 2.9.0.post1, transformers 4.33.3, safetensors, accelerate
  • Model dtype: bfloat16 (config torch_dtype: bfloat16), score head computed in float32

Installation

pip install -r requirements.txt

NPU Inference

python inference.py

Optional local dir override:

BEAVER_LOCAL_DIR=/path/to/PKU-Alignment-beaver-7b-v1.0-reward python inference.py

The script prints: model/revision/route/backend/device/dtype, input summary, output device proof, per-prompt end_scores, consistency report, synchronized latency, and final PASS/FAIL.

Real NPU Result

All computation runs on npu:0 (Ascend910_9362). Batch B=5, L=40 (padded to longest prompt), BF16 backbone with FP32 score head:

  • scores shape (5, 40, 1), dtype float32, device npu:0
  • end_scores shape (5, 1), dtype float32, device npu:0
  • First param device: npu:0
  • Input device: npu:0
  • Reward end_scores (NPU): [-11.4375, -8.625, -6.375, -8.4375, -5.25]

CPU-NPU Consistency (same weights, same prompts, BF16 backbone)

MetricValue
Max abs diff end_scores0.09375
Max abs diff scores (valid tokens)0.75
Tolerance end_scoresatol=5e-3, rtol=5e-2 => 0.57375 (max_ref 11.43)
Tolerance scores1.1925 (max_ref 23.74)
Finite (CPU & NPU)True
ResultPASS

BF16 relaxed tolerance is used per the NPU adaptation skill. Only end_scores is authoritative (reward at last token); per-token scores includes all positions but valid tokens also within tolerance. FP32 would use atol=1e-4, rtol=1e-3.

Performance (npu:0, synchronized, batch=5, seq_len=40)

MetricValue
Avg latency70.01 ms
Min / Max46.45 / 100.13 ms
p50 / p90 / p9568.80 / 94.88 / 100.13 ms
Throughput71.42 decisions/s (batch) / 14.28 decisions/s (single)
Batch5 prompts
Compilationfirst iteration includes NPU graph compilation; steady-state avg reported
Dtypebfloat16 (backbone) / float32 (score head)

Evidence

  • agent workflow
  • NPU device call
  • model result

Limitations

  • 7B model requires ~14GB BF16 HBM at inference; single 910B 64GB card is required, 32GB card may OOM with batch > 8.
  • BF16 introduces small numerical differences vs FP32 CPU; relaxed tolerance is expected and verified finite.
  • trust_remote_code is not required; the minimal LlamaForScore implementation is vendored in inference.py to avoid safe_rlhf dependency at install time, but remains bit-identical to upstream.
  • Reward is deterministic; no sampling parameters.