Caffe2 - Python API
A deep learning, cross platform ML framework
common_cuda.py
1 r"""This file is allowed to initialize CUDA context when imported."""
2 
3 import torch
4 import torch.cuda
5 from common_utils import TEST_WITH_ROCM, TEST_NUMBA
6 
7 
8 TEST_CUDA = torch.cuda.is_available()
9 TEST_MULTIGPU = TEST_CUDA and torch.cuda.device_count() >= 2
10 CUDA_DEVICE = TEST_CUDA and torch.device("cuda:0")
11 # note: if ROCm is targeted, TEST_CUDNN is code for TEST_MIOPEN
12 TEST_CUDNN = TEST_CUDA and (TEST_WITH_ROCM or torch.backends.cudnn.is_acceptable(torch.tensor(1., device=CUDA_DEVICE)))
13 TEST_CUDNN_VERSION = TEST_CUDNN and torch.backends.cudnn.version()
14 
15 if TEST_NUMBA:
16  import numba.cuda
17  TEST_NUMBA_CUDA = numba.cuda.is_available()
18 else:
19  TEST_NUMBA_CUDA = False
20 
21 # Used below in `initialize_cuda_context_rng` to ensure that CUDA context and
22 # RNG have been initialized.
23 __cuda_ctx_rng_initialized = False
24 
25 
26 # after this call, CUDA context and RNG must have been initialized on each GPU
27 def initialize_cuda_context_rng():
28  global __cuda_ctx_rng_initialized
29  assert TEST_CUDA, 'CUDA must be available when calling initialize_cuda_context_rng'
30  if not __cuda_ctx_rng_initialized:
31  # initialize cuda context and rng for memory tests
32  for i in range(torch.cuda.device_count()):
33  torch.randn(1, device="cuda:{}".format(i))
34  __cuda_ctx_rng_initialized = True
def is_available()
Definition: __init__.py:45
def device_count()
Definition: __init__.py:341
def is_acceptable(tensor)
Definition: __init__.py:95