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

Infinite loop in inplace functionalization #1770

Open
beverlylytle opened this issue Feb 17, 2025 · 3 comments
Open

Infinite loop in inplace functionalization #1770

beverlylytle opened this issue Feb 17, 2025 · 3 comments
Labels
bug Something isn't working in-place

Comments

@beverlylytle
Copy link
Collaborator

beverlylytle commented Feb 17, 2025

The running the following snippet enters an infinite loop:

import thunder
import torch

def foo(a, b):
   x = a.add(1)
   y = x.view(-1)
   z = x.view(-1)
   x.add_(1)
   # y should have been updated.
   y2 = y + 1
   # z should have been updated too.
   z2 = z + 1
   return z2

a = torch.rand(10)
b = torch.rand(10)

jfoo = thunder.(foo)

jfoo(a, b)

Interrupting the program produces the following line in the trace:

  File ".../lightning-thunder/thunder/core/functionalization.py", line 572, in apply_functionalization_to_canonicalized_trace
    cur_v = swap_map[cur_k]

This line comes from a search over the graph defined by swap_map, which terminates when a leaf (a value which is not also a key) is reached. However for the function defined above swap_map contains a key-value pair where the key is the same as the value, resulting in an infinite loop.

Note the behavior is dependent on the shape of a.

@beverlylytle beverlylytle added the bug Something isn't working label Feb 17, 2025
@beverlylytle
Copy link
Collaborator Author

The function above is a variation on one appearing in test_multiple_views_before_inplace_to_base.

@crcrpar
Copy link
Collaborator

crcrpar commented Feb 17, 2025

swap_map contains a key-value pair where the key is the same as the value, resulting in an infinite loop.

I find it reasonable to have a check to avoid having a pair of key and value which have the same name.

Note the behavior is dependent on the shape of a.

Would you know a shape that does not cause thunder to get stuck in an infinite loop, out of curiosity?

@beverlylytle
Copy link
Collaborator Author

Would you know a shape that does not cause thunder to get stuck in an infinite loop, out of curiosity?

I didn't investigate this deeply, but 1-dimensional tensors yield an infinite loop, while 2-dimensional tensors do not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working in-place
Projects
None yet
Development

No branches or pull requests

3 participants