rhscope.probes.tokens

Token-level probe scoring - localise reward hacking inside a response.

rhscope.activations.extract_activations() returns one pooled activation per example, which is right for the primary scoring flow (one monitor score per rollout). This module adds the orthogonal capability: per-token scoring, so a trained probe can tell you which tokens in a response look hack-like.

Use cases:

  • Attribution. Show the user which fragment of a response the monitor is reacting to (hardcoded return, test-deletion comment, fake assertion, etc).

  • Mid-generation early stop. During RL rollout generation, score the partial response every N tokens and abort if the maximum-so-far crosses a threshold - cheaper than letting the full rollout finish and penalising afterwards.

  • Span-level benchmarks. Compute the probe’s response-span max and report it as the detector output when the probe was trained with response-level labels.

The scoring path reuses the probe’s `` score(activations)`` method without modification. Probes are linear in hidden state, so per-token and per-example scores are two different reductions of the same underlying forward pass.

class rhscope.probes.tokens.TokenScoreResult(response_tokens, per_token_scores, max_score, argmax_token)[source]

Bases: object

Per-response token-level probe scoring.

Variables:
  • response_tokens (list[list[str]]) – Decoded token strings for each example, aligned with per_token_scores. Prompt tokens are excluded.

  • per_token_scores (list[numpy.ndarray]) – Per-token probe scores, one 1-D numpy array per example. Higher means more hack-like.

  • max_score (numpy.ndarray) – (N,) per-example maximum score over response tokens. Suitable as a drop-in scalar in place of the usual pooled probe score.

  • argmax_token (numpy.ndarray) – (N,) per-example index of the peak-scoring response token; response_tokens[i][argmax_token[i]] is the single token the probe reacted to most strongly.

Parameters:
response_tokens: list[list[str]]
per_token_scores: list[ndarray]
max_score: ndarray
argmax_token: ndarray
rhscope.probes.tokens.extract_token_activations(model, tokenizer, prompts, responses, *, layer, batch_size=4, max_length=1024, device=None)[source]

Return per-response-token hidden states at the given layer.

The returned list has one tensor per example of shape (T_response_i, hidden_dim) on CPU in float32. Prompt tokens are excluded because they are not what the probe is supposed to score.

Parameters:
  • model (Any) – Loaded Hugging Face causal LM.

  • tokenizer (Any) – Matching tokenizer.

  • prompts (Sequence[str]) – One prompt per example.

  • responses (Sequence[str]) – Response text aligned with prompts.

  • layer (int) – Hidden-state layer index. 0 is the embedding layer; 1..num_hidden_layers are transformer block outputs.

  • batch_size (int) – Examples per forward pass.

  • max_length (int) – Truncation length for prompt + response.

  • device (str | device | None) – Target device; defaults to the model’s device.

Returns:

One (T, hidden_dim) float32 tensor per example on CPU.

Return type:

list[Tensor]

rhscope.probes.tokens.score_tokens(probe, model, tokenizer, prompts, responses, *, layer=None, batch_size=4, max_length=1024, device=None)[source]

Score every response token with a trained probe.

Parameters:
  • probe (Probe) – A fitted probe. The probe must expose a score() method that accepts activations of shape (T, hidden_dim).

  • model (Any) – Hugging Face causal LM the probe was trained on.

  • tokenizer (Any) – Matching tokenizer.

  • prompts (Sequence[str]) – One prompt per example.

  • responses (Sequence[str]) – Response text aligned with prompts.

  • layer (int | None) – Hidden-state layer. Defaults to probe.metadata.layer.

  • batch_size (int) – Examples per forward pass.

  • max_length (int) – Truncation length for prompt + response.

  • device (str | device | None) – Target device; defaults to the model’s device.

Returns:

TokenScoreResult with per-token scores, per-example max, and the index of the peak-scoring response token.

Return type:

TokenScoreResult