rhscope.monitor

User-facing monitor object.

HackMonitor wraps a trained Probe with the base model it was trained on, providing the score_batch surface the integrations (TRL, verl, DPO) consume. Everything downstream goes through score_batch(prompts, responses).

Two load paths:

HackMonitor is deliberately tied to probes. If you want to compose with a custom detector (an external judge, a rule you wrote, another team’s probe), use CompositeDetector or wrap your callable in CallableDetector - those compose with the probe detector at the same level.

Example:

from rhscope import HackMonitor

monitor = HackMonitor.from_pretrained("my-org/my-probe")
scores  = monitor.score_batch(prompts, completions)
class rhscope.monitor.HackMonitor(detector, *, threshold=0.0)[source]

Bases: object

A trained probe plus the base model it reads activations from.

Parameters:
  • detector (Detector) – Typically a ProbeDetector. Any other Detector also works (a CompositeDetector over several probes, say) but the probe-specific accessors (base_model, layer, pooling, score, require_base_model) will raise AttributeError.

  • threshold (float) – Score threshold for predict() / predict_batch(). Probe scores sit on either side of zero after DoM / logistic training; 0.0 is the default.

classmethod from_probe(probe, model, tokenizer, *, batch_size=8, max_length=1024, threshold=0.0)[source]

Wrap an already-loaded Probe + its base model as a monitor.

Prefer from_pretrained() when loading from disk / Hub.

Parameters:
Return type:

HackMonitor

classmethod from_pretrained(path_or_hub_id, *, model=None, tokenizer=None, model_name_or_path=None, threshold=0.0, revision=None, cache_dir=None, device=None, dtype=torch.float32, trust_remote_code=False, batch_size=8, max_length=1024)[source]

Load a trained probe + its base model and wrap them as a monitor.

Parameters:
  • path_or_hub_id (str | Path) – Local probe stem or Hugging Face Hub repo ID.

  • model (Any) – Optional pre-loaded policy model to probe. If omitted, the probe’s own base_model metadata is loaded unless model_name_or_path overrides it.

  • tokenizer (Any) – Required when model is provided.

  • model_name_or_path (str | None) – Override the model to load; default is the probe’s recorded base_model. Overriding is allowed but emits a warning - probes rarely generalize across models.

  • threshold (float) – Decision threshold for predict().

  • cache_dir (str | Path | None) – Hub resolution controls.

  • max_length (int) – Policy-model load + probe-scoring controls.

  • revision (str | None)

  • cache_dir

  • device (str | device | None)

  • dtype (dtype)

  • trust_remote_code (bool)

  • batch_size (int)

  • max_length

Returns:

A HackMonitor wrapping a ProbeDetector.

Return type:

HackMonitor

property detector: Detector

The underlying detector.

property threshold: float

Decision threshold for predict().

property name: str

Detector’s display name.

property requires_base_model: str | None

Return the base model this monitor is bound to, or None.

Probe-backed monitors return the probe’s recorded base_model. Non-probe monitors (a user-supplied CompositeDetector or CallableDetector) return None since we cannot know their binding.

score_batch(prompts, responses)[source]

Score a batch of (prompt, response) pairs. Higher = more hack-like.

Parameters:
Return type:

ndarray

predict_batch(prompts, responses)[source]

Boolean mask: score_batch(...) >= threshold.

Parameters:
Return type:

ndarray

property probe: Probe

The wrapped probe (probe-backed monitors only).

property metadata: ProbeMetadata

Probe metadata (probe-backed monitors only).

property base_model: str

Base model the probe was trained against (probe-backed monitors only).

property layer: int

Transformer-block index the probe reads from (probe monitors only).

property pooling: Literal['last_token', 'mean', 'max']

Pooling strategy the probe uses (probe monitors only).

property hidden_dim: int

Expected hidden-state dim (probe monitors only).

property category: str

Hacking category (probe monitors only).

score(activations)[source]

Low-level activation scoring (probe-backed monitors only).

Equivalent to monitor.probe.score(activations). Prefer score_batch() for most code - it handles the activation forward pass internally.

Parameters:

activations (Tensor)

Return type:

Tensor

predict(activations)[source]

Low-level binary prediction on pre-extracted activations.

Parameters:

activations (Tensor)

Return type:

Tensor

require_base_model(model_name_or_path, *, strict=True)[source]

Verify the monitor is applicable to model_name_or_path.

Probe-backed monitors check probe.metadata.base_model. Mismatches raise ValueError by default (set strict=False to warn instead). Non-probe monitors are a no-op since we cannot reason about their binding.

Parameters:
  • model_name_or_path (str)

  • strict (bool)

Return type:

None