Since you are detaching the computation graph from your tensor manually, x won’t get gradients anymore.
I assume you are detaching x to call expm from scipy.
You could try to implement the Pade approximation in PyTorch or write an autograd.Function with a custom backward method.