Thanks, but while my function is element-wise, my output is not a scalar.
So i think torch.autograd.grad requires me to supply grad_outputs, but I don’t really understand the documentation.

grad_outputs  should be a sequence of length matching  output  containing the pre-computed gradients w.r.t. each of the outputs.

But I don’t have the gradient yet! That’s what I want to use torch.autograd.grad for.
EDIT: The input can be quite big (for example a full 256x256x3 image) ! The function is used as a normal activation function, so if torch.autograd.grad computes the full jacobian, then this is not what I want. I need an element-wise derivative for the element-wise acting function.