@G.M  The problem with writing in C++ is that I have to distribute this as a library, so binary blobs might cause their own issues, especially since I distribute it as setup on machine, rather than as a wheel.
Would you mind explaining how to replace this kind of loop with a Pytroch method ?