Oh this is almost there, I just need to figure out how to pass in a python variable into a jl.seval("")
call. When I hardcode the pointer integer, this works
import torch
sz = (100, 100)
arr = np.random.choice([0, 1], size=sz)
# Step 1: Create a PyTorch tensor and transfer it to GPU
tensor = torch.tensor(arr, dtype=torch.float32).cuda()
print("Pytorch Tensor : ", tensor)
# Step 2: Get pointer of the tensor
ptr = tensor.data_ptr()
print("pointer: ", ptr)
# DOESN"T WORK
# cu_ptr = jl.seval("""
# CuPtr{Float32}(pyconvert(UInt, ptr))
# """)
# Convert to julia CuPtr (IDK how to pass in the variable `ptr`)
cu_ptr = jl.seval("""
CuPtr{Float32}(pyconvert(UInt, 138064357793280))
""")
print("julia pointer: ", cu_ptr)
# Convert to CUDA array
cu_arr = jl.unsafe_wrap(jl.CuArray, cu_ptr, sz)
cu_arr
Pytorch Tensor : tensor([[0., 0., 0., ..., 0., 1., 1.],
[0., 1., 0., ..., 1., 1., 1.],
[0., 1., 1., ..., 0., 1., 0.],
...,
[0., 1., 0., ..., 0., 1., 1.],
[1., 0., 1., ..., 0., 1., 1.],
[1., 1., 0., ..., 1., 0., 0.]], device='cuda:0')
pointer: 138064357752832
julia pointer: CuPtr{Float32}(0x00007d919d009e00)
100×100 CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}:
0.0 1.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 1.0 … 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0
0.0 1.0 0.0 0.0 1.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0
0.0 0.0 1.0 1.0 0.0 1.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 1.0 0.0 1.0 0.0 0.0 1.0
0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 1.0 0.0 0.0 0.0 1.0 0.0 1.0 1.0 0.0 1.0 1.0
1.0 1.0 0.0 1.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 1.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