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 or ~1e-6, which is expected due to the limited floating point precision and which is caused by a different order of operations.