code: def _checkpointed_forward(
      original_forward: Callable[..., _T],
      weak_self,
      *args,
      **kwargs,
  ) -> _T:
      module = weak_self()
      assert (
          module is not None
      ), "patched forward method called after module has been garbage collected!"
  
      # If in eval mode, just use the original `.forward()` method.
      if not module.training:
          return original_forward(module, *args, **kwargs)
  
      # The function that the `CheckpointFunction` is applied to needs to have at least one
      # input tensor that has `requires_grad=True`.
      # So to avoid having to make users manually set the `requires_grad` flag on input tensors,
      # we wrap the `original_forward` with a `run_function` that takes an additional dummy tensor
      # as input, and we'll set `requires_grad=True` on this dummy tensor.
  
      def run_function(dummy_tensor, *forward_args, **forward_kwargs):
          return original_forward(
              module, *forward_args, **forward_kwargs
          )
  
      dummy_tensor = torch.tensor([], requires_grad=True)
      return CheckpointFunction.apply(
          run_function, True, dummy_tensor, *args, **kwargs
      )
  
file_path: allennlp/allennlp/nn/checkpoint/checkpoint_wrapper.py
imports: import functools
  from typing import Callable, TypeVar
  import weakref
  import torch
  import torch.nn
  from torch.utils.checkpoint import CheckpointFunction
  from allennlp.common.registrable import Registrable
label_desc: Use image node_id 1 for calling a global function with example usage: _checkpointed_forward(original_forward, weak_) and returns: CheckpointFunction, original_forward, original_forward