This is expected I’m afraid. The chain rule that the autograd uses only works at points where the function is differentiable. But 1/x here is not, hence the nan.
You can indeed use a custom Function to achieve that.
Can you share your issue with that?
One thing to keep in mind for double backward is that you should not save intermediary Tensors and re-use them. Only input/output. You can make intermediary Tensors dummy outputs if needed here.