安装带有 cupy 的 pip 包作为要求将安装置于永无止境的循环中

Installing a pip package with cupy as a requirement puts install in never ending loop

我正在尝试制作一个带有 cupy 的 pip 包作为要求之一,但我在要求中包含了 cupy,pip 安装以永无止境的循环结束。我正在尝试在已经安装了 Cupy 的 Google Colab 上安装该软件包,因此它应该只检查 Cupy 是否已经安装,而不是再次尝试安装它。

我在 github 中制作了一个最小的 pip 包,其中 cupy 是唯一的要求。

https://github.com/Santosh-Gupta/TroubleShootCupyInstall

我尝试将它安装在 Google Colab 中

!pip install --verbose https://github.com/Santosh-Gupta/TroubleShootCupyInstall/archive/master.zip --log 'file.log'

输出很多,因为它很冗长,但这些是在进入循环时打印的行。

 x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -D_FORCE_INLINES=1 -I/usr/local/cuda/include -I/usr/include/python3.6m -c cupy/cuda/nvtx.cpp -o build/temp.linux-x86_64-3.6/cupy/cuda/nvtx.o
  x86_64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.6/cupy/cuda/nvtx.o -L/usr/local/cuda/lib64 -lnvToolsExt -o build/lib.linux-x86_64-3.6/cupy/cuda/nvtx.cpython-36m-x86_64-linux-gnu.so -Wl,--disable-new-dtags,-rpath,/usr/local/cuda/lib64
  building 'cupy.cuda.thrust' extension
  x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -D_FORCE_INLINES=1 -I/usr/local/cuda/include -I/usr/include/python3.6m -c cupy/cuda/thrust.cpp -o build/temp.linux-x86_64-3.6/cupy/cuda/thrust.o
  NVCC options: ['--generate-code=arch=compute_30,code=compute_30', '--generate-code=arch=compute_50,code=compute_50', '--generate-code=arch=compute_60,code=sm_60', '--generate-code=arch=compute_61,code=sm_61', '--generate-code=arch=compute_70,code=sm_70', '--generate-code=arch=compute_75,code=sm_75', '--generate-code=arch=compute_70,code=compute_70', '-O2', '--compiler-options="-fPIC"']
  /usr/local/cuda/bin/nvcc -D_FORCE_INLINES=1 -I/usr/local/cuda/include -I/usr/include/python3.6m -c cupy/cuda/cupy_thrust.cu -o build/temp.linux-x86_64-3.6/cupy/cuda/cupy_thrust.o --generate-code=arch=compute_30,code=compute_30 --generate-code=arch=compute_50,code=compute_50 --generate-code=arch=compute_60,code=sm_60 --generate-code=arch=compute_61,code=sm_61 --generate-code=arch=compute_70,code=sm_70 --generate-code=arch=compute_75,code=sm_75 --generate-code=arch=compute_70,code=compute_70 -O2 --compiler-options="-fPIC"

为了方便起见,我制作了一个 Google Colab notebook,它运行这条线,并具有完整的输出。

https://colab.research.google.com/drive/1DFR78cJ07KaHkJfpjh8370SxNw0HXI50

CuPy 目前提供名为 cupy 的源码包和名为 cupy-cudaXX 的二进制分发包(其中 XX 是 CUDA 版本)。 目前 Google Colab 附带 cupy-cuda100,因为它使用的是 CUDA 10.0。 如果您将 cupy 指定为包的要求,将下载并安装 cupy 源包(需要花费几分钟的构建),即使 CuPy 已经可以通过 cupy-cuda100 使用。

不幸的是,Python 包分发工具(例如 setuptoolspip 等)没有提供很好地处理这种复杂包组合的方法。

解决方法 1

setup.py 中(或在您的包裹的 __init__.py 中)

try:
  import cupy
except Exception:
  raise RuntimeError('CuPy is not available. Please install it manually: https://docs-cupy.chainer.org/en/stable/install.html#install-cupy')

# You can also use `cupy.__version__` and `numpy.lib.NumpyVersion` to test CuPy version requirement here.

解决方法 2

使用 pkg_resourcessetuptools 的一部分)手动检查需求,就像在 Chainer 中完成的那样。