lczerolens.lens#
Generic lens class.
Classes#
Generic lens class for analysing model activations. |
Module Contents#
- class lczerolens.lens.Lens(pattern=None)[source]#
Bases:
abc.ABCGeneric lens class for analysing model activations.
- Parameters:
pattern (Optional[str])
- classmethod register(name)[source]#
Registers the lens.
- Parameters:
name (str) – The name of the lens.
- Returns:
The decorator to register the lens.
- Return type:
Callable
- Raises:
ValueError – If the lens name is already registered.
- classmethod from_name(name, *args, **kwargs)[source]#
Returns the lens from its name.
- Parameters:
name (str) – The name of the lens.
- Returns:
The lens instance.
- Return type:
- Raises:
KeyError – If the lens name is not found.
- _get_modules(model)[source]#
Get the modules to intervene on.
- Parameters:
model (lczerolens.model.LczeroModel)
- Return type:
Generator[tuple[str, Any], None, None]
- is_compatible(model)[source]#
Returns whether the lens is compatible with the model.
- Parameters:
model (LczeroModel) – The NNsight model.
- Returns:
Whether the lens is compatible with the model.
- Return type:
bool
- _ensure_compatible(model)[source]#
Ensure the lens is compatible with the model.
- Parameters:
model (LczeroModel) – The NNsight model.
- Raises:
ValueError – If the lens is not compatible with the model.
- prepare(model, **kwargs)[source]#
Prepare the model for the lens.
- Parameters:
model (LczeroModel) – The NNsight model.
- Returns:
The prepared model.
- Return type:
- abstract _intervene(model, **kwargs)[source]#
Intervene on the model.
- Parameters:
model (LczeroModel) – The NNsight model.
- Returns:
The intervention results.
- Return type:
dict
- _trace(model, *inputs, model_kwargs, intervention_kwargs)[source]#
Trace the model and intervene on it.
- Parameters:
model (LczeroModel) – The NNsight model.
inputs (Union[LczeroBoard, torch.Tensor]) – The inputs.
model_kwargs (dict) – The model kwargs.
intervention_kwargs (dict) – The intervention kwargs.
- Returns:
The intervention results.
- Return type:
dict
- analyse(model, *inputs, **kwargs)[source]#
Analyse the input.
- Parameters:
model (LczeroModel) – The NNsight model.
inputs (Union[LczeroBoard, torch.Tensor]) – The inputs.
- Returns:
The analysis results.
- Return type:
dict
- Raises:
ValueError – If the lens is not compatible with the model.
- analyse_batched(model, iter_inputs, **kwargs)[source]#
Analyse a batches of inputs.
- Parameters:
model (LczeroModel) – The NNsight model.
iter_inputs (Iterable[Tuple[Union[LczeroBoard, torch.Tensor], dict]]) – The iterator over the inputs.
- Returns:
The iterator over the statistics.
- Return type:
Generator[dict, None, None]
- Raises:
ValueError – If the lens is not compatible with the model.