rhscope.integrations.dpo

Offline monitor scoring for DPO-style preference datasets.

DPO (and its siblings IPO, KTO, ORPO, SimPO) optimize a policy against preference pairs - chosen / rejected completions for a shared prompt - with no online rollouts. There is no reward signal to penalize mid-training, so the natural integration point is before training: score both halves of each pair with the monitor and let the caller filter or re-weight preferences that reward hacking behaviors.

score_dpo_pairs() is the single entry point. Its return value carries both score vectors plus per-pair metadata that downstream code can turn into filter masks.

class rhscope.integrations.dpo.DPOPairScores(chosen_scores, rejected_scores, threshold, preference_flips_to_hacker)[source]

Bases: object

Monitor scores for a batch of DPO preference pairs.

Variables:
  • chosen_scores (numpy.ndarray) – (N,) scores for the preferred completion of each pair.

  • rejected_scores (numpy.ndarray) – (N,) scores for the dispreferred completion.

  • threshold (float) – The monitor’s binary-decision threshold at scoring time.

  • preference_flips_to_hacker (numpy.ndarray) – Boolean mask; True where the chosen completion scores higher than the rejected - i.e. the preference data rewards more hacking. Prime candidates for filtering.

Parameters:
chosen_scores: ndarray
rejected_scores: ndarray
threshold: float
preference_flips_to_hacker: ndarray
property mean_chosen: float

Mean monitor score across chosen completions (0.0 if empty).

property mean_rejected: float

Mean monitor score across rejected completions (0.0 if empty).

property hacking_rate_chosen: float

Fraction of chosen completions whose score is at or above threshold.

property hacking_rate_rejected: float

Fraction of rejected completions at or above threshold.

filter_mask(*, drop_preference_flips=True)[source]

Return a boolean mask over pairs the caller may want to keep.

Parameters:

drop_preference_flips (bool) – If True, excludes pairs where the chosen completion is more hack-like than the rejected (i.e. the preference data is actively pushing the policy toward hacking).

Returns:

(N,) boolean array; True means “keep this pair”.

Return type:

ndarray

rhscope.integrations.dpo.score_dpo_pairs(monitor, model, tokenizer, prompts, chosen, rejected, *, batch_size=8, max_length=1024)[source]

Score both halves of every preference pair with monitor.

The chosen and rejected completions are scored independently against the policy model that monitor was trained for - the caller is responsible for aligning the model argument with monitor.base_model.

Parameters:
  • monitor (HackMonitor) – Loaded HackMonitor.

  • model (Any) – Policy model whose activations the probe reads. Should match monitor.base_model.

  • tokenizer (Any) – Matching tokenizer.

  • prompts (Sequence[str]) – Shared prompts, one per preference pair.

  • chosen (Sequence[str]) – Preferred completions, aligned with prompts.

  • rejected (Sequence[str]) – Dispreferred completions, aligned with prompts.

  • batch_size (int) – Forward-pass batch size.

  • max_length (int) – Token truncation length.

Returns:

DPOPairScores carrying both score vectors.

Raises:

ValueError – If input lengths differ.

Return type:

DPOPairScores