2

I'm using CuPy to generate some ground truth + network input data on the GPU, which I want to send to a TensorFlow network through the standard API:

feed_dict = {
    network.Labels: labels,
    network.Network_Input: networkInput
}

However, TensorFlow 1.15 expects NumPy and not CuPy and I get this error:

File "/lib/python3.6/site-packages/numpy/core/numeric.py", line 538, in asarray return array(a, dtype, copy=False, order=order)
ValueError: object __array__ method not producing an array

Trying to convert the CuPy arrays to NumPy arrays using

labels = numpy.array(labels, dtype=np.float)

I get the error:

ValueError: object __array__ method not producing an array

Is there a way to pass CuPy data to tensorflow ?

He3lixxx
  • 3,263
  • 1
  • 12
  • 31
Ilan
  • 21
  • 1
  • 2

2 Answers2

2

DLPack tensor structure is now supported in Tensorflow 2 in the tf.experimental.dlpack package.

You can find the howto usage in this repo: https://github.com/VoVAllen/tf-dlpack#cupy---tensorflow

# pip install numba numpy
import numpy as np
from numba import cuda

# NumPy - CPU Array
cpu_arr = np.random.rand(10_000, 10_000)

# Use Numba to move to GPU
numba_gpu_arr = cuda.to_device(cpu_arr)

# Use CuPy's asarray function and toDlpack to create DLPack capsule. There are multiple other ways to do this (i.e. PyTorch Utils)
dlpack_arr = cp.asarray(numba_gpu_arr).toDlpack()

# Migrate from Numba, used for custom CUDA JIT kernels to PyTorch
tf_tensor = tfdlpack.from_dlpack(dlpack_arr)

# Confirm TF tensor is on GPU
print(tf_tensor.device)

# Use `tfdlpack` to migrate back to Numba
dlpack_capsule = tfdlpack.to_dlpack(tf_tensor)
numba_arr = cuda.to_device(cp.fromDlpack(dlpack_capsule))
John
  • 168
  • 1
  • 10
-1

To copy CuPy array (on GPU) to NumPy array (on CPU), use cupy.asnumpy(arr) or arr.get().

https://docs-cupy.chainer.org/en/latest/reference/ndarray.html#conversion-to-from-numpy-arrays

kmaehashi
  • 879
  • 6
  • 10
  • It seems CuPy has a special API to PyTorch, allowing to convert CuPy arrays to PyTorch tensors on the GPU, without going through NumPy on the CPU. However, such a support for TensorFlow is missing :-( – Ilan Nov 17 '19 at 06:45
  • 2
    CuPy supports standard protocols (DLPack and cuda_array_interface) but TF does not. It seems there are some discussions ongoing: https://github.com/tensorflow/tensorflow/issues/24453 https://github.com/tensorflow/tensorflow/issues/29039 – kmaehashi Nov 18 '19 at 06:46