Skip to content

Commit

Permalink
Do not build PyTorch with caffe2 by default (pytorch#66658)
Browse files Browse the repository at this point in the history
Summary:
CAFFE2 has been deprecated for a while, but still included in every PyTorch build.
We should stop building it by default, although CI should still validate that caffe2 code is buildable.

Build even fewer dependencies when compiling mobile builds without Caffe2
Introduce `TEST_CAFFE2` in torch.common.utils
Skip `TestQuantizedEmbeddingOps` and `TestJit.test_old_models_bc`  is code is compiled without Caffe2
Should be landed after pytorch/builder#864

Pull Request resolved: pytorch#66658

Reviewed By: driazati, seemethere, janeyx99

Differential Revision: D31669156

Pulled By: malfet

fbshipit-source-id: 1cc45e2d402daf913a4685eb9f841cc3863e458d
  • Loading branch information
malfet authored and facebook-github-bot committed Oct 22, 2021
1 parent 4fe8055 commit 77becca
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 43 deletions.
8 changes: 4 additions & 4 deletions .github/generated-ciflow-ruleset.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .github/scripts/generate_ci_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ def generate_workflow_file(self, workflow_template: jinja2.Template) -> None:
labels={LABEL_CIFLOW_LINUX, LABEL_CIFLOW_CPU},
),
),
# Build PyTorch with BUILD_CAFFE2=OFF
# Build PyTorch with BUILD_CAFFE2=ON
CIWorkflow(
arch="linux",
build_environment="puretorch-linux-xenial-py3.6-gcc5.4",
build_environment="caffe2-linux-xenial-py3.6-gcc5.4",
docker_image_base=f"{DOCKER_REGISTRY}/pytorch/pytorch-linux-xenial-py3.6-gcc5.4",
test_runner_type=LINUX_CPU_TEST_RUNNER,
exclude_test=True,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .jenkins/pytorch/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ if [[ "$BUILD_ENVIRONMENT" == *cuda11* ]]; then
export BUILD_SPLIT_CUDA=ON
fi

if [[ ${BUILD_ENVIRONMENT} == *"pure_torch"* || ${BUILD_ENVIRONMENT} == *"puretorch"* ]]; then
export BUILD_CAFFE2=OFF
if [[ ${BUILD_ENVIRONMENT} == *"caffe2"* || ${BUILD_ENVIRONMENT} == *"onnx"* ]]; then
export BUILD_CAFFE2=ON
fi

if [[ ${BUILD_ENVIRONMENT} == *"paralleltbb"* ]]; then
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ option(BUILD_BINARY "Build C++ binaries" OFF)
option(BUILD_DOCS "Build Caffe2 documentation" OFF)
option(BUILD_CUSTOM_PROTOBUF "Build and use Caffe2's own protobuf under third_party" ON)
option(BUILD_PYTHON "Build Python binaries" ON)
option(BUILD_CAFFE2 "Master flag to build Caffe2" ON)
option(BUILD_CAFFE2 "Master flag to build Caffe2" OFF)
option(BUILD_LITE_INTERPRETER "Master flag to build Lite Interpreter" OFF)
cmake_dependent_option(
BUILD_CAFFE2_OPS "Build Caffe2 operators" ON
Expand Down
5 changes: 5 additions & 0 deletions aten/src/ATen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ if(NOT BUILD_LITE_INTERPRETER)
file(GLOB_RECURSE ATen_CORE_TEST_SRCS "core/*_test.cpp")
endif()
EXCLUDE(ATen_CORE_SRCS "${ATen_CORE_SRCS}" ${ATen_CORE_TEST_SRCS})
# Exclude TensorImpl_test.cpp if compiling without Caffe2
if(NOT BUILD_CAFFE2 AND NOT BUILD_LITE_INTERPRETER)
file(GLOB_RECURSE ATen_CORE_EXCLUDED_TEST_SRCS "core/TensorImpl_test.cpp")
EXCLUDE(ATen_CORE_TEST_SRCS "${ATen_CORE_TEST_SRCS}" ${ATen_CORE_EXCLUDED_TEST_SRCS})
endif()

file(GLOB base_h "*.h" "detail/*.h" "cpu/*.h" "cpu/vec/vec512/*.h" "cpu/vec/vec256/*.h" "cpu/vec/*.h" "quantized/*.h")
file(GLOB base_cpp "*.cpp" "detail/*.cpp" "cpu/*.cpp")
Expand Down
11 changes: 7 additions & 4 deletions aten/tools/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ VALGRIND=${VALGRIND:=ON}
./dlconvertor_test
./native_test
./scalar_tensor_test
./tensor_interop_test
if [[ -x ./tensor_interop_test ]]; then
./tensor_interop_test
fi
./undefined_tensor_test
./extension_backend_test
./lazy_tensor_test
Expand Down Expand Up @@ -62,10 +64,11 @@ fi
if [[ -x ./cuda_cub_test ]]; then
./cuda_cub_test
fi
if [ "$VALGRIND" == "ON" ]
then
if [ "$VALGRIND" == "ON" ]; then
valgrind --suppressions="$VALGRIND_SUP" --error-exitcode=1 ./basic --gtest_filter='-*CUDA'
valgrind --suppressions="$VALGRIND_SUP" --error-exitcode=1 ./tensor_interop_test
if [[ -x ./tensor_interop_test ]]; then
valgrind --suppressions="$VALGRIND_SUP" --error-exitcode=1 ./tensor_interop_test
fi
fi

popd
4 changes: 2 additions & 2 deletions caffe2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ if(BUILD_CAFFE2 AND (NOT INTERN_BUILD_MOBILE OR BUILD_CAFFE2_MOBILE))
# add_subdirectory(test) # todo: use caffe2_gtest_main instead of gtest_main because we will need to call GlobalInit
add_subdirectory(transforms)
endif()
if(NOT BUILD_CAFFE2)
if(NOT BUILD_CAFFE2 AND (NOT INTERN_BUILD_MOBILE OR BUILD_CAFFE2_MOBILE))
add_subdirectory(proto)
endif()

Expand Down Expand Up @@ -1852,7 +1852,7 @@ if(BUILD_TEST)
endif()

# For special tests that explicitly uses dependencies, we add them here
if(USE_MPI)
if(BUILD_CAFFE2 AND USE_MPI)
target_link_libraries(mpi_test ${MPI_CXX_LIBRARIES})
if(USE_CUDA)
target_link_libraries(mpi_gpu_test ${MPI_CXX_LIBRARIES})
Expand Down
2 changes: 1 addition & 1 deletion caffe2/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if((NOT BUILD_CAFFE2) OR (INTERN_BUILD_MOBILE AND NOT BUILD_CAFFE2_MOBILE))
)
endif()

if(NOT BUILD_CAFFE2)
if(NOT BUILD_CAFFE2 AND NOT INTERN_BUILD_MOBILE)
list(APPEND Caffe2_CPU_SRCS
utils/proto_wrap.cc
)
Expand Down
27 changes: 14 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,23 +824,24 @@ def make_relative_rpath_args(path):

# These extensions are built by cmake and copied manually in build_extensions()
# inside the build_ext implementation
extensions.append(
Extension(
name=str('caffe2.python.caffe2_pybind11_state'),
sources=[]),
)
if cmake_cache_vars['USE_CUDA']:
if cmake_cache_vars['BUILD_CAFFE2']:
extensions.append(
Extension(
name=str('caffe2.python.caffe2_pybind11_state_gpu'),
sources=[]),
)
if cmake_cache_vars['USE_ROCM']:
extensions.append(
Extension(
name=str('caffe2.python.caffe2_pybind11_state_hip'),
name=str('caffe2.python.caffe2_pybind11_state'),
sources=[]),
)
if cmake_cache_vars['USE_CUDA']:
extensions.append(
Extension(
name=str('caffe2.python.caffe2_pybind11_state_gpu'),
sources=[]),
)
if cmake_cache_vars['USE_ROCM']:
extensions.append(
Extension(
name=str('caffe2.python.caffe2_pybind11_state_hip'),
sources=[]),
)

cmdclass = {
'bdist_wheel': wheel_concatenate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
("aten::_baddbmm_mkl_", datetime.date(2021, 10, 31)),
("aten::grid_sampler_2d_backward", datetime.date(2021, 10, 21)),
("prim::TensorExprDynamicGuard", datetime.date(2021, 11, 20)),
("caffe2::", datetime.date(2021, 10, 23)),
]

ALLOW_LIST_COMPILED = [
Expand Down
6 changes: 3 additions & 3 deletions test/quantization/core/test_quantized_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import copy
import itertools
import numpy as np
import sys
import unittest
import operator
import random
Expand All @@ -23,7 +22,7 @@
hu.assert_deadline_disabled()

from torch.testing._internal.common_utils import TestCase
from torch.testing._internal.common_utils import IS_PPC, TEST_WITH_UBSAN, IS_MACOS
from torch.testing._internal.common_utils import IS_PPC, TEST_WITH_UBSAN, IS_MACOS, BUILD_WITH_CAFFE2
from torch.testing._internal.common_quantization import skipIfNoFBGEMM, skipIfNoQNNPACK
from torch.testing._internal.common_quantized import _quantize, _dequantize, _calculate_dynamic_qparams, \
override_quantized_engine, supported_qengines, override_qengines, _snr
Expand Down Expand Up @@ -3229,7 +3228,8 @@ def test_qlinear_unpack(self, W, use_channelwise):
W_q.q_zero_point(), W_q_origin.q_zero_point())


@unittest.skipIf(sys.platform == "darwin", "Known test failure on Mac.")
@unittest.skipIf(IS_MACOS, "Known test failure on Mac.")
@unittest.skipIf(not BUILD_WITH_CAFFE2, "Test needs Caffe2")
class TestQuantizedEmbeddingOps(TestCase):
def _test_embedding_bag_unpack_fn(self, pack_fn, unpack_fn, num_embeddings, embedding_dim, bit_rate, optimized_qparams,
num_batches, data_type=np.float32):
Expand Down
3 changes: 2 additions & 1 deletion test/test_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
from torch.testing._internal import jit_utils
from torch.testing._internal.common_jit import check_against_reference
from torch.testing._internal.common_utils import run_tests, IS_WINDOWS, TEST_WITH_UBSAN, \
suppress_warnings, IS_SANDCASTLE, GRAPH_EXECUTOR, ProfilingMode, TestCase, \
suppress_warnings, BUILD_WITH_CAFFE2, IS_SANDCASTLE, GRAPH_EXECUTOR, ProfilingMode, TestCase, \
freeze_rng_state, slowTest, TemporaryFileName, skipIfCompiledWithoutNumpy, \
enable_profiling_mode_for_profiling_tests, TEST_MKL, set_default_dtype, num_profiled_runs
from torch.testing._internal.jit_utils import JitTestCase, enable_cpu_fuser, disable_autodiff_subgraph_inlining, \
Expand Down Expand Up @@ -15322,6 +15322,7 @@ def is_tensor_value(item):
self.assertEqual(value, getattr(loaded, "_" + name))

@unittest.skipIf(IS_WINDOWS or IS_SANDCASTLE, "NYI: TemporaryFileName support for Windows or Sandcastle")
@unittest.skipIf(not BUILD_WITH_CAFFE2, "PyTorch is build without Caffe2 support")
def test_old_models_bc(self):
model = {
'archive/version': b'1',
Expand Down
4 changes: 3 additions & 1 deletion test/test_tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

TEST_CAFFE2 = True
try:
import caffe2.python.caffe2_pybind11_state as _caffe2_pybind11_state # noqa: F401
from caffe2.python import brew, cnn, core, workspace
from caffe2.python.model_helper import ModelHelper
except ImportError:
Expand Down Expand Up @@ -71,10 +72,11 @@ def tearDown(self):
from torch.utils.tensorboard import summary, SummaryWriter
from torch.utils.tensorboard._utils import _prepare_video, convert_to_HWC
from torch.utils.tensorboard._convert_np import make_np
from torch.utils.tensorboard import _caffe2_graph as c2_graph
from torch.utils.tensorboard._pytorch_graph import graph
from google.protobuf import text_format
from PIL import Image
if TEST_TENSORBOARD and TEST_CAFFE2:
from torch.utils.tensorboard import _caffe2_graph as c2_graph

class TestTensorBoardPyTorchNumpy(BaseTestCase):
def test_pytorch_np(self):
Expand Down
3 changes: 2 additions & 1 deletion test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
skipCUDAMemoryLeakCheckIf, BytesIOContext, noarchTest,
skipIfRocm, skipIfNoSciPy, TemporaryFileName, TemporaryDirectoryName,
wrapDeterministicFlagAPITest, DeterministicGuard, CudaSyncGuard,
bytes_to_scalar)
skipIfNotRegistered, bytes_to_scalar)
from multiprocessing.reduction import ForkingPickler
from torch.testing._internal.common_device_type import (
instantiate_device_type_tests,
Expand Down Expand Up @@ -2473,6 +2473,7 @@ def do_test(t):
do_test(torch.tensor([[1, 2]]).data)
do_test(torch.tensor([[1, 2]]).detach())

@skipIfNotRegistered("LayerNorm", "Skipping as LayerNorm is not registered")
def test_c10_layer_norm(self):
# test that we can call c10 ops and they return a reasonable result
X = torch.rand(5, 5, dtype=torch.float)
Expand Down
15 changes: 11 additions & 4 deletions torch/testing/_internal/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,16 +668,19 @@ def TemporaryDirectoryName(suffix=None):

IS_FILESYSTEM_UTF8_ENCODING = sys.getfilesystemencoding() == 'utf-8'

def _check_module_exists(name):
def _check_module_exists(name: str) -> bool:
r"""Returns if a top-level module with :attr:`name` exists *without**
importing it. This is generally safer than try-catch block around a
`import X`. It avoids third party libraries breaking assumptions of some of
our tests, e.g., setting multiprocessing start method when imported
(see librosa/#747, torchvision/#544).
"""
import importlib.util
spec = importlib.util.find_spec(name)
return spec is not None
try:
import importlib.util
spec = importlib.util.find_spec(name)
return spec is not None
except ImportError:
return False

TEST_NUMPY = _check_module_exists('numpy')
TEST_SCIPY = _check_module_exists('scipy')
Expand All @@ -688,6 +691,8 @@ def _check_module_exists(name):

TEST_LIBROSA = _check_module_exists('librosa')

BUILD_WITH_CAFFE2 = _check_module_exists("caffe2.python.caffe2_pybind11_state")

# Python 2.7 doesn't have spawn
NO_MULTIPROCESSING_SPAWN = os.environ.get('NO_MULTIPROCESSING_SPAWN', '0') == '1'
TEST_WITH_ASAN = os.getenv('PYTORCH_TEST_WITH_ASAN', '0') == '1'
Expand Down Expand Up @@ -941,6 +946,8 @@ def skipIfNotRegistered(op_name, message):
@skipIfNotRegistered('MyOp', 'MyOp is not linked!')
This will check if 'MyOp' is in the caffe2.python.core
"""
if not BUILD_WITH_CAFFE2:
return unittest.skip("Pytorch is compiled without Caffe2")
try:
from caffe2.python import core
skipper = unittest.skipIf(op_name not in core._REGISTERED_OPERATORS,
Expand Down

0 comments on commit 77becca

Please sign in to comment.