Skip to content

Commit 00c5859

Browse files
arunppsgpytorchmergebot
authored andcommitted
[dynamo] Add support for DELETE_SUBSCR (pytorch#123526)
Fixes pytorch#123317 Co-authored-by: Jason Ansel <jansel@meta.com> Pull Request resolved: pytorch#123526 Approved by: https://github.com/jansel
1 parent 8c515a1 commit 00c5859

13 files changed

+32
-6
lines changed

test/dynamo/test_repros.py

+23
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,29 @@ def fn(x, obj):
33003300
obj1 = MyObj(x, x)
33013301
self.assertRaises(AttributeError, lambda: fn(x, obj1))
33023302

3303+
def test_delsubscr(self):
3304+
@torch.compile(backend="eager")
3305+
def fn(x):
3306+
del x["a"]
3307+
y = x["b"] + 1
3308+
return y
3309+
3310+
x = {"a": torch.tensor([1]), "b": torch.tensor([1])}
3311+
result = fn(x)
3312+
self.assertFalse(hasattr(x, "a"))
3313+
self.assertEqual(result.item(), 2)
3314+
3315+
def test_delsubscr_raises(self):
3316+
@torch.compile(backend="eager")
3317+
def fn(x):
3318+
del x["a"]
3319+
y = x["a"] + 1 # should raise KeyError
3320+
return y
3321+
3322+
x = {"a": torch.tensor([1]), "b": torch.tensor([1])}
3323+
# FIXME It should be KeyError here
3324+
self.assertRaises(torch._dynamo.exc.InternalTorchDynamoError, lambda: fn(x))
3325+
33033326
def test_attached_attribute_in_dir(self):
33043327
class MyModule(torch.nn.Module):
33053328
def __init__(self):

test/dynamo_expected_failures/TestAOTModuleSimplified.test_aot_module_simplified_fake_tensor_gm_raises

Whitespace-only changes.

test/dynamo_expected_failures/TestReductionsCPU.test_std_vs_numpy_cpu_complex128

Whitespace-only changes.

test/dynamo_expected_failures/TestReductionsCPU.test_std_vs_numpy_cpu_complex64

Whitespace-only changes.

test/dynamo_expected_failures/TestReductionsCPU.test_std_vs_numpy_cpu_float32

Whitespace-only changes.

test/dynamo_expected_failures/TestReductionsCPU.test_std_vs_numpy_cpu_float64

Whitespace-only changes.

test/dynamo_expected_failures/TestReductionsCPU.test_var_vs_numpy_cpu_complex128

Whitespace-only changes.

test/dynamo_expected_failures/TestReductionsCPU.test_var_vs_numpy_cpu_complex64

Whitespace-only changes.

test/dynamo_expected_failures/TestReductionsCPU.test_var_vs_numpy_cpu_float32

Whitespace-only changes.

test/dynamo_expected_failures/TestReductionsCPU.test_var_vs_numpy_cpu_float64

Whitespace-only changes.

test/dynamo_skips/TestLoadStateDict.test_load_state_dict_BC_swap_True

Whitespace-only changes.

torch/_dynamo/symbolic_convert.py

+4
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,10 @@ def STORE_SUBSCR(self, inst):
14201420
val, obj, key = self.popn(3)
14211421
result = obj.call_method(self, "__setitem__", [key, val], {})
14221422

1423+
def DELETE_SUBSCR(self, inst):
1424+
obj, key = self.popn(2)
1425+
obj.call_method(self, "__delitem__", [key], {})
1426+
14231427
def BUILD_TUPLE(self, inst):
14241428
items = self.popn(inst.argval)
14251429
self.push(TupleVariable(items))

torch/_dynamo/variables/dicts.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ def call_method(
249249
tx.output.side_effects.mutation(self)
250250
self.items[Hashable(args[0])] = args[1]
251251
return ConstantVariable.create(None)
252+
elif name == "__delitem__" and arg_hashable and self.mutable_local:
253+
tx.output.side_effects.mutation(self)
254+
self.items.__delitem__(Hashable(args[0]))
255+
return ConstantVariable.create(None)
252256
elif name in ("pop", "get") and len(args) in (1, 2) and args[0] not in self:
253257
# missing item, return the default value
254258
if len(args) == 1:
@@ -889,18 +893,13 @@ def reconstruct(self, codegen):
889893
def call_method(
890894
self, tx, name, args: List[VariableTracker], kwargs: Dict[str, VariableTracker]
891895
):
892-
from .builder import VariableBuilder
893-
894896
if name == "__getitem__":
895897
return self.call_getitem(tx, *args, **kwargs)
896898
elif name == "get":
897899
return self.call_get(tx, *args, **kwargs)
898900
elif name == "__contains__":
899901
return self.call_contains(tx, *args, **kwargs)
900-
901-
# Fallback to dict implementation
902-
real_dict = VariableBuilder(tx, self.source)(sys.modules)
903-
return real_dict.call_method(tx, name, args, kwargs)
902+
unimplemented(f"sys.modules.{name}(*{args}, **{kwargs})")
904903

905904
def _contains_helper(self, tx, key: VariableTracker):
906905
k = key.as_python_constant()

0 commit comments

Comments
 (0)