You could either pass the gradients in the same shape as the output (torch.ones_like(loss) would be the default) or reduce the loss to a scalar.
By default the loss functions will calculate the loss average and will return a scalar.