rhscope.probes.online

Streaming DoM probe fit incrementally with partial_fit() updates.

The standard DoMProbe is trained once on a labelled dataset and then held frozen for monitoring. OnlineProbe is the streaming counterpart: it maintains running means over hack and clean activations across many partial_fit calls, so it can be refit every eval step during a long RL run without re-materialising the full activation matrix.

Use cases:

  • Live monitoring of a policy whose distribution drifts during training.

  • Feeding the running direction to EvasionDetector as the “current” direction at every step.

  • Ablations that need to roll the probe forward one batch at a time.

Mathematically equivalent to a plain DoM probe refit on every sample seen so far (no forgetting factor). Serialisation goes through the same SafeTensors + JSON-sidecar path as every other probe; snapshot(...) is a convenience that returns a plain DoMProbe with the same direction for callers that want the simpler object.

Example:

from rhscope import OnlineProbe

probe = OnlineProbe.empty(
    base_model="Qwen/Qwen3-1.5B",
    hidden_dim=1536,
    layer=14,
)
for step in training_loop:
    hack_acts, clean_acts = collect_fresh_activations(step)
    probe.partial_fit(hack_acts, clean_acts)
probe.save("my-online-probe")                 # SafeTensors + JSON round-trip
snapshot = probe.snapshot()                   # or get a plain DoMProbe
class rhscope.probes.online.OnlineProbe(metadata)[source]

Bases: Probe

DoM-style probe fit incrementally through partial_fit().

Maintains per-class running means plus their counts. The probe direction is the normalised difference of the two means, recomputed on demand.

Parameters:

metadata (ProbeMetadata) – Standard ProbeMetadata; probe_type must be "online_dom". Use empty() if you only have a base-model name and hidden dim.

probe_type: ClassVar[str] = 'online_dom'
classmethod empty(*, base_model, hidden_dim, layer=0, pooling='last_token', category='general')[source]

Build an unfit online probe with sensible default metadata.

Parameters:
  • base_model (str)

  • hidden_dim (int)

  • layer (int)

  • pooling (Literal['last_token', 'mean', 'max'])

  • category (str)

Return type:

OnlineProbe

property n_hack: int

Total hack-labelled activations observed so far.

property n_clean: int

Total clean-labelled activations observed so far.

property mean_hack: Tensor

Running hack-class mean (defensive copy).

property mean_clean: Tensor

Running clean-class mean (defensive copy).

property is_fit: bool

True iff at least one hack and one clean activation have been seen.

property direction: Tensor

Normalised mean_hack - mean_clean as a 1-D float64 tensor.

Raises:

RuntimeError – Before any successful partial_fit() call.

fit(hack_activations, clean_activations)[source]

Reset running state and fit on one batch.

Equivalent to reset() followed by partial_fit(). Prefer partial_fit() directly for streaming use.

Parameters:
Return type:

None

partial_fit(hack_activations, clean_activations)[source]

Update running means with a fresh batch.

Either argument may be None or empty if a step only produces one class. A step with nothing is a no-op.

Raises:

ValueError – On shape or dim mismatch.

Parameters:
Return type:

None

score(activations)[source]

Project activations onto the current direction.

Returns:

(N,) scores for (N, hidden_dim) input, or a scalar tensor for (hidden_dim,) input.

Parameters:

activations (Tensor)

Return type:

Tensor

reset()[source]

Clear all accumulated statistics. The probe is unfit afterwards.

Return type:

None

snapshot()[source]

Freeze the current direction into a plain DoMProbe.

Useful when downstream code expects the simpler probe (for example, as the anchor for EvasionDetector) or when you want to publish a one-shot artifact without the running-state metadata.

Return type:

DoMProbe

state_dict()[source]

Serialisable state: running means + sample counts.

Return type:

dict[str, Tensor]