Skip to content

Commit b142a22

Browse files
Thiago Crepaldipytorchmergebot
Thiago Crepaldi
authored andcommitted
Add warning when importing caffe2 on build without BUILD_CAFFE2=1
Confusing backtraces are issues to user when they try to run tests or actual scripts using Caffe2 on a pytorch build without Caffe2 enabled through BUILD_CAFFE2=1 This PR adds a warning in more than one place to return a friendly message for the user Pull Request resolved: pytorch#73770 Approved by: https://github.com/BowenBao, https://github.com/malfet, https://github.com/garymm
1 parent 950dc1b commit b142a22

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

caffe2/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import warnings
2+
3+
4+
try:
5+
from caffe2.proto import caffe2_pb2
6+
except ImportError:
7+
warnings.warn("Caffe2 was not built with this PyTorch build. "
8+
"Please enable Caffe2 by building PyTorch from source with `BUILD_CAFFE2=1` flag.")

caffe2/proto/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
# expected caffe2.NetDef got caffe2.NetDef."
99
#
1010
# This has to be done for all python targets, so listing them here
11-
from caffe2.proto import caffe2_pb2, metanet_pb2, torch_pb2
11+
try:
12+
from caffe2.proto import caffe2_pb2, metanet_pb2, torch_pb2
13+
except ImportError as exc:
14+
raise ImportError('Caffe2 support is not enabled in this PyTorch build. '
15+
'Please enable Caffe2 by building PyTorch from source with `BUILD_CAFFE2=1` flag.'
16+
'\nDetails from original exception:\n' + str(exc))
17+
1218
try:
1319
from caffe2.caffe2.fb.session.proto import session_pb2
1420
except ImportError:

caffe2/python/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
2-
from caffe2.proto import caffe2_pb2
31
import os
42
import sys
3+
4+
5+
try:
6+
from caffe2.proto import caffe2_pb2
7+
except ImportError as exc:
8+
raise ImportError('Caffe2 support is not enabled in this PyTorch build. '
9+
'Please enable Caffe2 by building PyTorch from source with `BUILD_CAFFE2=1` flag.'
10+
'\nDetails from original exception:\n' + str(exc))
11+
12+
513
# TODO: refactor & remove the following alias
614
caffe2_pb2.CPU = caffe2_pb2.PROTO_CPU
715
caffe2_pb2.CUDA = caffe2_pb2.PROTO_CUDA

0 commit comments

Comments
 (0)