Hi,
The thing is that copy_() is modifying store inplace.
So the store used in the first part is actually the same as the one used in the second evaluation. And so running backward on the second one also tries to backward through the first one
You can see that by running in colab:
# !pip install torchviz

import torch
from torch import nn
from torchviz import make_dot

x=torch.randn(5,3)
m=nn.Linear(3,2)
store=torch.zeros(5,2)

y=m(x)
store.copy_(y)
res = store.mean()
print("First backward...")
res.backward()
make_dot(res)


image440×810 27.1 KB

y=m(x)
store.copy_(y)
res = store.mean()
print("Before second backward...")
make_dot(res)


image686×976 45 KB

You can see that before the second backward, the graph also contains the one from the first backward.