comptime.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. """
  2. This module provides the public comptime interface to TorchDynamo, enabling users to execute
  3. arbitrary Python code during symbolic evaluation of their programs.
  4. The comptime interface allows inspection and modification of TorchDynamo's compilation
  5. process while it is running. This can be useful for:
  6. - Debugging compilation issues
  7. - Inspecting intermediate state
  8. - Adding custom guards or graph breaks
  9. - Analyzing symbolic shapes and values
  10. Example usage:
  11. import torch
  12. from torch._dynamo.comptime import comptime
  13. def my_model(x):
  14. # Print the compile-time known information about x
  15. comptime.print(x)
  16. # Print the current FX graph being constructed
  17. comptime.print_graph()
  18. # Force a value to be treated as static
  19. if comptime(lambda ctx: ctx.get_local("x").is_dynamic()):
  20. comptime.force_static(x)
  21. # Add a manual graph break
  22. comptime.graph_break()
  23. Note: While this API provides significant flexibility, it intentionally avoids
  24. exposing internal implementation details of TorchDynamo to maintain compatibility
  25. across versions.
  26. """
  27. import builtins
  28. import dis
  29. import time
  30. import traceback
  31. from collections.abc import Callable, Sequence
  32. from typing import Any, Optional, TextIO, Union
  33. import torch
  34. from torch._dynamo.symbolic_convert import InstructionTranslatorBase
  35. from torch._dynamo.variables.base import VariableTracker
  36. from torch._subclasses.fake_tensor import FakeTensor
  37. from torch.fx.experimental.symbolic_shapes import free_symbols
  38. from .exc import unimplemented
  39. from .variables import CellVariable
  40. from .variables.tensor import SymNodeVariable
  41. class ComptimeVar:
  42. """
  43. A ComptimeVar represents a Python value, at some particular point
  44. in time, in the Python code we are symbolically evaluating with
  45. torchdynamo. This must be distinguished from a runtime value, as
  46. at compile-time there are some properties of the variable we
  47. do not know (for example, if the ComptimeVar represents a Tensor,
  48. we only know metadata about the tensor; we do NOT know what the
  49. actual data in the Tensor is.)
  50. """
  51. def __init__(self, v: VariableTracker) -> None:
  52. self.__variable = v
  53. def as_proxy(self) -> Union[VariableTracker, Sequence[VariableTracker]]:
  54. """
  55. Returns an fx.Proxy (or tuple/list of fx.Proxy) representing
  56. this variable in the FX graph we are assembling to pass
  57. to the user compiler.
  58. This method only works for variables we actually track in
  59. the FX graph, aka Tensors (and ints, if you are compiling
  60. with dynamic shapes). In particular, if you have a list
  61. or tuple of tensors, you will get a list/tuple of proxies
  62. (not a single proxy representing the entire list/tuple).
  63. """
  64. return self.__variable.as_proxy()
  65. def is_proxy(self) -> bool:
  66. """
  67. Returns True if as_proxy() would succeed.
  68. """
  69. return self.__variable.is_proxy()
  70. def as_fake(self) -> Union[FakeTensor, torch.SymInt]:
  71. """
  72. Returns a "fake" value (either a FakeTensor or a SymInt)
  73. representing the variable in question. This only works
  74. for variables that denote Tensor or int. You can use
  75. this to query metadata; e.g., v.as_fake().size(0) will
  76. tell you the compile-time known size of the tensor.
  77. WARNING: Do NOT mutate the returned tensor.
  78. """
  79. return self.__variable.as_proxy().node.meta["example_value"]
  80. def size(self, dim: Optional[int] = None) -> Union[int, torch.SymInt]:
  81. """
  82. Returns the size of the tensor (if dim is None) or the size
  83. at the dimension dim. The returned size may be a SymInt.
  84. """
  85. return self.as_fake().size(dim) # type: ignore[union-attr, return-value]
  86. def python_type(self) -> type:
  87. """
  88. Returns what type(v) would have returned for the variable
  89. at compile time.
  90. """
  91. return self.__variable.python_type()
  92. def as_python_constant(self) -> Any:
  93. """
  94. Returns the Python value this variable would have, but only if it is
  95. completely known at compile-time (e.g., it is constant).
  96. WARNING: Do NOT mutate the returned constant. The returned constant
  97. may or may not correspond to the actual value this variable may take
  98. on at runtime; for example, if the variable in question is a constant
  99. list, we may return a copy of that list.
  100. """
  101. return self.__variable.as_python_constant()
  102. def is_python_constant(self) -> bool:
  103. """
  104. Returns True if as_python_constant would succeed.
  105. """
  106. return self.__variable.is_python_constant()
  107. def is_dynamic(self) -> bool:
  108. if isinstance(self.__variable, SymNodeVariable):
  109. fs = free_symbols(self.__variable.sym_num)
  110. return bool(fs)
  111. return False
  112. def force_static(self) -> None:
  113. """
  114. Forces that a value is static, inducing a guard on its specific value
  115. """
  116. if isinstance(self.__variable, SymNodeVariable):
  117. self.__variable.evaluate_expr()
  118. elif self.__variable.is_python_constant():
  119. # TODO: Maybe complain if this isn't a int/bool/float variable
  120. pass
  121. else:
  122. raise AssertionError(
  123. f"cannot force {self.__variable} ({type(self.__variable)}) static"
  124. )
  125. def _i_will_not_complain_if_bc_breaks_VariableTracker(self) -> VariableTracker:
  126. """
  127. Returns the internal data structure VariableTracker that Dynamo uses
  128. to represent variables at compile time. There are no BC guarantees on
  129. this API and WE RESERVE THE RIGHT TO BREAK YOUR CODE if you rely on
  130. it.
  131. """
  132. return self.__variable
  133. def __repr__(self) -> str:
  134. return self.__variable.debug_repr()
  135. # TODO: API for adding a custom guard
  136. class ComptimeContext:
  137. """
  138. This context class provides access to a public API for Dynamo's internals.
  139. If there is something here you would find useful that is missing, please
  140. file a feature request at https://github.com/pytorch/pytorch/
  141. """
  142. def __init__(self, tx: InstructionTranslatorBase) -> None:
  143. self.__tx = tx
  144. def get_local(self, name: str, *, stacklevel: int = 0) -> ComptimeVar:
  145. """
  146. Retrieve the compile-time known information about a local.
  147. """
  148. tx = self.__get_tx(stacklevel)
  149. var = tx.symbolic_locals[name]
  150. # Auto-dereference when accessing cell locals in python.
  151. if isinstance(var, CellVariable):
  152. return ComptimeVar(tx.output.side_effects.load_cell(var))
  153. return ComptimeVar(var)
  154. def graph_break(self, msg: str = "ComptimeContext.graph_break") -> None:
  155. """
  156. Manually trigger a graph break
  157. """
  158. unimplemented(
  159. gb_type="ComptimeContext graph break",
  160. context=msg,
  161. explanation=f"Manually triggered ComptimeContext graph break with message {msg}.",
  162. hints=[],
  163. )
  164. def graph(self) -> torch.fx.Graph:
  165. """
  166. Retrieve the partially constructed FX graph that would be
  167. passed to the user compiler after compilation.
  168. """
  169. return self.__tx.output.graph
  170. def assert_static(self, val: ComptimeVar) -> None:
  171. """
  172. Asserts that the int is static (and not dynamic, per dynamic shapes)
  173. """
  174. assert not val.is_dynamic(), (
  175. "expected static but got dynamic (run with TORCH_LOGS=dynamic for more info)"
  176. )
  177. def print_graph(
  178. self, *, verbose: bool = True, file: Optional[TextIO] = None
  179. ) -> None:
  180. """
  181. Print the partially constructed FX graph that would be passed
  182. to the user compiler after compilation.
  183. """
  184. print(
  185. self.__tx.output.graph.python_code("self", verbose=verbose).src, file=file
  186. )
  187. def parent(self) -> "ComptimeContext":
  188. return ComptimeContext(self.__tx.parent) # type: ignore[arg-type]
  189. def __get_tx(self, stacklevel: int) -> Any:
  190. tx = self.__tx
  191. # pyrefly: ignore [bad-assignment]
  192. for _ in range(stacklevel):
  193. tx = tx.parent # type: ignore[assignment]
  194. return tx
  195. def print(self, val: Any, *, file: Optional[TextIO] = None) -> None:
  196. print(repr(val), file=file)
  197. def print_disas(
  198. self, *, file: Optional[TextIO] = None, stacklevel: int = 0
  199. ) -> None:
  200. """
  201. Print the current series of opcodes being executed (not including
  202. parent frames), including where you are in the particular opcode
  203. stream.
  204. """
  205. tx = self.__get_tx(stacklevel)
  206. print(
  207. dis.Bytecode(
  208. tx.f_code,
  209. current_offset=tx.instructions[tx.instruction_pointer].offset,
  210. ).dis(),
  211. file=file,
  212. )
  213. def print_value_stack(
  214. self, *, file: Optional[TextIO] = None, stacklevel: int = 0
  215. ) -> None:
  216. """
  217. Print the current Python value stack. Note that this is NOT the same
  218. as the traceback; use print_bt() to print that. Note that at
  219. stacklevel=0, this will typically be empty, as comptime cannot
  220. currently be used in an expression context where there would be
  221. intermediates on the stack. If you would find this useful, please
  222. file a bug at https://github.com/pytorch/pytorch/
  223. NB: Stack grows downwards in our print
  224. """
  225. tx = self.__get_tx(stacklevel)
  226. for s in tx.stack:
  227. print(f"- {s.debug_repr()}", file=file)
  228. def print_locals(
  229. self, *, file: Optional[TextIO] = None, stacklevel: int = 0
  230. ) -> None:
  231. """
  232. Print all of the locals available in the current context.
  233. By default this view is very limited; you can get more information
  234. about any individual local using get_local().
  235. """
  236. tx = self.__get_tx(stacklevel)
  237. for k, v in tx.symbolic_locals.items():
  238. print(f"{k} = {v.debug_repr()}", file=file)
  239. def print_bt(self, *, file: Optional[TextIO] = None, stacklevel: int = 0) -> None:
  240. """
  241. Print the user code backtrace, starting at the beginning of the
  242. frame Dynamo started evaluating. Note that this MAY NOT go all
  243. the way to the torch.compile invocation, as we may have done
  244. a graph break and are compiling an intermediate frame as the
  245. starting point. If you think the other behavior would be better,
  246. file a bug at https://github.com/pytorch/pytorch/
  247. """
  248. stack = []
  249. tx = self.__get_tx(stacklevel)
  250. while tx is not None:
  251. stack.append(tx.frame_summary())
  252. tx = getattr(tx, "parent", None)
  253. print(
  254. "".join(traceback.StackSummary.from_list(reversed(stack)).format()),
  255. file=file,
  256. )
  257. def print_guards(self, *, file: Optional[TextIO] = None) -> None:
  258. """
  259. Print the currently installed guards for the Dynamo context.
  260. This does NOT include guards associated with variables that
  261. may or may not be installed in the future if those variables
  262. are used.
  263. """
  264. # TODO: improve print format, current guard format is extremely
  265. # verbose
  266. print(
  267. "\n".join(f"{repr(guard)}" for guard in sorted(self.__tx.output.guards)),
  268. file=file,
  269. )
  270. def _i_will_not_complain_if_bc_breaks_InstructionTranslator(
  271. self,
  272. ) -> InstructionTranslatorBase:
  273. """
  274. Returns the internal data structure InstructionTranslator that Dynamo
  275. uses to track state of symbolic evaluation. There are no BC
  276. guarantees on this API and WE RESERVE THE RIGHT TO BREAK YOUR CODE if
  277. you rely on it.
  278. """
  279. return self.__tx
  280. def sleep(self, sec: Union[int, float]) -> None:
  281. time.sleep(sec)
  282. class _Comptime:
  283. @staticmethod
  284. def __call__(
  285. fn: Callable[[ComptimeContext], Any],
  286. fallback_fn: Callable[[], Any] = lambda: None,
  287. ) -> Any:
  288. """fn gets called at compile time in TorchDynamo, calls fallback_fn otherwise"""
  289. fallback_fn()
  290. # Convenience wrappers that are more compact to use
  291. @staticmethod
  292. def graph_break() -> None:
  293. comptime(lambda ctx: ctx.graph_break())
  294. @staticmethod
  295. def print(e: Any) -> None:
  296. comptime(lambda ctx: ctx.print(ctx.get_local("e")), lambda: print(e))
  297. @staticmethod
  298. def print_graph() -> None:
  299. comptime(lambda ctx: ctx.print_graph())
  300. @staticmethod
  301. def print_disas(*, stacklevel: int = 0) -> None:
  302. comptime(
  303. lambda ctx: ctx.print_disas(
  304. stacklevel=ctx.get_local("stacklevel").as_python_constant() + 1
  305. )
  306. )
  307. @staticmethod
  308. def print_value_stack(*, stacklevel: int = 0) -> None:
  309. comptime(
  310. lambda ctx: ctx.print_value_stack(
  311. stacklevel=ctx.get_local("stacklevel").as_python_constant() + 1
  312. )
  313. )
  314. # This is a more useful variant of print_value_stack that can be used
  315. # in an expression context; e.g., x + print_value_stack_and_return(y + z),
  316. # you will see x on the stack prior to the addition operation
  317. @staticmethod
  318. def print_value_stack_and_return(e: Any, *, stacklevel: int = 0) -> Any:
  319. comptime(
  320. lambda ctx: ctx.print_value_stack(
  321. stacklevel=ctx.get_local("stacklevel").as_python_constant() + 1
  322. )
  323. )
  324. return e
  325. @staticmethod
  326. def print_locals(*, stacklevel: int = 0) -> None:
  327. comptime(
  328. lambda ctx: ctx.print_locals(
  329. stacklevel=ctx.get_local("stacklevel").as_python_constant() + 1
  330. )
  331. )
  332. @staticmethod
  333. def print_bt(*, stacklevel: int = 0) -> None:
  334. comptime(
  335. lambda ctx: ctx.print_bt(
  336. stacklevel=ctx.get_local("stacklevel").as_python_constant() + 1
  337. )
  338. )
  339. @staticmethod
  340. def print_guards() -> None:
  341. comptime(lambda ctx: ctx.print_guards())
  342. @staticmethod
  343. def assert_static(val: Any) -> None:
  344. comptime(lambda ctx: ctx.assert_static(ctx.get_local("val")))
  345. @staticmethod
  346. def force_static(val: Any) -> None:
  347. comptime(lambda ctx: ctx.get_local("val").force_static())
  348. @staticmethod
  349. def breakpoint() -> None:
  350. """
  351. Like pdb breakpoint(), but drop into pdb whenever this line
  352. of code is compiled by dynamo. Use it by putting
  353. this in your model code::
  354. from torch._dynamo.comptime import comptime
  355. comptime.breakpoint()
  356. And then, inside pdb, you can access 'ctx' to query things
  357. about the compilation context::
  358. (Pdb) !ctx.print_bt()
  359. (Pdb) !ctx.print_locals()
  360. (Pdb) p ctx.get_local("attention").as_fake()
  361. """
  362. def inner(inner_ctx: ComptimeContext) -> None:
  363. ctx = inner_ctx.parent() # noqa: F841
  364. builtins.breakpoint()
  365. comptime(inner)
  366. @staticmethod
  367. def sleep(sec: Union[int, float]) -> None:
  368. comptime(lambda ctx: ctx.sleep(ctx.get_local("sec").as_python_constant()))
  369. comptime = _Comptime()