dicts.py 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  1. """
  2. Dictionary-related variable tracking classes for PyTorch Dynamo.
  3. This module implements variable tracking for different types of dictionary-like objects:
  4. - Regular Python dictionaries (dict)
  5. - Ordered dictionaries (collections.OrderedDict)
  6. - Default dictionaries (collections.defaultdict)
  7. - Dictionary views (keys and values)
  8. - Sets and frozensets (implemented internally using dictionaries)
  9. These classes are responsible for tracking dictionary operations during graph compilation,
  10. maintaining proper guards for dictionary mutations and key existence checks. They handle
  11. dictionary creation, modification, key/value access, and view operations while ensuring
  12. correct behavior in the compiled code through appropriate guard installation.
  13. The implementation uses a special _HashableTracker wrapper to handle dictionary keys
  14. while preserving proper aliasing semantics. Sets are implemented as dictionaries with
  15. None values for efficiency and code reuse.
  16. """
  17. import collections
  18. import functools
  19. import operator
  20. import types
  21. from collections.abc import Iterable, Sequence
  22. from typing import Any, Literal, Optional, TYPE_CHECKING, Union
  23. from torch.utils._ordered_set import OrderedSet
  24. from .. import graph_break_hints, polyfills, variables
  25. from ..bytecode_transformation import (
  26. create_call_function,
  27. create_call_method,
  28. create_dup_top,
  29. create_instruction,
  30. )
  31. from ..exc import raise_observed_exception, unimplemented
  32. from ..guards import GuardBuilder, install_guard
  33. from ..source import AttrSource, is_constant_source, is_from_local_source
  34. from ..utils import (
  35. cmp_name_to_op_mapping,
  36. dict_items,
  37. dict_keys,
  38. dict_values,
  39. istype,
  40. raise_args_mismatch,
  41. specialize_symnode,
  42. )
  43. from .base import ValueMutationNew, VariableTracker
  44. from .constant import CONSTANT_VARIABLE_NONE, ConstantVariable
  45. if TYPE_CHECKING:
  46. from torch._dynamo.codegen import PyCodegen
  47. from torch._dynamo.symbolic_convert import InstructionTranslator
  48. from torch._dynamo.variables.builtin import BuiltinVariable
  49. from .functions import UserFunctionVariable
  50. # [Adding a new supported class within the keys of ConstDictVariable]
  51. # - Implement is_python_hashable() method in the VariableTracker subclass
  52. # - Implement get_python_hash() and is_python_equal() methods for hashable types
  53. def was_instancecheck_override(obj: Any) -> bool:
  54. return type(obj).__dict__.get("__instancecheck__", False)
  55. def raise_unhashable(
  56. arg: VariableTracker, tx: Optional["InstructionTranslator"] = None
  57. ) -> None:
  58. from .builder import SourcelessBuilder
  59. if tx is None:
  60. from torch._dynamo.symbolic_convert import InstructionTranslator
  61. tx = InstructionTranslator.current_tx()
  62. try:
  63. arg_type = arg.python_type()
  64. except Exception:
  65. arg_type = type(arg)
  66. raise_observed_exception(
  67. TypeError,
  68. tx,
  69. args=[
  70. SourcelessBuilder.create(
  71. tx,
  72. f"unhashable type: {arg_type!r} and variable tracker = {type(arg.realize())}",
  73. )
  74. ],
  75. )
  76. def is_hashable(x: VariableTracker) -> bool:
  77. # NB - performing isinstance check on a LazVT realizes the VT, accidentally
  78. # inserting the guard. To avoid this, lazyVT `is_hashable` methods looks at
  79. # the underlying value without realizing the VT. Consider updating the
  80. # lazyVT `is_hashable` method if you see unnecessary guarding for a key VT.
  81. if (
  82. isinstance(x, variables.LazyVariableTracker)
  83. and not x.is_realized()
  84. and x.is_hashable()
  85. ):
  86. return True
  87. return x.is_python_hashable()
  88. class ConstDictVariable(VariableTracker):
  89. CONTAINS_GUARD = GuardBuilder.DICT_CONTAINS
  90. _nonvar_fields = {
  91. "user_cls",
  92. *VariableTracker._nonvar_fields,
  93. }
  94. class _HashableTracker:
  95. """
  96. Auxiliary opaque internal class that wraps a VariableTracker and makes it hashable
  97. This should not be seen or touched by anything outside of ConstDictVariable and its children
  98. Note that it's also fine to put VTs into dictionaries and sets, but doing so does not take into account aliasing
  99. """
  100. def __init__(self, vt: VariableTracker) -> None:
  101. # We specialize SymNodes
  102. vt = specialize_symnode(vt)
  103. # If Dynamo does not know the hashability of the vt, it will raise unsupported here
  104. if not is_hashable(vt):
  105. raise_unhashable(vt)
  106. self.vt = vt
  107. def __hash__(self) -> int:
  108. """
  109. Computes the hash value for the wrapped VariableTracker.
  110. For unrealized LazyVariableTrackers, uses the hash of the original value
  111. to avoid realizing the tracker and inserting unnecessary guards.
  112. For all other cases, delegates to the VariableTracker's get_python_hash method.
  113. Returns:
  114. The hash value of the underlying variable tracker
  115. """
  116. if (
  117. isinstance(self.vt, variables.LazyVariableTracker)
  118. and not self.vt.is_realized()
  119. and self.vt.is_hashable()
  120. ):
  121. return hash(self.vt.original_value())
  122. return self.vt.get_python_hash()
  123. def __eq__(self, other: object) -> bool:
  124. """
  125. Checks equality between two _HashableTracker instances.
  126. Delegates to the VariableTracker's is_python_equal method to compare
  127. the underlying variable trackers for Python-level equality.
  128. Args:
  129. other: Another _HashableTracker instance to compare with
  130. Returns:
  131. True if the underlying variable trackers are Python-equal, False otherwise
  132. """
  133. if not isinstance(other, ConstDictVariable._HashableTracker):
  134. return False
  135. if self.vt is other.vt:
  136. return True
  137. return self.vt.is_python_equal(other.vt)
  138. def __init__(
  139. self,
  140. items: dict[VariableTracker, VariableTracker],
  141. user_cls: type = dict,
  142. **kwargs: Any,
  143. ) -> None:
  144. # .clone() pass these arguments in kwargs but they're recreated a few
  145. # lines below
  146. if "original_items" in kwargs:
  147. kwargs.pop("original_items")
  148. if "should_reconstruct_all" in kwargs:
  149. kwargs.pop("should_reconstruct_all")
  150. super().__init__(**kwargs)
  151. Hashable = ConstDictVariable._HashableTracker
  152. # Keys will just be HashableTrackers when cloning, in any other case they'll be VariableTrackers
  153. assert all(
  154. isinstance(x, (VariableTracker, Hashable))
  155. and isinstance(v, VariableTracker)
  156. for x, v in items.items()
  157. )
  158. def make_hashable(
  159. key: Union[VariableTracker, "ConstDictVariable._HashableTracker"],
  160. ) -> "ConstDictVariable._HashableTracker":
  161. return key if isinstance(key, Hashable) else Hashable(key)
  162. dict_cls = self._get_dict_cls_from_user_cls(user_cls)
  163. self.items = dict_cls({make_hashable(x): v for x, v in items.items()})
  164. # need to reconstruct everything if the dictionary is an intermediate value
  165. # or if a pop/delitem was executed
  166. self.should_reconstruct_all = (
  167. not is_from_local_source(self.source) if self.source else True
  168. )
  169. self.original_items = items.copy()
  170. self.user_cls = user_cls
  171. def _get_dict_cls_from_user_cls(self, user_cls: type) -> type:
  172. accepted_dict_types = (dict, collections.OrderedDict, collections.defaultdict)
  173. # avoid executing user code if user_cls is a dict subclass
  174. if user_cls in accepted_dict_types:
  175. dict_cls = user_cls
  176. else:
  177. # <Subclass, ..., dict, object>
  178. dict_cls = next(
  179. base for base in user_cls.__mro__ if base in accepted_dict_types
  180. )
  181. assert dict_cls in accepted_dict_types, dict_cls
  182. # Use a dict instead as the call "defaultdict({make_hashable(x): v ..})"
  183. # would fail as defaultdict expects a callable as first argument
  184. if dict_cls is collections.defaultdict:
  185. dict_cls = dict
  186. return dict_cls
  187. def as_proxy(self) -> dict[Any, Any]:
  188. return {k.vt.as_proxy(): v.as_proxy() for k, v in self.items.items()}
  189. def debug_repr(self) -> str:
  190. items: list[str] = []
  191. for k, v in self.items.items():
  192. key_str = repr(k.vt.value) if hasattr(k.vt, "value") else k.vt.debug_repr()
  193. val_str = repr(v.value) if hasattr(v, "value") else v.debug_repr()
  194. items.append(f"{key_str}: {val_str}")
  195. return "{" + ", ".join(items) + "}"
  196. def as_python_constant(self) -> dict[Any, Any]:
  197. return {
  198. k.vt.as_python_constant(): v.as_python_constant()
  199. for k, v in self.items.items()
  200. }
  201. def keys_as_python_constant(self) -> dict[Any, VariableTracker]:
  202. self.install_dict_keys_match_guard()
  203. return {k.vt.as_python_constant(): v for k, v in self.items.items()}
  204. def python_type(self) -> type:
  205. return self.user_cls
  206. def __contains__(self, vt: VariableTracker) -> bool:
  207. assert isinstance(vt, VariableTracker)
  208. Hashable = ConstDictVariable._HashableTracker
  209. return (
  210. vt.is_python_hashable()
  211. and Hashable(vt) in self.items
  212. and not isinstance(self.items[Hashable(vt)], variables.DeletedVariable)
  213. )
  214. def call_tree_map_branch(
  215. self,
  216. tx: "InstructionTranslator",
  217. tree_map_fn: "UserFunctionVariable",
  218. map_fn: VariableTracker,
  219. rest: Sequence[VariableTracker],
  220. tree_map_kwargs: dict[str, VariableTracker],
  221. ) -> VariableTracker:
  222. other_dicts: list[ConstDictVariable] = []
  223. for candidate in rest:
  224. candidate = candidate.realize()
  225. if not isinstance(candidate, ConstDictVariable) or len(
  226. candidate.items
  227. ) != len(self.items):
  228. return self._tree_map_fallback(
  229. tx, tree_map_fn, map_fn, rest, tree_map_kwargs
  230. )
  231. other_dicts.append(candidate)
  232. new_items_hashed = type(self.items)()
  233. for key_tracker, value in self.items.items():
  234. sibling_leaves: list[VariableTracker] = []
  235. for candidate in other_dicts:
  236. try:
  237. sibling_leaves.append(candidate.items[key_tracker])
  238. except KeyError:
  239. return self._tree_map_fallback(
  240. tx, tree_map_fn, map_fn, rest, tree_map_kwargs
  241. )
  242. new_items_hashed[key_tracker] = value.call_tree_map(
  243. tx,
  244. tree_map_fn,
  245. map_fn,
  246. sibling_leaves,
  247. tree_map_kwargs,
  248. )
  249. updated_original_items = {
  250. key_tracker.vt: new_items_hashed[key_tracker]
  251. for key_tracker in new_items_hashed
  252. }
  253. return self.clone(
  254. items=new_items_hashed,
  255. original_items=updated_original_items,
  256. should_reconstruct_all=True,
  257. source=None,
  258. mutation_type=ValueMutationNew(),
  259. )
  260. def len(self) -> int:
  261. return sum(
  262. not isinstance(x, variables.DeletedVariable) for x in self.items.values()
  263. )
  264. def has_new_items(self) -> bool:
  265. return self.should_reconstruct_all or any(
  266. self.is_new_item(self.original_items.get(key.vt), value)
  267. for key, value in self.items.items()
  268. )
  269. def is_new_item(
  270. self, value: Optional[VariableTracker], other: VariableTracker
  271. ) -> bool:
  272. # compare the id of the realized values if both values are not lazy VTs
  273. if value and value.is_realized() and other.is_realized():
  274. return id(value.realize()) != id(other.realize())
  275. return id(value) != id(other)
  276. def reconstruct_kvs_into_new_dict(self, codegen: "PyCodegen") -> None:
  277. # Build a dictionary that contains the keys and values.
  278. num_args = 0
  279. for key, value in self.items.items():
  280. # We can safely call realize() here as it won't introduce any new guards
  281. item = self.original_items.get(key.vt)
  282. if self.is_new_item(item, value) or self.should_reconstruct_all:
  283. codegen(key.vt)
  284. codegen(value)
  285. num_args += 1
  286. codegen.append_output(create_instruction("BUILD_MAP", arg=num_args))
  287. def reconstruct(self, codegen: "PyCodegen") -> None:
  288. if self.user_cls is collections.OrderedDict:
  289. # emit `OrderedDict(constructed_dict)`
  290. codegen.add_push_null(
  291. lambda: codegen.extend_output(
  292. [
  293. codegen.create_load_python_module(collections),
  294. codegen.create_load_attr("OrderedDict"),
  295. ]
  296. )
  297. )
  298. if self._contains_self_reference():
  299. codegen.extend_output(
  300. [
  301. *create_call_function(0, False),
  302. create_dup_top(),
  303. ]
  304. )
  305. codegen.add_cache(self)
  306. codegen.append_output(create_dup_top())
  307. codegen.load_method("update")
  308. self.reconstruct_kvs_into_new_dict(codegen)
  309. codegen.extend_output(
  310. [
  311. *create_call_method(1),
  312. create_instruction("POP_TOP"),
  313. ]
  314. )
  315. else:
  316. self.reconstruct_kvs_into_new_dict(codegen)
  317. codegen.extend_output(create_call_function(1, False))
  318. else:
  319. if self._contains_self_reference():
  320. codegen.extend_output(
  321. [
  322. create_instruction("BUILD_MAP", arg=0),
  323. create_dup_top(),
  324. ]
  325. )
  326. codegen.add_cache(self)
  327. self.reconstruct_kvs_into_new_dict(codegen)
  328. codegen.append_output(create_instruction("DICT_UPDATE", arg=1))
  329. else:
  330. # Non-self-referential: use simple codegen
  331. self.reconstruct_kvs_into_new_dict(codegen)
  332. def getitem_const_raise_exception_if_absent(
  333. self, tx: "InstructionTranslator", arg: VariableTracker
  334. ) -> VariableTracker:
  335. key = ConstDictVariable._HashableTracker(arg)
  336. if key not in self.items:
  337. try:
  338. error_message = (
  339. f"Dict key lookup failed for {str(arg)}. "
  340. f"Debug representation of the key is {arg.debug_repr()!r}"
  341. )
  342. except Exception:
  343. error_message = ConstantVariable.create(
  344. f"Dict key lookup failed for {str(arg)}"
  345. )
  346. raise_observed_exception(KeyError, tx, args=[error_message])
  347. return self.items[key]
  348. def getitem_const(
  349. self, tx: "InstructionTranslator", arg: VariableTracker
  350. ) -> VariableTracker:
  351. key = ConstDictVariable._HashableTracker(arg)
  352. if key not in self.items:
  353. msg = f"Dictionary key {arg.value} not found during tracing" # type: ignore[attr-defined]
  354. unimplemented(
  355. gb_type="key not found in dict",
  356. context=f"Key {arg.value}", # type: ignore[attr-defined]
  357. explanation=msg,
  358. hints=[
  359. "Check if the key exists in the dictionary before accessing it.",
  360. *graph_break_hints.USER_ERROR,
  361. ],
  362. )
  363. return self.items[key]
  364. def maybe_getitem_const(self, arg: VariableTracker) -> Optional[VariableTracker]:
  365. key = ConstDictVariable._HashableTracker(arg)
  366. if key not in self.items:
  367. return None
  368. return self.items[key]
  369. def realize_key_vt(self, arg: VariableTracker) -> None:
  370. # Realize the LazyVT on a particular index
  371. assert arg in self
  372. key = ConstDictVariable._HashableTracker(arg)
  373. index = tuple(self.items.keys()).index(key)
  374. original_key_vt = tuple(self.original_items.keys())[index]
  375. if isinstance(original_key_vt, variables.LazyVariableTracker):
  376. original_key_vt.realize()
  377. def install_dict_keys_match_guard(self) -> None:
  378. if self.source:
  379. install_guard(self.make_guard(GuardBuilder.DICT_KEYS_MATCH))
  380. def install_dict_contains_guard(
  381. self, tx: "InstructionTranslator", args: list[VariableTracker]
  382. ) -> None:
  383. # Key guarding - These are the cases to consider
  384. # 1) The dict has been mutated. In this case, we would have already
  385. # inserted a DICT_KEYS_MATCH guard, so we can skip.
  386. #
  387. # 2) args[0].source is None. This happens for const keys. Here, we
  388. # have to insert the DICT_CONTAINS guard.
  389. #
  390. # 3) args[0].source is not None. This can happen for non-const VTs.
  391. # 3a) contains=True. In this case, we can access the lazyVT from
  392. # original_items and selectively realize it.
  393. # 3b) contains=False. There is no easy way to selectively apply this
  394. # DICT_NOT_CONTAINS guard because our guard are represented via trees.
  395. # Be conservative and add DICT_KEYS_MATCH guard.
  396. if not self.source:
  397. return
  398. if tx.output.side_effects.is_modified(self):
  399. return
  400. contains = args[0] in self
  401. if args[0].source is None and args[0].is_python_constant():
  402. install_guard(
  403. self.make_guard(
  404. functools.partial(
  405. type(self).CONTAINS_GUARD,
  406. key=args[0].as_python_constant(),
  407. invert=not contains,
  408. )
  409. )
  410. )
  411. elif args[0].source:
  412. if contains:
  413. self.realize_key_vt(args[0])
  414. else:
  415. self.install_dict_keys_match_guard()
  416. def call_method(
  417. self,
  418. tx: "InstructionTranslator",
  419. name: str,
  420. args: list[VariableTracker],
  421. kwargs: dict[str, VariableTracker],
  422. ) -> VariableTracker:
  423. # NB - Both key and value are LazyVariableTrackers in the beginning. So,
  424. # we have to insert guards when a dict method is accessed. For this to
  425. # be simple, we are conservative and overguard. We skip guard only for
  426. # get/__getitem__ because the key guard will be inserted by the
  427. # corresponding value VT. For __contains__, we add a DICT_CONTAINS
  428. # guard. But for all the other methods, we insert the DICT_KEYS_MATCH
  429. # guard to be conservative.
  430. from . import BuiltinVariable, ConstantVariable
  431. from .builder import SourcelessBuilder
  432. Hashable = ConstDictVariable._HashableTracker
  433. if name == "__init__":
  434. temp_dict_vt = SourcelessBuilder.create(tx, dict).call_dict(
  435. tx, *args, **kwargs
  436. )
  437. tx.output.side_effects.mutation(self)
  438. self.items.update(temp_dict_vt.items) # type: ignore[attr-defined]
  439. return CONSTANT_VARIABLE_NONE
  440. elif name == "__getitem__":
  441. # Key guarding - Nothing to do. LazyVT for value will take care.
  442. if len(args) != 1:
  443. raise_args_mismatch(tx, name, "1 args", f"{len(args)} args")
  444. return self.getitem_const_raise_exception_if_absent(tx, args[0])
  445. elif name == "items":
  446. if args or kwargs:
  447. raise_args_mismatch(
  448. tx,
  449. name,
  450. "0 args and 0 kwargs",
  451. f"{len(args)} args and {len(kwargs)} kwargs",
  452. )
  453. self.install_dict_keys_match_guard()
  454. if self.source:
  455. tx.output.guard_on_key_order.add(self.source)
  456. return DictItemsVariable(self)
  457. elif name == "keys":
  458. if len(args):
  459. raise_args_mismatch(tx, name, "0 args", f"{len(args)} args")
  460. self.install_dict_keys_match_guard()
  461. if self.source:
  462. tx.output.guard_on_key_order.add(self.source)
  463. return DictKeysVariable(self)
  464. elif name == "values":
  465. if args or kwargs:
  466. raise_args_mismatch(
  467. tx,
  468. name,
  469. "0 args and 0 kwargs",
  470. f"{len(args)} args and {len(kwargs)} kwargs",
  471. )
  472. self.install_dict_keys_match_guard()
  473. if self.source:
  474. tx.output.guard_on_key_order.add(self.source)
  475. if args or kwargs:
  476. raise_observed_exception(TypeError, tx)
  477. return DictValuesVariable(self)
  478. elif name == "copy":
  479. self.install_dict_keys_match_guard()
  480. if args or kwargs:
  481. raise_args_mismatch(
  482. tx,
  483. name,
  484. "0 args and 0 kwargs",
  485. f"{len(args)} args and {len(kwargs)} kwargs",
  486. )
  487. return self.clone(
  488. items=self.items.copy(), mutation_type=ValueMutationNew(), source=None
  489. )
  490. elif name == "__len__":
  491. if args or kwargs:
  492. raise_args_mismatch(
  493. tx,
  494. name,
  495. "0 args and 0 kwargs",
  496. f"{len(args)} args and {len(kwargs)} kwargs",
  497. )
  498. self.install_dict_keys_match_guard()
  499. return ConstantVariable.create(len(self.items))
  500. elif name == "__setitem__" and self.is_mutable():
  501. arg_hashable = args and is_hashable(args[0])
  502. if not arg_hashable:
  503. raise_unhashable(args[0], tx)
  504. self.install_dict_keys_match_guard()
  505. if kwargs or len(args) != 2:
  506. raise_args_mismatch(
  507. tx,
  508. name,
  509. "2 args and 0 kwargs",
  510. f"{len(args)} args and {len(kwargs)} kwargs",
  511. )
  512. tx.output.side_effects.mutation(self)
  513. self.items[Hashable(args[0])] = args[1]
  514. return CONSTANT_VARIABLE_NONE
  515. elif name == "__delitem__" and self.is_mutable():
  516. arg_hashable = args and is_hashable(args[0])
  517. if arg_hashable:
  518. self.install_dict_keys_match_guard()
  519. self.should_reconstruct_all = True
  520. tx.output.side_effects.mutation(self)
  521. self.items.__delitem__(Hashable(args[0]))
  522. return CONSTANT_VARIABLE_NONE
  523. else:
  524. return super().call_method(tx, name, args, kwargs)
  525. elif name == "get":
  526. if len(args) not in (1, 2):
  527. raise_args_mismatch(tx, name, "1 or 2 args", f"{len(args)} args")
  528. arg_hashable = args and is_hashable(args[0])
  529. if not arg_hashable:
  530. raise_unhashable(args[0], tx)
  531. if args[0] not in self:
  532. self.install_dict_contains_guard(tx, args)
  533. if len(args) == 1:
  534. # if default is not given, return None
  535. return CONSTANT_VARIABLE_NONE
  536. return args[1]
  537. # Key guarding - Nothing to do.
  538. return self.getitem_const(tx, args[0])
  539. elif name == "pop" and self.is_mutable():
  540. if len(args) not in (1, 2):
  541. raise_args_mismatch(tx, name, "1 or 2 args", f"{len(args)} args")
  542. arg_hashable = args and is_hashable(args[0])
  543. if not arg_hashable:
  544. raise_unhashable(args[0], tx)
  545. if args[0] not in self:
  546. # missing item, return the default value. Install no DICT_CONTAINS guard.
  547. self.install_dict_contains_guard(tx, args)
  548. if len(args) == 1:
  549. # if default is not given, raise KeyError
  550. raise_observed_exception(KeyError, tx)
  551. return args[1]
  552. self.should_reconstruct_all = True
  553. tx.output.side_effects.mutation(self)
  554. return self.items.pop(Hashable(args[0]))
  555. elif name == "popitem" and self.is_mutable():
  556. if (
  557. issubclass(self.user_cls, dict)
  558. and not issubclass(self.user_cls, collections.OrderedDict)
  559. and len(args)
  560. ):
  561. raise_args_mismatch(tx, name)
  562. if not self.items:
  563. msg = ConstantVariable.create("popitem(): dictionary is empty")
  564. raise_observed_exception(KeyError, tx, args=[msg])
  565. if self.user_cls is collections.OrderedDict and (
  566. len(args) == 1 or "last" in kwargs
  567. ):
  568. if len(args) == 1 and args[0].is_python_constant():
  569. last = args[0].as_python_constant()
  570. elif (v := kwargs.get("last")) and v.is_python_constant():
  571. last = v.as_python_constant()
  572. else:
  573. raise_args_mismatch(tx, name)
  574. k, v = self.items.popitem(last=last) # type: ignore[possibly-undefined]
  575. else:
  576. k, v = self.items.popitem()
  577. self.should_reconstruct_all = True
  578. tx.output.side_effects.mutation(self)
  579. return variables.TupleVariable([k.vt, v])
  580. elif name == "clear":
  581. if args or kwargs:
  582. raise_args_mismatch(
  583. tx,
  584. name,
  585. "0 args and 0 kwargs",
  586. f"{len(args)} args and {len(kwargs)} kwargs",
  587. )
  588. self.should_reconstruct_all = True
  589. tx.output.side_effects.mutation(self)
  590. self.items.clear()
  591. return CONSTANT_VARIABLE_NONE
  592. elif name == "update" and self.is_mutable():
  593. # In general, this call looks like `a.update(b, x=1, y=2, ...)`.
  594. # Either `b` or the kwargs is omittable, but not both.
  595. self.install_dict_keys_match_guard()
  596. has_arg = len(args) == 1
  597. has_kwargs = len(kwargs) > 0
  598. if has_arg or has_kwargs:
  599. tx.output.side_effects.mutation(self)
  600. if has_arg:
  601. if isinstance(args[0], ConstDictVariable):
  602. # NB - Guard on all the keys of the other dict to ensure
  603. # correctness.
  604. args[0].install_dict_keys_match_guard()
  605. dict_vt: ConstDictVariable = args[0]
  606. else:
  607. dict_vt = BuiltinVariable.call_custom_dict(tx, dict, args[0]) # type: ignore[assignment]
  608. self.items.update(dict_vt.items) # type: ignore[attr-defined]
  609. if has_kwargs:
  610. # Handle kwargs
  611. kwargs_hashable = {
  612. Hashable(ConstantVariable.create(k)): v
  613. for k, v in kwargs.items()
  614. }
  615. self.items.update(kwargs_hashable)
  616. return CONSTANT_VARIABLE_NONE
  617. else:
  618. return super().call_method(tx, name, args, kwargs)
  619. elif name == "__contains__":
  620. if not len(args):
  621. raise_args_mismatch(
  622. tx,
  623. name,
  624. "more than 1 args and 0 kwargs",
  625. f"{len(args)} args and {len(kwargs)} kwargs",
  626. )
  627. arg_hashable = args and is_hashable(args[0])
  628. if not arg_hashable:
  629. raise_unhashable(args[0], tx)
  630. self.install_dict_contains_guard(tx, args)
  631. contains = args[0] in self
  632. return ConstantVariable.create(contains)
  633. elif name == "setdefault" and self.is_mutable():
  634. if len(args) not in (1, 2):
  635. raise_args_mismatch(
  636. tx,
  637. name,
  638. "1 or 2 args and 0 kwargs",
  639. f"{len(args)} args and {len(kwargs)} kwargs",
  640. )
  641. arg_hashable = args and is_hashable(args[0])
  642. if not arg_hashable:
  643. raise_unhashable(args[0], tx)
  644. self.install_dict_keys_match_guard()
  645. if kwargs or len(args) > 2:
  646. raise_args_mismatch(
  647. tx,
  648. name,
  649. "at most 2 args and 0 kwargs",
  650. f"{len(args)} args and {len(kwargs)} kwargs",
  651. )
  652. value = self.maybe_getitem_const(args[0])
  653. if value is not None:
  654. return value
  655. else:
  656. if len(args) == 1:
  657. x = CONSTANT_VARIABLE_NONE
  658. else:
  659. x = args[1]
  660. tx.output.side_effects.mutation(self)
  661. self.items[Hashable(args[0])] = x
  662. return x
  663. elif name == "move_to_end":
  664. self.install_dict_keys_match_guard()
  665. tx.output.side_effects.mutation(self)
  666. if args[0] not in self:
  667. raise_observed_exception(KeyError, tx)
  668. last = True
  669. if len(args) == 2 and args[1].is_python_constant():
  670. last = args[1].as_python_constant()
  671. if kwargs and "last" in kwargs and kwargs["last"].is_python_constant():
  672. last = kwargs.get("last").as_python_constant() # type: ignore[union-attr]
  673. key = Hashable(args[0])
  674. self.items.move_to_end(key, last=last)
  675. return CONSTANT_VARIABLE_NONE
  676. elif name == "__eq__" and istype(
  677. self, ConstDictVariable
  678. ): # don't let Set use this function
  679. if len(args) != 1:
  680. raise_args_mismatch(tx, name, "1 args", f"{len(args)} args")
  681. return SourcelessBuilder.create(tx, polyfills.dict___eq__).call_function(
  682. tx, [self, args[0]], {}
  683. )
  684. elif name == "__ne__":
  685. return ConstantVariable.create(
  686. not self.call_method(tx, "__eq__", args, kwargs).value # type: ignore[attr-defined]
  687. )
  688. elif name == "__or__":
  689. if len(args) != 1:
  690. raise_args_mismatch(tx, name, "1 args", f"{len(args)} args")
  691. other = args[0]
  692. # Method resolution for binops works as follow (using __or__ as example):
  693. # (1) dict.__or__(dict) => dict
  694. # (2) dict.__or__(subclass): return NotImplemented
  695. # (3) Check if subclass implements __ror__ => forward the call
  696. # to subclass.__ror__(dict)
  697. # Let's not forward the call to __ror__ yet because __ror__ can be
  698. # implemented in C (i.e. OrderedDict subclass) which Dynamo cannot
  699. # trace
  700. # if istype(other, variables.UserDefinedDictVariable):
  701. # if other.call_obj_hasattr(tx, "__ror__").value:
  702. # return other.call_method(tx, "__ror__", [self], kwargs)
  703. # The three dict types Dynamo can handle are dict, OrderedDict and
  704. # defaultdict.
  705. # TODO(guilhermeleobas): this check should be on builtin.py::call_or_
  706. if istype(
  707. other,
  708. (
  709. ConstDictVariable,
  710. variables.UserDefinedDictVariable,
  711. variables.DefaultDictVariable,
  712. ),
  713. ):
  714. # Always return the specialized dictionary, and in the case
  715. # both are specialized, take the first to be the type of the
  716. # new dictionary
  717. if self.user_cls is not dict:
  718. user_cls = self.user_cls
  719. to_cpy = self
  720. else:
  721. assert isinstance(other, ConstDictVariable)
  722. user_cls = other.user_cls
  723. to_cpy = other
  724. to_cpy.install_dict_keys_match_guard()
  725. new_dict_vt = to_cpy.clone(
  726. items=self.items.copy(),
  727. mutation_type=ValueMutationNew(),
  728. source=None,
  729. user_cls=user_cls,
  730. )
  731. # NB - Guard on all the keys of the other dict to ensure
  732. # correctness.
  733. args[0].install_dict_keys_match_guard() # type: ignore[attr-defined]
  734. new_dict_vt.items.update(args[0].items) # type: ignore[attr-defined]
  735. return new_dict_vt
  736. else:
  737. err_msg = (
  738. f"unsupported operand type(s) for |: '{self.python_type().__name__}'"
  739. f"and '{other.python_type().__name__}'"
  740. )
  741. raise_observed_exception(TypeError, tx, args=[err_msg])
  742. elif name == "__ior__":
  743. self.call_method(tx, "update", args, kwargs)
  744. return self
  745. elif name == "__iter__":
  746. from .lists import ListIteratorVariable
  747. if self.source and not is_constant_source(self.source):
  748. tx.output.guard_on_key_order.add(self.source)
  749. return ListIteratorVariable(
  750. self.unpack_var_sequence(tx), mutation_type=ValueMutationNew()
  751. )
  752. else:
  753. return super().call_method(tx, name, args, kwargs)
  754. def unpack_var_sequence(self, tx: "InstructionTranslator") -> list[VariableTracker]:
  755. self.install_dict_keys_match_guard()
  756. return [x.vt for x in self.items]
  757. def call_obj_hasattr(
  758. self, tx: "InstructionTranslator", name: str
  759. ) -> ConstantVariable:
  760. # dict not allow setting arbitrary attributes. OrderedDict and
  761. # defaultdict allow arbitrary setattr, but not deletion of default attrs
  762. if any(
  763. self.user_cls is t
  764. for t in (dict, collections.OrderedDict, collections.defaultdict)
  765. ):
  766. if hasattr(self.user_cls, name):
  767. return ConstantVariable.create(True)
  768. if self.user_cls is dict:
  769. return ConstantVariable.create(False)
  770. msg = f"hasattr on {self.user_cls} is not supported"
  771. unimplemented(
  772. gb_type="unsupported hasattr operation",
  773. context=f"Class {self.user_cls}",
  774. explanation=msg,
  775. hints=[
  776. "Consider using a regular dictionary instead",
  777. *graph_break_hints.SUPPORTABLE,
  778. ],
  779. )
  780. def clone(self, **kwargs: Any) -> VariableTracker:
  781. self.install_dict_keys_match_guard()
  782. return super().clone(**kwargs)
  783. def is_python_hashable(self) -> bool:
  784. """
  785. Dictionaries are mutable and therefore not hashable in Python.
  786. """
  787. return False
  788. class MappingProxyVariable(VariableTracker):
  789. # proxies to the original dict_vt
  790. def __init__(self, dv_dict: ConstDictVariable, **kwargs: Any) -> None:
  791. super().__init__(**kwargs)
  792. assert isinstance(dv_dict, ConstDictVariable)
  793. self.dv_dict = dv_dict
  794. def python_type(self) -> type:
  795. return types.MappingProxyType
  796. def unpack_var_sequence(self, tx: "InstructionTranslator") -> list[VariableTracker]:
  797. return self.dv_dict.unpack_var_sequence(tx)
  798. def reconstruct(self, codegen: "PyCodegen") -> None:
  799. # load types.MappingProxyType
  800. if self.source:
  801. msg = (
  802. f"Preexisting MappingProxyVariable (source: {self.source}) cannot be reconstructed "
  803. "because the connection to the original dict will be lost."
  804. )
  805. unimplemented(
  806. gb_type="mapping proxy cannot be reconstructed",
  807. context=f"Source: {self.source}",
  808. explanation=msg,
  809. hints=[
  810. "Use a mapping proxy constructed in the same `torch.compile` region.",
  811. *graph_break_hints.SUPPORTABLE,
  812. ],
  813. )
  814. codegen.add_push_null(
  815. lambda: codegen.extend_output(
  816. [
  817. codegen.create_load_python_module(types),
  818. codegen.create_load_attr("MappingProxyType"),
  819. ]
  820. )
  821. )
  822. codegen(self.dv_dict)
  823. codegen.extend_output(create_call_function(1, False))
  824. def call_method(
  825. self,
  826. tx: "InstructionTranslator",
  827. name: str,
  828. args: list[VariableTracker],
  829. kwargs: dict[str, VariableTracker],
  830. ) -> VariableTracker:
  831. if self.source and tx.output.side_effects.has_existing_dict_mutation():
  832. msg = (
  833. "A dict has been modified while we have an existing mappingproxy object. "
  834. "A mapping proxy object, as the name suggest, proxies a mapping "
  835. "object (usually a dict). If the original dict object mutates, it "
  836. "is reflected in the proxy object as well. For an existing proxy "
  837. "object, we do not know the original dict it points to. Therefore, "
  838. "for correctness we graph break when there is dict mutation and we "
  839. "are trying to access a proxy object."
  840. )
  841. unimplemented(
  842. gb_type="mapping proxy affected by dictionary mutation",
  843. context=f"Source: {self.source}, Dict mutation detected",
  844. explanation=msg,
  845. hints=[
  846. "Avoid modifying dictionaries that might be referenced by mapping proxy objects",
  847. "Or avoid using the mapping proxy objects after modifying its underlying dictionary",
  848. ],
  849. )
  850. return self.dv_dict.call_method(tx, name, args, kwargs)
  851. def call_obj_hasattr(
  852. self, tx: "InstructionTranslator", name: str
  853. ) -> ConstantVariable:
  854. if self.python_type() is types.MappingProxyType:
  855. return ConstantVariable.create(name in types.MappingProxyType.__dict__)
  856. return super().call_obj_hasattr(tx, name)
  857. class NNModuleHooksDictVariable(ConstDictVariable):
  858. # Special class to avoid adding any guards on the nn module hook ids.
  859. def install_dict_keys_match_guard(self) -> None:
  860. pass
  861. def install_dict_contains_guard(
  862. self, tx: "InstructionTranslator", args: list[VariableTracker]
  863. ) -> None:
  864. pass
  865. class DefaultDictVariable(ConstDictVariable):
  866. def __init__(
  867. self,
  868. items: dict[VariableTracker, VariableTracker],
  869. user_cls: type,
  870. default_factory: Optional[VariableTracker] = None,
  871. **kwargs: Any,
  872. ) -> None:
  873. super().__init__(items, user_cls, **kwargs)
  874. assert user_cls is collections.defaultdict
  875. if default_factory is None:
  876. default_factory = CONSTANT_VARIABLE_NONE
  877. self.default_factory = default_factory
  878. def is_python_constant(self) -> bool:
  879. # Return false for unsupported defaults. This ensures that a bad handler
  880. # path is not taken in BuiltinVariable for getitem.
  881. if self.default_factory not in [list, tuple, dict] and not self.items:
  882. return False
  883. return super().is_python_constant()
  884. def debug_repr(self) -> str:
  885. assert self.default_factory is not None
  886. return (
  887. f"defaultdict({self.default_factory.debug_repr()}, {super().debug_repr()})"
  888. )
  889. @staticmethod
  890. def is_supported_arg(arg: VariableTracker) -> bool:
  891. return isinstance(
  892. arg,
  893. (
  894. variables.BuiltinVariable,
  895. variables.functions.BaseUserFunctionVariable,
  896. variables.functions.PolyfilledFunctionVariable,
  897. ),
  898. ) or (isinstance(arg, variables.ConstantVariable) and arg.value is None)
  899. def call_method(
  900. self,
  901. tx: "InstructionTranslator",
  902. name: str,
  903. args: list[VariableTracker],
  904. kwargs: dict[str, VariableTracker],
  905. ) -> VariableTracker:
  906. if name == "__getitem__":
  907. if len(args) != 1:
  908. raise_args_mismatch(tx, name, "1 args", f"{len(args)} args")
  909. if args[0] in self:
  910. return self.getitem_const(tx, args[0])
  911. else:
  912. if (
  913. istype(self.default_factory, ConstantVariable)
  914. and self.default_factory.value is None
  915. ):
  916. raise_observed_exception(KeyError, tx, args=[args[0]])
  917. else:
  918. default_var = self.default_factory.call_function(tx, [], {})
  919. super().call_method(
  920. tx, "__setitem__", [args[0], default_var], kwargs
  921. )
  922. return default_var
  923. elif name == "__setattr__" and self.is_mutable:
  924. if len(args) != 2:
  925. raise_args_mismatch(tx, name, "2 args", f"{len(args)} args")
  926. # Setting a default factory must be a callable or None type
  927. if (
  928. istype(args[0], ConstantVariable) and args[0].value == "default_factory"
  929. ) and self.is_supported_arg(args[1]):
  930. tx.output.side_effects.mutation(self)
  931. self.default_factory = args[1]
  932. return CONSTANT_VARIABLE_NONE
  933. return super().call_method(tx, name, args, kwargs)
  934. elif name == "__eq__":
  935. if len(args) != 1:
  936. raise_args_mismatch(tx, name, "1 args", f"{len(args)} args")
  937. return variables.UserFunctionVariable(polyfills.dict___eq__).call_function(
  938. tx, [self, args[0]], {}
  939. )
  940. else:
  941. return super().call_method(tx, name, args, kwargs)
  942. def var_getattr(
  943. self,
  944. tx: "InstructionTranslator",
  945. name: str,
  946. ) -> VariableTracker:
  947. if name == "default_factory":
  948. return self.default_factory
  949. return super().var_getattr(tx, name)
  950. def reconstruct(self, codegen: "PyCodegen") -> None:
  951. # emit `defaultdict(default_factory, new_dict)`
  952. codegen.add_push_null(
  953. lambda: codegen.extend_output(
  954. [
  955. codegen.create_load_python_module(collections),
  956. codegen.create_load_attr("defaultdict"),
  957. ]
  958. )
  959. )
  960. codegen(self.default_factory)
  961. codegen.extend_output(
  962. [
  963. *create_call_function(1, False),
  964. create_dup_top(),
  965. ]
  966. )
  967. codegen.add_cache(self)
  968. codegen.append_output(create_dup_top())
  969. codegen.load_method("update")
  970. self.reconstruct_kvs_into_new_dict(codegen)
  971. codegen.extend_output(
  972. [
  973. *create_call_method(1),
  974. create_instruction("POP_TOP"),
  975. ]
  976. )
  977. # TODO: Implementing this via inheritance rather than composition is a
  978. # footgun, because self method calls in dict will route back to the set
  979. # implementation, which is almost assuredly wrong
  980. class SetVariable(ConstDictVariable):
  981. """We model a sets as dictionary with None values"""
  982. CONTAINS_GUARD = GuardBuilder.SET_CONTAINS
  983. def __init__(
  984. self,
  985. items: Iterable[VariableTracker],
  986. **kwargs: Any,
  987. ) -> None:
  988. # Items can be either VariableTrackers or _HashableTrackers (from set ops).
  989. # For VariableTrackers, realize them to ensure aliasing guards are installed
  990. # when the same object appears multiple times.
  991. realized_items = []
  992. for item in items:
  993. if isinstance(item, ConstDictVariable._HashableTracker):
  994. # Already a _HashableTracker from a set operation
  995. realized_items.append(item)
  996. else:
  997. # VariableTracker - realize to install guards
  998. # pyrefly: ignore [bad-argument-type]
  999. realized_items.append(item.realize())
  1000. # pyrefly: ignore[bad-assignment]
  1001. items = dict.fromkeys(realized_items, SetVariable._default_value())
  1002. # pyrefly: ignore[bad-argument-type]
  1003. super().__init__(items, **kwargs)
  1004. def debug_repr(self) -> str:
  1005. if not self.items:
  1006. return "set()"
  1007. else:
  1008. items: list[str] = []
  1009. for v in self.items:
  1010. vt = v.vt if isinstance(v, ConstDictVariable._HashableTracker) else v
  1011. val_str = repr(vt.value) if hasattr(vt, "value") else vt.debug_repr()
  1012. items.append(val_str)
  1013. return "{" + ",".join(items) + "}"
  1014. @property
  1015. def set_items(self) -> set["ConstDictVariable._HashableTracker"]:
  1016. return set(self.items.keys())
  1017. @staticmethod
  1018. def _default_value() -> VariableTracker:
  1019. # Variable to fill in he keys of the dictionary
  1020. return CONSTANT_VARIABLE_NONE
  1021. def as_proxy(self) -> Any:
  1022. return {k.vt.as_proxy() for k in self.set_items}
  1023. def python_type(self) -> type:
  1024. return set
  1025. def as_python_constant(self) -> Any:
  1026. return {k.vt.as_python_constant() for k in self.set_items}
  1027. def reconstruct(self, codegen: "PyCodegen") -> None:
  1028. codegen.foreach([x.vt for x in self.set_items])
  1029. codegen.append_output(create_instruction("BUILD_SET", arg=len(self.set_items)))
  1030. def _fast_set_method(
  1031. self,
  1032. tx: "InstructionTranslator",
  1033. fn: Any,
  1034. args: list[VariableTracker],
  1035. kwargs: dict[str, VariableTracker],
  1036. ) -> VariableTracker:
  1037. try:
  1038. res = fn(
  1039. *[x.as_python_constant() for x in [self, *args]],
  1040. **{k: v.as_python_constant() for k, v in kwargs.items()},
  1041. )
  1042. except Exception as exc:
  1043. raise_observed_exception(
  1044. type(exc), tx, args=list(map(ConstantVariable.create, exc.args))
  1045. )
  1046. # pyrefly: ignore[unbound-name]
  1047. return VariableTracker.build(tx, res)
  1048. def call_method(
  1049. self,
  1050. tx: "InstructionTranslator",
  1051. name: str,
  1052. args: list[VariableTracker],
  1053. kwargs: dict[str, VariableTracker],
  1054. ) -> VariableTracker:
  1055. # We forward the calls to the dictionary model
  1056. from ..utils import check_constant_args
  1057. from .builder import SourcelessBuilder
  1058. if (
  1059. name
  1060. in (
  1061. "isdisjoint",
  1062. "union",
  1063. "intersection",
  1064. "difference",
  1065. "symmetric_difference",
  1066. )
  1067. and check_constant_args(args, kwargs)
  1068. and self.python_type() is set
  1069. ):
  1070. py_type = self.python_type()
  1071. return self._fast_set_method(tx, getattr(py_type, name), args, kwargs)
  1072. if name == "__init__":
  1073. temp_set_vt = SourcelessBuilder.create(tx, set).call_set(
  1074. tx, *args, **kwargs
  1075. )
  1076. tx.output.side_effects.mutation(self)
  1077. self.items.clear()
  1078. self.items.update(temp_set_vt.items) # type: ignore[attr-defined]
  1079. return CONSTANT_VARIABLE_NONE
  1080. elif name == "add":
  1081. if kwargs or len(args) != 1:
  1082. raise_args_mismatch(
  1083. tx,
  1084. name,
  1085. "1 args and 0 kwargs",
  1086. f"{len(args)} args and {len(kwargs)} kwargs",
  1087. )
  1088. name = "__setitem__"
  1089. args = [args[0], SetVariable._default_value()]
  1090. elif name == "pop":
  1091. if kwargs or args:
  1092. raise_args_mismatch(
  1093. tx,
  1094. name,
  1095. "0 args and 0 kwargs",
  1096. f"{len(args)} args and {len(kwargs)} kwargs",
  1097. )
  1098. # Choose an item at random and pop it via the Dict.pop method
  1099. try:
  1100. result: VariableTracker = self.set_items.pop().vt # type: ignore[assignment]
  1101. except KeyError as e:
  1102. raise_observed_exception(
  1103. KeyError, tx, args=list(map(ConstantVariable.create, e.args))
  1104. )
  1105. # pyrefly: ignore[unbound-name]
  1106. super().call_method(tx, name, [result], kwargs)
  1107. # pyrefly: ignore[unbound-name]
  1108. return result
  1109. elif name == "isdisjoint":
  1110. if kwargs or len(args) != 1:
  1111. raise_args_mismatch(
  1112. tx,
  1113. name,
  1114. "1 args and 0 kwargs",
  1115. f"{len(args)} args and {len(kwargs)} kwargs",
  1116. )
  1117. return SourcelessBuilder.create(tx, polyfills.set_isdisjoint).call_function(
  1118. tx, [self, args[0]], {}
  1119. )
  1120. elif name == "intersection":
  1121. if kwargs:
  1122. raise_args_mismatch(tx, name, "0 kwargs", f"{len(kwargs)} kwargs")
  1123. return SourcelessBuilder.create(
  1124. tx, polyfills.set_intersection
  1125. ).call_function(
  1126. tx,
  1127. [self, *args],
  1128. {"cls": self.python_type_var()},
  1129. )
  1130. elif name == "intersection_update":
  1131. if kwargs:
  1132. raise_args_mismatch(tx, name, "0 kwargs", f"{len(kwargs)} kwargs")
  1133. return SourcelessBuilder.create(
  1134. tx, polyfills.set_intersection_update
  1135. ).call_function(tx, [self, *args], {})
  1136. elif name == "union":
  1137. if kwargs:
  1138. raise_args_mismatch(tx, name, "0 kwargs", f"{len(kwargs)} kwargs")
  1139. return SourcelessBuilder.create(tx, polyfills.set_union).call_function(
  1140. tx,
  1141. [self, *args],
  1142. {"cls": self.python_type_var()},
  1143. )
  1144. elif name == "difference":
  1145. if kwargs:
  1146. raise_args_mismatch(
  1147. tx, name, f"Expect: 0 kwargs, Actual: {len(kwargs)} kwargs"
  1148. )
  1149. return SourcelessBuilder.create(tx, polyfills.set_difference).call_function(
  1150. tx,
  1151. [self, *args],
  1152. {"cls": self.python_type_var()},
  1153. )
  1154. elif name == "difference_update":
  1155. if kwargs:
  1156. raise_args_mismatch(tx, name, "0 kwargs", f"{len(kwargs)} kwargs")
  1157. return SourcelessBuilder.create(
  1158. tx, polyfills.set_difference_update
  1159. ).call_function(tx, [self, *args], {})
  1160. elif name == "symmetric_difference":
  1161. if kwargs or len(args) != 1:
  1162. raise_args_mismatch(
  1163. tx,
  1164. name,
  1165. "1 args and 0 kwargs",
  1166. f"{len(args)} args and {len(kwargs)} kwargs",
  1167. )
  1168. return SourcelessBuilder.create(
  1169. tx, polyfills.set_symmetric_difference
  1170. ).call_function(
  1171. tx,
  1172. [self, *args],
  1173. {"cls": self.python_type_var()},
  1174. )
  1175. elif name == "symmetric_difference_update":
  1176. if kwargs or len(args) != 1:
  1177. raise_args_mismatch(
  1178. tx,
  1179. name,
  1180. "1 args and 0 kwargs",
  1181. f"{len(args)} args and {len(kwargs)} kwargs",
  1182. )
  1183. return SourcelessBuilder.create(
  1184. tx, polyfills.set_symmetric_difference_update
  1185. ).call_function(tx, [self, *args], {})
  1186. elif name == "update" and self.is_mutable():
  1187. if kwargs:
  1188. raise_args_mismatch(tx, name, "0 kwargs", f"{len(kwargs)} kwargs")
  1189. return SourcelessBuilder.create(tx, polyfills.set_update).call_function(
  1190. tx, [self, *args], {}
  1191. )
  1192. elif name == "remove":
  1193. if kwargs or len(args) != 1:
  1194. raise_args_mismatch(
  1195. tx,
  1196. name,
  1197. "1 args and 0 kwargs",
  1198. f"{len(args)} args and {len(kwargs)} kwargs",
  1199. )
  1200. if args[0] not in self:
  1201. raise_observed_exception(KeyError, tx, args=args)
  1202. return super().call_method(tx, "pop", args, kwargs)
  1203. elif name == "discard":
  1204. if kwargs or len(args) != 1:
  1205. raise_args_mismatch(
  1206. tx,
  1207. name,
  1208. "1 args and 0 kwargs",
  1209. f"{len(args)} args and {len(kwargs)} kwargs",
  1210. )
  1211. if args[0] in self:
  1212. return super().call_method(tx, "pop", args, kwargs)
  1213. else:
  1214. return CONSTANT_VARIABLE_NONE
  1215. elif name in ("issubset", "issuperset"):
  1216. if len(args) != 1:
  1217. raise_args_mismatch(tx, name, "1 args", f"{len(args)} args")
  1218. op = {
  1219. "issubset": operator.le,
  1220. "issuperset": operator.ge,
  1221. }
  1222. other = args[0].realize()
  1223. if not istype(other, SetVariable):
  1224. other = SourcelessBuilder.create(tx, set).call_function(tx, [other], {})
  1225. return SourcelessBuilder.create(tx, op.get(name)).call_function(
  1226. tx, [self, other], {}
  1227. )
  1228. elif name in ("__and__", "__or__", "__xor__", "__sub__"):
  1229. m = {
  1230. "__and__": "intersection",
  1231. "__or__": "union",
  1232. "__xor__": "symmetric_difference",
  1233. "__sub__": "difference",
  1234. }.get(name)
  1235. if not isinstance(args[0], (SetVariable, variables.UserDefinedSetVariable)):
  1236. msg = ConstantVariable.create(
  1237. f"unsupported operand type(s) for {name}: '{self.python_type_name()}' and '{args[0].python_type_name()}'"
  1238. )
  1239. raise_observed_exception(TypeError, tx, args=[msg])
  1240. assert m is not None
  1241. return self.call_method(tx, m, args, kwargs)
  1242. elif name in ("__iand__", "__ior__", "__ixor__", "__isub__"):
  1243. if not isinstance(args[0], (SetVariable, variables.UserDefinedSetVariable)):
  1244. msg = ConstantVariable.create(
  1245. f"unsupported operand type(s) for {name}: '{self.python_type_name()}' and '{args[0].python_type_name()}'"
  1246. )
  1247. raise_observed_exception(TypeError, tx, args=[msg])
  1248. m = {
  1249. "__iand__": "intersection_update",
  1250. "__ior__": "update",
  1251. "__ixor__": "symmetric_difference_update",
  1252. "__isub__": "difference_update",
  1253. }.get(name)
  1254. assert m is not None
  1255. self.call_method(tx, m, args, kwargs)
  1256. return self
  1257. elif name == "__eq__":
  1258. if not isinstance(args[0], (SetVariable, variables.UserDefinedSetVariable)):
  1259. return ConstantVariable.create(False)
  1260. r = self.call_method(tx, "symmetric_difference", args, kwargs)
  1261. return ConstantVariable.create(len(r.set_items) == 0) # type: ignore[attr-defined]
  1262. elif name in cmp_name_to_op_mapping:
  1263. if not isinstance(args[0], (SetVariable, variables.UserDefinedSetVariable)):
  1264. return ConstantVariable.create(NotImplemented)
  1265. return ConstantVariable.create(
  1266. cmp_name_to_op_mapping[name](self.set_items, args[0].set_items) # type: ignore[attr-defined]
  1267. )
  1268. return super().call_method(tx, name, args, kwargs)
  1269. def python_type_var(self) -> "BuiltinVariable":
  1270. return variables.BuiltinVariable(set)
  1271. def getitem_const(
  1272. self, tx: "InstructionTranslator", arg: VariableTracker
  1273. ) -> VariableTracker:
  1274. raise RuntimeError("Illegal to getitem on a set")
  1275. def install_dict_keys_match_guard(self) -> None:
  1276. # Already EQUALS_MATCH guarded
  1277. pass
  1278. class OrderedSetClassVariable(VariableTracker):
  1279. def __init__(self, **kwargs: Any) -> None:
  1280. super().__init__(**kwargs)
  1281. def as_python_constant(self) -> type[OrderedSet[Any]]:
  1282. return OrderedSet
  1283. def var_getattr(self, tx: "InstructionTranslator", name: str) -> VariableTracker:
  1284. if name == "__new__":
  1285. from .misc import GetAttrVariable
  1286. if self.source:
  1287. attr_source = AttrSource(self.source, name)
  1288. else:
  1289. attr_source = None
  1290. return GetAttrVariable(self, name, source=attr_source)
  1291. else:
  1292. return super().var_getattr(tx, name)
  1293. def call_method(
  1294. self,
  1295. tx: "InstructionTranslator",
  1296. name: str,
  1297. args: list[VariableTracker],
  1298. kwargs: dict[str, VariableTracker],
  1299. ) -> VariableTracker:
  1300. from .builtin import set_methods
  1301. if name == "__new__":
  1302. if len(args) != 2 or kwargs:
  1303. raise_args_mismatch(
  1304. tx,
  1305. name,
  1306. "OrderedSet.__new__ only accepts one arg"
  1307. f"{len(args)} args and {len(kwargs)} kwargs",
  1308. )
  1309. return variables.OrderedSetVariable([], mutation_type=ValueMutationNew())
  1310. resolved_fn = getattr(set, name)
  1311. if resolved_fn in set_methods and isinstance(args[0], variables.SetVariable):
  1312. return args[0].call_method(tx, name, args[1:], kwargs)
  1313. return super().call_method(tx, name, args, kwargs)
  1314. def call_function(
  1315. self,
  1316. tx: "InstructionTranslator",
  1317. args: Sequence[VariableTracker],
  1318. kwargs: dict[str, VariableTracker],
  1319. ) -> "OrderedSetVariable":
  1320. if len(args) > 1 or kwargs:
  1321. raise_args_mismatch(
  1322. tx,
  1323. "OrderedSet",
  1324. "OrderedSet only accepts one arg"
  1325. f"{len(args)} args and {len(kwargs)} kwargs",
  1326. )
  1327. if len(args) == 0:
  1328. # pyrefly: ignore [implicit-any]
  1329. items = []
  1330. else:
  1331. items = args[0].force_unpack_var_sequence(tx)
  1332. return variables.OrderedSetVariable(items, mutation_type=ValueMutationNew())
  1333. class OrderedSetVariable(SetVariable):
  1334. def debug_repr(self) -> str:
  1335. if not self.items:
  1336. return "OrderedSet([])"
  1337. else:
  1338. items: list[str] = []
  1339. for k, v in self.items:
  1340. key_str = (
  1341. repr(k.vt.value) if hasattr(k.vt, "value") else k.vt.debug_repr()
  1342. )
  1343. items.append(key_str)
  1344. return "OrderedSet([" + ",".join(items) + "])"
  1345. def as_python_constant(self) -> OrderedSet[Any]:
  1346. return OrderedSet([k.vt.as_python_constant() for k in self.set_items])
  1347. def python_type(self) -> type[OrderedSet[Any]]:
  1348. return OrderedSet
  1349. # pyrefly: ignore[bad-override]
  1350. def python_type_var(self) -> OrderedSetClassVariable:
  1351. return OrderedSetClassVariable()
  1352. def reconstruct(self, codegen: "PyCodegen") -> None:
  1353. codegen.add_push_null(
  1354. lambda: codegen.load_import_from("torch.utils._ordered_set", "OrderedSet")
  1355. )
  1356. codegen.foreach([x.vt for x in self.set_items])
  1357. codegen.append_output(create_instruction("BUILD_LIST", arg=len(self.set_items)))
  1358. codegen.extend_output(create_call_function(1, False))
  1359. class FrozensetVariable(SetVariable):
  1360. def debug_repr(self) -> str:
  1361. if not self.items:
  1362. return "frozenset()"
  1363. else:
  1364. items: list[str] = []
  1365. for k in self.items:
  1366. key_str = (
  1367. repr(k.vt.value) if hasattr(k.vt, "value") else k.vt.debug_repr()
  1368. )
  1369. items.append(key_str)
  1370. return "{" + ",".join(items) + "}"
  1371. @property
  1372. def set_items(self) -> set["ConstDictVariable._HashableTracker"]:
  1373. return self.items.keys()
  1374. def python_type(self) -> type:
  1375. return frozenset
  1376. def python_type_var(self) -> "BuiltinVariable":
  1377. return variables.BuiltinVariable(frozenset)
  1378. def as_python_constant(self) -> Any:
  1379. return frozenset({k.vt.as_python_constant() for k in self.set_items})
  1380. def reconstruct(self, codegen: "PyCodegen") -> None:
  1381. codegen.add_push_null(
  1382. lambda: codegen.extend_output(
  1383. [
  1384. codegen.create_load_global("frozenset"),
  1385. ]
  1386. )
  1387. )
  1388. codegen.foreach([x.vt for x in self.set_items])
  1389. codegen.extend_output(
  1390. [
  1391. create_instruction("BUILD_LIST", arg=len(self.set_items)),
  1392. *create_call_function(1, False),
  1393. ]
  1394. )
  1395. def call_method(
  1396. self,
  1397. tx: "InstructionTranslator",
  1398. name: str,
  1399. args: list[VariableTracker],
  1400. kwargs: dict[str, VariableTracker],
  1401. ) -> VariableTracker:
  1402. if name in ["add", "pop", "update", "remove", "discard", "clear"]:
  1403. raise RuntimeError(f"Illegal call_method {name} on a frozenset")
  1404. elif name == "__init__":
  1405. # frozenset is immutable. Calling __init__ again shouldn't have any effect
  1406. # In[1]: s = frozenset([1, 2])
  1407. #
  1408. # In[2]: s.__init__([3, 4])
  1409. #
  1410. # In[3]: s
  1411. # frozenset({1, 2})
  1412. return CONSTANT_VARIABLE_NONE
  1413. elif name in (
  1414. "copy",
  1415. "difference",
  1416. "intersection",
  1417. "symmetric_difference",
  1418. ):
  1419. r = super().call_method(tx, name, args, kwargs)
  1420. return FrozensetVariable(r.items) # type: ignore[attr-defined]
  1421. return super().call_method(tx, name, args, kwargs)
  1422. def is_python_hashable(self) -> Literal[True]:
  1423. """
  1424. Frozensets are immutable and hashable in Python.
  1425. """
  1426. return True
  1427. def get_python_hash(self) -> int:
  1428. return hash(self.as_python_constant())
  1429. def is_python_equal(self, other: object) -> bool:
  1430. return (
  1431. isinstance(other, VariableTracker)
  1432. and self.as_python_constant() == other.as_python_constant()
  1433. )
  1434. class DictKeySetVariable(SetVariable):
  1435. def debug_repr(self) -> str:
  1436. if not self.items:
  1437. return "dict_keys([])"
  1438. else:
  1439. items: list[str] = []
  1440. for k in self.items:
  1441. key_str = (
  1442. repr(k.vt.value) if hasattr(k.vt, "value") else k.vt.debug_repr()
  1443. )
  1444. items.append(key_str)
  1445. return "dict_keys([" + ",".join(items) + "])"
  1446. def install_dict_keys_match_guard(self) -> None:
  1447. # Already EQUALS_MATCH guarded
  1448. pass
  1449. def install_dict_contains_guard(
  1450. self, tx: "InstructionTranslator", args: list[VariableTracker]
  1451. ) -> None:
  1452. # Already EQUALS_MATCH guarded
  1453. pass
  1454. @property
  1455. def set_items(self) -> Any:
  1456. return self.items
  1457. def python_type(self) -> type:
  1458. return dict_keys
  1459. def as_python_constant(self) -> Any:
  1460. return dict.fromkeys(
  1461. {k.vt.as_python_constant() for k in self.set_items}, None
  1462. ).keys()
  1463. def call_method(
  1464. self,
  1465. tx: "InstructionTranslator",
  1466. name: str,
  1467. args: list[VariableTracker],
  1468. kwargs: dict[str, VariableTracker],
  1469. ) -> VariableTracker:
  1470. if name in ["add", "pop", "update", "remove", "discard", "clear"]:
  1471. raise RuntimeError(f"Illegal call_method {name} on a dict_keys")
  1472. return super().call_method(tx, name, args, kwargs)
  1473. class DictViewVariable(VariableTracker):
  1474. """
  1475. Models _PyDictViewObject
  1476. This is an "abstract" class. Subclasses will override kv and the items method
  1477. """
  1478. kv: Optional[str] = None
  1479. def __init__(self, dv_dict: ConstDictVariable, **kwargs: Any) -> None:
  1480. super().__init__(**kwargs)
  1481. assert self.kv in ("keys", "values", "items")
  1482. assert isinstance(dv_dict, ConstDictVariable)
  1483. self.dv_dict = dv_dict
  1484. @property
  1485. def view_items(self) -> Any:
  1486. assert self.kv is not None
  1487. return getattr(self.dv_dict.items, self.kv)()
  1488. @property
  1489. def view_items_vt(self) -> list[VariableTracker]:
  1490. # Returns an iterable of the unpacked items
  1491. # Implement in the subclasses
  1492. raise NotImplementedError
  1493. def unpack_var_sequence(self, tx: "InstructionTranslator") -> list[VariableTracker]:
  1494. return self.view_items_vt
  1495. def reconstruct(self, codegen: "PyCodegen") -> None:
  1496. assert self.kv is not None
  1497. codegen(self.dv_dict)
  1498. codegen.load_method(self.kv)
  1499. codegen.call_method(0)
  1500. def call_obj_hasattr(
  1501. self, tx: "InstructionTranslator", name: str
  1502. ) -> ConstantVariable:
  1503. assert self.kv is not None
  1504. if name in self.python_type().__dict__:
  1505. return ConstantVariable.create(True)
  1506. return ConstantVariable.create(False)
  1507. def call_method(
  1508. self,
  1509. tx: "InstructionTranslator",
  1510. name: str,
  1511. args: list[VariableTracker],
  1512. kwargs: dict[str, VariableTracker],
  1513. ) -> VariableTracker:
  1514. if name == "__len__":
  1515. return self.dv_dict.call_method(tx, name, args, kwargs)
  1516. elif name == "__iter__":
  1517. from .lists import ListIteratorVariable
  1518. return ListIteratorVariable(
  1519. self.view_items_vt, mutation_type=ValueMutationNew()
  1520. )
  1521. elif name == "__repr__":
  1522. return ConstantVariable.create(self.debug_repr())
  1523. return super().call_method(tx, name, args, kwargs)
  1524. class DictKeysVariable(DictViewVariable):
  1525. kv = "keys"
  1526. @property
  1527. def set_items(self) -> set[VariableTracker]:
  1528. return set(self.view_items)
  1529. @property
  1530. def view_items_vt(self) -> list[VariableTracker]:
  1531. # Returns an iterable of the unpacked items
  1532. return [x.vt for x in self.view_items]
  1533. def python_type(self) -> type:
  1534. return dict_keys
  1535. def debug_repr(self) -> str:
  1536. if not self.view_items:
  1537. return "dict_keys([])"
  1538. else:
  1539. items: list[str] = []
  1540. for k in self.view_items:
  1541. key_str = (
  1542. repr(k.vt.value) if hasattr(k.vt, "value") else k.vt.debug_repr()
  1543. )
  1544. items.append(key_str)
  1545. return "dict_keys([" + ",".join(items) + "])"
  1546. def call_method(
  1547. self,
  1548. tx: "InstructionTranslator",
  1549. name: str,
  1550. args: list[VariableTracker],
  1551. kwargs: dict[str, VariableTracker],
  1552. ) -> VariableTracker:
  1553. if name == "__contains__":
  1554. return self.dv_dict.call_method(tx, name, args, kwargs)
  1555. elif name in (
  1556. "__and__",
  1557. "__iand__",
  1558. "__or__",
  1559. "__ior__",
  1560. "__sub__",
  1561. "__isub__",
  1562. "__xor__",
  1563. "__ixor__",
  1564. ):
  1565. # These methods always returns a set
  1566. m = getattr(self.set_items, name)
  1567. r = m(args[0].set_items) # type: ignore[attr-defined]
  1568. return SetVariable(r)
  1569. if name in cmp_name_to_op_mapping:
  1570. if not isinstance(args[0], (SetVariable, DictKeysVariable)):
  1571. return ConstantVariable.create(NotImplemented)
  1572. return ConstantVariable.create(
  1573. cmp_name_to_op_mapping[name](self.set_items, args[0].set_items) # type: ignore[attr-defined]
  1574. )
  1575. return super().call_method(tx, name, args, kwargs)
  1576. class DictValuesVariable(DictViewVariable):
  1577. # DictValuesVariable is an iterable but cannot be compared.
  1578. kv = "values"
  1579. @property
  1580. def view_items_vt(self) -> list[VariableTracker]:
  1581. return list(self.view_items)
  1582. def python_type(self) -> type:
  1583. return dict_values
  1584. def debug_repr(self) -> str:
  1585. if not self.view_items:
  1586. return "dict_values([])"
  1587. else:
  1588. items: list[str] = []
  1589. for v in self.view_items:
  1590. val_str = repr(v.value) if hasattr(v, "value") else v.debug_repr()
  1591. items.append(val_str)
  1592. return "dict_values([" + ",".join(items) + "])"
  1593. class DictItemsVariable(DictViewVariable):
  1594. kv = "items"
  1595. @property
  1596. def view_items_vt(self) -> list[VariableTracker]:
  1597. # Returns an iterable of the unpacked items
  1598. return [variables.TupleVariable([k.vt, v]) for k, v in self.view_items]
  1599. def python_type(self) -> type:
  1600. return dict_items
  1601. def debug_repr(self) -> str:
  1602. if not self.view_items:
  1603. return "dict_items([])"
  1604. else:
  1605. items: list[str] = []
  1606. for k, v in self.view_items:
  1607. key_str = (
  1608. repr(k.vt.value) if hasattr(k.vt, "value") else k.vt.debug_repr()
  1609. )
  1610. val_str = repr(v.value) if hasattr(v, "value") else v.debug_repr()
  1611. items.append(f"({key_str}, {val_str})")
  1612. return "dict_items([" + ",".join(items) + "])"
  1613. def call_method(
  1614. self,
  1615. tx: "InstructionTranslator",
  1616. name: str,
  1617. args: list[VariableTracker],
  1618. kwargs: dict[str, VariableTracker],
  1619. ) -> VariableTracker:
  1620. # TODO(guilhermeleobas): This should actually check if args[0]
  1621. # implements the mapping protocol.
  1622. if name == "__eq__":
  1623. if len(args) != 1:
  1624. raise_args_mismatch(tx, name, "1 args", f"{len(args)} args")
  1625. if isinstance(args[0], DictItemsVariable):
  1626. return self.dv_dict.call_method(tx, "__eq__", [args[0].dv_dict], {})
  1627. return ConstantVariable.create(False)
  1628. elif name == "__iter__":
  1629. from .lists import ListIteratorVariable
  1630. return ListIteratorVariable(
  1631. self.view_items_vt, mutation_type=ValueMutationNew()
  1632. )
  1633. return super().call_method(tx, name, args, kwargs)
  1634. def is_python_hashable(self) -> Literal[False]:
  1635. """
  1636. Dictionary item views are not hashable in Python.
  1637. """
  1638. return False