cupy.asnumpy() 和 get() 之间的区别
Difference between cupy.asnumpy() and get()
给定一个 CuPy 数组 a
,有两种方法可以从中获取 numpy 数组:a.get()
和 cupy.asnumpy(a)
。它们之间有什么实际区别吗?
import cupy as cp
a = cp.random.randint(10, size=(4,5,6,7))
b = a.get()
c = cp.asnumpy(a)
assert type(b) == type(c) and (b == c).all()
cp.asnumpy
是调用 ndarray.get
的包装器。您可以在 cp.asnumpy
:
的代码中看到
def asnumpy(a, stream=None, order='C', out=None):
"""Returns an array on the host memory from an arbitrary source array.
Args:
a: Arbitrary object that can be converted to :class:`numpy.ndarray`.
stream (cupy.cuda.Stream): CUDA stream object. If it is specified, then
the device-to-host copy runs asynchronously. Otherwise, the copy is
synchronous. Note that if ``a`` is not a :class:`cupy.ndarray`
object, then this argument has no effect.
order ({'C', 'F', 'A'}): The desired memory layout of the host
array. When ``order`` is 'A', it uses 'F' if ``a`` is
fortran-contiguous and 'C' otherwise.
out (numpy.ndarray): The output array to be written to. It must have
compatible shape and dtype with those of ``a``'s.
Returns:
numpy.ndarray: Converted array on the host memory.
"""
if isinstance(a, ndarray):
return a.get(stream=stream, order=order, out=out)
elif hasattr(a, "__cuda_array_interface__"):
return array(a).get(stream=stream, order=order, out=out)
else:
temp = _numpy.asarray(a, order=order)
if out is not None:
out[...] = temp
else:
out = temp
return out
如您所见(在文档和代码中),cp.asnumpy
支持的输入类型不仅仅是 CuPy 数组。它支持作为具有 __cuda_array_interface__
属性的 CUDA 对象的输入以及可以实际转换为 Numpy 数组的任何对象。这包括 Numpy 数组本身和可迭代对象(例如列表、生成器等)。
给定一个 CuPy 数组 a
,有两种方法可以从中获取 numpy 数组:a.get()
和 cupy.asnumpy(a)
。它们之间有什么实际区别吗?
import cupy as cp
a = cp.random.randint(10, size=(4,5,6,7))
b = a.get()
c = cp.asnumpy(a)
assert type(b) == type(c) and (b == c).all()
cp.asnumpy
是调用 ndarray.get
的包装器。您可以在 cp.asnumpy
:
def asnumpy(a, stream=None, order='C', out=None):
"""Returns an array on the host memory from an arbitrary source array.
Args:
a: Arbitrary object that can be converted to :class:`numpy.ndarray`.
stream (cupy.cuda.Stream): CUDA stream object. If it is specified, then
the device-to-host copy runs asynchronously. Otherwise, the copy is
synchronous. Note that if ``a`` is not a :class:`cupy.ndarray`
object, then this argument has no effect.
order ({'C', 'F', 'A'}): The desired memory layout of the host
array. When ``order`` is 'A', it uses 'F' if ``a`` is
fortran-contiguous and 'C' otherwise.
out (numpy.ndarray): The output array to be written to. It must have
compatible shape and dtype with those of ``a``'s.
Returns:
numpy.ndarray: Converted array on the host memory.
"""
if isinstance(a, ndarray):
return a.get(stream=stream, order=order, out=out)
elif hasattr(a, "__cuda_array_interface__"):
return array(a).get(stream=stream, order=order, out=out)
else:
temp = _numpy.asarray(a, order=order)
if out is not None:
out[...] = temp
else:
out = temp
return out
如您所见(在文档和代码中),cp.asnumpy
支持的输入类型不仅仅是 CuPy 数组。它支持作为具有 __cuda_array_interface__
属性的 CUDA 对象的输入以及可以实际转换为 Numpy 数组的任何对象。这包括 Numpy 数组本身和可迭代对象(例如列表、生成器等)。