builtin.py 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349
  1. """
  2. Built-in function and type variable tracking for TorchDynamo's symbolic execution.
  3. This module contains variable tracker classes for Python built-in functions, types,
  4. and operations during graph compilation. It handles symbolic execution of:
  5. - Built-in functions (len, getattr, isinstance, etc.)
  6. - Type constructors (int, float, str, list, dict, etc.)
  7. - Built-in operators and methods
  8. - Special Python constructs (super, hasattr, etc.)
  9. Key classes:
  10. - BuiltinVariable: Tracks built-in functions and handles their execution
  11. - TypeVariable: Manages type constructor calls and type checking
  12. - SuperVariable: Handles super() calls in class hierarchies
  13. These variable trackers ensure that built-in Python operations are correctly
  14. handled during symbolic execution, either by executing them directly when safe
  15. or by creating appropriate graph nodes when needed.
  16. """
  17. import contextlib
  18. import functools
  19. import inspect
  20. import itertools
  21. import logging
  22. import math
  23. import operator
  24. import sys
  25. import types
  26. import typing
  27. import unittest
  28. from collections import defaultdict, OrderedDict
  29. from collections.abc import Callable, Iterable, KeysView, Sequence
  30. from typing import Any, cast, Literal, TYPE_CHECKING, Union
  31. import torch
  32. from torch import sym_float, sym_int
  33. from torch._subclasses.meta_utils import is_sparse_any
  34. from torch.overrides import BaseTorchFunctionMode
  35. from torch.utils._python_dispatch import is_traceable_wrapper_subclass
  36. from .. import config, graph_break_hints, polyfills, variables
  37. from ..exc import (
  38. ObservedAttributeError,
  39. ObservedUserStopIteration,
  40. raise_observed_exception,
  41. unimplemented,
  42. Unsupported,
  43. UserError,
  44. UserErrorType,
  45. )
  46. from ..guards import GuardBuilder, install_guard
  47. from ..replay_record import DummyModule
  48. from ..source import (
  49. AttrSource,
  50. GetItemSource,
  51. GlobalSource,
  52. is_constant_source,
  53. Source,
  54. TypeSource,
  55. )
  56. from ..utils import (
  57. check_constant_args,
  58. check_numpy_ndarray_args,
  59. check_unspec_or_constant_args,
  60. check_unspec_python_args,
  61. cmp_name_to_op_mapping,
  62. dict_methods,
  63. extract_fake_example_value,
  64. frozenset_methods,
  65. get_fake_value,
  66. guard_if_dyn,
  67. is_tensor_getset_descriptor,
  68. is_wrapper_or_member_descriptor,
  69. istype,
  70. numpy_operator_wrapper,
  71. proxy_args_kwargs,
  72. raise_args_mismatch,
  73. set_methods,
  74. str_methods,
  75. tensortype_to_dtype,
  76. )
  77. from .base import AsPythonConstantNotImplementedError, ValueMutationNew, VariableTracker
  78. from .constant import CONSTANT_VARIABLE_NONE, ConstantVariable, EnumVariable
  79. from .dicts import (
  80. ConstDictVariable,
  81. DefaultDictVariable,
  82. DictKeysVariable,
  83. DictViewVariable,
  84. FrozensetVariable,
  85. is_hashable,
  86. OrderedSetClassVariable,
  87. SetVariable,
  88. )
  89. from .lists import (
  90. BaseListVariable,
  91. ListIteratorVariable,
  92. ListVariable,
  93. RangeVariable,
  94. SizeVariable,
  95. TupleIteratorVariable,
  96. TupleVariable,
  97. )
  98. from .streams import EventVariable, StreamVariable
  99. from .tensor import (
  100. FakeItemVariable,
  101. supported_comparison_ops,
  102. SymNodeVariable,
  103. TensorVariable,
  104. UnspecializedPythonVariable,
  105. )
  106. from .user_defined import (
  107. MutableMappingVariable,
  108. UserDefinedDictVariable,
  109. UserDefinedObjectVariable,
  110. UserDefinedVariable,
  111. )
  112. if TYPE_CHECKING:
  113. # Cyclic dependency...
  114. from torch._dynamo.codegen import PyCodegen
  115. from torch._dynamo.symbolic_convert import InstructionTranslator
  116. log = logging.getLogger(__name__)
  117. IN_PLACE_DESUGARING_MAP = {
  118. operator.iadd: operator.add,
  119. operator.isub: operator.sub,
  120. operator.imul: operator.mul,
  121. operator.ifloordiv: operator.floordiv,
  122. operator.itruediv: operator.truediv,
  123. operator.imod: operator.mod,
  124. operator.imatmul: operator.imatmul,
  125. operator.ilshift: operator.lshift,
  126. operator.irshift: operator.rshift,
  127. operator.ipow: operator.pow,
  128. operator.iand: operator.and_,
  129. operator.ior: operator.or_,
  130. operator.ixor: operator.xor,
  131. }
  132. _HandlerCallback = Callable[
  133. ["InstructionTranslator", typing.Any, typing.Any], VariableTracker | None
  134. ]
  135. _TrackersType = Union[type[VariableTracker], tuple[type[VariableTracker], ...]]
  136. polyfill_fn_mapping = {
  137. operator.eq: polyfills.cmp_eq,
  138. operator.ne: polyfills.cmp_ne,
  139. operator.lt: polyfills.cmp_lt,
  140. operator.le: polyfills.cmp_le,
  141. operator.gt: polyfills.cmp_gt,
  142. operator.ge: polyfills.cmp_ge,
  143. }
  144. bin_ops = (
  145. operator.pow,
  146. operator.mul,
  147. operator.matmul,
  148. operator.floordiv,
  149. operator.truediv,
  150. operator.mod,
  151. operator.add,
  152. operator.lt,
  153. operator.gt,
  154. operator.ge,
  155. operator.le,
  156. operator.ne,
  157. operator.eq,
  158. operator.sub,
  159. operator.ipow,
  160. operator.imul,
  161. operator.imatmul,
  162. operator.ifloordiv,
  163. operator.itruediv,
  164. operator.imod,
  165. operator.iadd,
  166. operator.isub,
  167. )
  168. bin_int_ops = (
  169. operator.and_,
  170. operator.or_,
  171. operator.xor,
  172. operator.iand,
  173. operator.ixor,
  174. operator.ior,
  175. )
  176. un_int_ops = (operator.invert,)
  177. tensor_and_int_ops = (
  178. operator.lshift,
  179. operator.rshift,
  180. operator.ilshift,
  181. operator.irshift,
  182. operator.getitem,
  183. )
  184. un_ops = (
  185. operator.abs,
  186. operator.pos,
  187. operator.neg,
  188. operator.not_, # Note: this has a local scalar dense call
  189. operator.length_hint,
  190. )
  191. BUILTIN_TO_TENSOR_FN_MAP: dict[Callable[..., Any], Callable[..., Any]] = {}
  192. # These functions represent the r* versions of the above ops
  193. # Basically, if __add__(1, Tensor) is called, it is translated
  194. # to __radd__(Tensor, 1).
  195. # In the builtin var, we check if there is a tensor in the first args position,
  196. # if not, we swap the args and use the r* version of the op.
  197. BUILTIN_TO_TENSOR_RFN_MAP: dict[Callable[..., Any], Callable[..., Any]] = {}
  198. def populate_builtin_to_tensor_fn_map() -> None:
  199. global BUILTIN_TO_TENSOR_FN_MAP
  200. if len(BUILTIN_TO_TENSOR_FN_MAP) > 0:
  201. # Only populate once; after there are elements present no need to
  202. # repopulate
  203. return
  204. most_recent_func: Callable[..., Any] | None = None
  205. class GetMethodMode(BaseTorchFunctionMode):
  206. """
  207. Mode to extract the correct methods from torch function invocations
  208. (Used to get the correct torch.Tensor methods from builtins)
  209. """
  210. def __torch_function__(
  211. self,
  212. func: Callable[..., Any],
  213. types: Any,
  214. args: Sequence[Any] = (),
  215. kwargs: dict[str, Any] | None = None,
  216. ) -> Any:
  217. kwargs = kwargs or {}
  218. nonlocal most_recent_func
  219. most_recent_func = func
  220. return func(*args, **kwargs)
  221. inp0 = torch.ones(1)
  222. inp1 = torch.ones(1)
  223. inp0_int = torch.ones(1, dtype=torch.int32)
  224. inp1_int = torch.ones(1, dtype=torch.int32)
  225. with GetMethodMode():
  226. setups_and_oplists: list[tuple[Callable[..., Any], Iterable[Any]]] = [
  227. (lambda o: o(inp0), un_ops),
  228. (lambda o: o(inp0_int), un_int_ops),
  229. (lambda o: o(inp0, inp1), bin_ops),
  230. (lambda o: o(inp0_int, inp1_int), bin_int_ops),
  231. (lambda o: o(inp0_int, 0), tensor_and_int_ops),
  232. ]
  233. for setup_fn, op_list in setups_and_oplists:
  234. for op in op_list:
  235. setup_fn(op)
  236. assert most_recent_func is not None
  237. BUILTIN_TO_TENSOR_FN_MAP[op] = most_recent_func
  238. # gather the reverse functions
  239. rsetups_and_oplists: list[tuple[Callable[..., Any], Iterable[Any]]] = [
  240. (
  241. lambda o: o(1, inp1),
  242. bin_ops,
  243. ), # Get r* ops, (ex. __sub__(int, Tensor) -> __rsub__(Tensor, int))
  244. (lambda o: o(1, inp1_int), bin_int_ops),
  245. (lambda o: o(0, inp0_int), tensor_and_int_ops),
  246. ]
  247. rskips = {operator.matmul, operator.imatmul, operator.getitem}
  248. for setup_fn, op_list in rsetups_and_oplists:
  249. for op in op_list:
  250. if op in rskips:
  251. continue
  252. setup_fn(op)
  253. assert most_recent_func is not None
  254. if most_recent_func != BUILTIN_TO_TENSOR_FN_MAP[op]:
  255. BUILTIN_TO_TENSOR_RFN_MAP[op] = most_recent_func
  256. class BuiltinVariable(VariableTracker):
  257. """
  258. A VariableTracker that represents a built-in value (functions and operators).
  259. A lot of the code here assumes it will be a function object.
  260. The BuiltinVariable class wraps Python built-in functions (like len, isinstance, etc.)
  261. and operators (like +, -, *, etc.) to enable symbolic execution during tracing. This allows
  262. Dynamo to properly handle these operations when converting Python code to FX graphs while
  263. maintaining correct semantics and enabling optimizations.
  264. """
  265. _SENTINEL = object()
  266. _nonvar_fields = {
  267. "fn",
  268. *VariableTracker._nonvar_fields,
  269. }
  270. @classmethod
  271. def create_with_source(cls, value: Any, source: Source) -> "BuiltinVariable":
  272. install_guard(source.make_guard(GuardBuilder.BUILTIN_MATCH))
  273. return cls(value, source=source)
  274. @staticmethod
  275. @functools.cache
  276. def _constant_fold_functions() -> set[Callable[..., Any]]:
  277. fns: set[Callable[..., Any]] = {
  278. abs,
  279. all,
  280. any,
  281. bool,
  282. callable,
  283. chr,
  284. complex,
  285. divmod,
  286. float,
  287. getattr,
  288. int,
  289. len,
  290. max,
  291. min,
  292. ord,
  293. pow,
  294. repr,
  295. round,
  296. str,
  297. str.format,
  298. sum,
  299. type,
  300. operator.abs,
  301. operator.pos,
  302. operator.neg,
  303. operator.not_,
  304. operator.truth,
  305. operator.invert,
  306. operator.pow,
  307. operator.mul,
  308. operator.matmul,
  309. operator.floordiv,
  310. operator.truediv,
  311. operator.mod,
  312. operator.add,
  313. operator.sub,
  314. operator.getitem,
  315. operator.length_hint,
  316. operator.lshift,
  317. operator.rshift,
  318. operator.and_,
  319. operator.or_,
  320. operator.xor,
  321. operator.ipow,
  322. operator.imul,
  323. operator.imatmul,
  324. operator.ifloordiv,
  325. operator.itruediv,
  326. operator.imod,
  327. operator.iadd,
  328. operator.isub,
  329. operator.ilshift,
  330. operator.irshift,
  331. operator.iand,
  332. operator.ixor,
  333. operator.ior,
  334. operator.index,
  335. }
  336. from .tensor import supported_comparison_ops
  337. fns.update(supported_comparison_ops.values())
  338. fns.update(x for x in math.__dict__.values() if isinstance(x, type(math.sqrt)))
  339. return fns
  340. def can_constant_fold_through(self) -> bool:
  341. return self.fn in self._constant_fold_functions()
  342. @staticmethod
  343. @functools.cache
  344. def _fx_graph_functions() -> set[Callable[..., Any]]:
  345. fns = {
  346. operator.abs,
  347. operator.pos,
  348. operator.neg,
  349. operator.not_,
  350. operator.invert,
  351. operator.pow,
  352. operator.mul,
  353. operator.matmul,
  354. operator.floordiv,
  355. operator.truediv,
  356. operator.mod,
  357. operator.add,
  358. operator.lt,
  359. operator.gt,
  360. operator.ge,
  361. operator.le,
  362. operator.ne,
  363. operator.eq,
  364. operator.sub,
  365. operator.length_hint,
  366. operator.lshift,
  367. operator.rshift,
  368. operator.and_,
  369. operator.or_,
  370. operator.xor,
  371. operator.ipow,
  372. operator.imul,
  373. operator.imatmul,
  374. operator.ifloordiv,
  375. operator.itruediv,
  376. operator.getitem,
  377. operator.imod,
  378. operator.iadd,
  379. operator.isub,
  380. operator.ilshift,
  381. operator.irshift,
  382. operator.iand,
  383. operator.ixor,
  384. operator.ior,
  385. }
  386. return fns # type: ignore[return-value]
  387. @staticmethod
  388. @functools.cache
  389. def _binops() -> dict[
  390. Callable[..., object], tuple[list[str], Callable[..., object]]
  391. ]:
  392. # function -> ([forward name, reverse name, in-place name], in-place op)
  393. fns: dict[Callable[..., object], tuple[list[str], Callable[..., object]]] = {
  394. operator.add: (["__add__", "__radd__", "__iadd__"], operator.iadd),
  395. operator.sub: (["__sub__", "__rsub__", "__isub__"], operator.isub),
  396. operator.mul: (["__mul__", "__rmul__", "__imul__"], operator.imul),
  397. operator.truediv: (
  398. ["__truediv__", "__rtruediv__", "__itruediv__"],
  399. operator.itruediv,
  400. ),
  401. operator.floordiv: (
  402. ["__floordiv__", "__rfloordiv__", "__ifloordiv__"],
  403. operator.ifloordiv,
  404. ),
  405. operator.mod: (["__mod__", "__rmod__", "__imod__"], operator.imod),
  406. pow: (["__pow__", "__rpow__", "__ipow__"], operator.ipow),
  407. operator.pow: (["__pow__", "__rpow__", "__ipow__"], operator.ipow),
  408. operator.lshift: (
  409. ["__lshift__", "__rlshift__", "__ilshift__"],
  410. operator.ilshift,
  411. ),
  412. operator.rshift: (
  413. ["__rshift__", "__rrshift__", "__irshift__"],
  414. operator.irshift,
  415. ),
  416. operator.xor: (["__xor__", "__rxor__", "__ixor__"], operator.xor),
  417. # NB: The follow binary operators are not supported for now, since the
  418. # corresponding magic methods aren't defined on SymInt / SymFloat:
  419. # operator.matmul
  420. # divmod
  421. # operator.and_
  422. # operator.or_
  423. }
  424. return fns
  425. @staticmethod
  426. @functools.cache
  427. def _binop_handlers() -> dict[
  428. Callable[..., object],
  429. list[
  430. tuple[
  431. tuple[
  432. type[VariableTracker],
  433. _TrackersType,
  434. ],
  435. _HandlerCallback,
  436. ]
  437. ],
  438. ]:
  439. # Multiple dispatch mechanism defining custom binop behavior for certain type
  440. # combinations. Handlers are attempted in order, and will be used if the type checks
  441. # match. They are expected to have the signature:
  442. # fn(tx, arg0: VariableTracker, arg1: VariableTracker) -> VariableTracker
  443. from .functions import BaseUserFunctionVariable, UserFunctionVariable
  444. from .nn_module import NNModuleVariable
  445. from .tensor import supported_const_comparison_ops
  446. from .torch import BaseTorchVariable
  447. from .user_defined import (
  448. UserDefinedClassVariable,
  449. UserDefinedObjectVariable,
  450. UserDefinedVariable,
  451. )
  452. # Override table contains: op_fn -> [list of handlers]
  453. op_handlers: dict[Any, list[Any]] = {}
  454. for (
  455. op,
  456. (magic_method_names, in_place_op),
  457. ) in BuiltinVariable._binops().items():
  458. op_handlers[op] = []
  459. op_handlers[in_place_op] = []
  460. forward_name, reverse_name, inplace_name = magic_method_names
  461. # User-defined args (highest precedence)
  462. def user_defined_handler(
  463. tx: "InstructionTranslator",
  464. a: VariableTracker,
  465. b: VariableTracker,
  466. *,
  467. forward_name: str = forward_name,
  468. reverse_name: str = reverse_name,
  469. ) -> VariableTracker:
  470. # Manually handle reversing logic if needed (e.g. call __radd__)
  471. # TODO: If we expand this to handle tensor args, we need to manually
  472. # handle cases like this:
  473. #
  474. # class A(int):
  475. # def __radd__(self, other):
  476. # print("woof")
  477. # torch.randn(3) + A(3)
  478. #
  479. # In this example, A.__radd__() is not called -> nothing is printed, because
  480. # Tensor.__add__ only does a subtype test against int, ignoring the subclass.
  481. # To be fully correct, we should not call A.__radd__() here, and there may be
  482. # other cases to reason about and add exceptions for.
  483. if isinstance(a, UserDefinedVariable):
  484. return a.call_method(tx, forward_name, [b], {})
  485. else:
  486. return b.call_method(tx, reverse_name, [a], {})
  487. op_handlers[op].append(
  488. ((UserDefinedVariable, VariableTracker), user_defined_handler)
  489. )
  490. op_handlers[op].append(
  491. ((VariableTracker, UserDefinedVariable), user_defined_handler)
  492. )
  493. def user_defined_inplace_handler(
  494. tx: "InstructionTranslator",
  495. a: VariableTracker,
  496. b: VariableTracker,
  497. *,
  498. forward_name: str = inplace_name,
  499. ) -> VariableTracker:
  500. return a.call_method(tx, forward_name, [b], {})
  501. op_handlers[in_place_op].append(
  502. ((UserDefinedVariable, VariableTracker), user_defined_inplace_handler)
  503. )
  504. op_handlers[in_place_op].append(
  505. ((VariableTracker, UserDefinedVariable), user_defined_inplace_handler)
  506. )
  507. # Dynamic shape args
  508. def dynamic_handler(
  509. tx: "InstructionTranslator",
  510. a: VariableTracker,
  511. b: VariableTracker,
  512. *,
  513. fn: Callable[..., Any] = op,
  514. ) -> VariableTracker:
  515. from .builder import wrap_fx_proxy
  516. return wrap_fx_proxy(
  517. tx,
  518. tx.output.create_proxy(
  519. "call_function", fn, *proxy_args_kwargs([a, b], {})
  520. ),
  521. )
  522. op_handlers[op].append(
  523. ((SymNodeVariable, VariableTracker), dynamic_handler)
  524. )
  525. op_handlers[op].append(
  526. ((VariableTracker, SymNodeVariable), dynamic_handler)
  527. )
  528. # NB: Prefer out-of-place op when calling in-place op to generate valid graph
  529. op_handlers[in_place_op].append(
  530. ((SymNodeVariable, VariableTracker), dynamic_handler)
  531. )
  532. op_handlers[in_place_op].append(
  533. ((VariableTracker, SymNodeVariable), dynamic_handler)
  534. )
  535. # Special cases - lower precedence but still prefer these over constant folding
  536. # List-like addition (e.g. [1, 2] + [3, 4])
  537. def tuple_add_handler(
  538. tx: "InstructionTranslator", a: BaseListVariable, b: VariableTracker
  539. ) -> VariableTracker:
  540. return TupleVariable([*a.items, *b.unpack_var_sequence(tx)])
  541. def size_add_handler(
  542. tx: "InstructionTranslator", a: BaseListVariable, b: VariableTracker
  543. ) -> VariableTracker:
  544. return SizeVariable([*a.items, *b.unpack_var_sequence(tx)])
  545. list_like_addition_handlers: list[
  546. tuple[
  547. tuple[
  548. type[VariableTracker],
  549. _TrackersType,
  550. ],
  551. _HandlerCallback,
  552. ]
  553. ] = [
  554. # NB: Prefer the tuple-specific logic over base logic because of
  555. # some SizeVariable weirdness. Specifically, the tuple-specific logic
  556. # drops the subclass type (e.g. SizeVariable) and returns TupleVariables.
  557. (
  558. (SizeVariable, SizeVariable),
  559. size_add_handler,
  560. ),
  561. (
  562. (SizeVariable, TupleVariable),
  563. size_add_handler,
  564. ),
  565. (
  566. (TupleVariable, SizeVariable),
  567. size_add_handler,
  568. ),
  569. (
  570. (TupleVariable, TupleVariable),
  571. tuple_add_handler,
  572. ),
  573. (
  574. (TupleVariable, ConstantVariable),
  575. tuple_add_handler,
  576. ),
  577. (
  578. (ConstantVariable, TupleVariable),
  579. lambda tx, a, b: TupleVariable(
  580. [
  581. *a.unpack_var_sequence(tx),
  582. *b.items,
  583. ],
  584. ),
  585. ),
  586. (
  587. (
  588. ListVariable,
  589. (BaseListVariable, ConstantVariable, ListIteratorVariable),
  590. ),
  591. lambda tx, a, b: ListVariable(
  592. [*a.items, *b.unpack_var_sequence(tx)],
  593. mutation_type=ValueMutationNew(),
  594. ),
  595. ),
  596. (
  597. (BaseListVariable, BaseListVariable),
  598. lambda tx, a, b: type(a)(
  599. [
  600. *a.items,
  601. *b.items,
  602. ]
  603. ),
  604. ),
  605. ]
  606. op_handlers[operator.add].extend(list_like_addition_handlers)
  607. def list_iadd_handler(
  608. tx: "InstructionTranslator", a: BaseListVariable, b: VariableTracker
  609. ) -> Any:
  610. if a.is_immutable() or not b.has_unpack_var_sequence(tx):
  611. # Handler doesn't apply
  612. return None
  613. seq = b.unpack_var_sequence(tx)
  614. tx.output.side_effects.mutation(a)
  615. a.items.extend(seq)
  616. return a
  617. list_like_iadd_handlers: list[Any] = [
  618. (
  619. (ListVariable, VariableTracker),
  620. list_iadd_handler,
  621. ),
  622. (
  623. (TupleVariable, TupleVariable),
  624. tuple_add_handler,
  625. ),
  626. (
  627. (TupleVariable, ConstantVariable),
  628. tuple_add_handler,
  629. ),
  630. ]
  631. op_handlers[operator.iadd].extend(list_like_iadd_handlers)
  632. # List-like expansion (e.g. [1, 2, 3] * 3)
  633. def expand_list_like(
  634. tx: "InstructionTranslator", lst: VariableTracker, const: VariableTracker
  635. ) -> VariableTracker:
  636. if not isinstance(lst, BaseListVariable) and lst.is_python_constant():
  637. lst, const = const, lst
  638. try:
  639. assert isinstance(lst, BaseListVariable)
  640. return lst.__class__(
  641. items=lst.items * const.as_python_constant(),
  642. mutation_type=ValueMutationNew(),
  643. )
  644. except MemoryError as exc:
  645. raise_observed_exception(
  646. type(exc),
  647. tx,
  648. args=list(map(ConstantVariable.create, exc.args)),
  649. )
  650. list_like_expansion_handlers: list[
  651. tuple[
  652. tuple[type[VariableTracker], type[VariableTracker]],
  653. _HandlerCallback,
  654. ]
  655. ] = [
  656. ((ListVariable, ConstantVariable), expand_list_like),
  657. ((TupleVariable, ConstantVariable), expand_list_like),
  658. ((ConstantVariable, ListVariable), expand_list_like),
  659. ((ConstantVariable, TupleVariable), expand_list_like),
  660. ]
  661. op_handlers[operator.mul].extend(list_like_expansion_handlers)
  662. def create_cmp_op_handlers(
  663. op: Callable[..., Any],
  664. ) -> list[tuple[tuple[_TrackersType, _TrackersType], _HandlerCallback]]:
  665. def compare_by_value(
  666. tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  667. ) -> VariableTracker:
  668. try:
  669. return ConstantVariable(op(a.value, b.value)) # type: ignore[attr-defined]
  670. except TypeError as exc:
  671. raise_observed_exception(
  672. type(exc),
  673. tx,
  674. args=list(map(ConstantVariable.create, exc.args)),
  675. )
  676. result: list[
  677. tuple[
  678. tuple[
  679. _TrackersType,
  680. _TrackersType,
  681. ],
  682. _HandlerCallback,
  683. ]
  684. ] = [((ConstantVariable, ConstantVariable), compare_by_value)]
  685. if op in polyfill_fn_mapping:
  686. # For constants, speedup the comparison instead of using
  687. # polyfill. Removing this line causes major regression for pr
  688. # time benchmark - add_loop_eager.
  689. result = [
  690. ((ConstantVariable, ConstantVariable), compare_by_value),
  691. ((EnumVariable, EnumVariable), compare_by_value),
  692. ]
  693. op_var = BuiltinVariable(op)
  694. # Special handling of SymNode variable
  695. result.extend(
  696. [
  697. (
  698. (SymNodeVariable, VariableTracker),
  699. op_var._comparison_with_symnode,
  700. ),
  701. (
  702. (VariableTracker, SymNodeVariable),
  703. op_var._comparison_with_symnode,
  704. ),
  705. ]
  706. )
  707. def handler(
  708. tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  709. ) -> VariableTracker:
  710. return tx.inline_user_function_return(
  711. VariableTracker.build(tx, polyfill_fn_mapping[op]), [a, b], {}
  712. )
  713. result.append(((VariableTracker, VariableTracker), handler))
  714. return result
  715. result = [((ConstantVariable, ConstantVariable), compare_by_value)]
  716. if op in supported_const_comparison_ops.values() and op.__name__.startswith(
  717. "is_"
  718. ):
  719. # Tensor is None, List is not None, etc
  720. none_result = op(object(), None)
  721. def never(
  722. tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  723. ) -> VariableTracker:
  724. return ConstantVariable(none_result)
  725. obj_op_none = never
  726. none_op_obj = never
  727. types_that_are_never_none = (
  728. TensorVariable,
  729. SymNodeVariable,
  730. NNModuleVariable,
  731. BaseListVariable,
  732. UserDefinedVariable,
  733. BaseUserFunctionVariable,
  734. ConstDictVariable,
  735. BaseTorchVariable,
  736. )
  737. result.extend(
  738. [
  739. (
  740. (types_that_are_never_none, ConstantVariable),
  741. obj_op_none,
  742. ),
  743. (
  744. (ConstantVariable, types_that_are_never_none),
  745. none_op_obj,
  746. ),
  747. ]
  748. )
  749. op_var = BuiltinVariable(op)
  750. result.extend(
  751. [
  752. (
  753. (
  754. (UserFunctionVariable, BuiltinVariable),
  755. (UserFunctionVariable, BuiltinVariable),
  756. ),
  757. lambda tx, a, b: ConstantVariable(op(a.fn, b.fn)),
  758. ),
  759. (
  760. (
  761. NNModuleVariable,
  762. NNModuleVariable,
  763. ),
  764. lambda tx, a, b: ConstantVariable(
  765. op(
  766. tx.output.get_submodule(a.module_key),
  767. tx.output.get_submodule(b.module_key),
  768. )
  769. ),
  770. ),
  771. (
  772. (UserDefinedObjectVariable, UserDefinedObjectVariable),
  773. compare_by_value,
  774. ),
  775. (
  776. (UserDefinedClassVariable, UserDefinedClassVariable),
  777. compare_by_value,
  778. ),
  779. (
  780. (
  781. (StreamVariable, EventVariable, ConstantVariable),
  782. (StreamVariable, EventVariable, ConstantVariable),
  783. ),
  784. compare_by_value,
  785. ),
  786. (
  787. (TensorVariable, VariableTracker),
  788. op_var._comparison_with_tensor,
  789. ),
  790. (
  791. (VariableTracker, TensorVariable),
  792. op_var._comparison_with_tensor,
  793. ),
  794. (
  795. (SymNodeVariable, VariableTracker),
  796. op_var._comparison_with_symnode,
  797. ),
  798. (
  799. (VariableTracker, SymNodeVariable),
  800. op_var._comparison_with_symnode,
  801. ),
  802. ]
  803. )
  804. def handle_is(
  805. tx: "InstructionTranslator",
  806. left: VariableTracker,
  807. right: VariableTracker,
  808. ) -> VariableTracker | None:
  809. # If the two objects are of different type, we can safely return False
  810. # and True for `is` and `is not`, respectively
  811. if type(left) is not type(right):
  812. return ConstantVariable.create(op.__name__ != "is_")
  813. if left is right:
  814. return ConstantVariable.create(op(left, right))
  815. if istype(left, variables.ObjectVariable) and istype(
  816. right, variables.ObjectVariable
  817. ):
  818. return ConstantVariable.create(op(left.value, right.value))
  819. if (
  820. istype(left, variables.ExceptionVariable)
  821. and istype(right, variables.ExceptionVariable)
  822. and left.exc_type is not right.exc_type
  823. ):
  824. return ConstantVariable.create(op(left, right))
  825. result.append(((VariableTracker, VariableTracker), handle_is)) # type: ignore[arg-type]
  826. return result
  827. for op in supported_comparison_ops.values():
  828. assert callable(op)
  829. assert op not in op_handlers
  830. op_handlers[op] = create_cmp_op_handlers(op)
  831. return op_handlers
  832. @staticmethod
  833. def _find_binop_handler(
  834. op: Callable[..., Any], a_type: type[VariableTracker], b_type: type
  835. ) -> list[_HandlerCallback] | None:
  836. handlers = BuiltinVariable._binop_handlers().get(op)
  837. if handlers is None:
  838. return None
  839. matches = []
  840. for (type1, type2), handler in handlers:
  841. if issubclass(a_type, type1) and issubclass(b_type, type2):
  842. matches.append(handler)
  843. return matches
  844. def can_insert_in_graph(self) -> bool:
  845. return self.fn in self._fx_graph_functions()
  846. def __init__(self, fn: Any, **kwargs: Any) -> None:
  847. super().__init__(**kwargs)
  848. self.fn = fn
  849. def __repr__(self) -> str:
  850. if self.fn is None:
  851. name = "None"
  852. else:
  853. name = self.fn.__name__
  854. return f"{self.__class__.__name__}({name})"
  855. def as_python_constant(self) -> Any:
  856. return self.fn
  857. def as_proxy(self) -> Any:
  858. DTYPE = {
  859. bool: torch.bool,
  860. int: torch.int64,
  861. float: torch.float64,
  862. }
  863. if self.fn in DTYPE:
  864. return DTYPE[self.fn]
  865. return super().as_proxy()
  866. def reconstruct(self, codegen: "PyCodegen") -> None:
  867. name = self.fn.__name__
  868. assert self.fn.__module__ == "builtins"
  869. assert name not in codegen.tx.f_globals, "shadowed global"
  870. codegen.append_output(codegen.create_load_global(name, add=True))
  871. def constant_args(self, *args: VariableTracker, **kwargs: VariableTracker) -> bool:
  872. return check_constant_args(args, kwargs)
  873. def tensor_args(self, *args: VariableTracker) -> bool:
  874. any_tensor = False
  875. for arg in args:
  876. if isinstance(arg, variables.GetAttrVariable):
  877. return False
  878. any_tensor = any_tensor or arg.is_tensor()
  879. return any_tensor
  880. def tensor_args_type(self, arg_types: list[type]) -> bool:
  881. any_tensor = False
  882. for arg_type in arg_types:
  883. if issubclass(arg_type, variables.GetAttrVariable):
  884. return False
  885. any_tensor = any_tensor or issubclass(arg_type, variables.TensorVariable)
  886. return any_tensor
  887. def python_and_tensor_constant_only(
  888. self, *args: VariableTracker, **kwargs: VariableTracker
  889. ) -> bool:
  890. tensor_args = []
  891. non_tensor_args = []
  892. for i in itertools.chain(args, kwargs.values()):
  893. if i.is_tensor():
  894. tensor_args.append(i)
  895. else:
  896. non_tensor_args.append(i)
  897. return all(
  898. is_constant_source(t.source) if t.source is not None else False
  899. for t in tensor_args
  900. ) and self.constant_args(*non_tensor_args)
  901. @staticmethod
  902. def unwrap_unspec_args_kwargs(
  903. args: Sequence[VariableTracker], kwargs: dict[str, VariableTracker]
  904. ) -> tuple[list[Any], dict[str, Any]]:
  905. return [x.as_python_constant() for x in args], {
  906. k: v.as_python_constant() for k, v in kwargs.items()
  907. }
  908. def has_constant_handler(
  909. self, args: Sequence[VariableTracker], kwargs: dict[str, VariableTracker]
  910. ) -> bool:
  911. return self.can_constant_fold_through() and check_unspec_or_constant_args(
  912. args, kwargs
  913. )
  914. @staticmethod
  915. def _make_handler(
  916. fn: Callable[..., Any], arg_types: list[type], has_kwargs: bool
  917. ) -> Callable[
  918. [
  919. "InstructionTranslator",
  920. tuple[VariableTracker, ...],
  921. dict[str, VariableTracker],
  922. ],
  923. VariableTracker | None,
  924. ]:
  925. from .lazy import LazyVariableTracker
  926. obj = BuiltinVariable(fn)
  927. handlers: list[_HandlerCallback] = []
  928. if any(issubclass(t, LazyVariableTracker) for t in arg_types):
  929. return lambda tx, args, kwargs: obj.call_function(
  930. tx, [v.realize() for v in args], kwargs
  931. )
  932. if inspect.isclass(fn) and (
  933. issubclass(fn, BaseException)
  934. # GeneratorExit doesn't inherit from Exception
  935. # >>> issubclass(GeneratorExit, Exception)
  936. # False
  937. or fn is GeneratorExit
  938. ):
  939. def create_exception_class_object(
  940. tx: "InstructionTranslator",
  941. args: tuple[VariableTracker, ...],
  942. kwargs: dict[str, VariableTracker],
  943. ) -> VariableTracker:
  944. if fn is AssertionError and not all(
  945. x.is_python_constant() and isinstance(x.as_python_constant(), str)
  946. for x in args
  947. ):
  948. unimplemented(
  949. gb_type="assert with non-string message",
  950. context=str(args),
  951. explanation="Dynamo only supports asserts with string messages",
  952. hints=[*graph_break_hints.SUPPORTABLE],
  953. )
  954. return variables.ExceptionVariable(fn, args, kwargs)
  955. return create_exception_class_object
  956. if obj.can_insert_in_graph() and not (
  957. fn is operator.getitem
  958. and not issubclass(arg_types[0], variables.TensorVariable)
  959. ):
  960. if obj.tensor_args_type(arg_types):
  961. return obj._handle_insert_op_in_graph
  962. elif has_kwargs:
  963. # need runtime check for kwargs
  964. handlers.append(obj._handle_insert_op_in_graph)
  965. # Handle binary ops (e.g. __add__ / __radd__, __iadd__, etc.)
  966. # NB: Tensor args are handled above and not here
  967. if len(arg_types) == 2 and not has_kwargs:
  968. # Try to find a handler for the arg types; otherwise, fall through to constant handler
  969. binop_handlers = BuiltinVariable._find_binop_handler(fn, *arg_types)
  970. if not binop_handlers:
  971. pass
  972. elif len(binop_handlers) == 1:
  973. (binop_handler,) = binop_handlers
  974. handlers.append(lambda tx, args, _: binop_handler(tx, *args))
  975. else:
  976. def call_binop_handlers(
  977. tx: "InstructionTranslator", args: Any, _: Any
  978. ) -> Any:
  979. # pyrefly: ignore [not-iterable]
  980. for fn in binop_handlers:
  981. rv = fn(tx, *args)
  982. if rv:
  983. return rv
  984. return None
  985. handlers.append(call_binop_handlers)
  986. self_handler = getattr(obj, f"call_{fn.__name__}", None)
  987. if self_handler:
  988. def call_self_handler(
  989. tx: "InstructionTranslator",
  990. args: Sequence[VariableTracker],
  991. kwargs: dict[str, VariableTracker],
  992. ) -> VariableTracker | None:
  993. try:
  994. # pyrefly: ignore [not-callable]
  995. return self_handler(tx, *args, **kwargs)
  996. except TypeError:
  997. # Check if binding is bad. inspect signature bind is expensive.
  998. # So check only when handler call fails.
  999. try:
  1000. # pyrefly: ignore [bad-argument-type]
  1001. inspect.signature(self_handler).bind(tx, *args, **kwargs)
  1002. except TypeError as e:
  1003. has_constant_handler = obj.has_constant_handler(args, kwargs)
  1004. if not has_constant_handler:
  1005. log.warning( # noqa: G200
  1006. "incorrect arg count %s %s and no constant handler",
  1007. self_handler,
  1008. e,
  1009. )
  1010. unimplemented(
  1011. gb_type="invalid call to builtin op handler",
  1012. context=f"invalid args to {self_handler}: {args} {kwargs}",
  1013. explanation=f"Encountered TypeError when trying to handle op {fn.__name__}",
  1014. hints=[*graph_break_hints.DIFFICULT],
  1015. )
  1016. else:
  1017. raise
  1018. except Unsupported as exc:
  1019. has_constant_handler = obj.has_constant_handler(args, kwargs)
  1020. if not has_constant_handler:
  1021. raise
  1022. # Actually, we will handle this just fine
  1023. exc.remove_from_stats()
  1024. return None
  1025. handlers.append(call_self_handler)
  1026. if obj.can_constant_fold_through():
  1027. if (
  1028. all(issubclass(x, ConstantVariable) for x in arg_types)
  1029. and not has_kwargs
  1030. ):
  1031. def constant_fold_handler(
  1032. tx: "InstructionTranslator",
  1033. args: Sequence[VariableTracker],
  1034. kwargs: dict[str, VariableTracker],
  1035. ) -> VariableTracker | None:
  1036. # fast path
  1037. try:
  1038. res = fn(
  1039. *[x.as_python_constant() for x in args],
  1040. )
  1041. except Exception as exc:
  1042. raise_observed_exception(
  1043. type(exc),
  1044. tx,
  1045. args=list(map(ConstantVariable.create, exc.args)),
  1046. )
  1047. except AsPythonConstantNotImplementedError as exc:
  1048. unimplemented(
  1049. gb_type="constant fold exception",
  1050. context=f"attempted to run function {fn} with arguments {args}",
  1051. explanation="Encountered exception when attempting to constant fold.",
  1052. hints=[*graph_break_hints.DYNAMO_BUG],
  1053. from_exc=exc,
  1054. )
  1055. # pyrefly: ignore [unbound-name]
  1056. return VariableTracker.build(tx, res)
  1057. else:
  1058. def constant_fold_handler(
  1059. tx: "InstructionTranslator",
  1060. args: Sequence[VariableTracker],
  1061. kwargs: dict[str, VariableTracker],
  1062. ) -> VariableTracker | None:
  1063. # path with a runtime check
  1064. if check_unspec_or_constant_args(args, kwargs):
  1065. try:
  1066. res = fn(
  1067. *[x.as_python_constant() for x in args],
  1068. **{
  1069. k: v.as_python_constant() for k, v in kwargs.items()
  1070. },
  1071. )
  1072. except AsPythonConstantNotImplementedError as exc:
  1073. unimplemented(
  1074. gb_type="constant fold exception",
  1075. context=f"attempted to run function {fn} with arguments {args}",
  1076. explanation="Encountered exception when attempting to constant fold.",
  1077. hints=[*graph_break_hints.DYNAMO_BUG],
  1078. from_exc=exc,
  1079. )
  1080. except Exception as exc:
  1081. raise_observed_exception(
  1082. type(exc),
  1083. tx,
  1084. args=list(map(ConstantVariable.create, exc.args)),
  1085. )
  1086. # pyrefly: ignore [unbound-name]
  1087. return VariableTracker.build(tx, res)
  1088. return None
  1089. handlers.append(constant_fold_handler)
  1090. def call_unimplemented(args: Sequence[VariableTracker]) -> None:
  1091. real_arg_types = [arg.python_type_name() for arg in args]
  1092. unimplemented(
  1093. gb_type="Failed to trace builtin operator",
  1094. context=f"builtin {fn.__name__} {arg_types} {has_kwargs}",
  1095. explanation=f"Dynamo does not know how to trace builtin operator `{fn.__name__}` "
  1096. f"with argument types {real_arg_types} (has_kwargs {has_kwargs})",
  1097. hints=[
  1098. f"Avoid calling builtin `{fn.__name__}` with argument types {real_arg_types}. "
  1099. f"Consider using an equivalent alternative function/method to `{fn.__name__}`.",
  1100. "If you are attempting to call a logging function (e.g. `print`), "
  1101. "you can try adding it to `torch._dynamo.config.reorderable_logging_functions`.",
  1102. "Please report an issue to PyTorch.",
  1103. ],
  1104. )
  1105. if len(handlers) == 0:
  1106. return lambda tx, args, kwargs: call_unimplemented(args)
  1107. elif len(handlers) == 1:
  1108. (handler,) = handlers
  1109. def builtin_dispatch(
  1110. tx: "InstructionTranslator",
  1111. args: Sequence[VariableTracker],
  1112. kwargs: dict[str, VariableTracker],
  1113. ) -> VariableTracker | None:
  1114. rv = handler(tx, args, kwargs)
  1115. if rv:
  1116. return rv
  1117. call_unimplemented(args)
  1118. return rv
  1119. else:
  1120. def builtin_dispatch(
  1121. tx: "InstructionTranslator",
  1122. args: Sequence[VariableTracker],
  1123. kwargs: dict[str, VariableTracker],
  1124. ) -> VariableTracker | None:
  1125. rv = None
  1126. for fn in handlers:
  1127. rv = fn(tx, args, kwargs)
  1128. if rv:
  1129. return rv
  1130. call_unimplemented(args)
  1131. return rv
  1132. return builtin_dispatch
  1133. def call_vars(self, tx: "InstructionTranslator", *args: Any) -> VariableTracker:
  1134. if len(args) == 0:
  1135. unimplemented(
  1136. gb_type="unimplemented builtin op vars() with no arguments",
  1137. context=f"vars: {self} {args}",
  1138. explanation=f"Dynamo does not know how to trace builtin operator {self.fn} with no arguments",
  1139. hints=[*graph_break_hints.SUPPORTABLE],
  1140. )
  1141. assert len(args) == 1
  1142. # vars(obj) is obj.__dict__ if __dict__ is present else TypeError
  1143. try:
  1144. return args[0].var_getattr(tx, "__dict__")
  1145. except ObservedAttributeError:
  1146. raise_observed_exception(TypeError, tx)
  1147. def _handle_insert_op_in_graph(
  1148. self,
  1149. tx: "InstructionTranslator",
  1150. args: Sequence[VariableTracker],
  1151. kwargs: dict[str, VariableTracker],
  1152. ) -> VariableTracker | None:
  1153. from .builder import wrap_fx_proxy, wrap_fx_proxy_cls
  1154. if kwargs and not self.tensor_args(*args, *kwargs.values()):
  1155. return None
  1156. # insert handling for torch function here
  1157. from .builder import SourcelessBuilder
  1158. from .torch_function import can_dispatch_torch_function, dispatch_torch_function
  1159. global BUILTIN_TO_TENSOR_RFN_MAP, BUILTIN_TO_TENSOR_FN_MAP
  1160. if can_dispatch_torch_function(tx, args, kwargs):
  1161. # Only remap the fn to tensor methods if we aren't exporting
  1162. # export serde does not handle method descriptors today
  1163. if not tx.export:
  1164. # Ensure the builtin maps are populated before accessing them
  1165. populate_builtin_to_tensor_fn_map()
  1166. # Use sourceless builder, we built the map ourselves
  1167. if not args[0].is_tensor():
  1168. if self.fn in BUILTIN_TO_TENSOR_RFN_MAP:
  1169. func = BUILTIN_TO_TENSOR_RFN_MAP[self.fn]
  1170. else:
  1171. func = BUILTIN_TO_TENSOR_FN_MAP[self.fn]
  1172. tmp = args[0]
  1173. # swap args and call reverse version of func
  1174. args[0] = args[1] # type: ignore[index]
  1175. args[1] = tmp # type: ignore[index]
  1176. else:
  1177. func = BUILTIN_TO_TENSOR_FN_MAP[self.fn]
  1178. else:
  1179. func = self.fn
  1180. fn_var = SourcelessBuilder.create(tx, func)
  1181. return dispatch_torch_function(tx, fn_var, args, kwargs)
  1182. fn = self.fn
  1183. try:
  1184. # Constant fold for constant tensor and python constants
  1185. if self.python_and_tensor_constant_only(*args, **kwargs):
  1186. from ..bytecode_transformation import unique_id
  1187. from .functions import invoke_and_store_as_constant
  1188. return invoke_and_store_as_constant(
  1189. tx, fn, unique_id(fn.__name__), args, kwargs
  1190. )
  1191. if fn in IN_PLACE_DESUGARING_MAP and isinstance(
  1192. args[0], variables.ConstantVariable
  1193. ):
  1194. # In-place operators like += usually mustate tensor
  1195. # values, but in the edge case of immutable values they
  1196. # re-bind the variable.
  1197. #
  1198. # The easiest way to keep the graph consistent in this
  1199. # scenario is to de-sugar eagerly.
  1200. fn = IN_PLACE_DESUGARING_MAP[fn]
  1201. args = [args[0], args[1]] # type: ignore[assignment]
  1202. if fn is operator.getitem and isinstance(args[1], SymNodeVariable):
  1203. # Standard indexing will force specialization due to
  1204. # __index__. Rewrite as a regular torch op which will
  1205. # trace fine
  1206. fn = torch.select
  1207. args = [
  1208. args[0],
  1209. variables.ConstantVariable.create(0),
  1210. args[1],
  1211. ] # type: ignore[assignment]
  1212. # Interaction between ndarray and tensors:
  1213. # We prefer the tensor op whenever there are tensors involved
  1214. # NB: Use exact type check here - NumpyNdarrayVariable is a TensorVariable
  1215. # subclass but should NOT trigger the tensor path
  1216. if check_numpy_ndarray_args(args, kwargs) and not any(
  1217. type(arg) is TensorVariable for arg in args
  1218. ):
  1219. proxy = tx.output.create_proxy(
  1220. "call_function",
  1221. numpy_operator_wrapper(fn),
  1222. *proxy_args_kwargs(args, kwargs),
  1223. )
  1224. return wrap_fx_proxy_cls(variables.NumpyNdarrayVariable, tx, proxy)
  1225. if fn is operator.eq and len(args) == 2 and args[0].is_tensor():
  1226. # Dynamo expects `__eq__` str while operator.eq gives just `eq`
  1227. # TODO - supporting all comparison operators could also work but
  1228. # it fails lots of tests because graph str changes.
  1229. return args[0].call_method(tx, "__eq__", list(args[1:]), kwargs)
  1230. proxy = tx.output.create_proxy(
  1231. "call_function",
  1232. fn,
  1233. *proxy_args_kwargs(args, kwargs),
  1234. )
  1235. if any(isinstance(arg, FakeItemVariable) for arg in args):
  1236. return wrap_fx_proxy_cls(
  1237. FakeItemVariable,
  1238. tx,
  1239. proxy,
  1240. )
  1241. elif check_unspec_python_args(args, kwargs):
  1242. _args, _kwargs = self.unwrap_unspec_args_kwargs(args, kwargs)
  1243. raw_value = fn(*_args, **_kwargs)
  1244. need_unwrap = any(
  1245. x.need_unwrap
  1246. for x in itertools.chain(args, kwargs.values())
  1247. if isinstance(x, variables.UnspecializedPythonVariable)
  1248. )
  1249. return wrap_fx_proxy_cls(
  1250. UnspecializedPythonVariable,
  1251. tx,
  1252. proxy,
  1253. raw_value=raw_value,
  1254. need_unwrap=need_unwrap,
  1255. )
  1256. elif all(isinstance(x, SymNodeVariable) for x in args):
  1257. return SymNodeVariable.create(tx, proxy, None)
  1258. else:
  1259. # Work around for vision_maskrcnn due to precision difference
  1260. # specialize the dividend when float divide by tensor
  1261. if fn is operator.truediv and isinstance(
  1262. args[0], variables.UnspecializedPythonVariable
  1263. ):
  1264. args = list(args)
  1265. args[0] = args[0].as_python_constant()
  1266. return wrap_fx_proxy(tx, proxy)
  1267. except NotImplementedError:
  1268. unimplemented(
  1269. gb_type="unimplemented builtin op on tensor arguments",
  1270. context=f"partial tensor op: {self} {args} {kwargs}",
  1271. explanation=f"Dynamo does not know how to trace builtin operator {self.fn} with tensor arguments",
  1272. hints=[*graph_break_hints.SUPPORTABLE],
  1273. )
  1274. call_function_handler_cache: dict[
  1275. tuple[object, ...],
  1276. Callable[
  1277. [
  1278. "InstructionTranslator",
  1279. Sequence[VariableTracker],
  1280. dict[str, VariableTracker],
  1281. ],
  1282. VariableTracker,
  1283. ],
  1284. ] = {}
  1285. def call_function(
  1286. self,
  1287. tx: "InstructionTranslator",
  1288. args: Sequence[VariableTracker],
  1289. kwargs: dict[str, VariableTracker],
  1290. ) -> VariableTracker:
  1291. key: tuple[object, ...]
  1292. if kwargs:
  1293. kwargs = {k: v.realize() for k, v in kwargs.items()}
  1294. key = (self.fn, *(type(x) for x in args), True)
  1295. else:
  1296. key = (self.fn, *(type(x) for x in args))
  1297. handler = self.call_function_handler_cache.get(key)
  1298. if not handler:
  1299. self.call_function_handler_cache[key] = handler = self._make_handler( # type: ignore[assignment]
  1300. self.fn, [type(x) for x in args], bool(kwargs)
  1301. )
  1302. assert handler is not None
  1303. return handler(tx, args, kwargs) # type: ignore[return-value]
  1304. def call_method(
  1305. self,
  1306. tx: "InstructionTranslator",
  1307. name: str,
  1308. args: list[VariableTracker],
  1309. kwargs: dict[str, VariableTracker],
  1310. ) -> VariableTracker:
  1311. if self.fn is object and name == "__setattr__":
  1312. assert len(args) == 3
  1313. assert len(kwargs) == 0
  1314. obj, name_var, val = args
  1315. obj = obj.realize()
  1316. if (
  1317. isinstance(obj, UserDefinedObjectVariable)
  1318. and tx.output.side_effects.is_attribute_mutation(obj)
  1319. and name_var.is_python_constant()
  1320. ):
  1321. return obj.method_setattr_standard(tx, name_var, val)
  1322. if name == "__new__":
  1323. # Supported __new__ methods
  1324. if self.fn is object and len(args) == 1:
  1325. assert len(kwargs) == 0
  1326. return tx.output.side_effects.track_new_user_defined_object(
  1327. self, args[0], args[1:]
  1328. )
  1329. if self.fn is dict and len(args) == 1 and not kwargs:
  1330. dict_vt = ConstDictVariable({}, dict, mutation_type=ValueMutationNew())
  1331. if isinstance(args[0], BuiltinVariable) and args[0].fn is dict:
  1332. return dict_vt
  1333. # We don't have to set the underlying dict_vt in
  1334. # UserDefinedDictVariable because it will be set to empty
  1335. # ConstDictVariableTracker in the constructor.
  1336. return tx.output.side_effects.track_new_user_defined_object(
  1337. self,
  1338. args[0],
  1339. args[1:],
  1340. )
  1341. if (
  1342. self.fn is tuple
  1343. and len(args) == 2
  1344. and args[1].has_force_unpack_var_sequence(tx)
  1345. and not kwargs
  1346. ):
  1347. if isinstance(args[0], BuiltinVariable) and args[0].fn is tuple:
  1348. init_args = args[1].force_unpack_var_sequence(tx)
  1349. return variables.TupleVariable(
  1350. init_args, mutation_type=ValueMutationNew()
  1351. )
  1352. return tx.output.side_effects.track_new_user_defined_object(
  1353. self,
  1354. args[0],
  1355. args[1:],
  1356. )
  1357. if self.fn is list:
  1358. list_vt = ListVariable([], mutation_type=ValueMutationNew())
  1359. if isinstance(args[0], BuiltinVariable) and args[0].fn is list:
  1360. return list_vt
  1361. return tx.output.side_effects.track_new_user_defined_object(
  1362. self,
  1363. args[0],
  1364. args[1:],
  1365. )
  1366. if (
  1367. self.fn in (float, complex)
  1368. and len(args) == 1
  1369. and (
  1370. (self.fn is float and name in ("fromhex", "hex"))
  1371. or (name == "from_number" and sys.version_info >= (3, 14))
  1372. )
  1373. ):
  1374. if args[0].is_python_constant():
  1375. try:
  1376. fn = getattr(self.fn, name)
  1377. res = fn(args[0].as_python_constant())
  1378. return variables.ConstantVariable.create(res)
  1379. except (OverflowError, ValueError) as e:
  1380. raise_observed_exception(
  1381. type(e),
  1382. tx,
  1383. args=list(map(ConstantVariable.create, e.args)),
  1384. )
  1385. if self.fn is object and name == "__init__":
  1386. # object.__init__ is a no-op
  1387. return variables.CONSTANT_VARIABLE_NONE
  1388. if self.fn is dict and name == "fromkeys":
  1389. return BuiltinVariable.call_custom_dict_fromkeys(tx, dict, *args, **kwargs)
  1390. if self.fn is dict:
  1391. resolved_fn = getattr(self.fn, name)
  1392. if resolved_fn in dict_methods:
  1393. if isinstance(args[0], variables.UserDefinedDictVariable):
  1394. return args[0]._dict_vt.call_method(tx, name, args[1:], kwargs)
  1395. elif isinstance(args[0], variables.ConstDictVariable):
  1396. return args[0].call_method(tx, name, args[1:], kwargs)
  1397. if self.fn is set:
  1398. resolved_fn = getattr(self.fn, name)
  1399. if resolved_fn in set_methods:
  1400. if isinstance(args[0], variables.UserDefinedSetVariable):
  1401. return args[0]._set_vt.call_method(tx, name, args[1:], kwargs)
  1402. elif isinstance(args[0], variables.SetVariable):
  1403. return args[0].call_method(tx, name, args[1:], kwargs)
  1404. if self.fn is frozenset:
  1405. resolved_fn = getattr(self.fn, name)
  1406. if resolved_fn in frozenset_methods:
  1407. if isinstance(args[0], variables.FrozensetVariable):
  1408. return args[0].call_method(tx, name, args[1:], kwargs)
  1409. if self.fn is str and len(args) >= 1:
  1410. resolved_fn = getattr(self.fn, name)
  1411. if resolved_fn in str_methods:
  1412. # Only delegate to ConstantVariable, not other types that happen to be constants
  1413. if isinstance(args[0], ConstantVariable):
  1414. return args[0].call_method(tx, name, args[1:], kwargs)
  1415. if self.fn is float and len(args) >= 1:
  1416. # Only delegate to ConstantVariable, not other types that happen to be constants
  1417. if isinstance(args[0], ConstantVariable):
  1418. return ConstantVariable.create(
  1419. getattr(float, name)(args[0].as_python_constant())
  1420. )
  1421. return super().call_method(tx, name, args, kwargs)
  1422. def _call_int_float(
  1423. self, tx: "InstructionTranslator", arg: VariableTracker
  1424. ) -> VariableTracker | None:
  1425. # Handle cases like int(torch.seed())
  1426. # Also handle sym_float to sym_int cases
  1427. if arg.is_tensor() or isinstance(arg, SymNodeVariable):
  1428. if arg.is_tensor():
  1429. item = arg.call_method(tx, "item", [], {})
  1430. else:
  1431. item = arg
  1432. fn_ = sym_int if self.fn is int else sym_float
  1433. from torch._dynamo.variables.builder import wrap_fx_proxy
  1434. return wrap_fx_proxy(
  1435. tx=tx,
  1436. proxy=tx.output.create_proxy(
  1437. "call_function",
  1438. fn_,
  1439. (item.as_proxy(),),
  1440. {},
  1441. ),
  1442. )
  1443. return None
  1444. call_int = _call_int_float
  1445. call_float = _call_int_float
  1446. def call_bool(
  1447. self, tx: "InstructionTranslator", arg: VariableTracker
  1448. ) -> VariableTracker | None:
  1449. if arg.is_tensor():
  1450. item = arg.call_method(tx, "item", [], {})
  1451. if isinstance(item, SymNodeVariable) and isinstance(
  1452. item.sym_num, torch.SymBool
  1453. ):
  1454. return item
  1455. if isinstance(item, variables.ConstantVariable):
  1456. return variables.ConstantVariable.create(bool(item.value))
  1457. return SymNodeVariable.create(tx, item.as_proxy() != 0)
  1458. # Emulate `PyBool_Type.tp_vectorcall` which boils down to `PyObject_IsTrue`.
  1459. # https://github.com/python/cpython/blob/3.12/Objects/object.c#L1674-L1697
  1460. if isinstance(arg, SymNodeVariable):
  1461. # Note that we delay specializing on symbolic values to avoid
  1462. # unnecessary guards. Specialization will happen later if, e.g., the
  1463. # resulting boolean is used for branching.
  1464. if isinstance(arg.sym_num, torch.SymBool):
  1465. return arg
  1466. # Emulate `nb_bool` of int/float objects
  1467. # - https://github.com/python/cpython/blob/3.12/Objects/longobject.c#L4940-L4944
  1468. # - https://github.com/python/cpython/blob/3.12/Objects/floatobject.c#L878-L882
  1469. assert istype(arg.sym_num, (torch.SymInt, torch.SymFloat))
  1470. return SymNodeVariable.create(tx, arg.as_proxy() != 0)
  1471. # TODO handle more cases and merge this with this with `generic_jump`.
  1472. return None
  1473. def call_repr(
  1474. self, tx: "InstructionTranslator", arg: VariableTracker
  1475. ) -> VariableTracker | None:
  1476. """Handle repr() on user defined objects."""
  1477. if isinstance(arg, variables.UserDefinedObjectVariable):
  1478. repr_method = arg.value.__repr__
  1479. if type(arg.value).__repr__ is object.__repr__:
  1480. # Default repr - build and trace it
  1481. fn_vt = VariableTracker.build(tx, repr_method)
  1482. return fn_vt.call_function(tx, [], {})
  1483. elif is_wrapper_or_member_descriptor(repr_method):
  1484. unimplemented(
  1485. gb_type="Attempted to call repr() method implemented in C/C++",
  1486. context="",
  1487. explanation=f"{type(arg.value)} has a C/C++ based repr method. This is not supported.",
  1488. hints=["Write the repr method in Python"],
  1489. )
  1490. else:
  1491. bound_method = repr_method.__func__
  1492. fn_vt = VariableTracker.build(tx, bound_method)
  1493. return fn_vt.call_function(tx, [arg], {})
  1494. if isinstance(arg, variables.UserDefinedClassVariable):
  1495. if type(arg.value).__repr__ is type.__repr__:
  1496. return variables.ConstantVariable.create(repr(arg.value))
  1497. if isinstance(
  1498. arg,
  1499. (
  1500. RangeVariable,
  1501. ConstDictVariable,
  1502. DefaultDictVariable,
  1503. OrderedSetClassVariable,
  1504. DictViewVariable,
  1505. ),
  1506. ):
  1507. return variables.ConstantVariable.create(arg.debug_repr())
  1508. return None
  1509. def call_str(
  1510. self, tx: "InstructionTranslator", arg: VariableTracker
  1511. ) -> VariableTracker | None:
  1512. # Handle `str` on a user defined function or object
  1513. if isinstance(arg, (variables.UserFunctionVariable)):
  1514. return variables.ConstantVariable.create(value=str(arg.fn))
  1515. elif isinstance(arg, (variables.UserDefinedObjectVariable)):
  1516. # Check if object has __str__ method
  1517. if hasattr(arg.value, "__str__"):
  1518. str_method = arg.value.__str__
  1519. elif hasattr(arg.value, "__repr__"):
  1520. # account for __repr__ functions when __str__ is absent
  1521. str_method = arg.value.__repr__
  1522. else:
  1523. unimplemented(
  1524. gb_type="failed to call str() on user defined object",
  1525. context=str(arg),
  1526. explanation="User defined object has no __str__ or __repr__ method",
  1527. hints=[*graph_break_hints.USER_ERROR],
  1528. )
  1529. if type(arg.value).__str__ is object.__str__:
  1530. # Rely on the object str method
  1531. try:
  1532. # pyrefly: ignore [unbound-name]
  1533. return variables.ConstantVariable.create(value=str_method())
  1534. except AttributeError:
  1535. # Graph break
  1536. return None
  1537. # pyrefly: ignore [unbound-name]
  1538. elif is_wrapper_or_member_descriptor(str_method):
  1539. unimplemented(
  1540. gb_type="Attempted to a str() method implemented in C/C++",
  1541. context="",
  1542. explanation=f"{type(arg.value)} has a C/C++ based str method. This is not supported.",
  1543. hints=["Write the str method in Python"],
  1544. )
  1545. else:
  1546. # Overrides for custom str method
  1547. # Pass method as function to call tx.inline_user_function_return
  1548. bound_method = str_method.__func__ # type: ignore[attr-defined]
  1549. try:
  1550. # Only supports certain function types
  1551. user_func_variable = VariableTracker.build(tx, bound_method)
  1552. except AssertionError:
  1553. # Won't be able to do inline the str method, return to avoid graph break
  1554. log.warning("Failed to create UserFunctionVariable", exc_info=True)
  1555. return None
  1556. # Inline the user function
  1557. return user_func_variable.call_function(tx, [arg], {})
  1558. elif isinstance(arg, (variables.ExceptionVariable,)):
  1559. if len(arg.args) == 0:
  1560. value = f"{arg.exc_type}"
  1561. else:
  1562. value = ", ".join(a.as_python_constant() for a in arg.args)
  1563. return variables.ConstantVariable.create(value=value)
  1564. return None
  1565. def _call_min_max(
  1566. self, tx: "InstructionTranslator", *args: VariableTracker
  1567. ) -> VariableTracker | None:
  1568. if len(args) == 1 and args[0].has_force_unpack_var_sequence(tx):
  1569. items = args[0].force_unpack_var_sequence(tx)
  1570. return self._call_min_max_seq(tx, items)
  1571. elif len(args) == 2:
  1572. return self._call_min_max_binary(tx, args[0], args[1])
  1573. elif len(args) > 2:
  1574. return self._call_min_max_seq(tx, args)
  1575. return None
  1576. def _call_min_max_seq(
  1577. self, tx: "InstructionTranslator", items: Sequence[VariableTracker]
  1578. ) -> VariableTracker:
  1579. assert len(items) > 0
  1580. if len(items) == 1:
  1581. return items[0]
  1582. return functools.reduce(functools.partial(self._call_min_max_binary, tx), items) # type: ignore[arg-type,return-value]
  1583. def _call_min_max_binary(
  1584. self,
  1585. tx: "InstructionTranslator",
  1586. a: VariableTracker | None,
  1587. b: VariableTracker | None,
  1588. ) -> VariableTracker | None:
  1589. if a is None or b is None:
  1590. # a or b could be none if we reduce and _call_min_max_binary failed
  1591. # to return something
  1592. return None
  1593. if self.tensor_args(a, b):
  1594. if not a.is_tensor():
  1595. a, b = b, a
  1596. assert a.is_tensor()
  1597. # result of an item call is a scalar convert to a tensor
  1598. if isinstance(a, FakeItemVariable):
  1599. a = variables.TorchInGraphFunctionVariable(torch.tensor).call_function(
  1600. tx, [a], {}
  1601. )
  1602. # Dynamic input does not get resolved, rather, gets stored as call_function
  1603. if isinstance(a, SymNodeVariable) or isinstance(b, SymNodeVariable):
  1604. from .builder import wrap_fx_proxy_cls
  1605. return wrap_fx_proxy_cls(
  1606. type(a),
  1607. tx=tx,
  1608. proxy=tx.output.create_proxy(
  1609. "call_function",
  1610. self.fn,
  1611. *proxy_args_kwargs([a, b], {}),
  1612. ),
  1613. )
  1614. # convert min/max to torch ops
  1615. if b.is_python_constant():
  1616. fn: VariableTracker
  1617. if isinstance(a, variables.NumpyNdarrayVariable):
  1618. import numpy as np
  1619. fn = variables.NumpyVariable(np.clip)
  1620. else:
  1621. fn = variables.TorchInGraphFunctionVariable(torch.clamp)
  1622. kwargs = {"min": b} if (self.fn is max) else {"max": b}
  1623. result = fn.call_function(tx, [a], kwargs)
  1624. else:
  1625. if isinstance(a, variables.NumpyNdarrayVariable):
  1626. import numpy as np
  1627. np_fn = {max: np.maximum, min: np.minimum}[self.fn]
  1628. fn = variables.NumpyVariable(np_fn)
  1629. else:
  1630. torch_fn = {max: torch.maximum, min: torch.minimum}[self.fn]
  1631. fn = variables.TorchInGraphFunctionVariable(torch_fn)
  1632. result = fn.call_function(tx, [a, b], {})
  1633. # return unspec if both a, b are unspec or const
  1634. if all(
  1635. isinstance(
  1636. i,
  1637. (
  1638. variables.UnspecializedPythonVariable,
  1639. variables.ConstantVariable,
  1640. ),
  1641. )
  1642. for i in [a, b]
  1643. ):
  1644. if any(isinstance(val, FakeItemVariable) for val in [a, b]):
  1645. # type: ignore[arg-type]
  1646. return variables.FakeItemVariable.from_tensor_variable(result)
  1647. if b.is_python_constant():
  1648. raw_b = b.as_python_constant()
  1649. else:
  1650. raw_b = b.raw_value # type: ignore[attr-defined]
  1651. if self.fn is max:
  1652. raw_res = max(a.raw_value, raw_b) # type: ignore[attr-defined]
  1653. else:
  1654. raw_res = min(a.raw_value, raw_b) # type: ignore[attr-defined]
  1655. need_unwrap = any(
  1656. x.need_unwrap
  1657. for x in [a, b]
  1658. if isinstance(x, variables.UnspecializedPythonVariable)
  1659. )
  1660. return variables.UnspecializedPythonVariable.from_tensor_variable(
  1661. result, # type: ignore[arg-type]
  1662. raw_res,
  1663. need_unwrap,
  1664. )
  1665. # otherwise return tensor
  1666. else:
  1667. return result
  1668. elif isinstance(a, SymNodeVariable) or isinstance(b, SymNodeVariable):
  1669. py_fn = torch.sym_max if self.fn is max else torch.sym_min
  1670. proxy = tx.output.create_proxy(
  1671. "call_function", py_fn, *proxy_args_kwargs([a, b], {})
  1672. )
  1673. return SymNodeVariable.create(tx, proxy, None)
  1674. elif isinstance(a, ConstantVariable) and isinstance(b, ConstantVariable):
  1675. value = self.fn(
  1676. a.as_python_constant(),
  1677. b.as_python_constant(),
  1678. )
  1679. return ConstantVariable.create(value)
  1680. return None
  1681. call_min = _call_min_max
  1682. call_max = _call_min_max
  1683. def call_abs(
  1684. self, tx: "InstructionTranslator", arg: VariableTracker
  1685. ) -> VariableTracker:
  1686. from .builder import SourcelessBuilder
  1687. # Call arg.__abs__()
  1688. abs_method = SourcelessBuilder.create(tx, getattr).call_function(
  1689. tx, [arg, ConstantVariable.create("__abs__")], {}
  1690. )
  1691. return abs_method.call_function(tx, [], {})
  1692. def call_pos(
  1693. self, tx: "InstructionTranslator", arg: VariableTracker
  1694. ) -> VariableTracker:
  1695. from .builder import SourcelessBuilder
  1696. # Call arg.__pos__()
  1697. pos_method = SourcelessBuilder.create(tx, getattr).call_function(
  1698. tx, [arg, ConstantVariable.create("__pos__")], {}
  1699. )
  1700. return pos_method.call_function(tx, [], {})
  1701. def call_index(
  1702. self, tx: "InstructionTranslator", arg: VariableTracker
  1703. ) -> VariableTracker:
  1704. if arg.is_tensor():
  1705. unimplemented(
  1706. gb_type="unsupported index(Tensor)",
  1707. context="",
  1708. explanation="Dynamo does not support tracing builtin index() on a Tensor",
  1709. hints=[],
  1710. )
  1711. arg = guard_if_dyn(arg)
  1712. constant_value = operator.index(arg)
  1713. return variables.ConstantVariable.create(constant_value)
  1714. def call_round(
  1715. self,
  1716. tx: "InstructionTranslator",
  1717. arg: VariableTracker,
  1718. *args: VariableTracker,
  1719. **kwargs: VariableTracker,
  1720. ) -> VariableTracker:
  1721. from .builder import SourcelessBuilder
  1722. # Call arg.__round__()
  1723. round_method = SourcelessBuilder.create(tx, getattr).call_function(
  1724. tx, [arg, ConstantVariable.create("__round__")], {}
  1725. )
  1726. return round_method.call_function(tx, args, kwargs)
  1727. def call_range(
  1728. self, tx: "InstructionTranslator", *args: VariableTracker
  1729. ) -> VariableTracker | None:
  1730. if check_unspec_or_constant_args(args, {}):
  1731. return variables.RangeVariable(args)
  1732. elif self._dynamic_args(*args):
  1733. args = tuple(
  1734. variables.ConstantVariable.create(guard_if_dyn(arg)) for arg in args
  1735. )
  1736. return variables.RangeVariable(args)
  1737. # None no-ops this handler and lets the driving function proceed
  1738. return None
  1739. def _dynamic_args(self, *args: VariableTracker, **kwargs: VariableTracker) -> bool:
  1740. return any(isinstance(x, SymNodeVariable) for x in args) or any(
  1741. isinstance(x, SymNodeVariable) for x in kwargs.values()
  1742. )
  1743. def call_slice(
  1744. self, tx: "InstructionTranslator", *args: VariableTracker
  1745. ) -> VariableTracker:
  1746. return variables.SliceVariable(args, tx)
  1747. def _dyn_proxy(
  1748. self, tx: "InstructionTranslator", *args: Any, **kwargs: Any
  1749. ) -> VariableTracker:
  1750. from .builder import wrap_fx_proxy
  1751. return wrap_fx_proxy(
  1752. tx,
  1753. tx.output.create_proxy(
  1754. "call_function", self.fn, *proxy_args_kwargs(args, kwargs)
  1755. ),
  1756. )
  1757. # NOTE must handle IteratorVariable separately!
  1758. def _call_iter_tuple_list(
  1759. self,
  1760. tx: "InstructionTranslator",
  1761. obj: VariableTracker | None = None,
  1762. *args: VariableTracker,
  1763. **kwargs: VariableTracker,
  1764. ) -> VariableTracker | None:
  1765. assert not isinstance(obj, variables.IteratorVariable)
  1766. if self._dynamic_args(*args, **kwargs):
  1767. return self._dyn_proxy(tx, *args, **kwargs)
  1768. cls = variables.BaseListVariable.cls_for(self.fn)
  1769. if obj is None:
  1770. return cls(
  1771. [],
  1772. mutation_type=ValueMutationNew(),
  1773. )
  1774. elif obj.has_unpack_var_sequence(tx):
  1775. if obj.source and not is_constant_source(obj.source):
  1776. if isinstance(obj, TupleIteratorVariable):
  1777. install_guard(
  1778. obj.source.make_guard(GuardBuilder.TUPLE_ITERATOR_LEN)
  1779. )
  1780. else:
  1781. if (
  1782. getattr(obj, "source", False)
  1783. and isinstance(obj, ConstDictVariable)
  1784. and not istype(obj, (SetVariable, FrozensetVariable))
  1785. ):
  1786. tx.output.guard_on_key_order.add(obj.source)
  1787. if isinstance(obj, variables.MappingProxyVariable):
  1788. # This could be an overguarding, but its rare to iterate
  1789. # through a mapping proxy and not use the keys.
  1790. install_guard(
  1791. obj.source.make_guard(GuardBuilder.MAPPING_KEYS_CHECK)
  1792. )
  1793. elif not isinstance(obj, variables.UnspecializedNNModuleVariable):
  1794. # Prevent calling __len__ method for guards, the tracing
  1795. # of __iter__ will insert the right guards later.
  1796. install_guard(
  1797. obj.source.make_guard(GuardBuilder.SEQUENCE_LENGTH)
  1798. )
  1799. return cls(
  1800. list(obj.unpack_var_sequence(tx)),
  1801. mutation_type=ValueMutationNew(),
  1802. )
  1803. return None
  1804. def _call_iter_tuple_generator(
  1805. self,
  1806. tx: "InstructionTranslator",
  1807. obj: VariableTracker,
  1808. *args: VariableTracker,
  1809. **kwargs: VariableTracker,
  1810. ) -> VariableTracker:
  1811. cls = variables.BaseListVariable.cls_for(self.fn)
  1812. return cls(
  1813. list(obj.force_unpack_var_sequence(tx)), # exhaust generator
  1814. mutation_type=ValueMutationNew(),
  1815. )
  1816. def _call_tuple_list(
  1817. self,
  1818. tx: "InstructionTranslator",
  1819. obj: VariableTracker | None = None,
  1820. *args: VariableTracker,
  1821. **kwargs: VariableTracker,
  1822. ) -> VariableTracker | None:
  1823. if isinstance(obj, variables.IteratorVariable):
  1824. cls = variables.BaseListVariable.cls_for(self.fn)
  1825. return cls(
  1826. list(obj.force_unpack_var_sequence(tx)),
  1827. mutation_type=ValueMutationNew(),
  1828. )
  1829. elif isinstance(obj, variables.LocalGeneratorObjectVariable) or (
  1830. isinstance(obj, UserDefinedObjectVariable)
  1831. and obj.has_force_unpack_var_sequence(tx)
  1832. ):
  1833. return self._call_iter_tuple_generator(tx, obj, *args, **kwargs)
  1834. else:
  1835. return self._call_iter_tuple_list(tx, obj, *args, **kwargs)
  1836. def call_iter(
  1837. self,
  1838. tx: "InstructionTranslator",
  1839. obj: VariableTracker,
  1840. *args: VariableTracker,
  1841. **kwargs: VariableTracker,
  1842. ) -> VariableTracker:
  1843. # avoid the overhead of tracing the polyfill if we already know the class implemented __iter__
  1844. if isinstance(
  1845. obj,
  1846. (
  1847. variables.ListVariable,
  1848. variables.RangeVariable,
  1849. variables.IteratorVariable,
  1850. variables.ConstDictVariable,
  1851. variables.NNModuleVariable,
  1852. variables.TensorVariable,
  1853. variables.TupleVariable,
  1854. DictViewVariable,
  1855. ),
  1856. ):
  1857. return obj.call_method(tx, "__iter__", [], {})
  1858. else:
  1859. # If the object doesn't implement a __iter__ method, it will be an error in eager mode when calling iter on it anyway.
  1860. # If the object implements a __iter__ method, inlining effectively forwards the call to another iter call
  1861. # (e.g. when __iter__ just returns iter(self.list)) or return a user-defined iterator.
  1862. # If the object implements a __getitem__ method, iter(...) will call obj.__getitem__()
  1863. # with an integer argument starting at 0, until __getitem__ raises IndexError
  1864. ret = variables.UserFunctionVariable(
  1865. polyfills.builtins.iter_ # type: ignore[arg-type]
  1866. ).call_function(tx, [obj, *args], {})
  1867. if args:
  1868. # iter(obj, sentinel) returns an object that implements
  1869. # __iter__ and __next__ methods (UserDefinedObjectVariable)
  1870. # Wrap the return value in a IteratorVariable subclass (LazyObjectIteratorVariable)
  1871. # that forwards the next_variable call to the object.
  1872. ret = variables.ObjectIteratorVariable(ret)
  1873. return ret
  1874. call_tuple = _call_tuple_list
  1875. call_list = _call_tuple_list
  1876. def call_callable(
  1877. self, tx: "InstructionTranslator", arg: VariableTracker
  1878. ) -> VariableTracker | None:
  1879. from .functions import BaseUserFunctionVariable, FunctoolsPartialVariable
  1880. from .nn_module import NNModuleVariable
  1881. if isinstance(
  1882. arg,
  1883. (
  1884. variables.UserDefinedClassVariable,
  1885. BaseUserFunctionVariable,
  1886. FunctoolsPartialVariable,
  1887. NNModuleVariable,
  1888. ),
  1889. ):
  1890. return variables.ConstantVariable.create(True)
  1891. elif isinstance(arg, UserDefinedVariable):
  1892. return variables.ConstantVariable.create(callable(arg.value))
  1893. elif isinstance(
  1894. arg,
  1895. (
  1896. ConstantVariable,
  1897. SymNodeVariable,
  1898. TensorVariable,
  1899. ListVariable,
  1900. TupleVariable,
  1901. ListIteratorVariable,
  1902. ),
  1903. ):
  1904. return variables.ConstantVariable.create(False)
  1905. else:
  1906. return None
  1907. def call_cast(
  1908. self, _: Any, *args: VariableTracker, **kwargs: VariableTracker
  1909. ) -> VariableTracker | None:
  1910. if len(args) == 2:
  1911. return args[1]
  1912. unimplemented(
  1913. gb_type="bad args to builtin cast()",
  1914. context=f"got args {args} {kwargs}",
  1915. explanation="Dynamo expects exactly 2 args to builtin cast().",
  1916. hints=["Ensure your call to cast() has exactly 2 arguments."],
  1917. )
  1918. def call_dir(
  1919. self, tx: "InstructionTranslator", arg: VariableTracker
  1920. ) -> VariableTracker | None:
  1921. if isinstance(arg, variables.UserDefinedClassVariable):
  1922. return VariableTracker.build(tx, dir(arg.value))
  1923. if isinstance(arg, BuiltinVariable):
  1924. return VariableTracker.build(tx, dir(arg.fn))
  1925. return None
  1926. def call_dict(
  1927. self,
  1928. tx: "InstructionTranslator",
  1929. /,
  1930. *args: VariableTracker,
  1931. **kwargs: VariableTracker,
  1932. ) -> VariableTracker:
  1933. return BuiltinVariable.call_custom_dict(tx, dict, *args, **kwargs)
  1934. @staticmethod
  1935. def call_custom_dict(
  1936. tx: "InstructionTranslator",
  1937. user_cls: type,
  1938. /,
  1939. *args: VariableTracker,
  1940. **kwargs: VariableTracker,
  1941. ) -> VariableTracker:
  1942. args_list = list(args)
  1943. if (
  1944. len(args_list) == 1
  1945. and isinstance(args_list[0], variables.GetAttrVariable)
  1946. and isinstance(args_list[0].obj, variables.UserDefinedClassVariable)
  1947. and not tx.output.side_effects.has_pending_mutation(args_list[0].obj)
  1948. ):
  1949. # Forward the GetAttrVariable(foo, "__dict__") to a realized vt of
  1950. # VT(foo.__dict__). This simplifies the construction of the new
  1951. # dict.
  1952. args_list[0] = args_list[0].get_forwarded_dict(tx)
  1953. return tx.inline_user_function_return(
  1954. VariableTracker.build(tx, polyfills.construct_dict),
  1955. [VariableTracker.build(tx, user_cls), *args_list],
  1956. kwargs,
  1957. )
  1958. @staticmethod
  1959. def call_custom_dict_fromkeys(
  1960. tx: "InstructionTranslator",
  1961. user_cls: type,
  1962. /,
  1963. *args: VariableTracker,
  1964. **kwargs: VariableTracker,
  1965. ) -> VariableTracker:
  1966. if user_cls not in {dict, OrderedDict, defaultdict}:
  1967. unimplemented(
  1968. gb_type="Unsupported dict type for fromkeys()",
  1969. context=f"{user_cls.__name__}.fromkeys(): {args} {kwargs}",
  1970. explanation=f"Failed to call {user_cls.__name__}.fromkeys() because "
  1971. f"{user_cls.__name__} is not any type of dict, OrderedDict, or defaultdict",
  1972. hints=[
  1973. f"Ensure {user_cls.__name__} is a type of dict, OrderedDict, or defaultdict.",
  1974. ],
  1975. )
  1976. if kwargs:
  1977. # Only `OrderedDict.fromkeys` accepts `value` passed by keyword
  1978. if (
  1979. user_cls is not OrderedDict
  1980. or len(args) != 1
  1981. or len(kwargs) != 1
  1982. or "value" not in kwargs
  1983. ):
  1984. raise_args_mismatch(
  1985. tx,
  1986. f"{user_cls.__name__}.fromkeys",
  1987. "1 args and 1 kwargs (`value`)",
  1988. f"{len(args)} args and {len(kwargs)} kwargs",
  1989. )
  1990. args = (*args, kwargs.pop("value"))
  1991. if len(args) == 0:
  1992. raise_args_mismatch(
  1993. tx,
  1994. f"{user_cls.__name__}.fromkeys",
  1995. "at least 1 args",
  1996. f"{len(args)} args",
  1997. )
  1998. if len(args) == 1:
  1999. args = (*args, CONSTANT_VARIABLE_NONE)
  2000. if len(args) != 2:
  2001. raise_args_mismatch(
  2002. tx,
  2003. f"{user_cls.__name__}.fromkeys",
  2004. "2 args",
  2005. f"{len(args)} args",
  2006. )
  2007. arg, value = args
  2008. DictVariableType = (
  2009. ConstDictVariable if user_cls is not defaultdict else DefaultDictVariable
  2010. )
  2011. if isinstance(arg, dict):
  2012. arg_list = [ConstantVariable.create(k) for k in arg]
  2013. return DictVariableType(
  2014. dict.fromkeys(arg_list, value),
  2015. user_cls,
  2016. mutation_type=ValueMutationNew(),
  2017. )
  2018. elif arg.has_force_unpack_var_sequence(tx):
  2019. keys = arg.force_unpack_var_sequence(tx)
  2020. if all(is_hashable(v) for v in keys):
  2021. return DictVariableType(
  2022. dict.fromkeys(keys, value),
  2023. user_cls,
  2024. mutation_type=ValueMutationNew(),
  2025. )
  2026. unimplemented(
  2027. gb_type="failed to call dict.fromkeys()",
  2028. context=f"{user_cls.__name__}.fromkeys(): {args} {kwargs}",
  2029. explanation=f"Failed to call {user_cls.__name__}.fromkeys() because "
  2030. "arguments could not be automatically converted to a list, "
  2031. "or some dict key is not hashable.",
  2032. hints=[
  2033. "Manually convert the argument to a list.",
  2034. "Ensure all keys are hashable.",
  2035. ],
  2036. )
  2037. def call_set(
  2038. self,
  2039. tx: "InstructionTranslator",
  2040. *args: VariableTracker,
  2041. **kwargs: VariableTracker,
  2042. ) -> VariableTracker:
  2043. from .builder import SourcelessBuilder
  2044. # Can we merge this implementation and call_dict's one?
  2045. assert not kwargs
  2046. if not args:
  2047. return SetVariable([], mutation_type=ValueMutationNew())
  2048. if len(args) != 1:
  2049. raise_observed_exception(
  2050. TypeError,
  2051. tx,
  2052. args=[
  2053. ConstantVariable.create(
  2054. f"set() takes 1 positional argument but {len(args)} were given"
  2055. )
  2056. ],
  2057. )
  2058. arg = args[0]
  2059. if istype(arg, variables.SetVariable):
  2060. return arg.clone(mutation_type=ValueMutationNew())
  2061. elif arg.has_force_unpack_var_sequence(tx):
  2062. items = arg.force_unpack_var_sequence(tx)
  2063. return SetVariable(items, mutation_type=ValueMutationNew())
  2064. elif isinstance(arg, variables.UserDefinedObjectVariable) and isinstance(
  2065. arg.value, KeysView
  2066. ):
  2067. iter_fn = arg.var_getattr(tx, "__iter__")
  2068. if isinstance(iter_fn, variables.UserMethodVariable):
  2069. out = tx.inline_user_function_return(iter_fn, args, kwargs)
  2070. if isinstance(out, SetVariable):
  2071. return out
  2072. return SourcelessBuilder.create(tx, set).call_set(tx, out)
  2073. raise_observed_exception(
  2074. TypeError,
  2075. tx,
  2076. args=[ConstantVariable.create("failed to construct builtin set()")],
  2077. )
  2078. def call_frozenset(
  2079. self,
  2080. tx: "InstructionTranslator",
  2081. *args: VariableTracker,
  2082. **kwargs: VariableTracker,
  2083. ) -> VariableTracker:
  2084. assert not kwargs
  2085. if not args:
  2086. return FrozensetVariable([])
  2087. if len(args) != 1:
  2088. raise_observed_exception(
  2089. TypeError,
  2090. tx,
  2091. args=[
  2092. ConstantVariable.create(
  2093. f"frozenset() takes 1 positional argument but {len(args)} were given"
  2094. )
  2095. ],
  2096. )
  2097. arg = args[0]
  2098. if istype(arg, variables.FrozensetVariable):
  2099. return FrozensetVariable([x.vt for x in arg.set_items])
  2100. elif arg.has_force_unpack_var_sequence(tx):
  2101. items = arg.force_unpack_var_sequence(tx)
  2102. return FrozensetVariable(items)
  2103. raise_observed_exception(
  2104. TypeError,
  2105. tx,
  2106. args=[ConstantVariable.create("failed to construct builtin frozenset()")],
  2107. )
  2108. def call_zip(
  2109. self,
  2110. tx: "InstructionTranslator",
  2111. *args: VariableTracker,
  2112. **kwargs: VariableTracker,
  2113. ) -> VariableTracker:
  2114. from .builder import SourcelessBuilder
  2115. if kwargs:
  2116. if not (len(kwargs) == 1 and "strict" in kwargs):
  2117. raise_args_mismatch(
  2118. tx,
  2119. "zip",
  2120. "1 kwargs (`strict`)",
  2121. f"{len(kwargs)} kwargs",
  2122. )
  2123. strict = kwargs.pop("strict", ConstantVariable.create(False))
  2124. iter_args = [
  2125. SourcelessBuilder.create(tx, iter).call_function(tx, [arg], {})
  2126. for arg in args
  2127. ]
  2128. return variables.ZipVariable(
  2129. iter_args,
  2130. strict=strict.as_python_constant(),
  2131. mutation_type=ValueMutationNew(),
  2132. )
  2133. def call_len(
  2134. self,
  2135. tx: "InstructionTranslator",
  2136. *args: VariableTracker,
  2137. **kwargs: VariableTracker,
  2138. ) -> VariableTracker:
  2139. try:
  2140. return args[0].call_method(tx, "__len__", list(args[1:]), kwargs)
  2141. except AttributeError as e:
  2142. raise_observed_exception(type(e), tx, args=list(e.args))
  2143. def call_getitem(
  2144. self,
  2145. tx: "InstructionTranslator",
  2146. *args: VariableTracker,
  2147. **kwargs: VariableTracker,
  2148. ) -> VariableTracker:
  2149. return args[0].call_method(tx, "__getitem__", list(args[1:]), kwargs)
  2150. def call_isinstance(
  2151. self,
  2152. tx: "InstructionTranslator",
  2153. arg: VariableTracker,
  2154. isinstance_type_var: VariableTracker,
  2155. ) -> VariableTracker:
  2156. try:
  2157. arg_type = arg.python_type()
  2158. except NotImplementedError:
  2159. unimplemented(
  2160. gb_type="builtin isinstance() cannot determine type of argument",
  2161. context=f"isinstance({arg}, {isinstance_type_var})",
  2162. explanation=f"Dynamo doesn't have a rule to determine the type of argument {arg}",
  2163. hints=[*graph_break_hints.DYNAMO_BUG],
  2164. )
  2165. isinstance_type = isinstance_type_var.as_python_constant()
  2166. if isinstance(arg, variables.TensorVariable) and arg.dtype is not None:
  2167. def _tensor_isinstance(
  2168. tensor_var: VariableTracker, tensor_type: Any
  2169. ) -> bool:
  2170. def check_type(ty: Any) -> bool:
  2171. if ty not in tensortype_to_dtype:
  2172. example_val = arg.as_proxy().node.meta["example_value"]
  2173. if (
  2174. is_traceable_wrapper_subclass(example_val)
  2175. and ty is torch.nn.parameter.Parameter
  2176. ):
  2177. # N.B: we are calling isinstance directly on the example value.
  2178. # torch.nn.Parameter has a meta-class that overrides __isinstance__,
  2179. # the isinstance check here allows us to invoke that logic.
  2180. return isinstance(example_val, ty)
  2181. else:
  2182. return issubclass(arg.python_type(), ty)
  2183. dtypes = tensortype_to_dtype[ty]
  2184. # pyrefly: ignore [missing-attribute]
  2185. return arg.dtype in dtypes
  2186. if type(tensor_type) is tuple:
  2187. return any(check_type(ty) for ty in tensor_type)
  2188. else:
  2189. return check_type(tensor_type)
  2190. return variables.ConstantVariable.create(
  2191. _tensor_isinstance(arg, isinstance_type)
  2192. )
  2193. # UserDefinedObject with C extensions can have torch.Tensor attributes,
  2194. # so break graph.
  2195. if isinstance(arg, variables.UserDefinedObjectVariable) and isinstance(
  2196. arg.value, types.MemberDescriptorType
  2197. ):
  2198. unimplemented(
  2199. gb_type="isinstance() called on user defined object with C extensions",
  2200. context=f"isinstance({arg}, {isinstance_type})",
  2201. explanation="User-defined object with C extensions can have torch.Tensor "
  2202. "attributes; intentionally graph breaking.",
  2203. hints=[*graph_break_hints.SUPPORTABLE],
  2204. )
  2205. # handle __instancecheck__ defined in user class
  2206. if (
  2207. isinstance(arg, variables.UserDefinedObjectVariable)
  2208. and "__instancecheck__" in isinstance_type.__class__.__dict__
  2209. ):
  2210. return variables.ConstantVariable.create(
  2211. isinstance_type.__class__.__instancecheck__(isinstance_type, arg.value)
  2212. )
  2213. if isinstance(arg, variables.UserDefinedExceptionClassVariable):
  2214. # pyrefly: ignore [unbound-name]
  2215. return ConstantVariable.create(isinstance(arg_type, isinstance_type))
  2216. isinstance_type_tuple: tuple[type, ...]
  2217. if isinstance(isinstance_type, type) or callable(
  2218. # E.g. isinstance(obj, typing.Sequence)
  2219. getattr(isinstance_type, "__instancecheck__", None)
  2220. ):
  2221. isinstance_type_tuple = (isinstance_type,)
  2222. elif isinstance(isinstance_type, types.UnionType):
  2223. isinstance_type_tuple = isinstance_type.__args__
  2224. elif isinstance(isinstance_type, tuple) and all(
  2225. isinstance(tp, type) or callable(getattr(tp, "__instancecheck__", None))
  2226. for tp in isinstance_type
  2227. ):
  2228. isinstance_type_tuple = isinstance_type
  2229. else:
  2230. raise_observed_exception(
  2231. TypeError,
  2232. tx,
  2233. args=[
  2234. "isinstance() arg 2 must be a type, a tuple of types, or a union"
  2235. ],
  2236. )
  2237. try:
  2238. # NB: `isinstance()` does not call `__subclasscheck__` but use `__instancecheck__`.
  2239. # But usually `isinstance(obj, type_info)` and `issubclass(type(obj), type_info)` gives
  2240. # the same result.
  2241. # WARNING: This might run arbitrary user code `__subclasscheck__` and we did not trace
  2242. # through it. This is a limitation of the current implementation.
  2243. # Usually `__subclasscheck__` and `__instancecheck__` can be constant fold through, it
  2244. # might not be a big issue and we trade off it for performance.
  2245. # pyrefly: ignore [unbound-name]
  2246. val = issubclass(arg_type, isinstance_type_tuple)
  2247. except TypeError:
  2248. # pyrefly: ignore [unbound-name]
  2249. val = arg_type in isinstance_type_tuple
  2250. return variables.ConstantVariable.create(val)
  2251. def call_issubclass(
  2252. self,
  2253. tx: "InstructionTranslator",
  2254. left_ty: VariableTracker,
  2255. right_ty: VariableTracker,
  2256. ) -> VariableTracker:
  2257. """Checks if first arg is subclass of right arg"""
  2258. try:
  2259. left_ty_py = left_ty.as_python_constant()
  2260. right_ty_py = right_ty.as_python_constant()
  2261. except NotImplementedError:
  2262. unimplemented(
  2263. gb_type="issubclass() with non-constant arguments",
  2264. context=f"issubclass({left_ty}, {right_ty})",
  2265. explanation="issubclass() with non-constant arguments not supported.",
  2266. hints=[
  2267. "Make sure your arguments are types.",
  2268. *graph_break_hints.USER_ERROR,
  2269. ],
  2270. )
  2271. # WARNING: This might run arbitrary user code `__subclasscheck__`.
  2272. # See the comment in call_isinstance above.
  2273. # pyrefly: ignore [unbound-name]
  2274. return variables.ConstantVariable(issubclass(left_ty_py, right_ty_py))
  2275. def call_super(
  2276. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  2277. ) -> VariableTracker:
  2278. return variables.SuperVariable(a, b)
  2279. def call_next(
  2280. self, tx: "InstructionTranslator", *args: VariableTracker
  2281. ) -> VariableTracker:
  2282. arg = args[0]
  2283. try:
  2284. return arg.next_variable(tx)
  2285. except ObservedUserStopIteration:
  2286. if len(args) == 2:
  2287. return args[1]
  2288. raise
  2289. except Unsupported as ex:
  2290. if isinstance(arg, variables.BaseListVariable):
  2291. ex.remove_from_stats()
  2292. return arg.items[0]
  2293. raise
  2294. def call_hasattr(
  2295. self, tx: "InstructionTranslator", obj: VariableTracker, attr: VariableTracker
  2296. ) -> VariableTracker | None:
  2297. if attr.is_python_constant():
  2298. name = attr.as_python_constant()
  2299. if isinstance(obj, variables.BuiltinVariable):
  2300. return variables.ConstantVariable(hasattr(obj.fn, name))
  2301. return obj.call_obj_hasattr(tx, name)
  2302. return None
  2303. def call_map(
  2304. self,
  2305. tx: "InstructionTranslator",
  2306. fn: VariableTracker,
  2307. *seqs: VariableTracker,
  2308. **kwargs: VariableTracker,
  2309. ) -> VariableTracker:
  2310. strict = ConstantVariable.create(False)
  2311. if kwargs:
  2312. if sys.version_info >= (3, 14):
  2313. if not (len(kwargs) == 1 and "strict" in kwargs):
  2314. raise_args_mismatch(
  2315. tx,
  2316. "map",
  2317. "1 kwargs (`strict`)",
  2318. f"{len(kwargs)} kwargs",
  2319. )
  2320. strict = kwargs.pop("strict", ConstantVariable.create(False))
  2321. else:
  2322. raise_args_mismatch(
  2323. tx,
  2324. "map",
  2325. "0 kwargs",
  2326. f"{len(kwargs)} kwargs",
  2327. )
  2328. seq_list = [
  2329. seq.unpack_var_sequence(tx) if seq.has_unpack_var_sequence(tx) else seq
  2330. for seq in seqs
  2331. ]
  2332. return variables.MapVariable(
  2333. fn,
  2334. seq_list, # type: ignore[arg-type]
  2335. strict=strict.as_python_constant(),
  2336. mutation_type=ValueMutationNew(),
  2337. )
  2338. def call_filter(
  2339. self, tx: "InstructionTranslator", fn: VariableTracker, seq: VariableTracker
  2340. ) -> VariableTracker:
  2341. seq_or_list = (
  2342. seq.unpack_var_sequence(tx) if seq.has_unpack_var_sequence(tx) else seq
  2343. )
  2344. return variables.FilterVariable(
  2345. fn,
  2346. seq_or_list, # type: ignore[arg-type]
  2347. mutation_type=ValueMutationNew(),
  2348. )
  2349. def var_getattr(self, tx: "InstructionTranslator", name: str) -> VariableTracker:
  2350. source = self.source and AttrSource(self.source, name)
  2351. if self.fn is object:
  2352. # for object, we can just directly read the attribute
  2353. try:
  2354. value = getattr(self.fn, name)
  2355. except AttributeError:
  2356. raise_observed_exception(AttributeError, tx)
  2357. # pyrefly: ignore [unbound-name]
  2358. if not callable(value):
  2359. # pyrefly: ignore [unbound-name]
  2360. return VariableTracker.build(tx, value, source)
  2361. return variables.GetAttrVariable(self, name, source=source)
  2362. def call_getattr(
  2363. self,
  2364. tx: "InstructionTranslator",
  2365. obj: VariableTracker,
  2366. name_var: VariableTracker,
  2367. default: VariableTracker | None = None,
  2368. ) -> VariableTracker | None:
  2369. if not name_var.is_python_constant():
  2370. unimplemented(
  2371. gb_type="getattr() with non-constant name argument",
  2372. context=f"getattr({obj}, {name_var}, {default})",
  2373. explanation="getattr() with non-constant name argument is not supported",
  2374. hints=["Ensure the name argument of getattr() is a string"],
  2375. )
  2376. name = name_var.as_python_constant()
  2377. # See NOTE [Tensor "grad" and "_grad" attr]
  2378. if obj.is_tensor() and name == "_grad":
  2379. name = "grad"
  2380. if tx.output.side_effects.is_attribute_mutation(obj):
  2381. if isinstance(obj, variables.UnspecializedNNModuleVariable):
  2382. if (
  2383. name
  2384. in (
  2385. "named_parameters",
  2386. "parameters",
  2387. "named_buffers",
  2388. "buffers",
  2389. "named_modules",
  2390. "modules",
  2391. )
  2392. and obj.is_state_mutated
  2393. and tx.output.side_effects.has_pending_mutation(obj)
  2394. ):
  2395. unimplemented(
  2396. gb_type="getattr() on nn.Module with pending mutation",
  2397. context=f"getattr({obj}, {name}, {default})",
  2398. explanation="Intentionally graph breaking on getattr() on a nn.Module "
  2399. "with a pending mutation",
  2400. hints=[],
  2401. )
  2402. if tx.output.side_effects.has_pending_mutation_of_attr(obj, name):
  2403. return tx.output.side_effects.load_attr(obj, name)
  2404. if default is not None:
  2405. hasattr_var = self.call_hasattr(tx, obj, name_var)
  2406. if hasattr_var is not None:
  2407. assert hasattr_var.is_constant_match(True, False)
  2408. if not hasattr_var.as_python_constant():
  2409. return default
  2410. else:
  2411. return default
  2412. source = obj.source and AttrSource(obj.source, name)
  2413. if name in {"__bases__", "__base__", "__flags__"}:
  2414. try:
  2415. value = obj.as_python_constant()
  2416. if isinstance(value, type):
  2417. if name == "__bases__":
  2418. tuple_args = [
  2419. VariableTracker.build(
  2420. tx, b, source and GetItemSource(source, i)
  2421. )
  2422. for i, b in enumerate(value.__bases__)
  2423. ]
  2424. return variables.TupleVariable(tuple_args, source=source)
  2425. if name == "__base__":
  2426. return VariableTracker.build(tx, value.__base__, source)
  2427. if name == "__flags__":
  2428. return ConstantVariable.create(value.__flags__)
  2429. except NotImplementedError:
  2430. pass
  2431. if isinstance(obj, variables.NNModuleVariable):
  2432. return obj.var_getattr(tx, name)
  2433. elif isinstance(
  2434. obj,
  2435. (
  2436. variables.TensorVariable,
  2437. variables.NamedTupleVariable,
  2438. variables.ConstantVariable,
  2439. variables.DefaultDictVariable,
  2440. variables.DistributedVariable,
  2441. variables.UserDefinedClassVariable,
  2442. variables.UserDefinedObjectVariable,
  2443. ),
  2444. ):
  2445. if (
  2446. isinstance(obj, variables.UserDefinedObjectVariable)
  2447. and issubclass(obj.value.__class__, unittest.TestCase)
  2448. and config.enable_trace_unittest
  2449. and name
  2450. in (
  2451. "assertRaisesRegex",
  2452. "assertNotWarns",
  2453. "assertWarnsRegex",
  2454. "assertWarns",
  2455. )
  2456. ):
  2457. unimplemented(
  2458. gb_type="Failed to trace unittest method",
  2459. context=f"function: unittest.TestCase.{name}",
  2460. explanation=f"Dynamo does not know how to trace unittest method `{name}` ",
  2461. hints=[
  2462. f"Avoid calling `TestCase.{name}`. "
  2463. "Please report an issue to PyTorch.",
  2464. ],
  2465. )
  2466. if obj.is_tensor():
  2467. # pyrefly: ignore[missing-attribute]
  2468. fake_val = obj.as_proxy().node.meta["example_value"]
  2469. if (
  2470. isinstance(fake_val, torch.Tensor)
  2471. and is_sparse_any(fake_val)
  2472. and (not tx.export or not config.capture_sparse_compute)
  2473. ):
  2474. unimplemented(
  2475. gb_type="Attempted to wrap sparse Tensor",
  2476. context="",
  2477. explanation="torch.compile does not support sparse Tensors",
  2478. hints=[*graph_break_hints.SPARSE_TENSOR],
  2479. )
  2480. try:
  2481. return obj.var_getattr(tx, name)
  2482. except AsPythonConstantNotImplementedError:
  2483. # dont fallback on as_python_constant error because this leads
  2484. # to a failure later on, and leads to a wrong stacktrace
  2485. raise
  2486. except NotImplementedError:
  2487. return variables.GetAttrVariable(obj, name, source=source)
  2488. elif isinstance(obj, variables.TorchInGraphFunctionVariable):
  2489. # Get OpOverload from an OpOverloadPacket, e.g., torch.ops.aten.add.default.
  2490. member = getattr(obj.value, name)
  2491. if isinstance(
  2492. member, (torch._ops.OpOverloadPacket, torch._ops.OpOverload)
  2493. ) and torch._dynamo.trace_rules.is_aten_op_or_tensor_method(member):
  2494. return variables.TorchInGraphFunctionVariable(member, source=source)
  2495. elif name in cmp_name_to_op_mapping:
  2496. return variables.GetAttrVariable(obj, name, source=source)
  2497. else:
  2498. return None
  2499. elif isinstance(obj, DummyModule):
  2500. # TODO(mlazos) - Do we need this?
  2501. if obj.is_torch or name not in obj.value.__dict__:
  2502. member = getattr(obj.value, name)
  2503. else:
  2504. member = obj.value.__dict__[name]
  2505. if config.replay_record_enabled:
  2506. tx.exec_recorder.record_module_access(obj.value, name, member) # type: ignore[arg-type, union-attr]
  2507. return VariableTracker.build(tx, member, source)
  2508. elif istype(obj, variables.UserFunctionVariable) and name in (
  2509. "__name__",
  2510. "__module__",
  2511. ):
  2512. return ConstantVariable.create(getattr(obj.fn, name))
  2513. else:
  2514. try:
  2515. return obj.var_getattr(tx, name)
  2516. except NotImplementedError:
  2517. return variables.GetAttrVariable(obj, name, source=source)
  2518. def call_setattr(
  2519. self,
  2520. tx: "InstructionTranslator",
  2521. obj: VariableTracker,
  2522. name_var: VariableTracker,
  2523. val: VariableTracker,
  2524. ) -> VariableTracker | None:
  2525. if isinstance(
  2526. obj,
  2527. (
  2528. variables.DefaultDictVariable,
  2529. variables.PlacementVariable,
  2530. variables.NamedTupleVariable,
  2531. variables.UserDefinedObjectVariable,
  2532. variables.NestedUserFunctionVariable,
  2533. variables.ExceptionVariable,
  2534. variables.TracebackVariable,
  2535. ),
  2536. ):
  2537. return obj.call_method(tx, "__setattr__", [name_var, val], {})
  2538. elif (
  2539. tx.output.side_effects.is_attribute_mutation(obj)
  2540. and name_var.is_python_constant()
  2541. ):
  2542. name = name_var.as_python_constant()
  2543. if obj.is_tensor():
  2544. from .builder import wrap_fx_proxy
  2545. # Some special handling for tensor attributes.
  2546. if name == "requires_grad":
  2547. # TODO(voz): Make it work properly
  2548. unimplemented(
  2549. gb_type="setattr() on Tensor.requires_grad",
  2550. context=f"setattr({obj}, {name}, {val})",
  2551. explanation="setattr() on Tensor.requires_grad not supported. "
  2552. "Mutating requires_grad can introduce a new leaf from non-leaf or vice versa in "
  2553. "the middle of the graph, which AOTAutograd does not currently know how to handle.",
  2554. hints=[*graph_break_hints.SUPPORTABLE],
  2555. )
  2556. elif name == "data":
  2557. # See comments on `test_set_data_on_scoped_tensor` for plans
  2558. # to support this.
  2559. if obj.source is None:
  2560. unimplemented(
  2561. gb_type="Failed to mutate tensor data attribute",
  2562. context=f"setattr({obj}, {name}, {val})",
  2563. explanation="Dyanmo only supports mutating `.data`"
  2564. " of tensor created outside `torch.compile` region",
  2565. hints=[
  2566. "Don't mutate `.data` on this tensor, or move "
  2567. "the mutation out of `torch.compile` region",
  2568. ],
  2569. )
  2570. elif obj.dtype != val.dtype: # type: ignore[attr-defined]
  2571. unimplemented(
  2572. gb_type="Failed to mutate tensor data attribute to different dtype",
  2573. context=f"setattr({obj}, {name}, {val})",
  2574. explanation="Dyanmo only supports mutating `.data`"
  2575. " of tensor to a new one with the same dtype",
  2576. hints=[
  2577. "Don't mutate `.data` on this tensor, or move "
  2578. "the mutation out of `torch.compile` region",
  2579. ],
  2580. )
  2581. # Remove the old reference in tracked fakes - if we don't do this
  2582. # new .data value size and shape differences will cause
  2583. # tracked fakes to produce incorrect guards. This is sound because the TensorVariable
  2584. # coming out of set_() below will be a new one, and get
  2585. # installed in tracked fakes.
  2586. to_remove = [
  2587. tf for tf in tx.output.tracked_fakes if tf.source == obj.source
  2588. ]
  2589. for tf in to_remove:
  2590. tx.output.tracked_fakes.remove(tf)
  2591. # Step 1 - disable grads
  2592. with dynamo_disable_grad(tx), torch.no_grad():
  2593. # Step 2 - call `set_`
  2594. out = wrap_fx_proxy(
  2595. tx,
  2596. tx.output.create_proxy(
  2597. "call_function",
  2598. torch.Tensor.set_,
  2599. *proxy_args_kwargs([obj, val], {}),
  2600. ),
  2601. )
  2602. # Step 3 - drop the version counter - this is a step required to get
  2603. # .data setting to play correctly with the autograd engine.
  2604. # Essentially, dynamo is trying to faithfully preserve the (absurd)
  2605. # behavior of .data= from eager mode
  2606. def _lower_version_count_by_1(x: torch.Tensor) -> torch.Tensor:
  2607. version = x._version
  2608. if version > 0:
  2609. version = version - 1
  2610. torch._C._autograd._unsafe_set_version_counter((x,), (version,))
  2611. return x
  2612. tx.output.create_proxy(
  2613. "call_function",
  2614. _lower_version_count_by_1,
  2615. (out.as_proxy(),),
  2616. {},
  2617. )
  2618. _lower_version_count_by_1(obj.as_proxy().node.meta["example_value"])
  2619. # This handles options prop, guards and ends with a clone
  2620. # Step 4 - replace all reference to the current object with the new one
  2621. return out
  2622. elif name in ("_grad", "grad"):
  2623. # NOTE: [Tensor "grad" and "_grad" attr]
  2624. # _grad and grad share the same setter/getter, see
  2625. # THPVariable_properties, and here we make sure setting one
  2626. # enables reading `val` from the other, by routing all
  2627. # read/write to `grad`.
  2628. name = "grad"
  2629. elif is_tensor_getset_descriptor(name):
  2630. # Attribute like `torch.Tensor.real` has special setters we
  2631. # don't yet support; it's not as simple adding an entry to
  2632. # the side effect mapping.
  2633. unimplemented(
  2634. gb_type="Failed to set tensor attribute",
  2635. context=f"setattr({obj}, {name}, {val})",
  2636. explanation="Dyanmo doesn't support setting these tensor attributes",
  2637. hints=[
  2638. f"Don't mutate attribute '{name}' on tensors, or "
  2639. "move the mutation out of `torch.compile` region",
  2640. ],
  2641. )
  2642. tx.output.side_effects.store_attr(obj, name, val)
  2643. return val
  2644. elif isinstance(obj, variables.NNModuleVariable):
  2645. if not tx.output.is_root_tracer():
  2646. unimplemented(
  2647. gb_type="nn.Module mutation in HigherOrderOp",
  2648. context=f"nn.Module: {obj}",
  2649. explanation="Inplace modifying nn.Module params/buffers inside HigherOrderOps is not allowed.",
  2650. hints=[
  2651. "Remove the mutation or move it outside of the HigherOrderOp.",
  2652. *graph_break_hints.FUNDAMENTAL,
  2653. ],
  2654. )
  2655. if name_var.is_python_constant() and isinstance(
  2656. val, variables.TensorVariable
  2657. ):
  2658. assigning_fake_val = get_fake_value(val.as_proxy().node, tx)
  2659. try:
  2660. getattr_var = obj.var_getattr(tx, name_var.as_python_constant())
  2661. except (AttributeError, ObservedAttributeError):
  2662. getattr_var = None
  2663. if getattr_var is not None and getattr_var.is_tensor():
  2664. # get_fake_val will get the same fake tensor
  2665. existing_fake_attr = get_fake_value(getattr_var.as_proxy().node, tx)
  2666. # same tensor identity, setattr is a no-op
  2667. mod_setattr = inspect.getattr_static(obj.module_type, "__setattr__")
  2668. if (
  2669. existing_fake_attr is assigning_fake_val
  2670. and mod_setattr is torch.nn.Module.__setattr__
  2671. ):
  2672. return getattr_var
  2673. obj.convert_to_unspecialized(tx)
  2674. return None
  2675. def call_delattr(
  2676. self,
  2677. tx: "InstructionTranslator",
  2678. obj: VariableTracker,
  2679. name_var: VariableTracker,
  2680. ) -> VariableTracker:
  2681. return obj.call_method(tx, "__delattr__", [name_var], {})
  2682. def call_type(
  2683. self, tx: "InstructionTranslator", obj: VariableTracker
  2684. ) -> VariableTracker:
  2685. try:
  2686. py_type = obj.python_type()
  2687. except NotImplementedError as error:
  2688. raise UserError(
  2689. UserErrorType.INVALID_INPUT,
  2690. str(error),
  2691. case_name="unknown_python_type",
  2692. ) from None
  2693. source = obj.source and TypeSource(obj.source)
  2694. if (
  2695. source is None
  2696. and isinstance(obj, variables.UserDefinedObjectVariable)
  2697. and obj.cls_source
  2698. ):
  2699. source = obj.cls_source
  2700. if py_type is torch.Tensor:
  2701. # In some cases torch isn't available in globals
  2702. name = tx.output.install_global_by_id("", torch)
  2703. source = AttrSource(GlobalSource(name), "Tensor")
  2704. return VariableTracker.build(tx, py_type, source)
  2705. def call_reversed(
  2706. self, tx: "InstructionTranslator", obj: VariableTracker
  2707. ) -> VariableTracker | None:
  2708. if obj.has_unpack_var_sequence(tx):
  2709. items = list(reversed(obj.unpack_var_sequence(tx)))
  2710. return variables.TupleVariable(items)
  2711. return None
  2712. def call_sorted(
  2713. self,
  2714. tx: "InstructionTranslator",
  2715. obj: VariableTracker,
  2716. **kwargs: VariableTracker,
  2717. ) -> VariableTracker | None:
  2718. if obj.has_force_unpack_var_sequence(tx) and not isinstance(
  2719. obj, variables.TensorVariable
  2720. ):
  2721. list_var = variables.ListVariable(
  2722. obj.force_unpack_var_sequence(tx),
  2723. mutation_type=ValueMutationNew(),
  2724. )
  2725. list_var.call_method(tx, "sort", [], kwargs)
  2726. return list_var
  2727. return None
  2728. # neg is a constant fold function, so we only get here if constant fold is not valid
  2729. def call_neg(
  2730. self, tx: "InstructionTranslator", a: VariableTracker
  2731. ) -> VariableTracker | None:
  2732. if isinstance(a, SymNodeVariable):
  2733. return SymNodeVariable.create(
  2734. tx,
  2735. (operator.neg)(a.as_proxy()),
  2736. sym_num=None,
  2737. )
  2738. if (
  2739. isinstance(a, UserDefinedObjectVariable)
  2740. and a.call_obj_hasattr(tx, "__neg__").value # type: ignore[attr-defined]
  2741. ):
  2742. return a.call_method(tx, "__neg__", [], {})
  2743. # None no-ops this handler and lets the driving function proceed
  2744. return None
  2745. def call_format(
  2746. self,
  2747. tx: "InstructionTranslator",
  2748. _format_string: VariableTracker,
  2749. *args: VariableTracker,
  2750. **kwargs: VariableTracker,
  2751. ) -> VariableTracker:
  2752. format_string = _format_string.as_python_constant()
  2753. format_string = str(format_string)
  2754. return variables.StringFormatVariable.create(format_string, args, kwargs)
  2755. def call_id(
  2756. self, tx: "InstructionTranslator", *args: VariableTracker
  2757. ) -> VariableTracker:
  2758. if len(args) > 0 and isinstance(args[0], variables.NNModuleVariable):
  2759. nn_mod_variable = args[0]
  2760. mod = tx.output.get_submodule(nn_mod_variable.module_key)
  2761. return variables.ConstantVariable.create(id(mod))
  2762. elif len(args) == 1 and isinstance(
  2763. args[0],
  2764. (variables.UserDefinedClassVariable, variables.UserDefinedObjectVariable),
  2765. ):
  2766. if args[0].source:
  2767. if isinstance(args[0], variables.UserDefinedClassVariable):
  2768. install_guard(args[0].source.make_guard(GuardBuilder.CLASS_MATCH))
  2769. else:
  2770. install_guard(args[0].source.make_guard(GuardBuilder.ID_MATCH))
  2771. constant_result = id(args[0].value)
  2772. return variables.ConstantVariable.create(constant_result)
  2773. elif len(args) == 1 and args[0].is_tensor():
  2774. tensor_variable = cast(TensorVariable, args[0])
  2775. return tensor_variable.call_id(tx)
  2776. elif istype(args[0], variables.UserFunctionVariable):
  2777. return variables.ConstantVariable.create(id(args[0].fn))
  2778. elif istype(args[0], variables.SkipFunctionVariable):
  2779. return variables.ConstantVariable.create(id(args[0].value))
  2780. elif istype(args[0], variables.FunctoolsPartialVariable):
  2781. return variables.ConstantVariable.create(id(args[0].fake_value))
  2782. else:
  2783. unimplemented(
  2784. gb_type="id() with unsupported args",
  2785. context=str(args),
  2786. explanation=f"Dynamo doesn't know how to trace id() call with args {args}",
  2787. hints=[
  2788. "Supported args are Tensors, and functions/nn.Modules/user-defined objects "
  2789. "from outside the compiled region.",
  2790. *graph_break_hints.SUPPORTABLE,
  2791. ],
  2792. )
  2793. def call_deepcopy(
  2794. self, tx: "InstructionTranslator", x: VariableTracker
  2795. ) -> VariableTracker:
  2796. unimplemented(
  2797. gb_type="copy.deepcopy()",
  2798. context=f"copy.deepcopy({x})",
  2799. explanation="Dynamo does not support copy.deepcopy()",
  2800. hints=[
  2801. "Avoid calling copy.deepcopy()",
  2802. *graph_break_hints.SUPPORTABLE,
  2803. ],
  2804. )
  2805. def _comparison_with_tensor(
  2806. self, tx: "InstructionTranslator", left: VariableTracker, right: VariableTracker
  2807. ) -> VariableTracker:
  2808. from .builder import wrap_fx_proxy_cls
  2809. from .tensor import supported_tensor_comparison_op_values
  2810. op = self.fn
  2811. if op in [operator.is_, operator.is_not]:
  2812. is_result = (
  2813. left.is_tensor()
  2814. and right.is_tensor()
  2815. and id(extract_fake_example_value(left.as_proxy().node))
  2816. == id(extract_fake_example_value(right.as_proxy().node))
  2817. )
  2818. if op is operator.is_:
  2819. return ConstantVariable.create(is_result)
  2820. else:
  2821. return ConstantVariable.create(not is_result)
  2822. if op not in supported_tensor_comparison_op_values:
  2823. unimplemented(
  2824. gb_type="unsupported Tensor comparison op",
  2825. context=f"{op.__name__}({left}, {right})",
  2826. explanation=f"Dynamo does not support the comparison op {op.__name__} "
  2827. f"with Tensor arguments {left}, {right}",
  2828. hints=[*graph_break_hints.SUPPORTABLE],
  2829. )
  2830. if (
  2831. isinstance(left, TensorVariable)
  2832. and isinstance(right, TensorVariable)
  2833. and (left.size and right.size) is not None
  2834. and left.size != right.size
  2835. ):
  2836. try:
  2837. torch.broadcast_shapes(left.size, right.size)
  2838. except RuntimeError:
  2839. # not broadcastable, can't be compared
  2840. unimplemented(
  2841. gb_type="failed to broadcast when attempting Tensor comparison op",
  2842. context=f"{op.__name__}({left}, {right})",
  2843. explanation=f"Dynamo was unable to broad cast the arguments {left}, {right} "
  2844. f"when attempting to trace the comparison op {op.__name__}.",
  2845. hints=[*graph_break_hints.USER_ERROR],
  2846. )
  2847. tensor_cls = left if left.is_tensor() else right
  2848. proxy = tx.output.create_proxy(
  2849. "call_function", op, (left.as_proxy(), right.as_proxy()), {}
  2850. )
  2851. return wrap_fx_proxy_cls(
  2852. type(tensor_cls), # handle Ndarrays and Tensors
  2853. tx,
  2854. proxy,
  2855. )
  2856. def _comparison_with_symnode(
  2857. self, tx: "InstructionTranslator", left: VariableTracker, right: VariableTracker
  2858. ) -> VariableTracker:
  2859. from .tensor import supported_tensor_comparison_op_values
  2860. op = self.fn
  2861. if op not in supported_tensor_comparison_op_values:
  2862. unimplemented(
  2863. gb_type="unsupported SymNode comparison op",
  2864. context=f"{op.__name__}({left}, {right})",
  2865. explanation=f"Dynamo does not support the comparison op {op.__name__} "
  2866. f"with SymNode arguments {left}, {right}",
  2867. hints=[*graph_break_hints.SUPPORTABLE],
  2868. )
  2869. # This is seen in inspect signature where we check if the value is a default value
  2870. if isinstance(right, variables.UserDefinedClassVariable):
  2871. return variables.ConstantVariable(op(object(), None))
  2872. proxy = tx.output.create_proxy(
  2873. "call_function", op, (left.as_proxy(), right.as_proxy()), {}
  2874. )
  2875. return SymNodeVariable.create(
  2876. tx,
  2877. proxy,
  2878. sym_num=None,
  2879. )
  2880. def call_xor(
  2881. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  2882. ) -> VariableTracker | None:
  2883. # Rely on constant_handler
  2884. if isinstance(a, ConstantVariable) and isinstance(b, ConstantVariable):
  2885. return None
  2886. if a.is_symnode_like() and b.is_symnode_like():
  2887. return SymNodeVariable.create(
  2888. tx,
  2889. tx.output.create_proxy(
  2890. "call_function", operator.xor, *proxy_args_kwargs([a, b], {})
  2891. ),
  2892. sym_num=None,
  2893. )
  2894. if isinstance(
  2895. a,
  2896. (DictKeysVariable, SetVariable, UserDefinedObjectVariable),
  2897. ):
  2898. return a.call_method(tx, "__xor__", [b], {})
  2899. return None
  2900. def call_ixor(
  2901. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  2902. ) -> VariableTracker | None:
  2903. if isinstance(a, (DictKeysVariable, SetVariable, UserDefinedObjectVariable)):
  2904. return a.call_method(tx, "__ixor__", [b], {})
  2905. return None
  2906. def call_sub(
  2907. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  2908. ) -> VariableTracker | None:
  2909. if isinstance(a, (DictKeysVariable, SetVariable, UserDefinedObjectVariable)):
  2910. return a.call_method(tx, "__sub__", [b], {})
  2911. return None
  2912. def call_isub(
  2913. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  2914. ) -> VariableTracker | None:
  2915. if isinstance(a, (DictKeysVariable, SetVariable, UserDefinedObjectVariable)):
  2916. return a.call_method(tx, "__isub__", [b], {})
  2917. return None
  2918. def call_and_(
  2919. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  2920. ) -> VariableTracker | None:
  2921. # Rely on constant_handler
  2922. if isinstance(a, ConstantVariable) and isinstance(b, ConstantVariable):
  2923. return None
  2924. if a.is_symnode_like() and b.is_symnode_like():
  2925. return SymNodeVariable.create(
  2926. tx,
  2927. tx.output.create_proxy(
  2928. "call_function", operator.and_, *proxy_args_kwargs([a, b], {})
  2929. ),
  2930. sym_num=None,
  2931. )
  2932. if isinstance(a, (DictKeysVariable, SetVariable, UserDefinedObjectVariable)):
  2933. return a.call_method(tx, "__and__", [b], {})
  2934. # None no-ops this handler and lets the driving function proceed
  2935. return None
  2936. def call_iand(
  2937. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  2938. ) -> VariableTracker | None:
  2939. # Rely on constant_handler
  2940. if isinstance(a, ConstantVariable) and isinstance(b, ConstantVariable):
  2941. return None
  2942. if a.is_symnode_like() and b.is_symnode_like():
  2943. return SymNodeVariable.create(
  2944. tx,
  2945. tx.output.create_proxy(
  2946. "call_function", operator.iand, *proxy_args_kwargs([a, b], {})
  2947. ),
  2948. sym_num=None,
  2949. )
  2950. if isinstance(a, (DictKeysVariable, SetVariable, UserDefinedObjectVariable)):
  2951. return a.call_method(tx, "__iand__", [b], {})
  2952. return None
  2953. def call_or_(
  2954. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  2955. ) -> VariableTracker | None:
  2956. # Rely on constant_handler
  2957. if isinstance(a, ConstantVariable) and isinstance(b, ConstantVariable):
  2958. return None
  2959. if a.is_symnode_like() and b.is_symnode_like():
  2960. return SymNodeVariable.create(
  2961. tx,
  2962. tx.output.create_proxy(
  2963. "call_function", operator.or_, *proxy_args_kwargs([a, b], {})
  2964. ),
  2965. sym_num=None,
  2966. )
  2967. # This call looks like `{"one": torch.ones(1)} | {"two": torch.ones(2)}`.
  2968. if isinstance(
  2969. a,
  2970. (
  2971. ConstDictVariable,
  2972. DictKeysVariable,
  2973. MutableMappingVariable,
  2974. SetVariable,
  2975. UserDefinedDictVariable,
  2976. UserDefinedObjectVariable,
  2977. ),
  2978. ):
  2979. # TODO(guilhermeleobas): forward the call to b.__ror__(a) if
  2980. # a.__ror__(b) returns NotImplemented
  2981. return a.call_method(tx, "__or__", [b], {})
  2982. # None no-ops this handler and lets the driving function proceed
  2983. return None
  2984. def call_ior(
  2985. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  2986. ) -> VariableTracker | None:
  2987. # Rely on constant_handler
  2988. if isinstance(a, ConstantVariable) and isinstance(b, ConstantVariable):
  2989. return None
  2990. if a.is_symnode_like() and b.is_symnode_like():
  2991. return SymNodeVariable.create(
  2992. tx,
  2993. tx.output.create_proxy(
  2994. "call_function", operator.ior, *proxy_args_kwargs([a, b], {})
  2995. ),
  2996. sym_num=None,
  2997. )
  2998. # This call looks like `{"one": torch.ones(1)} |= {"two": torch.ones(2)}`.
  2999. if isinstance(
  3000. a,
  3001. (
  3002. ConstDictVariable,
  3003. DictKeysVariable,
  3004. MutableMappingVariable,
  3005. SetVariable,
  3006. UserDefinedObjectVariable,
  3007. ),
  3008. ):
  3009. return a.call_method(tx, "__ior__", [b], {})
  3010. # None no-ops this handler and lets the driving function proceed
  3011. return None
  3012. def call_not_(
  3013. self, tx: "InstructionTranslator", a: VariableTracker
  3014. ) -> VariableTracker | None:
  3015. if isinstance(a, SymNodeVariable):
  3016. return SymNodeVariable.create(
  3017. tx,
  3018. tx.output.create_proxy(
  3019. "call_function", operator.not_, *proxy_args_kwargs([a], {})
  3020. ),
  3021. sym_num=None,
  3022. )
  3023. # Unwrap the underlying ConstDictVariable
  3024. if isinstance(a, DictViewVariable):
  3025. a = a.dv_dict
  3026. if isinstance(a, (ListVariable, ConstDictVariable)):
  3027. return ConstantVariable.create(len(a.items) == 0)
  3028. return None
  3029. def call_contains(
  3030. self, tx: "InstructionTranslator", a: VariableTracker, b: VariableTracker
  3031. ) -> VariableTracker:
  3032. return a.call_method(tx, "__contains__", [b], {})
  3033. def is_python_hashable(self) -> Literal[True]:
  3034. return True
  3035. def get_python_hash(self) -> int:
  3036. return hash(self.fn)
  3037. def is_python_equal(self, other: object) -> bool:
  3038. return isinstance(other, variables.BuiltinVariable) and self.fn is other.fn
  3039. @contextlib.contextmanager
  3040. def dynamo_disable_grad(tx: "InstructionTranslator") -> typing.Iterator[None]:
  3041. from . import GradModeVariable
  3042. gmv = GradModeVariable.create(tx, False)
  3043. try:
  3044. gmv.enter(tx)
  3045. yield
  3046. finally:
  3047. gmv.exit(tx)