Skip to content

Commit

Permalink
[BatchDotGrad] Implement base class
Browse files Browse the repository at this point in the history
  • Loading branch information
f-dangel committed Aug 21, 2020
1 parent 3b141e0 commit d1f76f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
31 changes: 31 additions & 0 deletions backpack/extensions/firstorder/batch_dot_grad/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Base class for extension ``BatchDotGrad``."""

import torch

from backpack.extensions.firstorder.base import FirstOrderModuleExtension


class BatchDotGradBase(FirstOrderModuleExtension):
def __init__(self, derivatives, params=None):
self.derivatives = derivatives
super().__init__(params=params)

def bias(self, ext, module, g_inp, g_out, bpQuantities):
grad_batch = self.derivatives.bias_jac_t_mat_prod(
module, g_inp, g_out, g_out[0], sum_batch=False
)
return self.pairwise_dot(grad_batch)

def weight(self, ext, module, g_inp, g_out, bpQuantities):
grad_batch = self.derivatives.weight_jac_t_mat_prod(
module, g_inp, g_out, g_out[0], sum_batch=False
)
return self.pairwise_dot(grad_batch)

@staticmethod
def pairwise_dot(grad_batch):
"""Compute pairwise dot products of individual gradients."""
# flatten all feature dimensions
grad_batch_flat = grad_batch.flatten(start_dim=1)
# pairwise dot product
return torch.einsum("if,jf->ij", grad_batch_flat, grad_batch_flat)
30 changes: 3 additions & 27 deletions backpack/extensions/firstorder/batch_dot_grad/linear.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
import torch

from backpack.core.derivatives.linear import LinearDerivatives
from backpack.extensions.firstorder.base import FirstOrderModuleExtension
from backpack.extensions.firstorder.batch_dot_grad.base import BatchDotGradBase


class BatchDotGradLinear(FirstOrderModuleExtension):
class BatchDotGradLinear(BatchDotGradBase):
def __init__(self):
self.derivatives = LinearDerivatives()
super().__init__(params=["bias", "weight"])

def bias(self, ext, module, g_inp, g_out, bpQuantities):
# Return value will be stored in savefield of extension
grad_batch = self.derivatives.bias_jac_t_mat_prod(
module, g_inp, g_out, g_out[0], sum_batch=False
)
return self.pairwise_dot(grad_batch)

def weight(self, ext, module, g_inp, g_out, bpQuantities):
# Return value will be stored in savefield of extension
grad_batch = self.derivatives.weight_jac_t_mat_prod(
module, g_inp, g_out, g_out[0], sum_batch=False
)
return self.pairwise_dot(grad_batch)

@staticmethod
def pairwise_dot(grad_batch):
# flatten all feature dimensions
grad_batch_flat = grad_batch.flatten(start_dim=1)
# pairwise dot product
return torch.einsum("if,jf->ij", grad_batch_flat, grad_batch_flat)
super().__init__(derivatives=LinearDerivatives(), params=["bias", "weight"])

0 comments on commit d1f76f4

Please sign in to comment.