rhscope.integrations.base¶
Backend-agnostic monitor-penalty primitive.
MonitorPenaltyAdapter is the framework-neutral core of the
“monitor-as-reward-shaping” pattern. Given a batch of
(prompt, response) pairs, it computes per-example monitor scores and
returns a reward delta. The adapter delegates scoring entirely to the
monitor’s score_batch() - so heuristic, judge,
probe, and composite monitors all plug in identically.
Framework-specific integrations (TRL, verl, OpenRLHF, a bespoke training
loop) wrap this with their expected call conventions. The TRL integration
(rhscope.integrations.trl) is a thin adapter on top of this module.
- class rhscope.integrations.base.MonitorStats(lock=<factory>, latest=<factory>, history=<factory>, call_count=0)[source]¶
Bases:
objectThread-safe buffer of per-batch monitor metrics.
Framework adapters consume this via
consume()to inject metrics into their trainer’s own log stream, so monitor scores show up alongside the trainer’s native metrics in whichever backend (TensorBoard, wandb, …) the trainer is configured with.- Parameters:
- lock: allocate_lock¶
- class rhscope.integrations.base.MonitorPenaltyResult(rewards, raw_scores, metrics)[source]¶
Bases:
objectWhat
MonitorPenaltyAdapter.score_batch()returns per call.- Variables:
rewards (numpy.ndarray) –
(N,)reward deltas. Each entry is-penalty_coef * score[i]and should be added to the base reward by the caller (TRL does this automatically when the adapter is registered as a reward function).raw_scores (numpy.ndarray) –
(N,)unmodified monitor scores. Exposed so advanced callers can apply custom shaping without re-running scoring.metrics (dict[str, float]) – Summary metrics for this batch (mean / max / min / hack fraction / penalty coef). Keyed by
"<metric_prefix>/<name>".
- Parameters:
- class rhscope.integrations.base.MonitorPenaltyAdapter(monitor, *, penalty_coef=0.0, sink=None, metric_prefix='monitor')[source]¶
Bases:
objectFramework-agnostic monitor-as-penalty primitive.
A single adapter instance owns:
A
HackMonitorwrapping any detector family (heuristic, judge, probe, composite).A
Sinkfor streaming metrics.A
MonitorStatsbuffer that framework adapters drain on each log tick.
The adapter does not care what kind of detector the monitor wraps - everything runs through
monitor.score_batch(prompts, responses).- Parameters:
monitor (HackMonitor) – Loaded
HackMonitor. Any detector family works.penalty_coef (float) – Coefficient applied to monitor scores.
0.0makes this adapter a pure logger: scores are recorded butMonitorPenaltyResult.rewardsis all zeros.sink (Sink | None) – Optional metric sink.
metric_prefix (str) – Namespace for logged metric keys.
- property monitor: HackMonitor¶
The wrapped
HackMonitor.
- property stats: MonitorStats¶
Shared metrics buffer; framework adapters read this on log ticks.
- score_batch(prompts, responses)[source]¶
Score
(prompts, responses)and compute reward deltas.- Parameters:
- Returns:
MonitorPenaltyResultwith rewards, raw scores, and the metric buffer that was recorded for this batch.- Raises:
ValueError – If the two sequences have different lengths.
- Return type: