2

I have written a python package that does stuff (the specifics are irrelevant). When using the package, the user can specify whether or not to make use of available GPUs. In my code, I use the CuPy package to utilize the available GPUs.

I would now like to distribute the package - ideally package it with setuptools, put it up on PyPI and have users install it using pip.

The issue:

I want it to be possible to install it on systems regardless of whether they have CUDA available or not. The problem is, if the system does not have CUDA, the installation (and consequently the import) of CuPy will fail. Therefore, I gather that I need to have two separate versions of the package, one that supports CUDA (and imports CuPy etc) and one that does not. Moreover, I need some mechanism that will choose the appropriate version during installation.

What I was hoping to find is some sort of tool that will check (during the installation process) whether CUDA is available (or, more practically - whether CuPy can be installed), and accordingly choose the proper version of my package to download and install.

Any ideas whether something of the sorts exists, and if not, then on the right way to implement it?

NOTE: I assume I could list CuPy in the dependencies in setuptools regardless of whether the system has CUDA or not, and assuming it does not, have my package catch any exceptions that arise due to trying to install/import/use it. But I feel this is rather barbaric, and was hoping to find something more elegant.

NOTE 2: Of course, I could let the user choose the right version for his system, but I would rather not, if possible.

Dalco
  • 21
  • 3
  • What does "the package implements CuPy" mean? Are you really asking about the necessary CUDA components for CuPY to work? CUDA isn't a monolithic thing -- it is a GPU driver, some redistributable runtime libraries, and a toolchain. Which do you actually need and want to test for? – talonmies Aug 25 '20 at 13:32
  • @talnomies You are right, I was not clear. What I meant to say is that in my package I make use of GPUs with CuPy. So basically, as far as I am concerned the question of whether CUDA is available or not boils down to whether I can install CuPy or not. I will edit the question, thank you. – Dalco Aug 25 '20 at 15:04
  • What's wrong with `try: import cupy; use_gpu(); except Exception: use_cpu()`? That's a pretty much common way of dealing with optional dependencies. I'd also put CuPy into `extras_require`, so the user installs `yourpkg[gpu]` explicitly if he wants to install CuPy and use CUDA. – hoefling Aug 28 '20 at 18:34
  • @hoefling That sounds like a great option, I believe that is what I will do. Thank you! – Dalco Aug 29 '20 at 19:44

0 Answers0