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:
objectResult of fitting a probe at a single candidate layer.
- Variables:
- Parameters:
- class rhscope.training.trainer.TrainProbeResult(probe, trials, best_trial, training_data_hash='', extra=<factory>)[source]¶
Bases:
objectReturn value of
train_probe.- Variables:
probe (rhscope.probes.base.Probe) – The trained probe (best layer, re-fit on train+val).
trials (list[rhscope.training.trainer.LayerTrialResult]) – Per-(layer, probe_type) AUROC summary across all candidates.
best_trial (rhscope.training.trainer.LayerTrialResult) – The winning trial (highest val AUROC).
- Parameters:
probe (Probe)
trials (list[LayerTrialResult])
best_trial (LayerTrialResult)
training_data_hash (str)
- trials: list[LayerTrialResult]¶
- best_trial: LayerTrialResult¶
- 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:
Split
examplesstratified into train and validation.Extract pooled activations at every candidate layer in a single forward pass per example.
For each candidate layer, fit a probe of
probe_typeon train activations and score validation activations.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 usecandidate_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:
TrainProbeResultwith the fitted probe, all trial metrics, and the winning trial. The probe’smetadata.val_aurocis populated with the best trial’s validation AUROC.- Raises:
ValueError – If inputs are invalid (empty, single-class, bad layer, …).
- Return type:
- 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_probecallers.Imports Transformers lazily so that it is only required when a model is actually being probed.
- Parameters:
- Returns:
A
(model, tokenizer)pair.- Return type: