code: def _conductance_grads(self, forward_fn, input, target_ind=None):
      with torch.autograd.set_grad_enabled(True):
          # Set a forward hook on specified module and run forward pass to
          # get output tensor size.
          saved_tensor = None
  
          def forward_hook(module, inp, out):
              nonlocal saved_tensor
              saved_tensor = out
  
          hook = self.layer.register_forward_hook(forward_hook)
          output = forward_fn(input)
  
          # Compute layer output tensor dimensions and total number of units.
          # The hidden layer tensor is assumed to have dimension (num_hidden, ...)
          # where the product of the dimensions >= 1 correspond to the total
          # number of hidden neurons in the layer.
          layer_size = tuple(saved_tensor.size())[1:]
          layer_units = int(np.prod(layer_size))
  
          # Remove unnecessary forward hook.
          hook.remove()
  
          # Backward hook function to override gradients in order to obtain
          # just the gradient of each hidden unit with respect to input.
          saved_grads = None
  
          def backward_hook(grads):
              nonlocal saved_grads
              saved_grads = grads
              zero_mat = torch.zeros((1,) + layer_size)
              scatter_indices = torch.arange(0, layer_units).view_as(
                  zero_mat
              )
              # Creates matrix with each layer containing a single unit with
              # value 1 and remaining zeros, which will provide gradients
              # with respect to each unit independently.
              to_return = torch.zeros(
                  (layer_units,) + layer_size
              ).scatter(0, scatter_indices, 1)
              to_repeat = [1] * len(to_return.shape)
              to_repeat[0] = grads.shape[0] // to_return.shape[0]
              expanded = to_return.repeat(to_repeat)
              return expanded
  
          # Create a forward hook in order to attach backward hook to appropriate
          # tensor. Save backward hook in order to remove hook appropriately.
          back_hook = None
  
          def forward_hook_register_back(module, inp, out):
              nonlocal back_hook
              back_hook = out.register_hook(backward_hook)
  
          hook = self.layer.register_forward_hook(
              forward_hook_register_back
          )
  
          # Expand input to include layer_units copies of each input.
          # This allows obtaining gradient with respect to each hidden unit
          # in one pass.
          expanded_input = torch.repeat_interleave(
              input, layer_units, dim=0
          )
          output = forward_fn(expanded_input)
          hook.remove()
          output = (
              output[:, target_ind]
              if target_ind is not None
              else output
          )
          input_grads = torch.autograd.grad(
              torch.unbind(output), expanded_input
          )
  
          # Remove backwards hook
          back_hook.remove()
  
          # Remove duplicates in gradient with respect to hidden layer,
          # choose one for each layer_units indices.
          output_mid_grads = torch.index_select(
              saved_grads,
              0,
              torch.tensor(
                  range(0, input_grads[0].shape[0], layer_units)
              ),
          )
      return input_grads[0], output_mid_grads, layer_units
  
file_path: captum/tests/attr/helpers/conductance_reference.py
imports: import numpy
  import torch
  from captum._utils.gradient import apply_gradient_requirements, undo_gradient_requirements
  from captum.attr._utils.approximation_methods import approximation_parameters
  from captum.attr._utils.attribution import LayerAttribution
  from captum.attr._utils.common import _reshape_and_sum
label_desc: Use image node_id 2 for calling the ConductanceReference obj's underlying member method code with example usage: obj._conductance_grads(forward_fn, input, target_ind) and returns: input_grads,  output_mid_grads,  layer_units, expanded