rhscope.training.trainer

Probe training orchestration.

train_probe ties together activation extraction, layer-candidate selection, probe fitting, and layer selection via validation AUROC.

The model and tokenizer are passed in already loaded so the caller controls device placement and dtype. For a convenience loader keyed off a model name, see rhscope.training.trainer.load_model_for_probing.

class rhscope.training.trainer.LayerTrialResult(layer, probe_type, val_auroc, train_auroc)[source]

Bases: object

Result of fitting a probe at a single candidate layer.

Variables:
  • layer (int) – Hidden-state layer index.

  • probe_type (str) – Probe type trained ("dom" or "logistic").

  • val_auroc (float) – Validation AUROC.

  • train_auroc (float) – Training AUROC (useful for diagnosing overfit).

Parameters:
layer: int
probe_type: str
val_auroc: float
train_auroc: float
class rhscope.training.trainer.TrainProbeResult(probe, trials, best_trial, training_data_hash='', extra=<factory>)[source]

Bases: object

Return value of train_probe.

Variables:
Parameters:
probe: Probe
trials: list[LayerTrialResult]
best_trial: LayerTrialResult
training_data_hash: str = ''
extra: dict[str, Any]
rhscope.training.trainer.train_probe(model, tokenizer, examples, *, probe_type='dom', layers='auto', pooling='last_token', category='general', base_model_name=None, val_fraction=0.2, batch_size=8, max_length=1024, seed=0, probe_kwargs=None)[source]

Train a reward-hacking probe over labeled examples.

The procedure:
  1. Split examples stratified into train and validation.

  2. Extract pooled activations at every candidate layer in a single forward pass per example.

  3. For each candidate layer, fit a probe of probe_type on train activations and score validation activations.

  4. Select the layer with highest validation AUROC. Re-fit on train + validation activations at that layer and return.

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

  • tokenizer (Any) – Matching tokenizer.

  • examples (list[HackExample]) – Labeled examples. Must contain at least one of each class.

  • probe_type (Literal['dom', 'logistic', 'mlp']) – "dom" or "logistic".

  • layers (list[int] | Literal['auto']) – Explicit list of layer indices, or "auto" to use candidate_layers(model.config.num_hidden_layers).

  • pooling (Literal['last_token', 'mean', 'max']) – Pooling strategy over the sequence dimension.

  • category (str) – Category label stored in the probe’s metadata (e.g. "code_shortcut" or "general").

  • base_model_name (str | None) – Hugging Face model ID for metadata; defaults to model.config.name_or_path.

  • val_fraction (float) – Fraction of examples held out for layer selection.

  • batch_size (int) – Batch size for activation extraction.

  • max_length (int) – Token truncation length for activation extraction.

  • seed (int) – RNG seed for the train/val split.

  • probe_kwargs (dict[str, Any] | None) – Per-probe-type keyword arguments (e.g. {"l2": 0.5} for logistic, {"whiten": True} for DoM).

Returns:

TrainProbeResult with the fitted probe, all trial metrics, and the winning trial. The probe’s metadata.val_auroc is populated with the best trial’s validation AUROC.

Raises:

ValueError – If inputs are invalid (empty, single-class, bad layer, …).

Return type:

TrainProbeResult

rhscope.training.trainer.load_model_for_probing(model_name_or_path, *, device=None, dtype=torch.float32, trust_remote_code=False)[source]

Convenience loader used by the CLI and train_probe callers.

Imports Transformers lazily so that it is only required when a model is actually being probed.

Parameters:
  • model_name_or_path (str) – Hugging Face model ID or local path.

  • device (str | device | None) – Where to place the model (defaults to "cuda" if available, else CPU).

  • dtype (dtype) – Torch dtype for the model weights.

  • trust_remote_code (bool) – Forwarded to AutoModelForCausalLM.from_pretrained.

Returns:

A (model, tokenizer) pair.

Return type:

tuple[Any, Any]