Yes.
One thing you can try to do if this function is simple but just too slow (because a lot of operations), you can try and convert it into a hand made autograd Function (see doc 20): basically, you will implement both the forward and the backward to replace the autodiff that can be slow if a lot of small operations are made.