autograd automatically condenses the gradient to the proper size for the weight regardless of the batch size.  If you just want to run your network with batches, you don’t need to modify the existing code as autograd will do that for you.
If, instead, you want manual access to the gradients, you may want to look at backward hooks: torch.nn.modules.module.register_module_backward_hook — PyTorch master documentation 3