Caffe2 - Python API
A deep learning, cross platform ML framework
_import_c_extension.py
1 ## @package _import_c_extension
2 # Module caffe2.python._import_c_extension
3 import atexit
4 import logging
5 import sys
6 from caffe2.python import extension_loader
7 
8 # We will first try to load the gpu-enabled caffe2. If it fails, we will then
9 # attempt to load the cpu version. The cpu backend is the minimum required, so
10 # if that still fails, we will exit loud.
11 with extension_loader.DlopenGuard():
12  try:
13  from caffe2.python.caffe2_pybind11_state_gpu import * # noqa
14  if num_cuda_devices(): # noqa
15  has_gpu_support = True
16  else:
17  has_gpu_support = False
18  except ImportError as e:
19  logging.warning(
20  'This caffe2 python run does not have GPU support. '
21  'Will run in CPU only mode.')
22  logging.warning('Debug message: {0}'.format(str(e)))
23  has_gpu_support = False
24  try:
25  from caffe2.python.caffe2_pybind11_state import * # noqa
26  except ImportError as e:
27  logging.critical(
28  'Cannot load caffe2.python. Error: {0}'.format(str(e)))
29  sys.exit(1)
30 
31 # libcaffe2_python contains a global Workspace that we need to properly delete
32 # when exiting. Otherwise, cudart will cause segfaults sometimes.
33 atexit.register(on_module_exit) # noqa
34 
35 
36 # Add functionalities for the TensorCPU interface.
37 def _TensorCPU_shape(self):
38  return tuple(self._shape)
39 
40 
41 def _TensorCPU_reshape(self, shape):
42  return self._reshape(list(shape))
43 
44 TensorCPU.shape = property(_TensorCPU_shape) # noqa
45 TensorCPU.reshape = _TensorCPU_reshape # noqa