Thanks for your reply, and your solution just works.
But there’s another question, if I modify my code:
import torch.nn as nn
import torch

input = torch.ones(2).cuda()
linear = nn.Linear(2, 2).cuda()
output = linear(input)
# output[0] = nn.ReLU(inplace=False)(output[0])
output[0] = output[0] * 2 + 1
output[0] = output[0] + output[1]
loss = torch.sum(output)
loss.backward()

Similarly,  I assigned the result to the same tensor in line 8 and line 9, but this code works well. What’s the different between the two versions?