The previous grad is same as current grad for all optimization steps except the first step (where prev_grad is zero), due to call by reference happening. Hence the diff is zero for all steps except the first step. Printing diff and plotting it with optimization steps shows this. It can be fixed by state['previous_grad'] = grad.clone(). That way the previous grad will be stored properly, without changing the values at the same memory location.