Skip to content

Change mutation type of MutableMappingVariable to AttributeMutationNew #159366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: gh/guilhermeleobas/215/base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions torch/_dynamo/polyfills/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ def set_difference_update(set1, *others):
set1.update(result)


def assert_dict_equal(self_, d1, d2, msg=None):
self_.assertTrue(d1 == d2, msg)


def assert_multi_line_equal(self_, first, second, msg=None):
return self_.assertTrue(first == second, msg)

Expand Down
2 changes: 1 addition & 1 deletion torch/_dynamo/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class CPythonTestCase(TestCase):
assertListEqual = unittest.TestCase.assertListEqual
assertTupleEqual = unittest.TestCase.assertTupleEqual
assertSetEqual = unittest.TestCase.assertSetEqual
assertDictEqual = unittest.TestCase.assertDictEqual
assertDictEqual = polyfills.assert_dict_equal
assertRaises = unittest.TestCase.assertRaises
assertRaisesRegex = unittest.TestCase.assertRaisesRegex
assertWarns = unittest.TestCase.assertWarns
Expand Down
17 changes: 15 additions & 2 deletions torch/_dynamo/variables/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
UnspecializedPythonVariable,
)
from .user_defined import (
MutableMappingVariable,
UserDefinedDictVariable,
UserDefinedObjectVariable,
UserDefinedSetVariable,
Expand Down Expand Up @@ -1865,6 +1866,12 @@ def call_cast(self, _, *args, **kwargs):
hints=["Ensure your call to cast() has exactly 2 arguments."],
)

def call_dir(self, tx: "InstructionTranslator", arg):
if isinstance(arg, variables.UserDefinedClassVariable):
return VariableTracker.build(tx, dir(arg.value))
if isinstance(arg, BuiltinVariable):
return VariableTracker.build(tx, dir(arg.fn))

def call_dict(self, tx: "InstructionTranslator", *args, **kwargs):
return BuiltinVariable.call_custom_dict(tx, dict, *args, **kwargs)

Expand Down Expand Up @@ -2255,7 +2262,6 @@ def call_getattr(
"assertRaisesRegex",
"assertNotWarns",
"assertWarnsRegex",
"assertDictEqual",
"assertWarns",
)
):
Expand Down Expand Up @@ -2742,6 +2748,7 @@ def call_or_(self, tx: "InstructionTranslator", a, b):
(
ConstDictVariable,
DictKeysVariable,
MutableMappingVariable,
SetVariable,
UserDefinedDictVariable,
UserDefinedSetVariable,
Expand Down Expand Up @@ -2770,7 +2777,13 @@ def call_ior(self, tx: "InstructionTranslator", a, b):
# This call looks like `{"one": torch.ones(1)} |= {"two": torch.ones(2)}`.
if isinstance(
a,
(ConstDictVariable, DictKeysVariable, SetVariable, UserDefinedSetVariable),
(
ConstDictVariable,
DictKeysVariable,
MutableMappingVariable,
SetVariable,
UserDefinedSetVariable,
),
):
return a.call_method(tx, "__ior__", [b], {})

Expand Down
4 changes: 2 additions & 2 deletions torch/_dynamo/variables/user_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
tuple_methods,
unpatched_nn_module_getattr,
)
from .base import AttributeMutationExisting, ValueMutationNew, VariableTracker
from .base import AttributeMutationNew, ValueMutationNew, VariableTracker
from .dicts import DefaultDictVariable
from .lists import SizeVariable

Expand Down Expand Up @@ -2073,7 +2073,7 @@ class MutableMappingVariable(UserDefinedObjectVariable):
def __init__(self, value, **kwargs):
super().__init__(value, **kwargs)
self.generic_dict_vt = variables.ConstDictVariable({})
self.mutation_type = AttributeMutationExisting()
self.mutation_type = AttributeMutationNew()

def var_getattr(self, tx: "InstructionTranslator", name: str) -> "VariableTracker":
# A common pattern in the init code of MutableMapping objects is to
Expand Down
Loading