class_docstr: Context manager under which tensors saved by the forward pass will be stored on cpu, then retrieved for backward.
  
  When performing operations within this context manager, intermediary
  results saved in the graph during the forward pass will be moved to CPU,
  then copied back to the original device when needed for the backward pass.
  If the graph was already on CPU, no tensor copy is performed.
  
  Use this context-manager to trade compute for GPU memory usage (e.g.
  when your model doesn't fit in GPU memory during training).
  
  Args:
      pin_memory (bool): If ``True`` tensors will be saved to CPU pinned memory
                         during packing and copied to GPU asynchronously during unpacking.
                         Defaults to ``False``.
                         Also see :ref:`cuda-memory-pinning`.
  
  
  Example::
  
      >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
      >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_AUTOGRAD)
      >>> a = torch.randn(5, requires_grad=True, device="cuda")
      >>> b = torch.randn(5, requires_grad=True, device="cuda")
      >>> c = torch.randn(5, requires_grad=True, device="cuda")
      >>>
      >>> def f(a, b, c):
      ...     prod_1 = a * b           # a and b are saved on GPU
      ...     with torch.autograd.graph.save_on_cpu():
      ...         prod_2 = prod_1 * c  # prod_1 and c are saved on CPU
      ...     y = prod_2 * a           # prod_2 and a are saved on GPU
      ...     return y
      >>>
      >>> y = f(a, b, c)
      >>> del a, b, c  # for illustration only
      >>> # the content of a, b, and prod_2 are still alive on GPU
      >>> # the content of prod_1 and c only live on CPU
      >>> y.sum().backward()  # all CPU tensors are moved back to GPU, for backward
      >>> # all intermediary tensors are released (deleted) after the call to backward
class_docstr_tok: ["Context","manager","under","which","tensors","saved","by","the","forward","pass","will","be","stored","on","cpu",",","then","retrieved","for","backward",".","When","performing","operations","within","this","context","manager",",","intermediary","results","saved","in","the","graph","during","the","forward","pass","will","be","moved","to","CPU",",","then","copied","back","to","the","original","device","when","needed","for","the","backward","pass",".","If","the","graph","was","already","on","CPU",",","no","tensor","copy","is","performed",".","Use","this","context-manager","to","trade","compute","for","GPU","memory","usage","(","e.g",".","when","your","model","does","n't","fit","in","GPU","memory","during","training",")",".","Args",":","pin_memory","(","bool",")",":","If","``","True","``","tensors","will","be","saved","to","CPU","pinned","memory","during","packing","and","copied","to","GPU","asynchronously","during","unpacking",".","Defaults","to","``","False","``",".","Also","see",":","ref",":","`","cuda-memory-pinning","`",".","Example",":",":",">",">",">","#","xdoctest",":","+REQUIRES","(","env",":","TORCH_DOCTEST_CUDA",")",">",">",">","#","xdoctest",":","+REQUIRES","(","env",":","TORCH_DOCTEST_AUTOGRAD",")",">",">",">","a","=","torch.randn","(","5",",","requires_grad=True",",","device=","''","cuda","''",")",">",">",">","b","=","torch.randn","(","5",",","requires_grad=True",",","device=","''","cuda","''",")",">",">",">","c","=","torch.randn","(","5",",","requires_grad=True",",","device=","''","cuda","''",")",">",">",">",">",">",">","def","f","(","a",",","b",",","c",")",":","...","prod_1","=","a","*","b","#","a","and","b","are","saved","on","GPU","...","with","torch.autograd.graph.save_on_cpu","(",")",":","...","prod_2","=","prod_1","*","c","#","prod_1","and","c","are","saved","on","CPU","...","y","=","prod_2","*","a","#","prod_2","and","a","are","saved","on","GPU","...","return","y",">",">",">",">",">",">","y","=","f","(","a",",","b",",","c",")",">",">",">","del","a",",","b",",","c","#","for","illustration","only",">",">",">","#","the","content","of","a",",","b",",","and","prod_2","are","still","alive","on","GPU",">",">",">","#","the","content","of","prod_1","and","c","only","live","on","CPU",">",">",">","y.sum","(",")",".backward","(",")","#","all","CPU","tensors","are","moved","back","to","GPU",",","for","backward",">",">",">","#","all","intermediary","tensors","are","released","(","deleted",")","after","the","call","to","backward"]
code: def __init__(self, pin_memory=False, device_type="cuda"):
      device_module = getattr(torch, device_type, torch.cuda)
  
      def pack_to_cpu(tensor):
          if not pin_memory:
              return (tensor.device, tensor.cpu())
          packed = torch.empty(
              tensor.size(),
              dtype=tensor.dtype,
              layout=tensor.layout,
              pin_memory=(
                  device_module.is_available() and not tensor.is_sparse
              ),
          )
          packed.copy_(tensor)
          return (tensor.device, packed)
  
      def unpack_from_cpu(packed):
          device, tensor = packed
          return tensor.to(device, non_blocking=pin_memory)
  
      super().__init__(pack_to_cpu, unpack_from_cpu)
  
imports: import abc
  import collections
  import contextlib
  import logging
  import weakref
  from collections import defaultdict, namedtuple
  from typing import Any, Callable, Deque, Dict, List, Optional, Sequence, Set, Tuple
  import torch
  from torch.autograd.variable import Variable
  from torch.utils._python_dispatch import TorchDispatchMode
  from torch.utils.hooks import RemovableHandle
label_desc: Use image node_id 1 to create a new save_on_cpu object from inherited base classes: saved_tensors_hooks with example: obj = save_on_cpu(pin_memory, device_type)