Solved by JuanFMontesinos in post #2 
              
              
                Both variables are pointing to the same memory address, you have to do a deep copy of p.grad in order not to be modified. You probably can do that by using the clone()  tensor method.