Can I get the CUDA tensor pointer in the python pytorch

Hello I have device tensors defined in the pyton script - can I get the pointer to the begining of the array

// get the pointer of variable anytensor (cast to float type, this is up to your data type)
auto c_pointer = anytensor.data_ptr<float>();

Thanks !, but can I do it from python script ?

AFAIK, there is no pointer data type in python. What do you want it for?

Frankly I want to pass it into Julia code through python - I created Julia library that uses CUDA acceleration for medical segmentation metrics - In Julia I need data on GPU it is done by the CUDA array - which is basically list of pointers. Julia is created for interoperability with python, so I can easily call Julia from python but it will copy data on CPU and than on GPU. In order to get around it I would like to have access to a pointer in memory - and then I would probably be able to parse it into Julia Array.

I don’t know if this works with Julia, but you could try to cast the x.data_ptr() to a void pointer via ctypes.c_void_p and pass it round.

1 Like