nnsight.intervention.tracing#

exception nnsight.intervention.tracing.base.ExitTracingException[source]#

Exception raised to exit the tracing process.

This exception is used as a control flow mechanism to cleanly exit a with block without executing the code inside it.

class nnsight.intervention.tracing.base.Tracer(*args, backend: Backend = None, _info: Info = None, **kwargs)[source]#

Captures and executes code within a tracing context.

This class allows for capturing code blocks within a ‘with’ statement, compiling them into callable functions, and executing them with access to the model and local variables. It provides a mechanism for intercepting and manipulating the execution flow of Python code.

The tracing process works by: 1. Capturing the code inside a ‘with’ block 2. Compiling it into a callable function 3. Executing it with the appropriate context

class Info(source: List[str], frame: FrameType, start_line: int, node: With, filename: str = None)[source]#

Container for information about the traced code.

This class stores metadata about the code being traced, including the source code itself and frame information from the call stack.

source#

List of source code lines from the traced block

frame#

Frame information from the call stack where tracing occurred

indent#

Number of spaces/tabs used for indentation in the original code

capture()[source]#

Capture the code block within the ‘with’ statement.

This method walks up the call stack to find the frame outside of nnsight, extracts the source code of the ‘with’ block, and prepares it for later execution. It identifies the exact code block to be traced by analyzing the source code structure.

compile() Callable[source]#

Compile the captured source code as a callable function.

Wraps the captured code in a function definition that accepts the necessary context parameters for execution.

Returns:

A callable function that executes the captured code block

execute(fn: Callable)[source]#

Execute the compiled function.

Runs the compiled function with the necessary context to execute the traced code block.

Parameters:

fn – The compiled function to execute

parse(source_lines: List[str], start_line: int)[source]#

Parse the source code to extract the source code.

Uses the Abstract Syntax Tree (AST) to identify the exact boundaries of the code from the specified line number.

Parameters:
  • source_lines – List of source code lines

  • start_line – Line number where the tracer creation statement begins

Returns:

List of source code lines.

push(state: Dict = None)[source]#

Push local variables back to the original execution frame.

This allows changes made during tracing to affect the original scope.

Parameters:

state – Dictionary of variable names and values to push to the frame. If None, automatically collects variables from the current frame.

exception nnsight.intervention.tracing.base.WithBlockNotFoundError[source]#

Exception raised when a with block is not found in the source code.

This exception is used to indicate that a with block was not found at the specified line number.

class nnsight.intervention.tracing.tracer.Cache(modules: List[Any | str] | None = None, device: device | None = device(type='cpu'), dtype: dtype | None = None, detach: bool | None = True, include_output: bool = True, include_inputs: bool = False, rename: Dict[str, str] | None = None, alias: Dict[str, str] | None = None)[source]#

A cache for storing and transforming tensor values during tracing.

This class provides functionality to store tensor values with optional transformations such as detaching from computation graph, moving to a specific device, or converting to a specific dtype.

class CacheDict(data: CacheDict | Dict[str, Entry], path: str = '', alias: Dict[str, str] = {}, rename: Dict[str, str] = {}, alias_paths: Dict[str, str] = {})[source]#

A dictionary subclass that provides convenient access to cached module activations.

This class extends the standard dictionary to provide both dictionary-style access and attribute-style access to cached activations. It supports hierarchical access to nested modules using dot notation and indexing for module lists.

Examples

Access cached activations using dictionary keys: >>> cache[‘model.transformer.h.0.attn’]

Access using attribute notation: >>> cache.model.transformer.h[0].attn

Access module outputs and inputs: >>> cache.model.transformer.h[0].output >>> cache.model.transformer.h[0].inputs >>> cache.model.transformer.h[0].input # First input argument

The class maintains an internal path that tracks the current location in the module hierarchy, allowing for intuitive navigation through nested modules.

property input#

Returns the input property from the Cache.Entry at the current path.

property inputs#

Returns the inputs attribute from the Cache.Entry at the current path.

keys() a set-like object providing a view on D's keys[source]#
property output#

Returns the output attribute from the Cache.Entry at the current path.

