capture
capture
¶
Activation and gradient capture helpers for attribution methods.
GradientCapture
¶
Capture activation and gradient for one camera target.
Initialize the capture state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
CameraExplanationTarget
|
Concrete camera target whose activation should be captured. |
required |
Source code in src/versatil/explainability/attribution/capture.py
forward_hook
¶
Capture the selected camera activation and attach a gradient hook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
Module that produced the activation. |
required |
module_input
|
tuple[Tensor, ...]
|
Positional module inputs from PyTorch's hook API. |
required |
module_output
|
TensorModuleOutput
|
Module output to capture. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the selected activation does not require gradients, or if the layer already produced a captured activation in this forward pass, which would pair the last activation with the gradient of an earlier one. |
Source code in src/versatil/explainability/attribution/capture.py
gradient_hook
¶
Record the gradient of the selected activation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gradient
|
Tensor
|
Gradient tensor produced for the captured activation. |
required |
Source code in src/versatil/explainability/attribution/capture.py
require_tensors
¶
Return captured activation and gradient tensors.
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple of |
Tensor
|
backward pass. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If either hook did not run. |
Source code in src/versatil/explainability/attribution/capture.py
ActivationCapture
¶
Capture activation for one perturbation target.
Initialize the capture state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
CameraExplanationTarget
|
Concrete camera target whose activation should be captured. |
required |
Source code in src/versatil/explainability/attribution/capture.py
forward_hook
¶
Capture the selected camera activation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
Module that produced the activation. |
required |
module_input
|
tuple[Tensor, ...]
|
Positional module inputs from PyTorch's hook API. |
required |
module_output
|
TensorModuleOutput
|
Module output to capture. |
required |
Source code in src/versatil/explainability/attribution/capture.py
require_activation
¶
Return the captured activation tensor.
Returns:
| Type | Description |
|---|---|
Tensor
|
Activation from the most recent forward pass. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the forward hook did not run. |
Source code in src/versatil/explainability/attribution/capture.py
select_tensor_output
¶
Select the tensor output used for attribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
TensorModuleOutput
|
Tensor output from a module hook, or a tuple of tensor/None entries from modules such as DFormer stages. |
required |
output_index
|
int | None
|
Optional index used when the target layer returns a tuple.
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The tensor to use as the attribution target. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
RuntimeError
|
If no tensor output is available. |
Source code in src/versatil/explainability/attribution/capture.py
select_camera_tensor
¶
Select the camera-specific slice from a captured target tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Tensor selected from the target-layer output. |
required |
target
|
CameraExplanationTarget
|
Concrete camera target describing capture routing. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor belonging to the requested camera. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a stacked camera batch cannot be split evenly. |
Source code in src/versatil/explainability/attribution/capture.py
replace_camera_tensor
¶
Replace the camera-specific slice inside a target-layer tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Original target-layer tensor. |
required |
replacement
|
Tensor
|
Replacement tensor for the selected camera. |
required |
target
|
CameraExplanationTarget
|
Concrete camera target describing capture routing. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor with the selected camera slice replaced. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a stacked camera batch cannot be split evenly. |
Source code in src/versatil/explainability/attribution/capture.py
should_capture_invocation
¶
Return whether a hook call belongs to the requested camera.