Coverage for transformer_lens/model_protocol.py: 100%
8 statements
« prev ^ index » next coverage.py v7.10.1, created at 2026-09-21 19:27 +0000
« prev ^ index » next coverage.py v7.10.1, created at 2026-09-21 19:27 +0000
1"""Structural model types for the model-agnostic interpretability utilities.
3The interpretability utilities (``patching``, ``head_detector``, ``ActivationCache``)
4only need ``cfg`` plus the ``run_with_*`` / tokenization surface and a few
5weight-processing helpers. Typing their model parameter as this Protocol accepts
6``TransformerBridge`` and any other structural match (e.g. ``RemoteBridge``).
8Members are typed loosely on purpose: implementations differ in signature detail
9but agree in use.
10"""
11from __future__ import annotations
13from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable
15if TYPE_CHECKING:
16 from transformer_lens.config import TransformerLensConfig
19# runtime_checkable so the interp utilities' beartype-decorated signatures can
20# isinstance-check the parameter at runtime (presence-only).
21@runtime_checkable
22class TransformerLensModel(Protocol):
23 """Minimal structural interface the interpretability utilities rely on."""
25 # Read-only property (not a bare attribute) so it is covariant: a concrete model
26 # whose cfg is a TransformerLensConfig *subclass* (e.g. TransformerBridgeConfig)
27 # still conforms. A mutable attribute would be invariant.
28 @property
29 def cfg(self) -> "TransformerLensConfig":
30 ...
32 def run_with_cache(self, *args: Any, **kwargs: Any) -> Any:
33 ...
35 def run_with_hooks(self, *args: Any, **kwargs: Any) -> Any:
36 ...
38 def to_tokens(self, *args: Any, **kwargs: Any) -> Any:
39 ...
42@runtime_checkable
43class TrainableTransformerLensModel(Protocol):
44 """Exactly the surface the ``tools.training`` loop touches: callable with
45 ``return_type="loss"`` plus the standard torch parameter/mode/device
46 methods. Deliberately standalone (not extending TransformerLensModel):
47 beartype validates protocols via ``getattr_static``, and demanding the
48 full TL surface would spuriously reject plain ``nn.Module`` models that
49 train fine through this loop."""
51 def parameters(self, *args: Any, **kwargs: Any) -> Any:
52 ...
54 def train(self, *args: Any, **kwargs: Any) -> Any:
55 ...
57 def to(self, *args: Any, **kwargs: Any) -> Any:
58 ...
60 def state_dict(self, *args: Any, **kwargs: Any) -> Any:
61 ...
63 def __call__(self, *args: Any, **kwargs: Any) -> Any:
64 ...
67@runtime_checkable
68class TransformerLensModelWithWeights(TransformerLensModel, Protocol):
69 """Adds the weight-processing surface that ``ActivationCache``'s advanced helpers
70 (LayerNorm folding, residual-direction projection) reach for. The bridge builds
71 them from its adapter."""
73 @property
74 def blocks(self) -> Any:
75 ...
77 @property
78 def ln_final(self) -> Any:
79 ...
81 def accumulated_bias(self, *args: Any, **kwargs: Any) -> Any:
82 ...
84 def to_single_token(self, *args: Any, **kwargs: Any) -> Any:
85 ...
87 def tokens_to_residual_directions(self, *args: Any, **kwargs: Any) -> Any:
88 ...