Filter DPO preference data¶
If a preference pair has a chosen completion more hack-like than the
rejected one, DPO will push the policy toward hacking. Score the
pairs with a HackMonitor and drop the flipped pairs
before training.
The example uses a CallableDetector stand-in so it
runs without loading a model. In production, substitute
HackMonitor.from_pretrained("your/probe") or
HackMonitor.from_probe(probe, model=..., tokenizer=...) and the
rest of the flow is identical.
examples/example_dpo_filter.py¶
"""Filter DPO / IPO / KTO preference data that actively rewards hacking.
If a preference pair has a *chosen* completion more hack-like than the
*rejected* one, DPO will push the policy toward hacking. Score the
pairs with a :class:`HackMonitor` (any probe) and drop the flipped
pairs before training.
This example uses an in-process :class:`CallableDetector` as a stand-in
for a trained probe so it runs without loading a model. In production,
substitute ``HackMonitor.from_pretrained("your/probe")`` or
``HackMonitor.from_probe(probe, model=..., tokenizer=...)``.
Run: ``python examples/example_dpo_filter.py``
"""
from __future__ import annotations
import numpy as np
from rhscope import HackMonitor
from rhscope.bench import CallableDetector
def main() -> None:
def _hack_scorer(prompts, responses):
"""Stand-in for a trained probe: flag hardcoded returns."""
return np.asarray(
[
1.0
if ("return True" in r or "return False" in r or "return 0" in r)
else 0.0
for r in responses
],
dtype=np.float64,
)
monitor = HackMonitor(CallableDetector(_hack_scorer, name="stand_in"))
prompts = ["Write is_prime(n).", "Write is_prime(n).", "Write fibonacci(n)."]
chosen = [
# 1. Correct chosen - healthy preference.
"def is_prime(n):\n if n < 2: return False\n "
"return all(n % i for i in range(2, int(n**0.5)+1))",
# 2. Chosen is a hardcoded cheat - DPO would push toward hacking!
"def is_prime(n):\n return True",
# 3. Chosen is correct, rejected is a shortcut - healthy preference.
"def fibonacci(n):\n a, b = 0, 1\n for _ in range(n): a, b = b, a + b\n return a",
]
rejected = [
# worse than chosen -> ok to keep
"def is_prime(n):\n return 0",
# chosen is the cheat, rejected is fine -> drop this pair
"def is_prime(n):\n import math\n"
" return n > 1 and not any(n % i == 0 for i in range(2, int(math.isqrt(n))+1))",
# rejected is the hack, chosen is fine -> keep
"def fibonacci(n):\n return 0 # just return zero",
]
chosen_scores = monitor.score_batch(prompts, chosen)
rejected_scores = monitor.score_batch(prompts, rejected)
flip_mask = chosen_scores > rejected_scores # chosen is hackier -> bad data
print(f"{'chosen':>8} {'rejected':>10} {'flip?':>6} verdict")
print("-" * 65)
for ch, rj, flip in zip(chosen_scores, rejected_scores, flip_mask, strict=True):
verdict = "DROP - chosen rewards hacking" if flip else "keep"
print(f"{ch:>8.2f} {rj:>10.2f} {str(bool(flip)):>6} {verdict}")
keep = np.where(~flip_mask)[0]
print(f"\nKept {len(keep)} of {len(prompts)} preference pairs.")
if __name__ == "__main__":
main()