It depends on. Pytorch is c++ based and ofc you can code whatever in c++.
The point is you need to provide a context variable which is the core of autograd. In short, you will have to provide loss and gradients in order to make it work.