class Entry(output: Any | None = None, inputs: Tuple[Tuple[Any, ...], Dict[str, Any]] | None = None)[source]#
property input#

Gets the first positional argument of the inputs value to the cached module. Returns None if no inputs were cached.

add(provider: str, value: Any)[source]#

Add a value to the cache with optional transformations.

Parameters:
  • provider – The key to store the value under

  • value – The tensor value to store

class nnsight.intervention.tracing.tracer.InterleavingTracer(fn: Callable, model: Any, *args, backend: Backend = None, **kwargs)[source]#

Tracer that manages the interleaving of model execution and interventions.

This class coordinates the execution of the model’s forward pass and user-defined intervention functions through the Interleaver.

barrier(n_participants: int)[source]#

nnsight barrier: A synchronization primitive for coordinating multiple concurrent invocations in nnsight.

This works similarly to a threading.Barrier, but is designed for use with nnsight’s model tracing and intervention system. A barrier allows you to pause execution in multiple parallel invocations until all participants have reached the barrier, at which point all are released to continue. This is useful when you want to synchronize the execution of different model runs, for example to ensure that all have reached a certain point (such as after embedding lookup) before proceeding to the next stage (such as generation or intervention).

Example usage:

with gpt2.generate(max_new_tokens=3) as tracer:

barrier = tracer.barrier(2)

with tracer.invoke(MSG_prompt):

embeddings = gpt2.transformer.wte.output barrier() output1 = gpt2.generator.output.save()

with tracer.invoke(”_ _ _ _ _ _ _ _ _”):

barrier() gpt2.transformer.wte.output = embeddings output2 = gpt2.generator.output.save()

In this example, both invocations will pause at the barrier until both have reached it, ensuring synchronization.

cache(modules: List[Any | str] | None = None, device: device | None = device(type='cpu'), dtype: dtype | None = None, detach: bool | None = True, include_output: bool = True, include_inputs: bool = False) Dict | Object[source]#

Get or create a cache for storing intermediate values during tracing.

Parameters:
  • modules – Optional list of modules to cache, defaults to all modules

  • device – Optional device to move tensors to, defaults to cpu

  • dtype – Optional dtype to convert tensors to, defaults to None

  • detach – Whether to detach tensors from computation graph, defaults to True

  • include_output – Whether to include output in the cached activations

  • include_inputs – Whether to include inputs in the cached activations

Returns:

A dictionary containing the cached values

capture()[source]#

Capture the code block within the ‘with’ statement.

compile() Callable[source]#

Compile the captured code block into a callable function.

Returns:

A callable function that executes the captured code block

execute(fn: Callable)[source]#

Execute the compiled function with interventions.

First executes the parent Tracer’s execute method to set up the context, then creates an Interleaver to manage the interventions during model execution.

Parameters:

fn – The compiled function to execute

get_frame()[source]#

Get the frame of the tracer.

invoke(*args, **kwargs)[source]#

Create an Invoker to capture and execute an intervention function.

Parameters:
  • *args – Additional arguments to pass to the intervention function

  • **kwargs – Additional keyword arguments to pass to the intervention function

Returns:

An Invoker instance

stop()[source]#

Raise an EarlyStopException to stop the execution of the model.

class nnsight.intervention.tracing.tracer.ScanningTracer(fn: Callable, model: Any, *args, backend: Backend = None, **kwargs)[source]#

A tracer that runs the model in fake tensor mode to validate operations and inspect tensor shapes.

This tracer uses PyTorch’s FakeTensorMode to run the model without actual computation, allowing for shape validation and operation checking. It populates the _fake_inputs and _fake_output attributes on each Envoy to store the shapes and types of tensors that would flow through the model during a real forward pass.

execute(fn: Callable)[source]#

Execute the model in fake tensor mode.

This method: 1. Registers forward hooks on all modules to capture fake input/output 2. Runs the model in fake tensor mode to validate operations 3. Stores the fake inputs/outputs on each Envoy for later inspection

Parameters:

fn – The function to execute (typically the model’s forward pass)

class nnsight.intervention.tracing.invoker.Invoker(tracer: Any, *args, **kwargs)[source]#

Extends the Tracer class to invoke intervention functions.

