cupy / cupy
CuPy / CuPy
CuPy is a NumPy/SciPy-compatible array library for GPU-accelerated computing with Python. CuPy acts as a drop-in replacement to run existing NumPy/SciPy code on NVIDIA CUDA or AMD ROCm platforms. CuPy 是一个兼容 NumPy/SciPy 的数组库,用于 Python 的 GPU 加速计算。CuPy 可以作为直接替代品,在 NVIDIA CUDA 或 AMD ROCm 平台上运行现有的 NumPy/SciPy 代码。
>>> import cupy as cp
>>> x = cp.arange(6).reshape(2, 3).astype('f')
>>> x
array([[ 0., 1., 2.],
[ 3., 4., 5.]], dtype=float32)
>>> x.sum(axis=1)
array([ 3., 12.], dtype=float32)
CuPy also provides access to low-level CUDA features. You can pass ndarray to existing CUDA C/C++ programs via RawKernels, use Streams for performance, or even call CUDA Runtime APIs directly. CuPy 还提供了对底层 CUDA 功能的访问。你可以通过 RawKernels 将 ndarray 传递给现有的 CUDA C/C++ 程序,使用 Streams 来提升性能,甚至直接调用 CUDA Runtime API。
Installation (安装)
Pip Binary packages (wheels) are available for Linux and Windows on PyPI. Choose the right package for your platform. PyPI 上提供适用于 Linux 和 Windows 的二进制包(wheels)。请根据你的平台选择合适的包。
| Platform | Architecture | Command |
|---|---|---|
| CUDA 12.x | x86_64 / aarch64 | pip install cupy-cuda12x |
| CUDA 13.x | x86_64 / aarch64 | pip install cupy-cuda13x |
| ROCm 7.0 (experimental) | x86_64 | pip install cupy-rocm-7-0 |
Note: To install pre-releases, append --pre -U -f https://pip.cupy.dev/pre (e.g., pip install cupy-cuda12x --pre -U -f https://pip.cupy.dev/pre).
注意:如需安装预发布版本,请添加 --pre -U -f https://pip.cupy.dev/pre(例如:pip install cupy-cuda12x --pre -U -f https://pip.cupy.dev/pre)。
Conda Binary packages are also available for Linux and Windows on Conda-Forge. Conda-Forge 上也提供适用于 Linux 和 Windows 的二进制包。
| Platform | Architecture | Command |
|---|---|---|
| CUDA | x86_64 / aarch64 / ppc64le | conda install -c conda-forge cupy |
If you need a slim installation (without also getting CUDA dependencies installed), you can do conda install -c conda-forge cupy-core. If you need to use a particular CUDA version (say 12.0), you can use the cuda-version metapackage to select the version, e.g. conda install -c conda-forge cupy cuda-version=12.0.
如果你需要精简安装(不安装 CUDA 依赖项),可以使用 conda install -c conda-forge cupy-core。如果你需要使用特定的 CUDA 版本(例如 12.0),可以使用 cuda-version 元包来指定版本,例如 conda install -c conda-forge cupy cuda-version=12.0。
Note: If you encounter any problem with CuPy installed from conda-forge, please feel free to report to cupy-feedstock, and we will help investigate if it is just a packaging issue in conda-forge’s recipe or a real issue in CuPy. 注意:如果你在使用从 conda-forge 安装的 CuPy 时遇到任何问题,请随时向 cupy-feedstock 报告,我们将协助调查这仅仅是 conda-forge 配方中的打包问题,还是 CuPy 本身的实际问题。
Docker Use NVIDIA Container Toolkit to run CuPy container images. 使用 NVIDIA Container Toolkit 来运行 CuPy 容器镜像。
$ docker run --gpus all -it cupy/cupy
Resources (资源)
- Installation Guide - instructions on building from source (安装指南 - 从源码构建说明)
- Release Notes (发布说明)
- Projects using CuPy (使用 CuPy 的项目)
- Contribution Guide (贡献指南)
- GPU Acceleration in Python using CuPy and Numba (GTC November 2021 Technical Session) (使用 CuPy 和 Numba 进行 Python GPU 加速 - GTC 2021 年 11 月技术会议)
- GPU-Acceleration of Signal Processing Workflows using CuPy and cuSignal1 (ICASSP’21 Tutorial) (使用 CuPy 和 cuSignal 进行信号处理工作流的 GPU 加速 - ICASSP’21 教程)
License (许可证)
MIT License (see LICENSE file). CuPy is designed based on NumPy’s API and SciPy’s API (see docs/source/license.rst file). CuPy is being developed and maintained by Preferred Networks and community contributors. MIT 许可证(见 LICENSE 文件)。CuPy 的设计基于 NumPy API 和 SciPy API(见 docs/source/license.rst 文件)。CuPy 由 Preferred Networks 和社区贡献者开发与维护。
Reference (参考)
Ryosuke Okuta, Yuya Unno, Daisuke Nishino, Shohei Hido and Crissman Loomis. CuPy: A NumPy-Compatible Library for NVIDIA GPU Calculations. Proceedings of Workshop on Machine Learning Systems (LearningSys) in The Thirty-first Annual Conference on Neural Information Processing Systems (NIPS), (2017). [PDF]
Footnotes (脚注)
cuSignal is now part of CuPy starting v13.0.0. 自 v13.0.0 版本起,cuSignal 已成为 CuPy 的一部分。