rhscope.recipes.probe_grid

Grid trainer for the pre-trained probe matrix.

A probe grid is the Cartesian product of (models) x (categories). Nothing in this module is specific to any particular model or category count; a single call trains the full matrix end-to-end.

The driver, train_probe_grid(), takes:

  • A list of ModelConfig (any Hugging Face causal LMs).

  • A list of category names to train probes for.

  • A RHDetectBench to curate training data from.

and produces, for each (model, category) cell:

  • A trained probe artifact on disk.

  • A model card alongside it.

  • An entry in a ProbeGridResult with the per-cell metrics.

Pushing to the Hub is optional and controlled by push_to_hub. Nothing in this module requires a specific model vendor.

rhscope.recipes.probe_grid.GENERAL_KEY = 'general'

Canonical name for the across-categories balanced probe.

class rhscope.recipes.probe_grid.ProbeGridCellResult(model_name_or_path, category, probe_stem, model_card_path, val_auroc, best_layer, train_examples, hub_url, weak)[source]

Bases: object

Outcome of training a single (model, category) cell.

Variables:
  • model_name_or_path (str) – HF model ID or local path.

  • category (str) – Category trained on ("general" for the balanced subset).

  • probe_stem (pathlib.Path) – Local probe stem on disk.

  • model_card_path (pathlib.Path) – Model-card markdown file alongside the artifact.

  • val_auroc (float) – Validation AUROC of the winning layer.

  • best_layer (int) – Winning transformer-block index.

  • train_examples (int) – Count of examples in the final (train + val) fit.

  • hub_url (str | None) – Hub URL if push_to_hub=True; None otherwise.

  • weak (bool) – True iff val_auroc < weak_threshold - surfaces under-performing cells without silently filtering them.

Parameters:
  • model_name_or_path (str)

  • category (str)

  • probe_stem (Path)

  • model_card_path (Path)

  • val_auroc (float)

  • best_layer (int)

  • train_examples (int)

  • hub_url (str | None)

  • weak (bool)

model_name_or_path: str
category: str
probe_stem: Path
model_card_path: Path
val_auroc: float
best_layer: int
train_examples: int
hub_url: str | None
weak: bool
class rhscope.recipes.probe_grid.ProbeGridResult(cells=<factory>, output_dir=None)[source]

Bases: object

Aggregate result of a full grid run.

Variables:
Parameters:
cells: list[ProbeGridCellResult]
output_dir: Path | None = None
weak_cells()[source]

Return cells whose validation AUROC fell below the weak threshold.

Return type:

list[ProbeGridCellResult]

summary()[source]

Markdown summary table across the grid.

Return type:

str

to_dict()[source]

Return a JSON-serializable dict representation of the grid result.

Return type:

dict[str, Any]

save_json(path, *, indent=2)[source]

Write to_dict() as JSON to path and return the resolved Path.

Parameters:
Return type:

Path

rhscope.recipes.probe_grid.train_probe_grid(models, categories, bench, *, output_dir, per_category_examples=2000, general_examples=5000, include_general=True, probe_type='dom', val_fraction=0.2, batch_size=8, max_length=1024, weak_threshold=0.7, seed=0, push_to_hub=False, hub_namespace=None, hub_private=False, hub_token=None, model_loader=<function _load_base_model>, probe_kwargs=None)[source]

Train a Cartesian grid of probes across models x categories.

Each cell loads the model once, extracts activations for a category-specific (or balanced “general”) subset of bench, fits a probe via rhscope.training.train_probe(), writes the artifact and a Hugging Face model card next to it, and optionally pushes both to the Hub.

Parameters:
  • models (Iterable[ModelConfig]) – Each ModelConfig names an HF causal LM. The models are loaded one at a time.

  • categories (Iterable[str]) – Category names to train category-specific probes for. "general" is reserved and appended automatically if include_general=True and not already listed.

  • bench (RHDetectBench) – Source RHDetectBench for curation.

  • output_dir (str | Path) – Root directory for all per-cell artifacts.

  • per_category_examples (int) – Target subset size per category.

  • general_examples (int) – Target subset size for the balanced general probe.

  • include_general (bool) – Whether to also train a general probe per model.

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

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

  • batch_size (int) – Activation forward-pass batch size.

  • max_length (int) – Token truncation length.

  • weak_threshold (float) – Val-AUROC below which a cell is flagged weak=True.

  • seed (int) – Base RNG seed. Each cell derives its own seed deterministically.

  • push_to_hub (bool) – Upload each probe to the Hub after training.

  • hub_namespace (str | None) – Hub org/user name used for auto-generated repo IDs.

  • hub_private (bool) – Create Hub repos as private.

  • hub_token (str | None) – Explicit Hub auth token; defaults to the env / cached login.

  • model_loader (Callable[[ModelConfig], tuple[Any, Any]]) – Callable (ModelConfig) -> (model, tokenizer). Lets tests inject a mock without touching real HF repos.

  • probe_kwargs (Mapping[str, Any] | None) – Forwarded to train_probe().

Returns:

ProbeGridResult with one entry per cell.

Raises:

ValueError – If any requested category is absent from bench.

Return type:

ProbeGridResult