Hi, these days I went back to this problem for some reason, it seems autograd did trace data attribute. Here is an example based on yours:
x = torch.ones(1, 2)
w = torch.ones(2, 1, requires_grad=True)
target = torch.full((1,), 10.)

# manipulate data attribute
x.data = torch.cat((x,x), dim=1)
w.data = torch.cat((w,w), dim=0)

output = x.mm(w)
optimizer = torch.optim.SGD([w], lr=1.)

loss = (output - target)**2
loss.backward()
print(w.grad)
> tensor([[-12.],
          [-12.],
          [-12.],
          [-12.]])

If autograd did not trace data attribute, should w.grad have a size of 2x1 instead of 4x1? Please correct me if I’m wrong. Thx!