Thanks @alexis-jacq .
I tried you modified code, but still got the same result on my machine:

image.png1105×299 25.8 KB

For this simple example, it is clear to see the gradient has been accumulated two times as you said. However, for the second example in my original post, it is not clear how it works. I am still not clear about how the computation graph is constructed when autograd.grad is called.