Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.
Merged
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
12 changes: 12 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,3 +1157,15 @@ def fn():
inst = dis.get_instructions(fn)
result = bytecode_transformation.assemble(inst, fn.__code__.co_firstlineno)
self.assertTrue(result[1] == fn.__code__.co_lnotab)

def test_python_slice(self):
def fn(input):
y = 0
for i, x in enumerate(input[2:], 1):
y = y + x
return y

cnts = torchdynamo.testing.CompileCounter()
with torchdynamo.optimize(cnts):
z = fn([1, 2, 3, 5])
self.assertEqual(z, 8)
2 changes: 1 addition & 1 deletion torchdynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def rot_n_helper(n):
def is_safe_constant(v):
if istype(v, (tuple, frozenset)):
return all(map(is_safe_constant, v))
return istype(v, (types.CodeType, int, float, bool, str, bytes, type(None)))
return istype(v, (types.CodeType, int, float, bool, str, bytes, type(None), slice))


def check_constant_args(args, kwargs):
Expand Down
9 changes: 6 additions & 3 deletions torchdynamo/variables/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .. import variables
from ..bytecode_transformation import create_instruction
from ..exc import unimplemented
from ..source import GetItemSource
from ..utils import namedtuple_fields
from .base import MutableLocal
from .base import VariableTracker
Expand Down Expand Up @@ -40,9 +41,11 @@ def as_proxy(self):
def getitem_const(self, arg: VariableTracker):
index = arg.as_python_constant()
if isinstance(index, slice):
return self.clone(items=self.items[index], mutable_local=None).add_options(
arg, self
)
return self.clone(
items=self.items[index],
source=GetItemSource(self.source, index),
mutable_local=None,
).add_options(arg, self)
else:
assert isinstance(index, int)
return self.items[index].add_options(arg, self)
Expand Down