code: def _register_backward_hook(
      module: Module, hook: Callable, attr_obj: Any
  ) -> List[torch.utils.hooks.RemovableHandle]:
      grad_out: Dict[device, Tensor] = {}
  
      def forward_hook(
          module: Module,
          inp: Union[Tensor, Tuple[Tensor, ...]],
          out: Union[Tensor, Tuple[Tensor, ...]],
      ) -> None:
          nonlocal grad_out
          grad_out = {}
  
          def output_tensor_hook(output_grad: Tensor) -> None:
              grad_out[output_grad.device] = output_grad
  
          if isinstance(out, tuple):
              assert (
                  len(out) == 1
              ), "Backward hooks not supported for module with >1 output"
              out[0].register_hook(output_tensor_hook)
          else:
              out.register_hook(output_tensor_hook)
  
      def pre_hook(module, inp):
          def input_tensor_hook(input_grad: Tensor):
              if len(grad_out) == 0:
                  return
              hook_out = hook(
                  module, input_grad, grad_out[input_grad.device]
              )
  
              if hook_out is not None:
                  return (
                      hook_out[0]
                      if isinstance(hook_out, tuple)
                      else hook_out
                  )
  
          if isinstance(inp, tuple):
              assert (
                  len(inp) == 1
              ), "Backward hooks not supported for module with >1 input"
              inp[0].register_hook(input_tensor_hook)
              return inp[0].clone()
          else:
              inp.register_hook(input_tensor_hook)
              return inp.clone()
  
      return [
          module.register_forward_pre_hook(pre_hook),
          module.register_forward_hook(forward_hook),
      ]
  
imports: import typing
  from enum import Enum
  from functools import reduce
  from inspect import signature
  from typing import Any, Callable, cast, Dict, List, overload, Tuple, Union
  import numpy
  import torch
  from captum._utils.typing import BaselineType, Literal, TargetType, TensorOrTupleOfTensorsGeneric, TupleOrTensorOrBoolGeneric
  from torch import device, Tensor
  from torch.nn import Module
label_desc: Use image node_id 46 for calling a global function with example usage: _register_backward_hook(module, hook, attr_obj) and returns: list, inp, inp, unknown