Solved by ptrblck in post #2 
              
              
                I don’t think the answer from the cross-post is correct, as your code doesn’t show any optimizer.step() usage so I assume you are purely comparing the gradients computed via using the entire dataset vs. single samples. 
If so, I would assume the final difference would show a relative error of ~1e-5 …