nnsight.intervention#
- exception nnsight.intervention.interleaver.Cancelation[source]#
Exception raised when a request is canceled.
- exception nnsight.intervention.interleaver.EarlyStopException[source]#
Exception raised to stop the execution of the model.
- class nnsight.intervention.interleaver.Events(value)[source]#
Enum for different types of events in the interleaving process.
- class nnsight.intervention.interleaver.Interleaver(invokers: List[Mediator] = None, tracer: InterleavingTracer = None, batcher: Batcher = None, user_cache: Cache | None = None)[source]#
Manages the interleaving of model execution and interventions.
This class coordinates the flow between the model’s forward pass and user-defined intervention functions, allowing for inspection and modification of intermediate values.
- handle(provider: Any | None = None, value: Any | None = None, iterate: bool = False)[source]#
Handle a provider’s value, allowing mediators to consume and modify it.
- Parameters:
provider – The identifier of the provider
value – The value being provided
- Returns:
The original or modified value
- wrap_operation(fn: Callable, name: str, bound_obj: Any | None = None)[source]#
Wrap an operation to intercept inputs and outputs for intervention.
- Parameters:
fn – The function to wrap
name – The name of the operation
bound_obj – The object fn is bound to if it is a method
- Returns:
A wrapped version of the function
- class nnsight.intervention.interleaver.Mediator(intervention: Callable, info: Tracer.Info, name: str | None = None, batch_group: int | None = 0, stop: int | None = None)[source]#
Mediates between the model execution and intervention functions.
This class handles the communication between the model’s forward pass and user-defined intervention functions, allowing for inspection and modification of intermediate values.
- exception(exception: Exception)[source]#
Signal that an exception occurred during intervention.
- Parameters:
exception – The exception that occurred
- property frame: FrameType#
Get the frame of the intervention function.
- Returns:
The frame of the intervention function
- handle(provider: Any | None = None)[source]#
Handle events in the event queue and process provider values.
- Parameters:
provider – The identifier of the provider
- Returns:
The original or modified value
- handle_barrier_event(provider: Any, participants: Set[str])[source]#
Handle a barrier event by setting a barrier.
- handle_exception_event(exception: Exception)[source]#
Handle an exception event by raising the exception.
- Parameters:
exception – The exception to raise
- Returns:
Boolean indicating whether to continue processing events
- handle_swap_event(provider: Any, requester: Any, swap_value: Any)[source]#
Handle a swap event by swapping the value if the provider matches the requester.
- Parameters:
requester – The identifier of the requester
provider – The identifier of the provider
swap_value – The value to swap in
- Returns:
Boolean indicating whether to continue processing events
- handle_value_event(requester: Any, provider: Any, value: Any)[source]#
Handle a value event by providing the requested value or recording a missed provider.
- Parameters:
requester – The identifier of the requester
provider – The identifier of the provider
value – The value being provided
- Returns:
Boolean indicating whether to continue processing events
- iter(mediator: Mediator, iteration: int | slice)[source]#
Iterate a mediator a specified number of times.
- Parameters:
mediator – The mediator to iterate
iteration – The number of iterations
- 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
- respond(value: Any | None = None)[source]#
Set the value for a pending value request.
- Parameters:
value – The value to provide
- send(event: Events, requester: Any)[source]#
Send an event to the event queue and wait for a response.
- Parameters:
event – The event to send
requester – The identifier of the requester
- Returns:
The response from the provider
- set_user_cache(cache: Cache)[source]#
Set the user cache for this mediator.
- Parameters:
cache – The cache to set
- start(interleaver: Interleaver)[source]#
Start the mediator’s intervention thread.
- Parameters:
interleaver – The interleaver managing this mediator
- exception nnsight.intervention.interleaver.SkipException(value: Any)[source]#
Exception raised to skip the execution of the model.
- class nnsight.intervention.serialization.CustomCloudPickler(file, protocol=None, buffer_callback=None)[source]#