rhscope.integrations.trl

TRL integration - monitor-driven reward penalty for rollout-based RL.

Thin adapter over rhscope.integrations.base.MonitorPenaltyAdapter. The adapter delegates scoring to monitor.score_batch, so this integration works identically for heuristic monitors, judge monitors, probe monitors, and composite monitors - no framework-level branching on detector type.

TRL’s trl.GRPOTrainer accepts a list of reward functions and sums their outputs. We register the monitor-penalty callable there and return negative values proportional to the monitor’s score - no subclassing of the trainer, no fragile callback hooks into TRL internals.

Public entry points:

make_monitor_reward_fn() - builds a reward callable + stats buffer matching the TRL signature.

MonitorStatsCallback() - a transformers.TrainerCallback that flushes buffered metrics on every on_log event so monitor scores appear in TensorBoard / stdout alongside the trainer’s own metrics.

class rhscope.integrations.trl.MonitorRewardFn(fn, stats, monitor, penalty_coef, adapter)[source]

Bases: object

Callable + its stats buffer, returned from make_monitor_reward_fn().

Variables:
Parameters:
fn: Callable[[...], list[float]]
stats: MonitorStats
monitor: HackMonitor
penalty_coef: float
adapter: MonitorPenaltyAdapter
rhscope.integrations.trl.make_monitor_reward_fn(monitor, *, penalty_coef=0.0, sink=None, metric_prefix='monitor')[source]

Build a TRL-compatible reward function that applies monitor as a penalty.

Works with any monitor - heuristics, judge, probe, or composite. The monitor owns whatever model / tokenizer it needs internally (probes own their policy reference; judges own their judge model; heuristics own nothing). The reward function is therefore model-agnostic at the TRL layer.

The resulting callable returns -penalty_coef * monitor.score per completion. Pair it with your base reward function in GRPOTrainer(reward_funcs=[base, monitor_rw]) - TRL sums the outputs, so a negative value from this function subtracts from the base reward.

Parameters:
  • monitor (HackMonitor) – Loaded HackMonitor.

  • penalty_coef (float) – Coefficient applied to monitor scores. 0.0 makes this function a pure logger that always returns zero rewards.

  • sink (Sink | None) – Optional metric sink.

  • metric_prefix (str) – Namespace for logged metrics.

Returns:

A MonitorRewardFn (acts as a callable via .fn).

Return type:

MonitorRewardFn

class rhscope.integrations.trl.MonitorStatsCallback(reward_fn)[source]

Bases: object

Trainer callback that injects buffered monitor metrics into on_log.

Pass this to GRPOTrainer(callbacks=[...]) to have monitor scores appear in the trainer’s own log stream (and therefore in whichever report_to backend is configured) without opening a second writer.

Parameters:

reward_fn (MonitorRewardFn)

on_log(args, state, control, logs=None, **_)[source]

Drain the monitor stats buffer into the trainer’s logs dict.

Parameters:
Return type:

Any