question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detect GPU and `import cupy as np`

See original GitHub issue

I could not find an example how to detect if GPU is present and swap numpy for cupy during import. Is that possible?

from cupy import gpu
if gpu:
  import cupy as np
else:
  import numpy as np

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kmaehashicommented, Aug 26, 2021

I am afraid that doesn’t answer the question, and it is still the catch everything antipattern.

I agree that catch-all is an antipattern but currently except Exception is the safest way. Please see https://github.com/cupy/cupy/issues/2148 for the context.

If there a plan to move non-specific functions into cupy.gpu namespace?

We are using HIP which is a CUDA-compatible layer for AMD/ROCm, so we currently assume everything to be common between two.

1reaction
kmaehashicommented, Aug 23, 2021

cupy.cuda.is_available() returns True if 1 or more devices are available.

import numpy as np
try:
    import cupy
    if cupy.cuda.is_available():
        np = cupy
except:
    pass
Read more comments on GitHub >

github_iconTop Results From Across the Web

Basics of CuPy — CuPy 11.4.0 documentation
CuPy is a GPU array backend that implements a subset of NumPy interface. In the following code, ... import numpy as np >>>...
Read more >
How to run python on GPU with CuPy? - Stack Overflow
However, when I run nvidia-smi , no GPU processes are found. nvidia-smi output. Here's the code: import numpy as np import cupy as...
Read more >
Using your GPU with CuPy - The Carpentries Incubator
CuPy is a GPU array library that implements a subset of the NumPy and SciPy ... import numpy as np # Construct an...
Read more >
Here's How to Use CuPy to Make Numpy Over 10X Faster
CuPy is a library that implements Numpy arrays on Nvidia GPUs by leveraging the CUDA GPU library. With that implementation, superior ...
Read more >
How to use the cupy.cuda.Device function in cupy - Snyk
import cupy as cp import numpy as np import torch import torch.nn.functional as F from mxtorch.vision.bbox_tools import loc2bbox from torch import nn from ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found