decomp_utils.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # mypy: allow-untyped-defs
  2. from collections.abc import Callable
  3. import torch
  4. from torch._export.utils import (
  5. _collect_all_valid_cia_ops,
  6. _collect_all_valid_cia_ops_for_aten_namespace,
  7. _get_decomp_for_cia,
  8. _is_aten_op,
  9. )
  10. __all__ = ["CustomDecompTable"]
  11. """
  12. Core ATen ops with Composite Implicit Autograd dispatch that should be excluded from decomposition
  13. by default. The decomposition logic should eventually exclude all core-tagged CIA ops, but until all
  14. backends are ready, this list allows opt-in one at a time.
  15. """
  16. PRESERVED_ATEN_CIA_OPS = {
  17. torch.ops.aten.upsample_bilinear2d.vec,
  18. torch.ops.aten.upsample_nearest2d.vec,
  19. # NB: don't use the C++ decomp, because it is not functional!
  20. torch.ops.aten.silu_backward.default,
  21. torch.ops.aten.mish_backward.default,
  22. torch.ops.aten._fused_rms_norm.default,
  23. }
  24. class CustomDecompTable(dict[torch._ops.OperatorBase, Callable]):
  25. """
  26. This is a custom dictionary that is specifically used for handling decomp_table in export.
  27. The reason we need this is because in the new world, you can only *delete* an op from decomp
  28. table to preserve it. This is problematic for custom ops because we don't know when the custom
  29. op will actually be loaded to the dispatcher. As a result, we need to record the custom ops operations
  30. until we really need to materialize it (which is when we run decomposition pass.)
  31. Invariants we hold are:
  32. 1. All aten decomp is loaded at the init time
  33. 2. We materialize ALL ops when user ever reads from the table to make it more likely
  34. that dispatcher picks up the custom op.
  35. 3. If it is write operation, we don't necessarily materialize
  36. 4. We load the final time during export, right before calling run_decompositions()
  37. """
  38. def __init__(self):
  39. super().__init__()
  40. from torch._decomp import _core_aten_decompositions_post_autograd
  41. # For aten ops, we load them up in the beginning
  42. self.decomp_table = _core_aten_decompositions_post_autograd()
  43. for op in _collect_all_valid_cia_ops_for_aten_namespace():
  44. if op not in PRESERVED_ATEN_CIA_OPS and op not in self.decomp_table:
  45. self.decomp_table[op] = _get_decomp_for_cia(op)
  46. # This is to track the *pending* deleted custom ops that haven't been materialized yet
  47. self.deleted_custom_ops = set()
  48. # When this is true, there shouldn't be any pending operations in the table.
  49. self.has_materialized = False
  50. def __getitem__(self, key):
  51. self._materialize_if_needed()
  52. return self.decomp_table.__getitem__(key)
  53. def __setitem__(self, key, value) -> None:
  54. self.decomp_table.__setitem__(key, value)
  55. if key in self.deleted_custom_ops:
  56. self.deleted_custom_ops.remove(key)
  57. def keys(self):
  58. self._materialize_if_needed()
  59. return self.decomp_table.keys()
  60. def __delitem__(self, key) -> None:
  61. self.pop(key)
  62. def update(self, other_dict): # type: ignore[override]
  63. for k, v in other_dict.items():
  64. self.decomp_table.__setitem__(k, v)
  65. def __missing__(self, key) -> bool:
  66. return not self.__contains__(key)
  67. def __contains__(self, key) -> bool:
  68. self._materialize_if_needed()
  69. return self.decomp_table.__contains__(key)
  70. def __len__(self) -> int:
  71. self._materialize_if_needed()
  72. return self.decomp_table.__len__()
  73. def __iter__(self):
  74. self._materialize_if_needed()
  75. return self.decomp_table.__iter__()
  76. def __reversed__(self):
  77. self._materialize_if_needed()
  78. return self.decomp_table.__reversed__()
  79. def copy(self) -> "CustomDecompTable":
  80. new_dict = CustomDecompTable()
  81. new_dict.decomp_table = self.decomp_table.copy()
  82. new_dict.deleted_custom_ops = self.deleted_custom_ops.copy()
  83. new_dict.has_materialized = self.has_materialized
  84. return new_dict
  85. def pop(self, *args):
  86. def _pop_if_can(key):
  87. if _is_aten_op(key):
  88. return self.decomp_table.pop(key)
  89. if key in self.decomp_table:
  90. # Even if we materialized it, we should add it to the deleted
  91. # custom ops list so that when we materialize next time,
  92. # we should respect user's intention.
  93. self.deleted_custom_ops.add(key)
  94. return self.decomp_table.pop(key)
  95. if key in self.deleted_custom_ops:
  96. raise KeyError(f"{key} doesn't exist in the table")
  97. self.deleted_custom_ops.add(key)
  98. # We would come here when user pops off something that is
  99. # not in the table. In this case, we just pretend that it
  100. # was in the table.
  101. return _get_decomp_for_cia(key)
  102. if len(args) == 1:
  103. return _pop_if_can(args[0])
  104. if len(args) == 2:
  105. try:
  106. return _pop_if_can(args[0])
  107. except KeyError:
  108. return args[1]
  109. def items(self):
  110. self._materialize_if_needed()
  111. return self.decomp_table.items()
  112. def materialize(self) -> dict[torch._ops.OperatorBase, Callable]:
  113. for op in _collect_all_valid_cia_ops():
  114. if _is_aten_op(op):
  115. continue
  116. elif op in self.decomp_table:
  117. continue
  118. elif op not in self.deleted_custom_ops:
  119. self.decomp_table[op] = _get_decomp_for_cia(op)
  120. self.has_materialized = True
  121. self.deleted_custom_ops = set()
  122. return {**self.decomp_table}
  123. def _materialize_if_needed(self) -> None:
  124. if not self.has_materialized:
  125. self.materialize()