constant.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # mypy: ignore-errors
  2. """
  3. Constant and enum variable tracking in Dynamo.
  4. This module is fundamental to Dynamo's ability to track and propagate constant
  5. values during compilation, ensuring proper handling of Python literals and
  6. maintaining type safety through the compilation process.
  7. """
  8. import operator
  9. from typing import TYPE_CHECKING
  10. import torch
  11. from torch._dynamo.source import AttrSource, GetItemSource
  12. from .. import graph_break_hints, variables
  13. from ..exc import raise_observed_exception, unimplemented_v2
  14. from ..utils import cmp_name_to_op_mapping, common_constant_types, istype, np
  15. from .base import VariableTracker
  16. if TYPE_CHECKING:
  17. from torch._dynamo.symbolic_convert import InstructionTranslator
  18. class ConstantVariable(VariableTracker):
  19. """
  20. Variable tracker for Python literals and basic immutable types, with automatic
  21. routing support for collection types (lists, tuples, sets, etc.).
  22. The create() method intelligently constructs appropriate variable types for
  23. nested collections.
  24. """
  25. @staticmethod
  26. def create(value, **kwargs) -> VariableTracker:
  27. """
  28. Create a `ConstantVariable` based on the given value, and supports
  29. automatic routing for collection types like `tuple` (in which case we'd
  30. create `ConstantVariable` for the leaf items).
  31. NOTE: the caller must install the proper guards if needed; most often
  32. the guard will be `CONSTANT_MATCH`.
  33. """
  34. source = kwargs.get("source", None)
  35. # Routing for supported collection literals.
  36. if isinstance(value, set):
  37. items = [ConstantVariable.create(x) for x in value]
  38. return variables.SetVariable(items, **kwargs)
  39. elif isinstance(value, frozenset):
  40. items = [ConstantVariable.create(x) for x in value]
  41. return variables.FrozensetVariable(items, **kwargs)
  42. elif isinstance(value, (list, tuple)):
  43. items = []
  44. for i, x in enumerate(value):
  45. item_source = GetItemSource(source, i) if source else None
  46. items.append(
  47. ConstantVariable.create(
  48. x,
  49. source=item_source,
  50. )
  51. )
  52. return variables.BaseListVariable.cls_for(type(value))(items, **kwargs)
  53. return ConstantVariable(value, **kwargs)
  54. def __init__(self, value, **kwargs) -> None:
  55. super().__init__(**kwargs)
  56. assert ConstantVariable.is_base_literal(value), f"""
  57. Cannot construct `ConstantVariable` for value of type {type(value)}.
  58. This failure likely due to PyTorch-internal use of `ConstantVariable` on
  59. non-literal python values, please try using `VariableTracker.build` instead. If
  60. you believe it's a necessary and legitimate use case (the value is immutable and
  61. can't easily be represented with another `VariableTracker` class), please add
  62. its type to `common_constant_types`.
  63. """
  64. if np is not None and isinstance(value, np.number):
  65. self.value = value.item()
  66. else:
  67. self.value = value
  68. def as_proxy(self):
  69. return self.value
  70. def __repr__(self) -> str:
  71. return f"ConstantVariable({type(self.value).__name__}: {repr(self.value)})"
  72. def as_python_constant(self):
  73. return self.value
  74. def is_python_constant(self):
  75. return True
  76. @property
  77. def items(self):
  78. """
  79. Need this when adding a BaseListVariable and a ConstantVariable together.
  80. Happens in detectron2.
  81. """
  82. return self.unpack_var_sequence(tx=None)
  83. def getitem_const(self, tx: "InstructionTranslator", arg: VariableTracker):
  84. return ConstantVariable.create(
  85. self.value[arg.as_python_constant()],
  86. )
  87. @staticmethod
  88. def is_base_literal(obj):
  89. return type(obj) in common_constant_types
  90. @staticmethod
  91. def is_literal(obj):
  92. if type(obj) in (list, tuple, set, frozenset, torch.Size):
  93. return all(ConstantVariable.is_literal(x) for x in obj)
  94. return ConstantVariable.is_base_literal(obj)
  95. def unpack_var_sequence(self, tx):
  96. try:
  97. return [ConstantVariable.create(x) for x in self.as_python_constant()]
  98. except TypeError as e:
  99. raise NotImplementedError from e
  100. def const_getattr(self, tx: "InstructionTranslator", name):
  101. if not hasattr(self.value, name):
  102. raise_observed_exception(AttributeError, tx, args=[name])
  103. member = getattr(self.value, name)
  104. if callable(member):
  105. raise NotImplementedError
  106. return member
  107. def call_method(
  108. self,
  109. tx: "InstructionTranslator",
  110. name,
  111. args: "list[VariableTracker]",
  112. kwargs: "dict[str, VariableTracker]",
  113. ) -> "VariableTracker":
  114. from .tensor import SymNodeVariable
  115. if name == "format" and istype(self.value, str):
  116. return variables.BuiltinVariable(str.format).call_function(
  117. tx, [self, *args], kwargs
  118. )
  119. elif name == "join" and istype(self.value, str):
  120. assert len(args) == 1 and len(kwargs) == 0
  121. arg_unpacked = args[0].force_unpack_var_sequence(tx)
  122. try:
  123. arg_const = [x.as_python_constant() for x in arg_unpacked]
  124. return ConstantVariable.create(self.value.join(arg_const))
  125. except NotImplementedError:
  126. return super().call_method(tx, name, args, kwargs)
  127. if any(isinstance(x, SymNodeVariable) for x in args):
  128. # Promote to SymNodeVariable for operations involving dynamic shapes.
  129. return variables.SymNodeVariable(self.as_proxy(), self.value).call_method(
  130. tx, name, args, kwargs
  131. )
  132. try:
  133. const_args = [a.as_python_constant() for a in args]
  134. const_kwargs = {k: v.as_python_constant() for k, v in kwargs.items()}
  135. except NotImplementedError:
  136. return super().call_method(tx, name, args, kwargs)
  137. if isinstance(self.value, str) and name in str.__dict__.keys():
  138. method = getattr(self.value, name)
  139. try:
  140. return ConstantVariable.create(method(*const_args, **const_kwargs))
  141. except Exception as e:
  142. raise_observed_exception(type(e), tx)
  143. elif isinstance(self.value, (float, int)):
  144. if not (args or kwargs):
  145. try:
  146. return ConstantVariable.create(getattr(self.value, name)())
  147. except (OverflowError, ValueError) as exc:
  148. raise_observed_exception(
  149. type(exc),
  150. tx,
  151. args=list(map(ConstantVariable.create, exc.args)),
  152. )
  153. if (
  154. hasattr(operator, name)
  155. and len(args) == 1
  156. and args[0].is_python_constant()
  157. ):
  158. add_target = const_args[0]
  159. op = getattr(operator, name)
  160. if isinstance(
  161. add_target, (torch.SymBool, torch.SymFloat, torch.SymInt)
  162. ):
  163. # Addition between a non sym and sym makes a sym
  164. proxy = tx.output.create_proxy(
  165. "call_function", op, (self.value, add_target), {}
  166. )
  167. return SymNodeVariable.create(tx, proxy, add_target)
  168. else:
  169. try:
  170. return ConstantVariable.create(op(self.value, add_target))
  171. except Exception as e:
  172. raise_observed_exception(
  173. type(e), tx, args=list(map(ConstantVariable.create, e.args))
  174. )
  175. elif isinstance(self.value, bytes) and name == "decode":
  176. method = getattr(self.value, name)
  177. return ConstantVariable.create(method(*const_args, **const_kwargs))
  178. elif type(self.value) is complex and name in complex.__dict__.keys():
  179. method = getattr(self.value, name)
  180. try:
  181. return ConstantVariable.create(method(*const_args, **const_kwargs))
  182. except Exception as e:
  183. raise_observed_exception(type(e), tx)
  184. if name == "__len__" and not (args or kwargs):
  185. return ConstantVariable.create(len(self.value))
  186. elif name == "__round__" and len(args) == 1 and args[0].is_python_constant():
  187. try:
  188. return ConstantVariable.create(
  189. round(self.value, args[0].as_python_constant())
  190. )
  191. except Exception as e:
  192. raise_observed_exception(
  193. type(e), tx, args=list(map(ConstantVariable.create, e.args))
  194. )
  195. elif name == "__contains__" and len(args) == 1 and args[0].is_python_constant():
  196. assert not kwargs
  197. search = args[0].as_python_constant()
  198. try:
  199. result = search in self.value
  200. return ConstantVariable.create(result)
  201. except TypeError as e:
  202. raise_observed_exception(
  203. type(e), tx, args=list(map(ConstantVariable.create, e.args))
  204. )
  205. return super().call_method(tx, name, args, kwargs)
  206. def call_obj_hasattr(
  207. self, tx: "InstructionTranslator", name: str
  208. ) -> "VariableTracker":
  209. result = hasattr(self.value, name)
  210. return variables.ConstantVariable.create(result)
  211. class EnumVariable(VariableTracker):
  212. """VariableTracker for enum.Enum and enum.IntEnum instances
  213. Provides specialized handling for Python enum types, supporting
  214. both standard Enum and IntEnum with proper value tracking and comparison.
  215. """
  216. def __init__(self, value, **kwargs) -> None:
  217. super().__init__(**kwargs)
  218. self.value = value
  219. @classmethod
  220. def create(cls, cls_type, value_vt, options):
  221. if isinstance(value_vt, variables.ConstantVariable):
  222. for member in list(cls_type):
  223. if member.value == value_vt.as_python_constant():
  224. return cls(member, **options)
  225. unimplemented_v2(
  226. gb_type="Failed to construct Enum variable",
  227. context=f"value: {value_vt}, allowed enum values: {list(cls_type)}",
  228. explanation="Attempted to construct an Enum value that is non-constant (e.g. int, string) "
  229. "or is not an acceptable value for the Enum. "
  230. f"Acceptable values for Enum `{cls_type}`: {list(cls_type)}.",
  231. hints=[*graph_break_hints.USER_ERROR, *graph_break_hints.SUPPORTABLE],
  232. )
  233. def as_proxy(self):
  234. if isinstance(self.value, int):
  235. return int(self.value) # convert IntEnum to a normal int
  236. return self.value
  237. def __repr__(self) -> str:
  238. return f"EnumVariable({type(self.value)})"
  239. def as_python_constant(self):
  240. return self.value
  241. def var_getattr(self, tx: "InstructionTranslator", name):
  242. if not hasattr(self.value, name):
  243. raise NotImplementedError
  244. if name in cmp_name_to_op_mapping:
  245. return variables.GetAttrVariable(self, name)
  246. member = getattr(self.value, name)
  247. source = self.source and AttrSource(self.source, name)
  248. return VariableTracker.build(tx, member, source=source)