albanD:

if it requires grad, it is a leaf only if it was created by the user (not the result of an op).


I thought that was correct but I tried running some code and the result of exponentiation results in the dummy loss being a leaf, which I thought was super bizarre. Is there anything wrong with my code? shouldn’t l_y NOT be a leaf?
import torch
from torchviz import make_dot

lst = []
lst2 = lst

print('-- lst vs lst2')
print(lst is lst2)
lst.append(2)
print(f'lst={lst} vs lst2={lst2}')

x = torch.tensor([2.0],requires_grad=False)
xx = x

print('-- x vs xx')
print(f'xx == x = {xx == x}')
print(f'xx is x = {xx is x}')
xx.fill_(4.0)
print(f'x = {x}')
print(f'xx = {xx}')

x = torch.tensor([2.0],requires_grad=False)
y = x**2
l_y = (y - x + 3)**3 + 123

print('\n-- x vs y')
print(f'y = {y}')
print(f'y == x = {y==x}')
print(f'y is x = {y is x}')
print(f'y.is_leaf = {y.is_leaf}')
print(f'l_y.is_leaf = {l_y.is_leaf}')
y.fill_(3.0)
print(f'x = {x}')
print(f'y = {y}')
print(f'x.is_leaf = {x.is_leaf}')
print(f'y.is_leaf = {y.is_leaf}')
print(f'l_y.is_leaf = {l_y.is_leaf}')

result:

-- lst vs lst2
True
lst=[2] vs lst2=[2]
-- x vs xx
xx == x = tensor([True])
xx is x = True
x = tensor([4.])
xx = tensor([4.])

-- x vs y
y = tensor([4.])
y == x = tensor([False])
y is x = False
y.is_leaf = True
l_y.is_leaf = True
x = tensor([2.])
y = tensor([3.])
x.is_leaf = True
y.is_leaf = True
l_y.is_leaf = True

I doubt its a bug in pytorch but I can’t see what I did wrong…or is my expectation wrong?