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()- atransformers.TrainerCallbackthat flushes buffered metrics on everyon_logevent 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:
objectCallable + its stats buffer, returned from
make_monitor_reward_fn().- Variables:
fn (collections.abc.Callable[[...], list[float]]) – The reward callable TRL invokes.
stats (rhscope.integrations.base.MonitorStats) – Shared buffer that
MonitorStatsCallbackconsumes.monitor (rhscope.monitor.HackMonitor) – The monitor being applied (for introspection / logging).
penalty_coef (float) – The penalty coefficient in effect.
adapter (rhscope.integrations.base.MonitorPenaltyAdapter) – The underlying backend-agnostic
MonitorPenaltyAdapter.
- Parameters:
stats (MonitorStats)
monitor (HackMonitor)
penalty_coef (float)
adapter (MonitorPenaltyAdapter)
- stats: MonitorStats¶
- monitor: HackMonitor¶
- 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
monitoras 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.scoreper completion. Pair it with your base reward function inGRPOTrainer(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.0makes 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:
- class rhscope.integrations.trl.MonitorStatsCallback(reward_fn)[source]¶
Bases:
objectTrainer 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 whicheverreport_tobackend is configured) without opening a second writer.- Parameters:
reward_fn (MonitorRewardFn)