Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing move to model device for EkfacInfluence implementation #570

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Fixed

- Fixed missing move of tensors to model device in `EkfacInfluence`
implementation [PR #570](https://github.com/aai-institute/pyDVL/pull/570)

### Added

- Add a device fixture for `pytest`, which depending on the availability and
Expand Down
17 changes: 10 additions & 7 deletions src/pydvl/influence/torch/influence_function_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ def influences_from_factors(
"""
if mode == InfluenceMode.Up:
return (
z_test_factors
z_test_factors.to(self.model_device)
@ self._loss_grad(x.to(self.model_device), y.to(self.model_device)).T
)
elif mode == InfluenceMode.Perturbation:
return torch.einsum(
"ia,j...a->ij...",
z_test_factors,
z_test_factors.to(self.model_device),
self._flat_loss_mixed_grad(
x.to(self.model_device), y.to(self.model_device)
),
Expand Down Expand Up @@ -1195,7 +1195,7 @@ def _get_kfac_blocks(
data, disable=not self.progress, desc="K-FAC blocks - batch progress"
):
data_len += x.shape[0]
pred_y = self.model(x)
pred_y = self.model(x.to(self.model_device))
loss = empirical_cross_entropy_loss_fn(pred_y)
loss.backward()

Expand Down Expand Up @@ -1319,7 +1319,7 @@ def _update_diag(
data, disable=not self.progress, desc="Update Diagonal - batch progress"
):
data_len += x.shape[0]
pred_y = self.model(x)
pred_y = self.model(x.to(self.model_device))
loss = empirical_cross_entropy_loss_fn(pred_y)
loss.backward()

Expand Down Expand Up @@ -1526,7 +1526,10 @@ def influences_from_factors_by_layer(
influences = {}
for layer_id, layer_z_test in z_test_factors.items():
end_idx = start_idx + layer_z_test.shape[1]
influences[layer_id] = layer_z_test @ total_grad[:, start_idx:end_idx].T
influences[layer_id] = (
layer_z_test.to(self.model_device)
@ total_grad[:, start_idx:end_idx].T
)
start_idx = end_idx
return influences
elif mode == InfluenceMode.Perturbation:
Expand All @@ -1539,7 +1542,7 @@ def influences_from_factors_by_layer(
end_idx = start_idx + layer_z_test.shape[1]
influences[layer_id] = torch.einsum(
"ia,j...a->ij...",
layer_z_test,
layer_z_test.to(self.model_device),
total_mixed_grad[:, start_idx:end_idx],
)
start_idx = end_idx
Expand Down Expand Up @@ -1626,7 +1629,7 @@ def explore_hessian_regularization(
being dictionaries containing the influences for each layer of the model,
with the layer name as key.
"""
grad = self._loss_grad(x, y)
grad = self._loss_grad(x.to(self.model_device), y.to(self.model_device))
influences_by_reg_value = {}
for reg_value in regularization_values:
reg_factors = self._solve_hvp_by_layer(
Expand Down
Loading