record_activations

steering_vectors.record_activations(model, layer_type='decoder_block', layer_config=None, clone_activations=True, layer_nums=None)[source]

Record the model activations at each layer of type layer_type. This function will record every forward pass through the model at all layers of the given layer_type.

Return type:

Generator[dict[int, list[Tensor]], None, None]

Parameters:
  • model – The model to record activations from

  • layer_type – The type of layer to record activations from

  • layer_config – A dictionary mapping layer types to layer matching functions. If not provided, this will be inferred automatically.

  • clone_activations – If True, clone the activations before recording them. Default True.

  • layer_nums – A list of layer numbers to record activations from. If None, record activations from all matching layers

Example

>>> model = AutoModelForCausalLM.from_pretrained("gpt2-xl")
>>> with record_activations(model, layer_type="decoder_block") as recorded_activations:
>>>     model.forward(...)
>>> # recorded_activations is a dictionary mapping layer numbers to lists of activations