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"] and data["responses"] are already lists of strings. Preferred because it is explicit.

  • Token ids plus a tokenizer. data["input_ids"] and data["responses"] are int tensors; a tokenizer is 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: object

verl-compatible reward model that penalises reward hacking.

Call the instance directly or via compute() with a verl DataProto (or anything that exposes .batch or quacks like a dict). 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.0 disables 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 penalty_coef: float

Active penalty coefficient.

property monitor: HackMonitor

Underlying monitor instance (read-only accessor).

property stats: Any

Buffered metrics stream, suitable for attaching to a verl logger.

compute(data)[source]

Score a verl batch and return rewards, raw scores, and metrics.

Parameters:

data (Any) – A verl DataProto (any object with a batch attribute) or a plain dict / Mapping with the same keys.

Returns:

MonitorRewardResult.

Return type:

MonitorRewardResult

class rhscope.integrations.verl.MonitorRewardResult(rewards, scores, metrics)[source]

Bases: object

Return 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:
rewards: Tensor
scores: ndarray
metrics: dict[str, float]
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 MonitorRewardModel directly; 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 MonitorRewardModel ready to be plugged into verl.

Return type:

MonitorRewardModel