CuPy is a NumPy/SciPy-compatible array library for GPU-accelerated computing with Python
—
Comprehensive array creation, manipulation, and transformation functions that mirror NumPy's API while operating on GPU memory. These functions form the foundation of CuPy's array computing capabilities.
Create arrays directly on GPU memory with various initialization patterns.
def array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0):
"""
Create an array on GPU.
Parameters:
- object: array-like input data
- dtype: data type, optional
- copy: whether to copy data
- order: memory layout ('C', 'F', 'A', 'K')
- subok: allow subclasses
- ndmin: minimum number of dimensions
Returns:
cupy.ndarray: GPU array
"""
def asarray(a, dtype=None, order=None):
"""Convert input to GPU array."""
def ascontiguousarray(a, dtype=None):
"""Return contiguous array in C order."""
def copy(a, order='K', subok=False):
"""Return array copy."""def zeros(shape, dtype=float, order='C'):
"""Return array filled with zeros."""
def ones(shape, dtype=None, order='C'):
"""Return array filled with ones."""
def empty(shape, dtype=float, order='C'):
"""Return uninitialized array."""
def full(shape, fill_value, dtype=None, order='C'):
"""Return array filled with fill_value."""
def zeros_like(a, dtype=None, order='K', subok=True, shape=None):
"""Return zeros array with same shape as a."""
def ones_like(a, dtype=None, order='K', subok=True, shape=None):
"""Return ones array with same shape as a."""
def empty_like(a, dtype=None, order='K', subok=True, shape=None):
"""Return empty array with same shape as a."""
def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None):
"""Return full array with same shape as a."""def arange(start, stop=None, step=1, dtype=None):
"""Return evenly spaced values within interval."""
def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0):
"""Return evenly spaced numbers over interval."""
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0):
"""Return numbers spaced evenly on log scale."""
def meshgrid(*xi, copy=True, sparse=False, indexing='xy'):
"""Return coordinate matrices from coordinate vectors."""def eye(N, M=None, k=0, dtype=float, order='C'):
"""Return 2-D array with ones on diagonal."""
def identity(n, dtype=None):
"""Return identity array."""
def diag(v, k=0):
"""Extract diagonal or construct diagonal array."""
def diagflat(v, k=0):
"""Create 2-D array with flattened input as diagonal."""
def tri(N, M=None, k=0, dtype=float):
"""Array with ones at and below given diagonal."""
def tril(m, k=0):
"""Lower triangle of array."""
def triu(m, k=0):
"""Upper triangle of array."""
def vander(x, N=None, increasing=False):
"""Generate Vandermonde matrix."""def reshape(a, newshape, order='C'):
"""Return array with new shape."""
def ravel(a, order='C'):
"""Return contiguous flattened array."""
def flatten(a, order='C'):
"""Return copy of array collapsed into one dimension."""
def squeeze(a, axis=None):
"""Remove single-dimensional entries."""
def expand_dims(a, axis):
"""Expand shape by inserting new axes."""def transpose(a, axes=None):
"""Reverse or permute axes."""
def swapaxes(a, axis1, axis2):
"""Interchange two axes."""
def moveaxis(a, source, destination):
"""Move axes to new positions."""
def rollaxis(a, axis, start=0):
"""Roll specified axis backwards."""def atleast_1d(*arys):
"""Convert inputs to arrays with at least 1 dimension."""
def atleast_2d(*arys):
"""Convert inputs to arrays with at least 2 dimensions."""
def atleast_3d(*arys):
"""Convert inputs to arrays with at least 3 dimensions."""
def broadcast_to(array, shape, subok=False):
"""Broadcast array to shape."""
def broadcast_arrays(*args, subok=False):
"""Broadcast arrays to common shape."""def concatenate(arrays, axis=0, out=None, dtype=None, casting='same_kind'):
"""Join arrays along existing axis."""
def stack(arrays, axis=0, out=None):
"""Join arrays along new axis."""
def vstack(tup):
"""Stack arrays vertically."""
def hstack(tup):
"""Stack arrays horizontally."""
def dstack(tup):
"""Stack arrays depth-wise."""
def column_stack(tup):
"""Stack 1-D arrays as columns into 2-D array."""def split(ary, indices_or_sections, axis=0):
"""Split array into multiple sub-arrays."""
def array_split(ary, indices_or_sections, axis=0):
"""Split array into multiple sub-arrays."""
def hsplit(ary, indices_or_sections):
"""Split array along 2nd axis."""
def vsplit(ary, indices_or_sections):
"""Split array along 1st axis."""
def dsplit(ary, indices_or_sections):
"""Split array along 3rd axis."""def tile(A, reps):
"""Construct array by repeating A."""
def repeat(a, repeats, axis=None):
"""Repeat elements of array."""def append(arr, values, axis=None):
"""Append values to end of array."""
def resize(a, new_shape):
"""Return new array with specified shape."""
def trim_zeros(filt, trim='fb'):
"""Trim leading/trailing zeros."""
def unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None):
"""Find unique elements."""def flip(m, axis=None):
"""Reverse order of elements along axis."""
def fliplr(m):
"""Flip array left to right."""
def flipud(m):
"""Flip array up to down."""
def roll(a, shift, axis=None):
"""Roll array elements along axis."""
def rot90(m, k=1, axes=(0, 1)):
"""Rotate array by 90 degrees."""def asnumpy(a, stream=None, order='C', out=None):
"""
Convert CuPy array to NumPy array.
Parameters:
- a: input CuPy array
- stream: CUDA stream for async transfer
- order: memory layout
- out: output NumPy array
Returns:
numpy.ndarray: array on CPU
"""
def asarray_chkfinite(a, dtype=None, order=None):
"""Convert to array checking for NaNs or Infs."""
def asfarray(a, dtype=float):
"""Return array converted to float type."""
def asfortranarray(a, dtype=None):
"""Return array laid out in Fortran order."""
def require(a, dtype=None, requirements=None):
"""Return array satisfying requirements."""import cupy as cp
# Create arrays with different initialization
zeros = cp.zeros((3, 4))
ones = cp.ones((2, 2), dtype=cp.float32)
identity = cp.eye(5)
range_array = cp.arange(10)
linear = cp.linspace(0, 1, 100)
# Create from existing data
cpu_data = [1, 2, 3, 4, 5]
gpu_array = cp.array(cpu_data)import cupy as cp
# Create and reshape arrays
arr = cp.arange(12)
reshaped = arr.reshape(3, 4)
flattened = reshaped.ravel()
# Transpose operations
matrix = cp.random.random((3, 4, 5))
transposed = cp.transpose(matrix, (2, 0, 1))
swapped = cp.swapaxes(matrix, 0, 2)import cupy as cp
# Join arrays
arr1 = cp.array([1, 2, 3])
arr2 = cp.array([4, 5, 6])
joined = cp.concatenate([arr1, arr2])
stacked = cp.stack([arr1, arr2])
# Split arrays
data = cp.arange(10)
split_arrays = cp.split(data, 5) # Split into 5 equal parts
first_half, second_half = cp.array_split(data, 2)Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda12x