The loss.backward() operation would implicitly create a gradient as torch.ones(1), if the loss is a scalar value.
Otherwise, if the loss is a tensor with multiple values, you would either have to provide the gradient explicitly or reduce the loss before, e.g. via loss.mean().backward().