See here  5, it is too complicated!

Yeah, this one seems a bit too complicated. 

I think the best way is to make a pytorch extension  4.

I never knew about this feature of PyTorch. Can give it a try!
Thanks, @Eta_C  ; though let’s keep the discussion open for any other possible solutions.