I understand your concern now. Assuming your loss_fn is a standard loss that linearly combines loss for each data, then doing backward using the aggregated loss is equivalent with doing it separately because \grad_X a X = a * 1, where 1 is the tensor with same shape as X but filled with all ones.
If you are using the loss fns in nn.* and want to do it more explicitly, you can use the reduce=False kwarg (doc here: http://pytorch.org/docs/master/nn.html#loss-functions 3). Then the loss will be a tensor, but this usually doesn’t average across data dimensions as well. To get per-data loss, you may need to do manual averaging across dimensions within data (depending on the fn you use). Then, you can specify gradients for each data and backward by loss.backward(torch.ones(batch_size)).