Solved by ptrblck in post #4 
              
              
                Your code should raise a warning and explain the reason and workaround for it: 
UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the gradient for a non-leaf Tensor, use .ret…