pydvl.influence.types
¶
This module offers a set of generic types, which can be used to build modular and flexible components for influence computation for different tensor frameworks.
Key components include:
-
GradientProvider: A generic abstract base class designed to provide methods for computing per-sample gradients and other related computations for given data batches.
-
BilinearForm: A generic abstract base class for representing bilinear forms for computing inner products involving gradients.
-
Operator: A generic abstract base class for operators that can apply transformations to vectors and matrices and can be represented as bilinear forms.
-
OperatorGradientComposition: A generic abstract composition class that integrates an operator with a gradient provider to compute interactions between batches of data.
-
BlockMapper: A generic abstract base class for mapping operations across multiple compositional blocks, given by objects of type OperatorGradientComposition, and aggregating the results.
To see the usage of these types, see the implementation ComposableInfluence . Using these components allows the straightforward implementation of various combinations of approximations of inverse Hessian applications (or Gauss-Newton approximations), different blocking strategies (e.g. layer-wise or block-wise) and different ways to compute gradients.
For the usage with a specific tensor framework, these types must be subclassed. An example for torch is provided in the module pydvl.influence.torch.base and the base class TorchComposableInfluence.
InfluenceMode
¶
Enum representation for the types of influence.
ATTRIBUTE | DESCRIPTION |
---|---|
Up |
|
Perturbation |
|
Batch
dataclass
¶
Bases: Generic[TensorType]
Represents a batch of data containing features and labels.
ATTRIBUTE | DESCRIPTION |
---|---|
x |
Represents the input features of the batch.
TYPE:
|
y |
Represents the labels or targets associated with the input features.
TYPE:
|
GradientProvider
¶
Bases: Generic[BatchType, TensorType]
, ABC
Provides an interface for calculating per-sample gradients and other related computations for a given batch of data.
This class must be subclassed with implementations for its abstract methods tailored to specific gradient computation needs, e.g. using an autograd engine for a model loss function. Consider a function
e.g. a two layer neural network \(f\) with a loss function, then this object should compute the expressions:
jacobian_prod
abstractmethod
¶
Computes the matrix-Jacobian product for the provided batch and input tensor. Given the example in the class docstring, this means
where g must be a tensor of shape \((K, d_1+d_2)\), so the resulting tensor is of shape \((N, K)\).
PARAMETER | DESCRIPTION |
---|---|
batch |
The batch of data for which to compute the Jacobian.
TYPE:
|
g |
The tensor to be used in the matrix-Jacobian product calculation.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
The resulting tensor from the matrix-Jacobian product computation. |
Source code in src/pydvl/influence/types.py
flat_grads
abstractmethod
¶
Computes and returns the flat per-sample gradients for the provided batch. Given the example in the class docstring, this means
where the first dimension of the resulting tensor is always considered to be the batch dimension, so the shape of the resulting tensor is \((N, d_1+d_2)\), where \(N\) is the number of samples in the batch.
PARAMETER | DESCRIPTION |
---|---|
batch |
The batch of data for which to compute the gradients.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor containing the flat gradients computed per sample. |
Source code in src/pydvl/influence/types.py
flat_mixed_grads
abstractmethod
¶
Computes and returns the flat per-sample mixed gradients for the provided batch. Given the example in the class docstring, this means
where the first dimension of the resulting tensor is always considered to be the batch dimension and the last to be the non-batch input related derivatives. So the shape of the resulting tensor is \((N, n, d_1 + d_2)\), where \(N\) is the number of samples in the batch.
PARAMETER | DESCRIPTION |
---|---|
batch |
The batch of data for which to compute the flat mixed gradients.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor containing the flat mixed gradients computed per sample. |
Source code in src/pydvl/influence/types.py
BilinearForm
¶
Bases: Generic[TensorType, BatchType, GradientProviderType]
, ABC
Abstract base class for bilinear forms, which facilitates the computation of inner products involving gradients of batches of data.
inner_prod
abstractmethod
¶
inner_prod(left: TensorType, right: Optional[TensorType]) -> TensorType
Computes the inner product of two vectors, i.e.
if we denote the bilinear-form by \(\langle \cdot, \cdot \rangle_{B}\).
The implementations must take care of according vectorization to make
it applicable to the case, where left
and right
are not one-dimensional.
In this case, the trailing dimension of the left
and right
tensors are
considered for the computation of the inner product. For example,
if left
is a tensor of shape \((N, D)\) and, right
is of shape \((M,..., D)\),
then the result is of shape \((N, M, ...)\).
PARAMETER | DESCRIPTION |
---|---|
left |
The first tensor in the inner product computation.
TYPE:
|
right |
The second tensor, optional; if not provided, the inner product will
use
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the inner product. |
Source code in src/pydvl/influence/types.py
grads_inner_prod
¶
grads_inner_prod(
left: BatchType,
right: Optional[BatchType],
gradient_provider: GradientProviderType,
) -> TensorType
Computes the gradient inner product of two batches of data, i.e.
where \(\nabla_{\omega}\ell(\omega, \cdot, \cdot)\) is represented by the
gradient_provider
and the expression must be understood sample-wise.
PARAMETER | DESCRIPTION |
---|---|
left |
The first batch for gradient and inner product computation
TYPE:
|
right |
The second batch for gradient and inner product computation,
optional; if not provided, the inner product will use the gradient
computed for
TYPE:
|
gradient_provider |
The gradient provider to compute the gradients.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the inner products of the per-sample gradients |
Source code in src/pydvl/influence/types.py
mixed_grads_inner_prod
¶
mixed_grads_inner_prod(
left: BatchType,
right: Optional[BatchType],
gradient_provider: GradientProviderType,
) -> TensorType
Computes the mixed gradient inner product of two batches of data, i.e.
where \(\nabla_{\omega}\ell(\omega, \cdot)\) and
\(\nabla_{\omega}\nabla_{x}\ell(\omega, \cdot)\) are represented by the
gradient_provider
. The expression must be understood sample-wise.
PARAMETER | DESCRIPTION |
---|---|
left |
The first batch for gradient and inner product computation
TYPE:
|
right |
The second batch for gradient and inner product computation
TYPE:
|
gradient_provider |
The gradient provider to compute the gradients.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the inner products of the mixed per-sample gradients |
Source code in src/pydvl/influence/types.py
Operator
¶
Bases: Generic[TensorType, BilinearFormType]
, ABC
Abstract base class for operators, capable of applying transformations to vectors and matrices, and can be represented as a bilinear form.
input_size
abstractmethod
property
¶
input_size: int
Abstract property to get the needed size for inputs to the operator instance
RETURNS | DESCRIPTION |
---|---|
int
|
An integer representing the input size. |
apply
¶
Applies the operator to a tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor |
A tensor, whose tailing dimension must conform to the operator's input size
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the result of the operator application. |
Source code in src/pydvl/influence/types.py
as_bilinear_form
abstractmethod
¶
Represents the operator as a bilinear form, i.e. the weighted inner product
RETURNS | DESCRIPTION |
---|---|
BilinearFormType
|
An instance of type BilinearForm representing this operator. |
Source code in src/pydvl/influence/types.py
OperatorGradientComposition
¶
Bases: Generic[TensorType, BatchType, OperatorType, GradientProviderType]
Generic base class representing a composable block that integrates an operator and a gradient provider to compute interactions between batches of data.
This block is designed to be flexible, handling different computational modes via an abstract operator and gradient provider.
ATTRIBUTE | DESCRIPTION |
---|---|
op |
The operator used for transformations and influence computations.
|
gp |
The gradient provider used for obtaining necessary gradients.
|
Source code in src/pydvl/influence/types.py
interactions
¶
interactions(
left_batch: BatchType, right_batch: Optional[BatchType], mode: InfluenceMode
)
Computes the interaction between the gradients on two batches of data based on the specified mode weighted by the operator action, i.e.
for the case InfluenceMode.Up
and
for the case InfluenceMode.Perturbation
.
PARAMETER | DESCRIPTION |
---|---|
left_batch |
The left data batch for gradient computation.
TYPE:
|
right_batch |
The right data batch for gradient computation.
TYPE:
|
mode |
An instance of InfluenceMode determining the type of influence computation.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
The result of the influence computation as dictated by the mode. |
Source code in src/pydvl/influence/types.py
transformed_grads
¶
Computes the gradients of a data batch, transformed by the operator application , i.e. the expressions
PARAMETER | DESCRIPTION |
---|---|
batch |
The data batch for gradient computation.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
A tensor representing the application of the operator to the gradients. |
Source code in src/pydvl/influence/types.py
interactions_from_transformed_grads
¶
interactions_from_transformed_grads(
left_factors: TensorType, right_batch: BatchType, mode: InfluenceMode
)
Computes the interaction between the transformed gradients on two batches of data using pre-computed factors and a batch of data, based on the specified mode. This means
for the case InfluenceMode.Up
and
for the case InfluenceMode.Perturbation
.
PARAMETER | DESCRIPTION |
---|---|
left_factors |
Pre-computed tensor factors from a left batch.
TYPE:
|
right_batch |
The right data batch for influence computation.
TYPE:
|
mode |
An instance of InfluenceMode determining the type of influence computation.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
The result of the interaction computation using the provided factors and batch gradients. |
Source code in src/pydvl/influence/types.py
BlockMapper
¶
BlockMapper(
composable_block_dict: OrderedDict[str, OperatorGradientCompositionType]
)
Bases: Generic[TensorType, BatchType, OperatorGradientCompositionType]
, ABC
Abstract base class for mapping operations across multiple compositional blocks.
This class takes a dictionary of compositional blocks and applies their methods to batches or tensors, and aggregates the results.
ATTRIBUTE | DESCRIPTION |
---|---|
composable_block_dict |
A dictionary mapping string identifiers to composable blocks which define operations like transformations and interactions.
|
Source code in src/pydvl/influence/types.py
transformed_grads
¶
transformed_grads(batch: BatchType) -> OrderedDict[str, TensorType]
Computes and returns the transformed gradients for a batch in dictionary with the keys defined by the block names.
PARAMETER | DESCRIPTION |
---|---|
batch |
The batch of data for which to compute transformed gradients.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
OrderedDict[str, TensorType]
|
An ordered dictionary of transformed gradients by block. |
Source code in src/pydvl/influence/types.py
interactions
¶
interactions(
left_batch: BatchType, right_batch: BatchType, mode: InfluenceMode
) -> OrderedDict[str, TensorType]
Computes interactions between two batches, aggregated by block, based on a specified mode.
PARAMETER | DESCRIPTION |
---|---|
left_batch |
The left batch for interaction computation.
TYPE:
|
right_batch |
The right batch for interaction computation.
TYPE:
|
mode |
The mode determining the type of interactions.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
OrderedDict[str, TensorType]
|
An ordered dictionary of gradient interactions by block. |
Source code in src/pydvl/influence/types.py
interactions_from_transformed_grads
¶
interactions_from_transformed_grads(
left_factors: OrderedDict[str, TensorType],
right_batch: BatchType,
mode: InfluenceMode,
) -> OrderedDict[str, TensorType]
Computes interactions from transformed gradients and a right batch, aggregated by block and based on a mode.
PARAMETER | DESCRIPTION |
---|---|
left_factors |
Pre-computed factors as a tensor or an ordered dictionary of
tensors by block. If the input is a tensor, it is split into blocks
according to the ordering in the
TYPE:
|
right_batch |
The right batch for interaction computation.
TYPE:
|
mode |
The mode determining the type of interactions.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
OrderedDict[str, TensorType]
|
An ordered dictionary of interactions from transformed gradients by block. |
Source code in src/pydvl/influence/types.py
generate_transformed_grads
¶
generate_transformed_grads(
batch: BatchType,
) -> Generator[TensorType, None, None]
Generator that yields transformed gradients for a given batch, processed by each block.
PARAMETER | DESCRIPTION |
---|---|
batch |
The batch of data for which to generate transformed gradients.
TYPE:
|
YIELDS | DESCRIPTION |
---|---|
TensorType
|
Transformed gradients for each block. |
Source code in src/pydvl/influence/types.py
generate_interactions
¶
generate_interactions(
left_batch: BatchType, right_batch: Optional[BatchType], mode: InfluenceMode
) -> Generator[TensorType, None, None]
Generator that yields gradient interactions between two batches, processed by each block based on a mode.
PARAMETER | DESCRIPTION |
---|---|
left_batch |
The left batch for interaction computation.
TYPE:
|
right_batch |
The right batch for interaction computation.
TYPE:
|
mode |
The mode determining the type of interactions.
TYPE:
|
YIELDS | DESCRIPTION |
---|---|
TensorType
|
Gradient interactions for each block.
TYPE::
|
Source code in src/pydvl/influence/types.py
generate_interactions_from_transformed_grads
¶
generate_interactions_from_transformed_grads(
left_factors: Union[TensorType, OrderedDict[str, TensorType]],
right_batch: BatchType,
mode: InfluenceMode,
) -> Generator[TensorType, None, None]
Generator that yields interactions computed from pre-computed factors and a right batch, processed by each block based on a mode.
PARAMETER | DESCRIPTION |
---|---|
left_factors |
Pre-computed factors as a tensor or an ordered dictionary of tensors by block.
TYPE:
|
right_batch |
The right batch for interaction computation.
TYPE:
|
mode |
The mode determining the type of interactions.
TYPE:
|
YIELDS | DESCRIPTION |
---|---|
TensorType
|
Interactions for each block.
TYPE::
|