__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # mypy: allow-untyped-defs
  2. import copyreg
  3. import os.path as _osp
  4. import weakref
  5. import torch
  6. from torch.utils import (
  7. backcompat as backcompat,
  8. collect_env as collect_env,
  9. data as data,
  10. deterministic as deterministic,
  11. hooks as hooks,
  12. )
  13. from torch.utils.backend_registration import (
  14. generate_methods_for_privateuse1_backend,
  15. rename_privateuse1_backend,
  16. )
  17. from torch.utils.cpp_backtrace import get_cpp_backtrace
  18. from torch.utils.throughput_benchmark import ThroughputBenchmark
  19. def set_module(obj, mod):
  20. """
  21. Set the module attribute on a python object for a given object for nicer printing
  22. """
  23. if not isinstance(mod, str):
  24. raise TypeError("The mod argument should be a string")
  25. obj.__module__ = mod
  26. cmake_prefix_path = _osp.join(_osp.dirname(_osp.dirname(__file__)), "share", "cmake")
  27. def swap_tensors(t1, t2):
  28. """
  29. This function swaps the content of the two Tensor objects.
  30. At a high level, this will make t1 have the content of t2 while preserving
  31. its identity.
  32. This will not work if t1 and t2 have different slots.
  33. """
  34. # Ensure there are no weakrefs
  35. if weakref.getweakrefs(t1):
  36. raise RuntimeError("Cannot swap t1 because it has weakref associated with it")
  37. if weakref.getweakrefs(t2):
  38. raise RuntimeError("Cannot swap t2 because it has weakref associated with it")
  39. t1_slots = set(copyreg._slotnames(t1.__class__)) # type: ignore[attr-defined]
  40. t2_slots = set(copyreg._slotnames(t2.__class__)) # type: ignore[attr-defined]
  41. if t1_slots != t2_slots:
  42. raise RuntimeError("Cannot swap t1 and t2 if they have different slots")
  43. def swap_attr(name):
  44. tmp = getattr(t1, name)
  45. setattr(t1, name, (getattr(t2, name)))
  46. setattr(t2, name, tmp)
  47. def error_pre_hook(grad_outputs):
  48. raise RuntimeError(
  49. "Trying to execute AccumulateGrad node that was poisoned by swap_tensors "
  50. "this can happen when you try to run backward on a tensor that was swapped. "
  51. "For a module m with `torch.__future__.set_swap_module_params_on_conversion(True)` "
  52. "you should not change the device or dtype of the module (e.g. `m.cpu()` or `m.half()`) "
  53. "between running forward and backward. To resolve this, please only change the "
  54. "device/dtype before running forward (or after both forward and backward)."
  55. )
  56. def check_use_count(t, name="t1"):
  57. use_count = t._use_count()
  58. error_str = (
  59. f"Expected use_count of {name} to be 1 or 2 with an AccumulateGrad node but got {use_count} "
  60. f"make sure you are not holding references to the tensor in other places."
  61. )
  62. if use_count > 1:
  63. if use_count == 2 and t.is_leaf:
  64. accum_grad_node = torch.autograd.graph.get_gradient_edge(t).node
  65. # Make sure that the accumulate_grad node was not lazy_init-ed by get_gradient_edge
  66. if t._use_count() == 2:
  67. accum_grad_node.register_prehook(error_pre_hook)
  68. else:
  69. raise RuntimeError(error_str)
  70. else:
  71. raise RuntimeError(error_str)
  72. check_use_count(t1, "t1")
  73. check_use_count(t2, "t2")
  74. # Swap the types
  75. # Note that this will fail if there are mismatched slots
  76. swap_attr("__class__")
  77. # Swap the dynamic attributes
  78. swap_attr("__dict__")
  79. # Swap the slots
  80. for slot in t1_slots:
  81. if hasattr(t1, slot) and hasattr(t2, slot):
  82. swap_attr(slot)
  83. elif hasattr(t1, slot):
  84. setattr(t2, slot, (getattr(t1, slot)))
  85. delattr(t1, slot)
  86. elif hasattr(t2, slot):
  87. setattr(t1, slot, (getattr(t2, slot)))
  88. delattr(t2, slot)
  89. # Swap the at::Tensor they point to
  90. torch._C._swap_tensor_impl(t1, t2)