envoy¶
envoy
¶
Envoy
¶
Envoy(module: Module, interleaver: Optional[Interleaver] = None, path: Optional[str] = 'model', rename: Optional[Dict[str, Union[str, List[str]]]] = None, envoys: Optional[Union[Type['Envoy'], Dict[Type[Module], Type['Envoy']]]] = 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:
|
envoys
|
Controls which Envoy class wraps descendant modules. Propagates down the envoy tree.
- None (default): all descendants are wrapped with the base Envoy class.
- A class: all descendants are wrapped with that class.
- A dict whose values are
TYPE:
|
interleaver
instance-attribute
¶
interleaver = interleaver if interleaver is not None else Interleaver()
interleaving
property
¶
Check if the Envoy is currently nterleaving.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the Envoy is interleaving, False otherwise |
source
property
¶
source: SourceEnvoy
Get the source code representation of the module.
Lazily resolves to a :class:SourceEnvoy over the module's global
:class:SourceAccessor. The accessor — and its per-call-site
:class:OperationAccessor instances — are created once per module
and shared across all Envoys / Interleavers / Mediators that touch
it. This Envoy keeps a per-instance :class:SourceEnvoy so each
Envoy has its own user-facing wrapper.
Examples:
>>> model = LanguageModel("gpt2", device_map='auto', dispatch=True)
>>> print(model.transformer.h[0].attn.source)
>>> with model.trace("Hello World"):
... attn = model.transformer.h[0].attn.source.attention_interface_0.output.save()
| RETURNS | DESCRIPTION |
|---|---|
A
|
class:
TYPE:
|
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 |
inputs
¶
trace
¶
trace(*args, trace: bool = True, fn: Optional[Callable] = 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.
Examples:
>>> 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()
| PARAMETER | DESCRIPTION |
|---|---|
*args
|
Arguments to pass to the tracer
DEFAULT:
|
trace
|
If False, bypass tracing entirely — run the underlying module on the prepared input and return its output. Useful for one-shot forward passes that don't need intervention. Defaults to True.
TYPE:
|
**kwargs
|
Keyword arguments to pass to the tracer
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
An InterleavingTracer for this module, or — when |
|
|
— the module's output value directly. |
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.
Examples:
>>> 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().
Examples:
>>> 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.
Examples:
>>> 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.
Otherwise the write is mirrored to both the Envoy and the wrapped module
when applicable, so that reads via __dict__ short-circuit and reads via
__getattr__ (which falls through to _module) stay consistent.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The attribute name
TYPE:
|
value
|
The attribute value
TYPE:
|
Aliaser
¶
| PARAMETER | DESCRIPTION |
|---|---|
rename
|
Dictionary mapping module names to alias names. Examples: {"layer1": "first_layer", "layer2": "second_layer"} Examples: {".model.layers": ".layers"} <-- Mounts .layers to the root model. Examples: {".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:
|