Skip to content
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

[jit] allow slicing multiple dimensions with indices #45239

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions test/jit/test_list_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,43 @@ def test_over_slice():
return a[3:10] == [3, 4]
self.checkScript(test_backward_slice, ())

def test_slice_index(self):
a = torch.tensor(
[
[[1, 11], [2, 22]],
[[3, 33], [4, 44]],
[[5, 55], [6, 66]],
]
)

def test_index_slice1(x):
Lilyjjo marked this conversation as resolved.
Show resolved Hide resolved
x = x[:, :, [0, 1]]
return x
self.checkScript(test_index_slice1, (a,))

def test_index_slice2(x):
x = x[[2, 1, 0], :, :]
return x
self.checkScript(test_index_slice2, (a,))

def test_index_slice3(x):
x = x[[0, 1], :, [1]]
return x
self.checkScript(test_index_slice3, (a,))

def test_index_slice_empty_list(x):
empty_list: List[int] = []
x = x[empty_list, :, :]
return x
self.checkScript(test_index_slice_empty_list, (a,))

def test_index_slice_out_of_bounds_index(x):
x = x[[4], :, :]
return x
with self.assertRaisesRegex(RuntimeError, "index 4 is out of bounds for dimension 0 with size 3"):
self.checkScript(test_index_slice_out_of_bounds_index, (a,))


def test_mutable_list_append(self):
def test_append():
a = [0, 1]
Expand Down
5 changes: 2 additions & 3 deletions torch/jit/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,10 @@ def build_SliceExpr(ctx, base, slice_expr):
return SliceExpr(base.range(), lower, upper, step)

def build_Index(ctx, base, index_expr):
if isinstance(index_expr.value, ast.Tuple) or \
isinstance(index_expr.value, ast.List):
if isinstance(index_expr.value, ast.Tuple):
raise NotSupportedError(base.range(),
"slicing multiple dimensions with "
"sequences not supported yet")
"tuples not supported yet")
return build_expr(ctx, index_expr.value)

def build_ExtSlice(ctx, base, extslice):
Expand Down