CuPy 是一个兼容 NumPy/SciPy API 的 GPU 加速数组库,让现有 Python 数值计算代码无需大幅修改即可在 CUDA 或 ROCm 平台上高效运行。

# CuPy :面向 GPU 的 NumPy 和 SciPy
[](https://pypi.python.org/pypi/cupy)
[](https://anaconda.org/conda-forge/cupy)
[](https://github.com/cupy/cupy)
[](https://gitter.im/cupy/community)
[](https://twitter.com/CuPy_Team)
[](https://medium.com/cupy-team)
[**网站**](https://cupy.dev/)
| [**安装**](https://docs.cupy.dev/en/stable/install.html)
| [**教程**](https://docs.cupy.dev/en/stable/user_guide/basic.html)
| [**示例**](https://github.com/cupy/cupy/tree/main/examples)
| [**文档**](https://docs.cupy.dev/en/stable/)
| [**API 参考**](https://docs.cupy.dev/en/stable/reference/)
| [**论坛**](https://groups.google.com/forum/#!forum/cupy)
CuPy 是一个兼容 NumPy/SciPy 的数组库,用于通过 Python 进行 GPU 加速计算。
CuPy 可作为[直接替代品](https://docs.cupy.dev/en/stable/reference/comparison.html),在 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 还提供了对底层 CUDA 功能的访问。
你可以通过 [RawKernels](https://docs.cupy.dev/en/stable/user_guide/kernel.html#raw-kernels) 将 `ndarray` 传递给现有的 CUDA C/C++ 程序,使用 [Streams](https://docs.cupy.dev/en/stable/reference/cuda.html) 提升性能,甚至直接调用 [CUDA Runtime APIs](https://docs.cupy.dev/en/stable/reference/cuda.html#runtime-api)。
## 安装
### Pip
[PyPI](https://pypi.org/org/cupy/) 上提供了适用于 Linux 和 Windows 的二进制包(wheels)。
请为您的平台选择合适的包。
| 平台 | 架构 | 命令 |
|--------------------------------------------------------------------------------------------------------------| ----------------- |-----------------------------|
| CUDA 12.x | x86_64 / aarch64 | `pip install cupy-cuda12x` |
| CUDA 13.x | x86_64 / aarch64 | `pip install cupy-cuda13x` |
| ROCm 7.0 (*[实验性](https://docs.cupy.dev/en/latest/install.html#using-cupy-on-amd-gpu-experimental)*) | x86_64 | `pip install cupy-rocm-7-0` |
### Conda
[Conda-Forge](https://anaconda.org/conda-forge/cupy) 上也提供了适用于 Linux 和 Windows 的二进制包。
| 平台 | 架构 | 命令 |
| --------------------- | --------------------------- | ------------------------------------------------------------- |
| CUDA | x86_64 / aarch64 / ppc64le | `conda install -c conda-forge cupy` |
如果你需要进行轻量安装(不安装 CUDA 依赖),可以运行 `conda install -c conda-forge cupy-core`。
如果你需要使用特定的 CUDA 版本(例如 12.0),可以使用 `cuda-version` 元包来选择版本,例如 `conda install -c conda-forge cupy cuda-version=12.0`。
### Docker
使用 [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/overview.html) 来运行 [CuPy 容器镜像](https://hub.docker.com/r/cupy/cupy)。
```
$ docker run --gpus all -it cupy/cupy
```
## 资源
- [安装指南](https://docs.cupy.dev/en/stable/install.html) - 从源码构建的说明
- [发布说明](https://github.com/cupy/cupy/releases)
- [使用 CuPy 的项目](https://github.com/cupy/cupy/wiki/Projects-using-CuPy)
- [贡献指南](https://docs.cupy.dev/en/stable/contribution.html)
- [使用 CuPy 和 Numba 进行 Python GPU 加速(2021 年 11 月 GTC 技术会议)](https://www.nvidia.com/en-us/on-demand/session/gtcfall21-a31149/)
- [使用 CuPy 和 cuSignal[^1] 加速信号处理工作流(ICASSP'21 教程)](https://github.com/awthomp/cusignal-icassp-tutorial)
[^1]: cuSignal 自 v13.0.0 起已成为 CuPy 的一部分。
## 许可证
MIT 许可证(参见 `LICENSE` 文件)。
CuPy 基于 NumPy 的 API 和 SciPy 的 API 设计(参见 `docs/source/license.rst` 文件)。
CuPy 由 [Preferred Networks](https://www.preferred.jp/en/) 和 [社区贡献者](https://github.com/cupy/cupy/graphs/contributors) 共同开发和维护。
## 参考
Ryosuke Okuta, Yuya Unno, Daisuke Nishino, Shohei Hido 和 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](http://learningsys.org/nips17/assets/papers/paper_16.pdf)]
```
@inproceedings{cupy_learningsys2017,
author = "Okuta, Ryosuke and Unno, Yuya and Nishino, Daisuke and Hido, Shohei and Loomis, Crissman",
title = "CuPy: A NumPy-Compatible Library for NVIDIA GPU Calculations",
booktitle = "Proceedings of Workshop on Machine Learning Systems (LearningSys) in The Thirty-first Annual Conference on Neural Information Processing Systems (NIPS)",
year = "2017",
url = "http://learningsys.org/nips17/assets/papers/paper_16.pdf"
}
```