Model Wrapping
interpreto.model_wrapping.ModelWithSplitPoints
¶
ModelWithSplitPoints(model_or_repo_id, split_points, *args, model_autoclass=None, tokenizer=None, config=None, batch_size=1, device_map=None, **kwargs)
Bases: LanguageModel
Code: model_wrapping/model_with_split_points.py`
Generalized NNsight.LanguageModel wrapper around encoder-only, decoder-only and encoder-decoder language models. Handles splitting model at specified locations and activation extraction.
Inputs can be in the form of:
* One (`str`) or more (`list[str]`) prompts, including batched prompts (`list[list[str]]`).
* One (`list[int] or torch.Tensor`) or more (`list[list[int]] or torch.Tensor`) tokenized prompts.
* Direct model inputs: (`dic[str,Any]`)
Attributes:
Name | Type | Description |
---|---|---|
model_autoclass |
type
|
The AutoClass corresponding to the loaded model type. |
split_points |
list[str]
|
Getter/setters for model paths corresponding to split points inside the loaded model. Automatically handle validation, sorting and resolving int paths to strings. |
repo_id |
str
|
Either the model id in the HF Hub, or the path from which the model was loaded. |
generator |
Envoy | None
|
If the model is generative, a generator is provided to handle multi-step inference. None for encoder-only models. |
_model |
PreTrainedModel
|
Huggingface transformers model wrapped by NNSight. |
_model_paths |
list[str]
|
List of cached valid paths inside |
_split_points |
list[str]
|
List of split points, should be accessed with getter/setter. |
Examples:
Load the model from its repository id, split it at the first layer, and get the raw activations for the first layer.
>>> from datasets import load_dataset
>>> from interpreto import ModelWithSplitPoints
>>> # load and split the model
>>> model_with_split_points = ModelWithSplitPoints(
... "bert-base-uncased",
... split_points="bert.encoder.layer.1.output",
... model_autoclass=AutoModelForMaskedLM,
... batch_size=64,
... device_map="auto",
... )
>>> # get activations
>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes")["train"]["text"]
>>> activations = model_with_split_points.get_activations(
... dataset,
... activation_granularity=ModelWithSplitPoints.activation_granularities.TOKEN,
... )
Load the model then pass it the ModelWithSplitPoint
, split it at the first layer,
get the word activations for the first layer, skip special tokens, and aggregate tokens activations by mean into words.
>>> from transformers import AutoModelCausalLM, AutoTokenizer
>>> from datasets import load_dataset
>>> from interpreto import ModelWithSplitPoints
>>> # load the model
>>> model = AutoModelCausalLM.from_pretrained("gpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
>>> # wrap and split the model
>>> model_with_split_points = ModelWithSplitPoints(
... model,
... tokenizer=tokenizer,
... split_points="transformer.h.1.mlp"],,
... model_autoclass=AutoModelForMaskedLM,
... batch_size=16,
... device_map="auto",
... )
>>> # get activations
>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes")["train"]["text"]
>>> activations = model_with_split_points.get_activations(
... dataset,
... activation_granularity=ModelWithSplitPoints.activation_granularities.WORD,
... aggregation_strategy=ModelWithSplitPoints.aggregation_strategies.MEAN,
... )
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str | PreTrainedModel
|
One of:
|
required |
|
str | Sequence[str] | int | Sequence[int]
|
One or more to split locations inside the model.
Either the path is provided explicitly ( |
required |
|
Type
|
Huggingface AutoClass
corresponding to the desired type of model (e.g.
|
None
|
|
PretrainedConfig
|
Custom configuration for the loaded model. If not specified, it will be instantiated with the default configuration for the model. |
None
|
|
PreTrainedTokenizer
|
Custom tokenizer for the loaded model. If not specified, it will be instantiated with the default tokenizer for the model. |
None
|
|
int
|
Batch size for the model. |
1
|
|
str | None
|
Device map for the model. Directly passed to the model. |
None
|
get_activations
¶
get_activations(inputs, activation_granularity=ALL_TOKENS, aggregation_strategy=MEAN, pad_side='left', **kwargs)
Get intermediate activations for all model split points
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
list[str] | Tensor | BatchEncoding
|
Inputs to the model forward pass before or after tokenization.
In the case of a |
required |
|
ActivationGranularity
|
Selection strategy for activations. Options are:
-
|
ALL_TOKENS
|
|
AggregationProtocol
|
Strategy to aggregate token activations into larger inputs granularities.
Applied for
|
MEAN
|
|
str
|
'left' or 'right' — side on which to apply padding along dim=1 only for ALL strategy. |
'left'
|
|
Additional keyword arguments passed to the model forward pass. |
{}
|
Returns:
Type | Description |
---|---|
dict[str, LatentActivations]
|
(dict[str, LatentActivations]) Dictionary having one key, value pair for each split point defined for the model. Keys correspond to split
names in |
get_latent_shape
¶
Get the shape of the latent activations at the specified split point.