Interop between pycuda and pytorch

Is it possible to construct a pycuda array from a cuda array created in pytorch, or vice versa? Asking because there’s existing code for MPI based on pycuda (https://github.com/seba-1511/mpi4pycuda) that I would like to build on top of.

It is possible and you can find an example here. However, it’s quite hard to do it properly, as PyCuda initializes its own CUDA contexts instead of using a default one, so sometimes you may end up in a situation where PyTorch pointers are inaccessible form PyCuda and vice versa. Be careful with imports and initialization.