Skip to content

Commit d73bfe6

Browse files
Vivek Miglanifacebook-github-bot
authored andcommitted
add ArnoldiInfluenceFunction (#1187)
Summary: Pull Request resolved: #1187 This diff implements `ArnoldiInfluenceFunction`, which was described, along with `NaiveInfluenceFunction` in D40541294. Please see that diff for detailed description. Previously implementations of both methods had been 1 diff. Now, `ArnoldiInfluenceFunction` is separated out for easier review. Differential Revision: D42006733 fbshipit-source-id: c952e234455dd530af6af27441efcf83a6a2055e
1 parent d8cccf1 commit d73bfe6

File tree

8 files changed

+1690
-9
lines changed

8 files changed

+1690
-9
lines changed

captum/influence/_core/arnoldi_influence_function.py

Lines changed: 1022 additions & 0 deletions
Large diffs are not rendered by default.

captum/influence/_utils/common.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,60 @@ def _influence_route_to_helpers(
859859
)
860860

861861

862+
def _parameter_dot(
863+
params_1: Tuple[Tensor, ...], params_2: Tuple[Tensor, ...]
864+
) -> Tensor:
865+
"""
866+
returns the dot-product of 2 tensors, represented as tuple of tensors.
867+
"""
868+
sum_val = sum(
869+
torch.sum(param_1 * param_2) for (param_1, param_2) in zip(params_1, params_2)
870+
)
871+
return torch.tensor(
872+
sum(
873+
torch.sum(param_1 * param_2)
874+
for (param_1, param_2) in zip(params_1, params_2)
875+
)
876+
)
877+
878+
879+
def _parameter_add(
880+
params_1: Tuple[Tensor, ...], params_2: Tuple[Tensor, ...]
881+
) -> Tuple[Tensor, ...]:
882+
"""
883+
returns the sum of 2 tensors, represented as tuple of tensors.
884+
"""
885+
return tuple(param_1 + param_2 for (param_1, param_2) in zip(params_1, params_2))
886+
887+
888+
def _parameter_multiply(params: Tuple[Tensor, ...], c: Tensor) -> Tuple[Tensor, ...]:
889+
"""
890+
multiplies all tensors in a tuple of tensors by a given scalar
891+
"""
892+
return tuple(param * c for param in params)
893+
894+
895+
def _parameter_to(params: Tuple[Tensor, ...], **to_kwargs) -> Tuple[Tensor, ...]:
896+
"""
897+
applies the `to` method to all tensors in a tuple of tensors
898+
"""
899+
return tuple(param.to(**to_kwargs) for param in params)
900+
901+
902+
def _parameter_linear_combination(
903+
paramss: List[Tuple[Tensor, ...]], cs: Tensor
904+
) -> Tuple[Tensor, ...]:
905+
"""
906+
scales each parameter (tensor of tuples) in a list by the corresponding scalar in a
907+
1D tensor of the same length, and sums up the scaled parameters
908+
"""
909+
assert len(cs.shape) == 1
910+
result = _parameter_multiply(paramss[0], cs[0])
911+
for (params, c) in zip(paramss[1:], cs[1:]):
912+
result = _parameter_add(result, _parameter_multiply(params, c))
913+
return result
914+
915+
862916
def _compute_jacobian_sample_wise_grads_per_batch(
863917
influence_inst: Union["TracInCP", "InfluenceFunctionBase"],
864918
inputs: Tuple[Any, ...],
@@ -1015,7 +1069,9 @@ def _functional_call(model, d, features):
10151069
def _dataset_fn(dataloader, batch_fn, reduce_fn, *batch_fn_args, **batch_fn_kwargs):
10161070
"""
10171071
Applies `batch_fn` to each batch in `dataloader`, reducing the results using
1018-
`reduce_fn`. This is useful for computing Hessians over an entire dataloader.
1072+
`reduce_fn`. This is useful for computing Hessians and Hessian-vector
1073+
products over an entire dataloader, and is used by both `NaiveInfluenceFunction`
1074+
and `ArnoldiInfluenceFunction`.
10191075
"""
10201076
_dataloader = iter(dataloader)
10211077

0 commit comments

Comments
 (0)