CUDA utilities

Device, context and memory management on CuPy.

Note

The package chainer.cuda has been renamed to chainer.backends.cuda as of v4.0.0, but the previous module path chainer.cuda is also available.

Chainer uses CuPy (with very thin wrapper) to exploit the speed of GPU computation. Following modules and classes defined in CuPy are imported to chainer.backends.cuda module for convenience (refer to this table when reading chainer’s source codes).

imported name original name
chainer.backends.cuda.cupy cupy
chainer.backends.cuda.cupyx cupyx
chainer.backends.cuda.ndarray cupy.ndarray
chainer.backends.cuda.cupy.cuda cupy.cuda
chainer.backends.cuda.Device cupy.cuda.Device
chainer.backends.cuda.Event cupy.cuda.Event
chainer.backends.cuda.Stream cupy.cuda.Stream

Chainer replaces the default allocator of CuPy by its memory pool implementation. It enables us to reuse the device memory over multiple forward/backward computations, and temporary arrays for consecutive elementwise operations.

Devices

chainer.backends.cuda.get_device Gets the device from a device object, an ID integer or an array object.
chainer.backends.cuda.get_device_from_id Gets the device from an ID integer.
chainer.backends.cuda.get_device_from_array Gets the device from a list of CuPy array or a single CuPy array.

CuPy array allocation and copy

chainer.backends.cuda.copy Copies a cupy.ndarray object using the default stream.
chainer.backends.cuda.copyto Copies the elements of an ndarray to those of another one.
chainer.backends.cuda.to_cpu Copies the given GPU array to host CPU.
chainer.backends.cuda.to_gpu Copies the given CPU array to the specified device.

Kernel definition utilities

chainer.backends.cuda.memoize Makes a function memoizing the result for each argument and device.
chainer.backends.cuda.clear_memo Clears the memoized results for all functions decorated by memoize.
chainer.backends.cuda.elementwise Creates an elementwise kernel function.
chainer.backends.cuda.reduce Creates a global reduction kernel function.

CPU/GPU generic code support

chainer.backends.cuda.get_array_module Gets an appropriate one from numpy or cupy.

cuDNN support

chainer.backends.cuda.set_max_workspace_size Sets the workspace size for cuDNN.
chainer.backends.cuda.get_max_workspace_size Gets the workspace size for cuDNN.