base.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. # mypy: ignore-errors
  2. """
  3. Core variable tracking functionality for Dynamo. This module defines the fundamental
  4. classes and systems used to track and manage variables during Dynamo's operation.
  5. The module provides:
  6. 1. VariableTracker - The base class for tracking variables during compilation
  7. 2. MutationType system - Classes for tracking and managing mutations to variables
  8. 3. Source type management - Utilities for tracking variable origins and scope
  9. 4. Variable state management - Tools for managing variable state and transformations
  10. These components form the foundation of Dynamo's variable handling system,
  11. enabling accurate tracking and transformation of Python code into optimized
  12. computations.
  13. """
  14. import collections
  15. from collections.abc import ItemsView, KeysView, Sequence, ValuesView
  16. from enum import Enum
  17. from typing import Any, Callable, Optional, TYPE_CHECKING
  18. from .. import graph_break_hints, variables
  19. from ..current_scope_id import current_scope_id
  20. from ..exc import raise_observed_exception, unimplemented_v2
  21. from ..guards import GuardBuilder, install_guard
  22. from ..source import AttrSource, Source
  23. from ..utils import cmp_name_to_op_mapping, istype
  24. if TYPE_CHECKING:
  25. from ..codegen import PyCodegen
  26. from ..symbolic_convert import InstructionTranslator, InstructionTranslatorBase
  27. class SourceType(Enum):
  28. """
  29. This Enum divides VariableTracker into 2 cases, depending on the variable
  30. it represents:
  31. - already existed that Dynamo began tracking while introspection (Existing)
  32. - is a new variable that is created during Dynamo introspection (New)
  33. In general, we have these invariants:
  34. 1. for `VariableTracker` associated with `Existing`, its `source` field must not be None.
  35. 2. for `VariableTracker` associated with `New`, most of the time its
  36. `source` field is None, except for cases like side effect codegen for
  37. `AttributeMutationNew`, during which we generate a
  38. `LocalSource('tmp...')` for such variable, to facilitate codegen.
  39. """
  40. Existing = 0
  41. New = 1
  42. class MutationType:
  43. """
  44. Base class for Variable.mutation_type. It encodes information about
  45. 1. The type of mutation Dynamo allows on the variable.
  46. 2. Whether the value represented by this variable already existed before
  47. Dynamo tracing.
  48. """
  49. def __init__(self, typ: SourceType) -> None:
  50. # In HigherOrderOperator tracing, we need to distinguish
  51. # between MutationTypes inside the HigherOrderOperator and
  52. # ones outside it. For example, it is not safe to mutate
  53. # `a` in the following example because it was constructed
  54. # in a different scope.
  55. #
  56. # def f(x):
  57. # a = 1
  58. # def g(x):
  59. # nonlocal a
  60. # a = 2
  61. # return x
  62. # return wrap(g, x) + a
  63. #
  64. # We use self.scope to distinguish this.
  65. # scope == 0: The object was an existing variable
  66. # scope == 1: The object was created while Dynamo
  67. # was introspecting a function
  68. # (and no HigherOrderOps were involved)
  69. # scope >= 2: The object was created through
  70. # Dynamo introspection of a HigherOrderOp.
  71. # The exact number corresponds to the level
  72. # of nested HigherOrderOps.
  73. if typ is SourceType.Existing:
  74. self.scope = 0
  75. elif typ is SourceType.New:
  76. self.scope = current_scope_id()
  77. else:
  78. unimplemented_v2(
  79. gb_type="Unsupported SourceType",
  80. context=f"MutationType.__init__ {self} {typ}",
  81. explanation=f"Dynamo does not support the type `{typ}`",
  82. hints=[
  83. "This branch is not supposed to be reachable.",
  84. *graph_break_hints.DYNAMO_BUG,
  85. ],
  86. )
  87. class ValueMutationNew(MutationType):
  88. """
  89. This case of VariableTracker.mutation_type marker indicates
  90. 1. Dynamo allows mutation on the value itself (rather than its attributes).
  91. 2. The value is created by the bytecode Dynamo is tracing through.
  92. For instance, Dynamo could model a newly created list with this marker,
  93. indicating that while we need to model mutations to this list, we don't have
  94. to emit bytecode for these mutations if the list doesn't escape into the
  95. Python world.
  96. """
  97. def __init__(self) -> None:
  98. super().__init__(SourceType.New)
  99. def __hash__(self):
  100. return id(self)
  101. def __eq__(self, other):
  102. return self is other
  103. class ValueMutationExisting(MutationType):
  104. """
  105. This case of VariableTracker.mutation_type marker indicates
  106. 1. Dynamo allows mutation on the value itself (rather than its attributes).
  107. 2. The value exists before Dynamo tracing started.
  108. For instance, Dynamo could model a pre-existing list with this marker,
  109. indicating that if we encounter mutations to this list, we need to buffer
  110. and re-apply those mutations after the graph runs, since the list might be
  111. used afterwards in Python.
  112. """
  113. # A flag to indicate whether mutation happened on the associated
  114. # `VariableTracker`. This enables SideEffects to accurately and quickly
  115. # filter out which pre-existing values it needs to generate mutation for.
  116. is_modified: bool
  117. def __init__(self, is_modified: bool = False):
  118. super().__init__(SourceType.Existing)
  119. self.is_modified = is_modified
  120. class AttributeMutation(MutationType):
  121. """
  122. This case of VariableTracker.mutation_type marker indicates that Dynamo
  123. allows mutation on the value's attributes.
  124. """
  125. def __init__(self, typ: SourceType):
  126. super().__init__(typ)
  127. class AttributeMutationExisting(AttributeMutation):
  128. """
  129. This case of VariableTracker.mutation_type marker indicates
  130. 1. Dynamo allows mutation on the value's attributes.
  131. 2. The value exists before Dynamo tracing started.
  132. For instance, Dynamo could model a pre-existing object with this marker,
  133. indicating that if we encounter mutations to this object, we need to buffer
  134. then re-apply those mutations after the graph runs, since the object might
  135. be used afterwards in Python.
  136. """
  137. def __init__(self):
  138. super().__init__(SourceType.Existing)
  139. class AttributeMutationNew(AttributeMutation):
  140. """
  141. This case of VariableTracker.mutation_type marker indicates
  142. 1. Dynamo allows mutation on the value's attributes.
  143. 2. The value is created by the bytecode Dynamo is tracing through.
  144. For instance, Dynamo could model a newly created object with this marker,
  145. indicating that while we need to model mutations to this object, we don't
  146. have to emit bytecode for these mutations if the object doesn't escape into
  147. the Python world.
  148. """
  149. def __init__(self, cls_source: Optional[Source] = None):
  150. super().__init__(SourceType.New)
  151. self.cls_source = cls_source
  152. def _is_top_level_scope(scope_id):
  153. return scope_id == 1
  154. def is_side_effect_safe(m: MutationType):
  155. scope_id = current_scope_id()
  156. # In the top-level scope (if no HigherOrderOperators are involved),
  157. # we are allowed to modify variables created in this scope as well
  158. # as existing variables.
  159. if _is_top_level_scope(scope_id):
  160. return True
  161. # Otherwise, only allow local mutation of variables created in the current scope
  162. return m.scope == scope_id
  163. # This helps users of `as_python_constant` to catch unimplemented error with
  164. # more information; it inherits `NotImplementedError` for backward
  165. # compatibility reasons.
  166. class AsPythonConstantNotImplementedError(NotImplementedError):
  167. vt: "VariableTracker"
  168. def __init__(self, vt: "VariableTracker"):
  169. super().__init__(f"{vt} is not a constant")
  170. self.vt = vt
  171. class VariableTrackerMeta(type):
  172. all_subclasses = []
  173. def __instancecheck__(cls, instance) -> bool:
  174. """Make isinstance work with LazyVariableTracker"""
  175. # This is super expensive - just having it costs over 4% of tracing
  176. # time!
  177. if (type(instance) is variables.LazyVariableTracker) and (
  178. cls not in (VariableTracker, variables.LazyVariableTracker)
  179. ):
  180. instance = instance.realize()
  181. return type.__instancecheck__(cls, instance)
  182. def __init__(cls, name, bases, attrs) -> None:
  183. super().__init__(name, bases, attrs)
  184. VariableTrackerMeta.all_subclasses.append(cls)
  185. class VariableTracker(metaclass=VariableTrackerMeta):
  186. """
  187. Base class for tracked locals and stack values
  188. VariableTracker instances are immutable and should be copied in
  189. order to change them.
  190. Prefer the factory function VariableTracker.build() over VariableTracker.__init__().
  191. """
  192. # fields to leave unmodified in apply()
  193. _nonvar_fields = {
  194. "value",
  195. "guards",
  196. "source",
  197. "mutation_type",
  198. "parents_tracker",
  199. "user_code_variable_name",
  200. }
  201. def clone(self, **kwargs):
  202. """Shallow copy with some (optional) changes"""
  203. args = dict(self.__dict__)
  204. args.update(kwargs)
  205. return self.__class__(**args)
  206. @classmethod
  207. def visit(
  208. cls,
  209. fn: Callable[["VariableTracker"], None],
  210. value: Any,
  211. cache: Optional[dict[int, Any]] = None,
  212. ) -> None:
  213. """
  214. Walk value and call fn on all the VariableTracker instances
  215. """
  216. if cache is None:
  217. cache = {}
  218. idx = id(value)
  219. if idx in cache:
  220. return
  221. # save `value` to keep it alive and ensure id() isn't reused
  222. cache[idx] = value
  223. if isinstance(value, VariableTracker):
  224. value = value.unwrap()
  225. fn(value)
  226. value = value.unwrap() # calling fn() might have realized it
  227. nonvars = value._nonvar_fields
  228. for key, subvalue in value.__dict__.items():
  229. if key not in nonvars:
  230. cls.visit(fn, subvalue, cache)
  231. elif istype(value, (list, tuple)):
  232. for subvalue in value:
  233. cls.visit(fn, subvalue, cache)
  234. elif istype(value, (dict, collections.OrderedDict)):
  235. for subvalue in value.values():
  236. cls.visit(fn, subvalue, cache)
  237. def __repr__(self) -> str:
  238. return f"{self.__class__.__name__}()"
  239. def debug_repr(self):
  240. # Intended to be overridden to provide more info
  241. try:
  242. return repr(self.as_python_constant())
  243. except NotImplementedError:
  244. return repr(self)
  245. def python_type(self):
  246. """
  247. Abstract method to be implemented by subclasses of VariableTracker.
  248. This method should return the type represented by the instance of the subclass.
  249. The purpose is to provide a standardized way to retrieve the Python type information
  250. of the variable being tracked.
  251. Returns:
  252. type: The Python type (such as int, str, list, etc.) of the variable tracked by
  253. the subclass. If the type cannot be determined or is not relevant,
  254. leaving it undefined or invoking super() is always sound.
  255. Note:
  256. This is an abstract method and may be overridden in subclasses.
  257. Example:
  258. class SetVariable(VariableTracker):
  259. def python_type(self):
  260. return set
  261. Raises:
  262. NotImplementedError: If the method is not implemented in a subclass.
  263. """
  264. try:
  265. return type(self.as_python_constant())
  266. except NotImplementedError:
  267. raise NotImplementedError(f"{self} has no type") from None
  268. def python_type_name(self):
  269. try:
  270. return self.python_type().__name__
  271. except NotImplementedError:
  272. return "<unknown type>"
  273. def as_python_constant(self):
  274. """For constants"""
  275. raise AsPythonConstantNotImplementedError(self)
  276. def guard_as_python_constant(self):
  277. """Similar to as_python_constant(), but add ID_MATCH guards to try to force things to become constants"""
  278. try:
  279. return self.as_python_constant()
  280. except NotImplementedError:
  281. unimplemented_v2(
  282. gb_type="Not a Python constant",
  283. context=f"guard_as_python_constant {self}",
  284. explanation=f"Failed to convert {self} into a Python constant.",
  285. hints=[],
  286. )
  287. def is_python_constant(self):
  288. try:
  289. self.as_python_constant()
  290. return True
  291. except NotImplementedError:
  292. return False
  293. def make_guard(self, fn):
  294. if self.source:
  295. return self.source.make_guard(fn)
  296. raise NotImplementedError
  297. def const_getattr(self, tx: "InstructionTranslator", name: str) -> Any:
  298. """getattr(self, name) returning a python constant"""
  299. raise NotImplementedError
  300. def var_getattr(self, tx: "InstructionTranslator", name: str) -> "VariableTracker":
  301. """getattr(self, name) returning a new variable"""
  302. value = self.const_getattr(tx, name)
  303. if not variables.ConstantVariable.is_literal(value):
  304. raise NotImplementedError
  305. source = self.source and AttrSource(self.source, name)
  306. if source and not isinstance(self, variables.ConstantVariable):
  307. # The second condition is to avoid guards on const getattr objects
  308. # like __code__.co_argcount
  309. install_guard(source.make_guard(GuardBuilder.CONSTANT_MATCH))
  310. return variables.ConstantVariable.create(value, source=source)
  311. def is_proxy(self):
  312. try:
  313. self.as_proxy()
  314. return True
  315. except NotImplementedError:
  316. return False
  317. def as_proxy(self):
  318. raise NotImplementedError(str(self))
  319. def maybe_fx_node(self):
  320. try:
  321. proxy = self.as_proxy()
  322. import torch.fx
  323. if isinstance(proxy, torch.fx.Proxy):
  324. return proxy.node
  325. return None
  326. except NotImplementedError:
  327. return None
  328. def reconstruct(self, codegen: "PyCodegen"):
  329. raise NotImplementedError
  330. def unpack_var_sequence(self, tx) -> list["VariableTracker"]:
  331. raise NotImplementedError
  332. def force_unpack_var_sequence(self, tx) -> list["VariableTracker"]:
  333. # like unpack_var_sequence, but should only be used when it is
  334. # safe to eagerly (vs. lazily) unpack this variable.
  335. # e.g. map(f, x) is normally evaluated lazily but sometimes
  336. # we want to force eager unpacking, e.g. when converting to a list.
  337. # NOTE: this method is allowed to mutate the VariableTracker, so
  338. # it should only be called once.
  339. return self.unpack_var_sequence(tx)
  340. def has_unpack_var_sequence(self, tx) -> bool:
  341. try:
  342. self.unpack_var_sequence(tx)
  343. return True
  344. except NotImplementedError:
  345. return False
  346. # NB: don't call force_unpack_var_sequence, especially if it mutates!
  347. def has_force_unpack_var_sequence(self, tx) -> bool:
  348. return self.has_unpack_var_sequence(tx)
  349. # Forces unpacking the var sequence while also applying a function to each element.
  350. # Only use when it is safe to eagerly unpack this variable (like force_unpack_var_sequence).
  351. # INVARIANT: variable must satisfy has_force_unpack_var_sequence() == True!
  352. def force_apply_to_var_sequence(self, tx, fn) -> None:
  353. assert self.has_force_unpack_var_sequence(tx)
  354. for v in self.unpack_var_sequence(tx):
  355. fn(v)
  356. def inspect_parameter_names(self) -> list[str]:
  357. unimplemented_v2(
  358. gb_type="Unsupported inspect call",
  359. context=f"inspect_parameter_names {self}",
  360. explanation=f"Dynamo does not know how to trace the function `{self.debug_repr()}`",
  361. hints=[],
  362. )
  363. def call_obj_hasattr(
  364. self, tx: "InstructionTranslator", name: str
  365. ) -> "VariableTracker":
  366. unimplemented_v2(
  367. gb_type="Unsupported hasattr call",
  368. context=f"call_obj_hasattr {self} {name}",
  369. explanation=f"Dynamo does not know how to trace the function `{self.debug_repr()}`",
  370. hints=[
  371. f"Avoid calling `hasattr({self.__class__.__name__}, {name})` in your code.",
  372. *graph_break_hints.SUPPORTABLE,
  373. ],
  374. )
  375. def call_function(
  376. self,
  377. tx: "InstructionTranslator",
  378. args: Sequence["VariableTracker"],
  379. kwargs: "dict[str, VariableTracker]",
  380. ) -> "VariableTracker":
  381. unimplemented_v2(
  382. gb_type="Unsupported function call",
  383. context=f"call_function {self} {args} {kwargs}",
  384. explanation=f"Dynamo does not know how to trace the function `{self.debug_repr()}`",
  385. hints=[
  386. f"Avoid calling `{self.debug_repr()}` in your code.",
  387. "Please report an issue to PyTorch.",
  388. ],
  389. )
  390. def call_method(
  391. self,
  392. tx,
  393. name,
  394. args: "list[VariableTracker]",
  395. kwargs: "dict[str, VariableTracker]",
  396. ) -> "VariableTracker":
  397. if name == "__len__" and self.has_unpack_var_sequence(tx):
  398. assert not (args or kwargs)
  399. return variables.ConstantVariable.create(len(self.unpack_var_sequence(tx)))
  400. elif (
  401. name == "__getattr__"
  402. and len(args) == 1
  403. and args[0].is_python_constant()
  404. and not kwargs
  405. ):
  406. return self.var_getattr(tx, args[0].as_python_constant())
  407. elif name in cmp_name_to_op_mapping and len(args) == 1 and not kwargs:
  408. other = args[0]
  409. if not isinstance(self, type(other)) and not (
  410. isinstance(self, variables.GetAttrVariable)
  411. or isinstance(other, variables.GetAttrVariable)
  412. ):
  413. # NB: GetAttrVariable is a special case because sometimes an
  414. # object can map to GetAttrVariable but other time as
  415. # SkipFunctionVariable if it is an input to the compiled
  416. # function, e.g. tensor.data_ptr
  417. return variables.ConstantVariable.create(NotImplemented)
  418. # NB : Checking for mutation is necessary because we compare
  419. # constant values
  420. if (
  421. not self.is_python_constant()
  422. or not other.is_python_constant()
  423. or tx.output.side_effects.has_pending_mutation(self)
  424. or tx.output.side_effects.has_pending_mutation(other)
  425. ):
  426. unimplemented_v2(
  427. gb_type="Builtin `operator.*` comparison with constant `self` failed",
  428. context=f"call_method {self} {name} {args} {kwargs}",
  429. explanation=f"Failed to compare {self} with {other}, "
  430. + f"because {other} is not a Python constant or its mutation check fails.",
  431. hints=[],
  432. )
  433. try:
  434. return variables.ConstantVariable.create(
  435. cmp_name_to_op_mapping[name](
  436. self.as_python_constant(), other.as_python_constant()
  437. )
  438. )
  439. except Exception as e:
  440. raise_observed_exception(
  441. type(e),
  442. tx,
  443. args=[list(map(variables.ConstantVariable.create, e.args))],
  444. )
  445. hints = [
  446. f"Avoid calling `{self.python_type_name()}.{name}` in your code.",
  447. "Please report an issue to PyTorch.",
  448. ]
  449. # additional hint for method calls on improperly constructed iterators
  450. if isinstance(self, variables.UserDefinedObjectVariable) and name in (
  451. "__iter__",
  452. "__next__",
  453. ):
  454. if isinstance(self.value, (KeysView, ItemsView, ValuesView)):
  455. hints.append(
  456. "Consider moving the creation of dict view object (e.g. `dict.keys()`, `dict.items()`,) "
  457. "to the compiled region, instead of passing it as an input to the compiled region."
  458. )
  459. hints.append(
  460. "Dynamo does not fully support tracing builtin iterators (e.g. `map`, `zip`, `enumerate`) "
  461. "passed in from uncompiled to compiled regions (e.g. `torch.compile(fn)(enumerate(...))`). "
  462. "This can happen unintentionally if a previous graph break happens with a builtin iterator "
  463. "in the local scope."
  464. )
  465. hints.append(
  466. "List/dict comprehensions in Python <= 3.11 result in implicit function calls, which Dynamo "
  467. "cannot trace as a top level frame. Possible workarounds are (1) use a loop instead of a comprehension, "
  468. "(2) fix any graph breaks in the function above the comprehension, (3) wrap the comprehension in a "
  469. "function, or (4) use Python 3.12+."
  470. )
  471. unimplemented_v2(
  472. gb_type="Unsupported method call",
  473. context=f"call_method {self} {name} {args} {kwargs}",
  474. explanation=f"Dynamo does not know how to trace method `{name}` of class `{self.python_type_name()}`",
  475. hints=hints,
  476. )
  477. def set_name_hint(self, name):
  478. pass
  479. def realize(self) -> "VariableTracker":
  480. """Used by LazyVariableTracker to build the real VariableTracker"""
  481. return self
  482. def unwrap(self) -> "VariableTracker":
  483. """Used by LazyVariableTracker to return the real VariableTracker if it already exists"""
  484. return self
  485. def is_realized(self):
  486. """Used by LazyVariableTracker to indicate an unrealized node"""
  487. return True
  488. def next_variable(self, tx):
  489. unimplemented_v2(
  490. gb_type="Unsupported next() call",
  491. context=f"next({self})",
  492. explanation=f"Dynamo does not know how to trace calling `next()` on variable `{self}`.",
  493. hints=[*graph_break_hints.USER_ERROR],
  494. )
  495. def is_strict_mode(self, tx):
  496. return tx.strict_checks_fn and tx.strict_checks_fn(self)
  497. def is_mutable(self):
  498. """Whether Dynamo allows mutation on this variable."""
  499. return not self.is_immutable()
  500. def is_immutable(self):
  501. """Whether Dynamo bans mutation on this variable."""
  502. return self.mutation_type is None
  503. @staticmethod
  504. def build(
  505. tx: "InstructionTranslatorBase",
  506. value: Any,
  507. source: Optional[Source] = None,
  508. ) -> Any:
  509. """Create a new VariableTracker from a value and optional Source"""
  510. if source is None:
  511. return builder.SourcelessBuilder.create(tx, value)
  512. else:
  513. return variables.LazyVariableTracker.create(value, source)
  514. def __init__(
  515. self,
  516. *,
  517. source: Source = None,
  518. mutation_type: MutationType = None,
  519. ) -> None:
  520. super().__init__()
  521. self.source = source
  522. self.mutation_type = mutation_type
  523. # NOTE sometimes mutation_type is set afterwards for implementation
  524. # convenience, we don't validate those cases at the moment.
  525. if mutation_type is not None:
  526. if isinstance(mutation_type, (ValueMutationNew, AttributeMutationNew)):
  527. # If this fails, it's either
  528. # 1. one mistakenly passed in a source
  529. # 2. `mutation_type` is incorrect
  530. assert source is None
  531. else:
  532. assert isinstance(
  533. mutation_type, (ValueMutationExisting, AttributeMutationExisting)
  534. )
  535. # If this fails, it's either
  536. # 1. one forgot to pass in a source
  537. # 2. `mutation_type` is incorrect
  538. assert source is not None
  539. def typestr(*objs):
  540. if len(objs) == 1:
  541. (obj,) = objs
  542. if isinstance(obj, VariableTracker):
  543. return str(obj)
  544. else:
  545. return type(obj).__name__
  546. else:
  547. return " ".join(map(typestr, objs))
  548. from . import builder