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

Fix ZeroDivisionError when unfolding a zero-dimension tensor in compile mode #113259

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test/inductor/test_torchinductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6367,6 +6367,17 @@ def test_zero_dim_reductions(self):

self.assertTrue(torch.allclose(actual, expected, atol=1e-3, rtol=1e-3))

def test_unfold_zero_dimension_tensor(self):
def forward(x):
return torch.unfold_copy(dimension=1, input=x, size=0, step=7)

x = torch.rand([1, 0], dtype=torch.float32)

y = forward(x)
compiled_y = torch.compile(forward, fullgraph=True)(x)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lamroger Just out of curiosity, I executed this line only inside python interpreter. It still throws out ZeroDivisionError. Do you have any clue why? but pytest passes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm maybe you can reload the python REPL or look at the stack trace to see which files are being executed and make sure the right ones are patched locally.

That worked for me when I was fiddling around

Copy link

@akdong91 akdong91 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly. it is working now without an exception. Probably, the outdated module was still used.


self.assertEqual(y, compiled_y)

def test_zero_element_mutation(self):
class CustomModel(nn.Module):
def __init__(self):
Expand Down
8 changes: 5 additions & 3 deletions torch/_inductor/lowering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,12 +1186,14 @@ def unfold(x, dimension, size, step):
if ndim == 0:
return slice_(unsqueeze(x, 0), end=size)

dim_size = sizes[dim]
sizevars = V.graph.sizevars
sizevars.guard_leq(size, sizes[dim])
sizevars.guard_leq(size, dim_size)
sizevars.guard_lt(0, step)

new_dim_size = FloorDiv(sizes[dim] - size, step) + 1
x.mark_reuse(sizevars.size_hint(CeilDiv(new_dim_size * size, sizes[dim])))
new_dim_size = FloorDiv(dim_size - size, step) + 1
if sizevars.size_hint(dim_size) > 0:
x.mark_reuse(sizevars.size_hint(CeilDiv(new_dim_size * size, dim_size)))

out_size = [*sizes[:dim], new_dim_size, *sizes[dim + 1 :], size]

Expand Down
Loading