2017-03-29 13:44:02 +00:00
|
|
|
## @package _import_c_extension
|
|
|
|
|
# Module caffe2.python._import_c_extension
|
2016-05-13 21:43:48 +00:00
|
|
|
import atexit
|
|
|
|
|
import logging
|
|
|
|
|
import sys
|
2016-07-21 17:16:42 +00:00
|
|
|
from caffe2.python import extension_loader
|
2016-05-13 21:43:48 +00:00
|
|
|
|
|
|
|
|
# We will first try to load the gpu-enabled caffe2. If it fails, we will then
|
|
|
|
|
# attempt to load the cpu version. The cpu backend is the minimum required, so
|
|
|
|
|
# if that still fails, we will exit loud.
|
2016-07-21 17:16:42 +00:00
|
|
|
with extension_loader.DlopenGuard():
|
2018-07-19 07:11:20 +00:00
|
|
|
has_hip_support = False
|
2019-02-07 08:21:21 +00:00
|
|
|
has_cuda_support = False
|
2018-07-19 07:11:20 +00:00
|
|
|
has_gpu_support = False
|
|
|
|
|
|
2016-05-13 21:43:48 +00:00
|
|
|
try:
|
2016-09-06 22:54:56 +00:00
|
|
|
from caffe2.python.caffe2_pybind11_state_gpu import * # noqa
|
|
|
|
|
if num_cuda_devices(): # noqa
|
2019-02-07 08:21:21 +00:00
|
|
|
has_gpu_support = has_cuda_support = True
|
2018-07-19 07:11:20 +00:00
|
|
|
except ImportError as gpu_e:
|
|
|
|
|
logging.info('Failed to import cuda module: {}'.format(gpu_e))
|
2016-07-21 17:16:42 +00:00
|
|
|
try:
|
2018-12-12 07:20:31 +00:00
|
|
|
from caffe2.python.caffe2_pybind11_state_hip import * # noqa
|
2020-03-06 17:47:41 +00:00
|
|
|
# we stop checking whether we have AMD GPU devices on the host,
|
|
|
|
|
# because we may be constructing a net on a machine without GPU,
|
|
|
|
|
# and run the net on another one with GPU
|
|
|
|
|
has_gpu_support = has_hip_support = True
|
|
|
|
|
logging.info('This caffe2 python run has AMD GPU support!')
|
2018-07-19 07:11:20 +00:00
|
|
|
except ImportError as hip_e:
|
|
|
|
|
logging.info('Failed to import AMD hip module: {}'.format(hip_e))
|
2018-06-15 02:56:56 +00:00
|
|
|
|
|
|
|
|
logging.warning(
|
2020-02-04 22:14:02 +00:00
|
|
|
'This caffe2 python run failed to load cuda module:{},'
|
|
|
|
|
'and AMD hip module:{}.'
|
|
|
|
|
'Will run in CPU only mode.'.format(gpu_e, hip_e))
|
2018-06-15 02:56:56 +00:00
|
|
|
try:
|
|
|
|
|
from caffe2.python.caffe2_pybind11_state import * # noqa
|
2018-07-19 07:11:20 +00:00
|
|
|
except ImportError as cpu_e:
|
2018-06-15 02:56:56 +00:00
|
|
|
logging.critical(
|
2018-07-19 07:11:20 +00:00
|
|
|
'Cannot load caffe2.python. Error: {0}'.format(str(cpu_e)))
|
2018-06-15 02:56:56 +00:00
|
|
|
sys.exit(1)
|
2016-05-13 21:43:48 +00:00
|
|
|
|
|
|
|
|
# libcaffe2_python contains a global Workspace that we need to properly delete
|
|
|
|
|
# when exiting. Otherwise, cudart will cause segfaults sometimes.
|
2016-09-06 22:54:56 +00:00
|
|
|
atexit.register(on_module_exit) # noqa
|
2016-11-14 22:58:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Add functionalities for the TensorCPU interface.
|
|
|
|
|
def _TensorCPU_shape(self):
|
|
|
|
|
return tuple(self._shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _TensorCPU_reshape(self, shape):
|
|
|
|
|
return self._reshape(list(shape))
|
|
|
|
|
|
|
|
|
|
TensorCPU.shape = property(_TensorCPU_shape) # noqa
|
|
|
|
|
TensorCPU.reshape = _TensorCPU_reshape # noqa
|