rhscope.integrations.verl¶
verl integration - reward-model adapter for the second major RLHF framework.
verl is an open RLHF framework
that organises training around reward models: callables that consume a
batch of rollouts and return per-example scalar rewards. rhscope’s
monitor integrates with verl by exposing a thin adapter that unpacks
verl’s DataProto-shaped batches, scores each rollout with any
HackMonitor, and returns a reward tensor
suitable for either standalone use or additive combination with verl’s
task-reward tensor.
The integration is dependency-free: we do not import verl at
runtime. The adapter accepts anything exposing the batch dict surface
verl uses (batch["prompts"] / batch["responses"]) and returns
plain torch tensors. This lets the adapter be unit-tested without
installing verl and keeps rhscope’s core install lean.
Example:
from rhscope.integrations.verl import MonitorRewardModel
reward_model = MonitorRewardModel(monitor, penalty_coef=0.1)
# Register alongside verl's task reward. The two tensors sum to the
# final reward signal verl optimises against.
rewards = reward_model(data_proto) # or reward_model.compute(data_proto)
The adapter supports two batch encodings:
Decoded strings.
data["prompts"]anddata["responses"]are already lists of strings. Preferred because it is explicit.Token ids plus a tokenizer.
data["input_ids"]anddata["responses"]are int tensors; atokenizeris passed at construction time and used to decode.
- class rhscope.integrations.verl.MonitorRewardModel(monitor, *, penalty_coef=0.0, tokenizer=None, sink=None, metric_prefix='monitor')[source]¶
Bases:
objectverl-compatible reward model that penalises reward hacking.
Call the instance directly or via
compute()with a verlDataProto(or anything that exposes.batchor quacks like adict). Returns a tensor of per-example rewards sized(B,). Combine with verl’s own task reward by summing the two tensors; rhscope’s return is already signed so penalties are negative.- Parameters:
monitor (HackMonitor) – Loaded
HackMonitor(any detector family).penalty_coef (float) – Coefficient applied to monitor scores.
0.0disables the penalty and makes this a pure logger.tokenizer (Any) – Required iff the verl batch stores token ids rather than decoded strings. Must match the policy’s tokenizer.
sink (Sink | None) – Optional metric sink; receives
monitor/*metrics at every call.metric_prefix (str) – Namespace used for logged metrics. Defaults to
"monitor"; useful for distinguishing multiple monitors in the same run.
- property monitor: HackMonitor¶
Underlying monitor instance (read-only accessor).
- class rhscope.integrations.verl.MonitorRewardResult(rewards, scores, metrics)[source]¶
Bases:
objectReturn value of
MonitorRewardModel.compute().- Variables:
rewards (torch.Tensor) –
(B,)tensor. Negative where the monitor flags hacking; scale is-penalty_coef * monitor_score.scores (numpy.ndarray) – Raw monitor scores for each example, higher = more hack-like.
metrics (dict[str, float]) – Latest buffered metrics (
monitor/mean_score, …).
- Parameters:
- rhscope.integrations.verl.make_verl_reward_model(monitor, *, penalty_coef=0.0, tokenizer=None, sink=None, metric_prefix='monitor')[source]¶
Convenience factory mirroring
make_monitor_reward_fn()for TRL.Equivalent to constructing
MonitorRewardModeldirectly; exported for symmetry with the TRL side of the API.- Parameters:
monitor (HackMonitor) – Loaded
HackMonitor.penalty_coef (float) – Coefficient on monitor scores.
0.0-> logger only.tokenizer (Any) – Only needed if the verl batch stores token ids rather than decoded strings.
sink (Sink | None) – Optional metric sink.
metric_prefix (str) – Namespace for logged metrics.
- Returns:
A
MonitorRewardModelready to be plugged into verl.- Return type: