def th2nd(): ans = np.array([[1., 1., 1., 1.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) x = th.zeros((3, 4)) dl = dlpack.to_dlpack(x) y = nd.from_dlpack(dl) x[0] = 1 assert np.allclose(y.asnumpy(), ans)
def th2nd(): ans = np.array([[1., 1., 1., 1.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) x = F.zeros((3, 4)) dl = F.zerocopy_to_dlpack(x) y = nd.from_dlpack(dl) x[0] = 1 print(x) print(y) assert np.allclose(y.asnumpy(), ans)
def th2nd_incontiguous(): x = F.astype(F.tensor([[0, 1], [2, 3]]), F.int64) ans = np.array([0, 2]) y = x[:2, 0] # Uncomment this line and comment the one below to observe error #dl = dlpack.to_dlpack(y) dl = F.zerocopy_to_dlpack(y) z = nd.from_dlpack(dl) print(x) print(z) assert np.allclose(z.asnumpy(), ans)
def th2nd_incontiguous(): import dgl.backend as F x = th.LongTensor([[0, 1], [2, 3]]) ans = np.array([0, 2]) y = x[:2, 0] # Uncomment this line and comment the one below to observe error #dl = dlpack.to_dlpack(y) dl = F.zerocopy_to_dlpack(y) z = nd.from_dlpack(dl) assert np.allclose(z.asnumpy(), ans)
def zerocopy_to_dgl_ndarray_for_write(arr): return dglnd.from_dlpack(arr.to_dlpack_for_write())
def zerocopy_to_dgl_ndarray(arr): return dglnd.from_dlpack(arr.to_dlpack_for_read())