Yes, the gradient should be the same.
In my example I used single example in the for loop, so the scaling was straightforward.
If you use a batch size > 1, you would have to divide by the number of batches, if you use an averaged loss.
Your loss might differ, if your batch size is not constant, because the last batch is smaller for example.
Using a DataLoader, you could just drop the last batch, if it’s smaller, using drop_last=True.