envoy¶
envoy
¶
Envoy
¶
Envoy(module: Module, interleaver: Optional[Interleaver] = None, path: Optional[str] = 'model', rename: Optional[Dict[str, Union[str, List[str]]]] = None)
Bases: Batchable
A proxy class that wraps a PyTorch module to enable intervention during execution.
This class provides access to module inputs and outputs during forward passes, and allows for modification of these values through an interleaving mechanism. It serves as the primary interface for inspecting and modifying the behavior of neural network modules during execution.
| ATTRIBUTE | DESCRIPTION |
|---|---|
path |
The module's location in the model hierarchy. Example: "model.encoder.layer1" indicates this module is the first layer of the encoder in the model.
TYPE:
|
_module |
The underlying PyTorch module
TYPE:
|
_source |
Source code representation of the module
TYPE:
|
_interleaver |
Interleaver for managing execution flow
TYPE:
|
_default_mediators |
List of default mediators created with .edit
TYPE:
|
_children |
List of child Envoys
TYPE:
|
_alias |
Aliaser object for managing aliases
TYPE:
|
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The PyTorch module to wrap
TYPE:
|
interleaver
|
Optional interleaver for managing execution flow
TYPE:
|
path
|
Optional path string representing the module's location in the model hierarchy
TYPE:
|
rename
|
Optional dictionary mapping module names to alias names. Example: {"layer1": "first_layer", "layer2": "second_layer"} Example: {".model.layers": ".layers"} <-- Mounts .layers to the root model. Example: {".transformer": ["model", "mdl"]} <-- Allows access of .transformer as .model or .mdl
TYPE:
|
interleaving
property
¶
Check if the Envoy is currently nterleaving.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the Envoy is interleaving, False otherwise |
output
property
writable
¶
output: Object
Get the output of the module's forward pass.
This property allows access to the return values produced by the module during the forward pass.
Example
model = LanguageModel("gpt2", device_map='auto', dispatch=True) with model.trace("Hello World"): attn = model.transformer.h[0].attn.output[0].save() print(attn)
| RETURNS | DESCRIPTION |
|---|---|
Object
|
The module's output values |
inputs
property
writable
¶
Get the inputs to the module's forward pass.
This property provides access to all input values passed to the module during the forward pass.
Example
model = LanguageModel("gpt2", device_map='auto', dispatch=True) with model.trace("Hello World"): args, kwargs = model.transformer.h[0].attn.inputs
| RETURNS | DESCRIPTION |
|---|---|
Tuple[Tuple[Object], Dict[str, Object]]
|
The module's input values as a tuple of positional and keyword arguments. i.e (args, kwargs) |
input
property
writable
¶
input: Object
Get the first input to the module's forward pass.
This is a convenience property that returns just the first input value from all inputs passed to the module. So first positional argument, or first keyword argumetn if there are no positional arguments.
Example
model = LanguageModel("gpt2", device_map='auto', dispatch=True) with model.trace("Hello World"): hidden_states = model.transformer.h[0].attn.input.save() print(hidden_states)
| RETURNS | DESCRIPTION |
|---|---|
Object
|
The first input value |
source
property
¶
source: EnvoySource
Get the source code representation of the module.
This property provides access to the module's source code with operations highlighted, allowing for inspection and intervention at specific points.
Example:
>>> model = LanguageModel("gpt2", device_map='auto', dispatch=True)
>>> # We can print to see the formward method of the module and names associated with the operations within.
>>> print(model.transformer.h[0].attn.source)
60
61 if using_eager and self.reorder_and_upcast_attn:
self__upcast_and_reordered_attn_0 -> 62 attn_output, attn_weights = self._upcast_and_reordered_attn(
63 query_states, key_states, value_states, attention_mask, head_mask
64 )
65 else:
attention_interface_0 -> 66 attn_output, attn_weights = attention_interface(
67 self,
68 query_states,
69 key_states,
70 value_states,
71 attention_mask,
72 head_mask=head_mask,
73 dropout=self.attn_dropout.p if self.training else 0.0,
74 is_causal=is_causal,
75 **kwargs,
76 )
77
attn_output_reshape_0 -> 78 attn_output = attn_output.reshape(*attn_output.shape[:-2], -1).contiguous()
contiguous_0 -> + ...
self_c_proj_0 -> 79 attn_output = self.c_proj(attn_output)
self_resid_dropout_0 -> 80 attn_output = self.resid_dropout(attn_output)
81
82 return attn_output, attn_weights
83
>>> # We can print out one of these to see the only the operation and a few operations before and after.
>>> print(model.transformer.h[0].attn.source.attention_interface_0)
.transformer.h.0.attn.attention_interface_0:
....
if using_eager and self.reorder_and_upcast_attn:
attn_output, attn_weights = self._upcast_and_reordered_attn(
query_states, key_states, value_states, attention_mask, head_mask
)
else:
--> attn_output, attn_weights = attention_interface( <--
self,
query_states,
key_states,
value_states,
attention_mask,
head_mask=head_mask,
....
>>> with model.trace("Hello World"):
>>> # Now we can access it like we would any other Envoy with .input or .output to grab the intermediate value.
>>> attn = model.transformer.h[0].attn.source.attention_interface_0.output.save()
>>> print(attn)
| RETURNS | DESCRIPTION |
|---|---|
EnvoySource
|
An EnvoySource object containing the module's source code and operations |
device
property
¶
Get the device the module is on. Finds the first parameter and return its device.
devices
property
¶
Get the devices the module is on. Finds all parameters and return their devices.
__getitem__
¶
__getitem__(key: str) -> Envoy
Access a child Envoy by index for Module Lists.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The index of the child Envoy to retrieve
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Envoy
|
The child Envoy at the specified index |
trace
¶
trace(*args, fn: Optional[Callable] = None, trace: bool = None, tracer_cls: Type[InterleavingTracer] = InterleavingTracer, **kwargs)
Create a tracer for this module.
This method returns a tracer that can be used to capture and modify the execution of the module.
Example
model = LanguageModel("gpt2", device_map='auto', dispatch=True) with model.trace("Hello World"): model.transformer.h[0].attn.output[0][:] = 0
output = model.output.save()print(output)
| PARAMETER | DESCRIPTION |
|---|---|
*args
|
Arguments to pass to the tracer
DEFAULT:
|
**kwargs
|
Keyword arguments to pass to the tracer
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
An InterleavingTracer for this module |
scan
¶
Just like .trace() but runs the model in fake tensor mode to validate operations and inspect tensor shapes.
This method returns a tracer that runs the model in fake tensor mode to validate operations and inspect tensor shapes without performing actual computation. This is useful for: - Validating that operations will work with given input shapes - Inspecting the shapes and types of tensors that would flow through the model - Debugging shape mismatches or other tensor-related issues.
Note this will not dispatch the model if not dispatched.
Example
model = LanguageModel("gpt2", device_map='auto', dispatch=True)
Value error as the fake inputs and outputs have not been scanned in.¶
print(model.transformer.h[0].mlp.output.shape)
Scan the model to validate operations and inspect shapes¶
with model.scan("Hello World"): # Access fake inputs/outputs to inspect shapes attn_input = model.transformer.h[0].attn.input.save() attn_output = model.transformer.h[0].attn.output[0].save() print(f"Attention input shape: {attn_input.shape}") print(f"Attention output shape: {attn_output.shape}") print(model.transformer.h[0].mlp.output.shape)
| PARAMETER | DESCRIPTION |
|---|---|
*args
|
Arguments to pass to the tracer
DEFAULT:
|
**kwargs
|
Keyword arguments to pass to the tracer
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
A ScanningTracer for this module |
edit
¶
Create an editing tracer for this module. Allows for setting default interventions. This means this tracer won't execute the module, but will instead set default interventions that are applied on all future executions.
Edits can be cleared with Envoy.clear_edits().
Example
model = LanguageModel("gpt2", device_map='auto', dispatch=True)
Now the first layer attention output will always be 0.¶
with model.edit() as edited_model: edited_model.transformer.h[0].attn.output[:] = 0
with model.trace("Hello World"): output = model.output.save()
The orignal model will have the default output.¶
print(output)
with edited_model.trace("Hello World"): edited_output = edited_model.output.save()
The edited model will have the output after our intervention.¶
print(edited_output)
| PARAMETER | DESCRIPTION |
|---|---|
inplace
|
Whether to edit in place. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EditingTracer
|
An EditingTracer for this module |
export_edits
¶
TODO
| PARAMETER | DESCRIPTION |
|---|---|
name
|
description
TYPE:
|
export_dir
|
description. Defaults to None.
TYPE:
|
variant
|
description. Defaults to 'default'.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
description |
import_edits
¶
TODO
| PARAMETER | DESCRIPTION |
|---|---|
name
|
description
TYPE:
|
export_dir
|
description. Defaults to None.
TYPE:
|
variant
|
description. Defaults to 'default'.
TYPE:
|
skip
¶
Skips the execution of this module duting execution / interleaving. Behavior is the module will not be executed and will return a replacement value instead.
Example
model = LanguageModel("gpt2", device_map='auto', dispatch=True) with model.trace("Hello World"): # Skip the first layer and replace it with the input to the layer. model.transformer.h[0].skip((model.transformer.h[0].input, None)) output = model.output.save() print(output)
| PARAMETER | DESCRIPTION |
|---|---|
replacement
|
The replacement value to replace the module's output with.
TYPE:
|
to
¶
Move the module to a specific device.
This method moves the underlying PyTorch module to the specified device.
| PARAMETER | DESCRIPTION |
|---|---|
device
|
The device to move the module to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Self, for method chaining |
modules
¶
Get all modules in the Envoy tree.
This method returns all Envoys in the tree, optionally filtered by an inclusion function.
| PARAMETER | DESCRIPTION |
|---|---|
include_fn
|
Optional function to filter modules
TYPE:
|
names
|
Whether to include module names in the result
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[Envoy]
|
A list of Envoys or (name, Envoy) tuples |
named_modules
¶
named_modules(*args, **kwargs) -> List[Tuple[str, Envoy]]
Returns all Envoys in the Envoy tree along with their name/module_path.
This is a convenience method that calls modules() with names=True.
| PARAMETER | DESCRIPTION |
|---|---|
include_fn
|
Optional function to be ran against all Envoys to check if they should be included in the final collection of Envoys. Defaults to None.
TYPE:
|
*args, **kwargs
|
Additional arguments to pass to modules()
|
| RETURNS | DESCRIPTION |
|---|---|
List[Tuple[str, Envoy]]
|
List[Tuple[str, Envoy]]: Included Envoys and their names/module_paths. |
get
¶
get(path: str) -> Object
Gets the Envoy/Proxy via its path.
e.x: model = nnsight.LanguageModel("openai-community/gpt2")
module = model.get('transformer.h.0.mlp')
with model.trace("Hello"):
value = model.get('transformer.h.0.mlp.output').save()
| PARAMETER | DESCRIPTION |
|---|---|
path
|
'.' separated path.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Object
|
Union[Envoy, InterventionProxyType]: Fetched Envoy/Proxy |
__str__
¶
String representation of the Envoy.
| RETURNS | DESCRIPTION |
|---|---|
|
A string representation of the Envoy showing its path |
__repr__
¶
Representation of the Envoy.
| RETURNS | DESCRIPTION |
|---|---|
|
The string representation of the Envoy |
__getattr__
¶
__getattr__(name: str) -> Union[Module, Envoy, Any]
Get an attribute from the underlying module.
If the attribute is callable, it will be wrapped in a tracer to enable intervention during execution.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of the attribute to get
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Union[Module, Envoy, Any]
|
The attribute value, possibly wrapped in a tracer |
| RAISES | DESCRIPTION |
|---|---|
AttributeError
|
If the attribute doesn't exist |
__setattr__
¶
Set an attribute on the Envoy.
If the value is a PyTorch module, it will be wrapped in an Envoy to enable intervention during execution.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The attribute name
TYPE:
|
value
|
The attribute value
TYPE:
|
OperationEnvoy
¶
OperationEnvoy(name: str, source: str, line_number: int, interleaver: Optional[Interleaver] = None)
Represents a specific operation within a module's forward pass.
This class provides access to the inputs and outputs of individual operations within a module's execution, allowing for fine-grained inspection and intervention at the operation level.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The fully qualified name of the operation
TYPE:
|
source
|
The source code of the module containing the operation
TYPE:
|
line_number
|
The line number of the operation in the source
TYPE:
|
interleaver
|
Optional interleaver for managing execution flow
TYPE:
|
output
property
writable
¶
Get the output of this operation.
This property provides access to the return value(s) produced by the operation during execution.
| RETURNS | DESCRIPTION |
|---|---|
Union[Any, Tensor]
|
The operation's output value(s) |
inputs
property
writable
¶
Get the inputs to this operation.
This property provides access to all input value(s) passed to the operation during execution, structured as a tuple of positional and keyword arguments.
| RETURNS | DESCRIPTION |
|---|---|
Tuple[Tuple[Any, Tensor], Dict[str, Union[Tensor, Any]]]
|
The operation's input value(s) |
input
property
writable
¶
Get the first input to the operation.
This is a convenience property that returns just the first input value from all inputs passed to the operation.
| RETURNS | DESCRIPTION |
|---|---|
Union[Any, Tensor]
|
The first input value |
source
property
¶
source: EnvoySource
Get the source code of the operation.
This property provides access to the operation's source code with nested operations highlighted, allowing for inspection and intervention at specific points.
| RETURNS | DESCRIPTION |
|---|---|
EnvoySource
|
An EnvoySource object containing the operation's source code and nested operations |
__str__
¶
String representation showing the operation in context.
This method returns a formatted string showing the operation's source code with surrounding context lines and highlighting the operation line.
| RETURNS | DESCRIPTION |
|---|---|
|
A formatted string showing the operation's source code with context |
EnvoySource
¶
EnvoySource(name: str, source: str, line_numbers: dict, interleaver: Optional[Interleaver] = None)
Represents the source code of a module with operations highlighted.
This class provides access to the individual operations within a module's source code, allowing for inspection and intervention at specific points in the code. It serves as a bridge between the source code representation and the runtime execution of operations.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The fully qualified name of the module or operation
TYPE:
|
source
|
The source code string
TYPE:
|
line_numbers
|
A dictionary mapping operation names to line numbers
TYPE:
|
interleaver
|
Optional interleaver for managing execution flow
TYPE:
|
__str__
¶
String representation showing the source code with operations highlighted.
This method returns a formatted string showing the source code with operation names and line numbers, making it easy to identify intervention points.
| RETURNS | DESCRIPTION |
|---|---|
|
A formatted string showing the source code with operation names and line numbers |
Aliaser
¶
| PARAMETER | DESCRIPTION |
|---|---|
rename
|
Dictionary mapping module names to alias names. Example: {"layer1": "first_layer", "layer2": "second_layer"} Example: {".model.layers": ".layers"} <-- Mounts .layers to the root model. Example: {".transformer": ["model", "mdl"]} <-- Allows access of .transformer as .model or .mdl
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
rename |
Dictionary mapping module names to alias names.
TYPE:
|
alias_to_name |
Dictionary mapping alias names to module names.
TYPE:
|
name_to_aliases |
Dictionary mapping module names to list of alias names.
TYPE:
|
extras |
Dictionary mapping attribute paths (.transformer.h) to list of alias names. Used to show dot seperated attributes in the string representation of the Envoy.
TYPE:
|