I would like to define the function

This function is infinitely differentiable. It’s derivatives in zero are all zero.
def f(x):
    return (-1/x).exp() * (x > 0)

x = torch.tensor(0.0, requires_grad=True)
y = f(x)
y.backward()
x.grad  # NaN

How can I make torch.autograd return the correct derivatives (all of them)?
note: I tried with torch.autograd.Function but I had a problem with the second derivative.