I found this issue on Github and maybe it’s related to my problem.
backward pass different behaviors with inplace operation 19
To my understand, when calculate the derivative of  score, Pytorch’s autograd mechanism uses the formula S'(x) = S(x)(1-S(x)), which means we cannot change the S(x) because we need it. In my second case, the inplace operation is before the sigmoid operation, so the second case doesn’t raise error.
But here comes another question (), why the inplace operation on x doesn’t raise error? or more clearly, why the following codes doesn’t raise error?
a = torch.randn((5), requires_grad=True)
b = 2*a + 1
c = b*b + 1
b[1] = 0.
d = torch.mean(b)
d.backward()

in this case, b  is needed to correctly compute the gradient of  c, but b is modified after c,  why this case doesn’t raise error?