This class captures code blocks and compiles them into intervention functions that can be executed by the Interleaver.

compile()[source]#

Compile the captured code block into an intervention function.

The function is wrapped with try-catch logic to handle exceptions and signal completion to the mediator.

Returns:

A callable intervention function

execute(fn: Callable)[source]#

Execute the compiled intervention function.

Creates a new Mediator for the intervention function and adds it to the parent tracer’s mediators list.

Parameters:

fn – The compiled intervention function

exception nnsight.intervention.tracing.util.ExceptionWrapper(info: Tracer.Info, original: Exception, *args, **kwargs)[source]#

Wrapper for exceptions that provides additional details for tracer created code.

This class helps provide better error messages by including source code context and proper line numbers from the original code being traced.

set_info(info: Tracer.Info)[source]#

Updates the tracer information and recalculates line offsets.

Parameters:

info – New tracer information to use

exception nnsight.intervention.tracing.util.TracingDeffermentException[source]#

Exception raised when a tracing defferment is encountered.

This exception is used to indicate that a tracing defferment is encountered.

nnsight.intervention.tracing.util.get_dependencies(fn: Callable)[source]#

Extracts global dependencies used by a function.

Parameters:

fn – The function to analyze for dependencies

Returns:

Dictionary mapping names to their corresponding global objects used by the function

nnsight.intervention.tracing.util.indent(source: List[str], indent: int = 1)[source]#

Indents each line in the source list by a specified number of indentation levels.

Parameters:
  • source – List of strings to indent

  • indent – Number of indentation levels to apply (default: 1)

Returns:

List of indented strings

nnsight.intervention.tracing.util.try_catch(source: List[str], exception_source: List[str] = ['raise\n'], else_source: List[str] = ['pass\n'], finally_source: List[str] = ['pass\n'])[source]#

Wraps source code in a try-except-else-finally block.

Args:

source: The code to be wrapped in the try block exception_source: Code for the except block (default: [“raise

“])

else_source: Code for the else block (default: [“pass

“])

finally_source: Code for the finally block (default: [“pass

“])

Returns:

List of strings representing the complete try-catch block, properly indented

nnsight.intervention.tracing.util.wrap_exception(exception: Exception, info: Tracer.Info)[source]#

Wraps an exception with additional context from the tracer.

This function either updates an existing ExceptionWrapper or creates a new dynamically-typed exception class that inherits from both the original exception type and ExceptionWrapper.

Parameters:
  • exception – The exception to wrap

  • info – Tracer information containing context about where the exception occurred

Returns:

A wrapped exception with enhanced traceback information

class nnsight.intervention.tracing.backwards.BackwardsMediator(intervention: Callable, info: Tracer.Info, name: str | None = None, batch_group: int | None = 0, stop: int | None = None)[source]#
request(requester: Any)[source]#

Request a value from a specific provider.

Parameters:

requester – The identifier of the provider to request a value from

Returns:

The requested value

class nnsight.intervention.tracing.backwards.BackwardsTracer(tensor: Tensor, fn: Callable, *args, **kwargs)[source]#
execute(fn: Callable)[source]#

Execute the compiled intervention function.

Creates a new Mediator for the intervention function and adds it to the parent tracer’s mediators list.

Parameters:

fn – The compiled intervention function

nnsight.intervention.tracing.backwards.wrap_grad(interleaver: Interleaver)[source]#

Create a hook for gradient intervention.

Returns:

A function that can be used to intercept gradients

class nnsight.intervention.tracing.editing.EditingTracer(*args, backend: ~nnsight.intervention.backends.base.Backend = <nnsight.intervention.backends.editing.EditingBackend object>, inplace: bool = False, **kwargs)[source]#
class nnsight.intervention.tracing.iterator.IteratorTracer(iteration: int | slice, interleaver: Interleaver)[source]#
compile()[source]#

Compile the captured source code as a callable function.

Wraps the captured code in a function definition that accepts the necessary context parameters for execution.

Returns:

A callable function that executes the captured code block

execute(fn: Callable)[source]#

Execute the compiled function.

Runs the compiled function with the necessary context to execute the traced code block.

Parameters:

fn – The compiled function to execute