You can manually implement a custom function within the python API via torch.custom.autograd (with a second derivative as well)
https://pytorch.org/docs/stable/autograd.html?highlight=unction#torch.autograd.Function 1