rhscope.bench.ref_delta

Reference-model delta detector for reward hacking.

The research hook

Every RLHF/GRPO/DPO training loop holds two models in memory at once: the trained policy and a frozen reference. The reference-model KL penalty uses both to regularise drift. The same pair exposes a second, underused signal:

s(prompt, response) = log P_policy(response | prompt)
  • log P_reference(response | prompt)

High s means the trained policy is unusually eager to emit this response relative to the pre-RL baseline. Combined with the training reward, this is a running measure of what the policy has learned to say under the reward pressure. If the reward is hacking-inducing, high s correlates with hacking-shaped responses - and does so without any probe training, any labelled data, or any judge model.

Status

This is a cheap, model-agnostic baseline for monitor-free runs. It is not a replacement for a probe: on its own it flags drift, which overlaps with hacking only when the reward signal itself was pushing toward hacking. Use it as a complementary signal next to a probe (for example as a second input to CompositeDetector), as a first-pass detector for evaluation runs where you do not have a trained probe yet, or as a diagnostic plot during research sweeps.

The signal has not been empirically validated as a standalone hacking detector; treat it as research-grade.

Example:

from rhscope.bench import RefDeltaDetector
from transformers import AutoModelForCausalLM, AutoTokenizer

policy    = AutoModelForCausalLM.from_pretrained("my-rl-finetune")
reference = AutoModelForCausalLM.from_pretrained("my-rl-finetune-base")
tokenizer = AutoTokenizer.from_pretrained("my-rl-finetune-base")

det = RefDeltaDetector(
    policy=policy,
    reference=reference,
    tokenizer=tokenizer,
)
scores = det.score_batch(prompts, responses)
class rhscope.bench.ref_delta.RefDeltaDetector(policy, reference, tokenizer, *, batch_size=4, max_length=2048, aggregate='mean', name=None)[source]

Bases: Detector

Score hacking as a log-prob delta between policy and reference models.

For every (prompt, response) pair, compute

\[s = \frac{1}{T}\!\sum_{t=1}^{T}\!\bigl( \log P_{\mathrm{policy}}(x_t \mid x_{<t}) - \log P_{\mathrm{reference}}(x_t \mid x_{<t}) \bigr)\]

where the sum is over the T tokens of response (the prompt tokens are masked out). Length normalization makes the score comparable across responses of different lengths.

The computation uses teacher forcing: one forward pass per model over prompt + response with no sampling. Marginal cost in a live RL run is near zero because both models are already resident for the KL penalty.

Parameters:
  • policy (Any) – The trained / in-training policy model (a loaded AutoModelForCausalLM).

  • reference (Any) – The frozen pre-RL reference model. Must share a vocabulary with policy.

  • tokenizer (Any) – Tokenizer shared by both models.

  • batch_size (int) – Number of (prompt, response) pairs per forward pass.

  • max_length (int) – Truncation length for the concatenated prompt + response input.

  • aggregate (str) – How to collapse per-token deltas into a scalar score. "mean" (default) returns the average log-prob delta over response tokens; "sum" returns the un-normalized total.

  • name (str) – Override the detector’s display name.

Raises:

ValueError – If aggregate is not one of {"mean", "sum"}.

score_batch(prompts, responses)[source]

Score every (prompt, response) pair with the ref-delta signal.

Returns:

1-D numpy array. Positive values mean the policy is more eager than the reference to emit this response; under a hacking-inducing reward, high values are correlated with hacking.

Parameters:
Return type:

ndarray