Hi,
The error happens because out has ore than one element.
If you have a scalar loss and want the gradient wrt that, you can just compute it and call autograd.grad() on that.
If you want the gradient of the ith input wrt to the ith output. Then there are two cases:

If your net compute the value for each input element independently (no batchnorm or layer norm), then you can simply call autograd.grad() with out.sum() or grad_outputs=torch.ones_like(out).
If not, then you will have to do one backward for each element in your Tensor to make sure you can separate the gradient coming from each output element.