Skip to content

Commit

Permalink
Switch to use CXX11 ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
cyyever committed May 24, 2024
1 parent 4e92159 commit 8dcaa83
Show file tree
Hide file tree
Showing 11 changed files with 5 additions and 79 deletions.
10 changes: 2 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,12 @@ endif()
string(APPEND CMAKE_CUDA_FLAGS " -DLIBCUDACXX_ENABLE_SIMPLIFIED_COMPLEX_OPERATIONS")

if(LINUX)
include(cmake/CheckAbi.cmake)
string(APPEND CMAKE_CXX_FLAGS " -D_GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI}")
string(APPEND CMAKE_CUDA_FLAGS " -D_GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI}")
if(${GLIBCXX_USE_CXX11_ABI} EQUAL 1)
set(CXX_STANDARD_REQUIRED ON)
else()
# Please note this is required in order to ensure compatibility between gcc 9 and gcc 7
# This could be removed when all Linux PyTorch binary builds are compiled by the same toolchain again
append_cxx_flag_if_supported("-fabi-version=11" CMAKE_CXX_FLAGS)
endif()
set(GLIBCXX_USE_CXX11_ABI 1 CACHE BOOL "Use C++11 ABI")
endif()

set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_LINK_WHAT_YOU_USE TRUE)

Expand Down
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,6 @@ git submodule update --init --recursive
#### Install PyTorch
**On Linux**

If you would like to compile PyTorch with [new C++ ABI](https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html) enabled, then first run this command:
```bash
export _GLIBCXX_USE_CXX11_ABI=1
```

If you're compiling for AMD ROCm then first run this command:
```bash
# Only run this if you're compiling for ROCm
Expand Down
4 changes: 0 additions & 4 deletions caffe2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1276,10 +1276,6 @@ if(BUILD_TEST)
endif()
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
include(../cmake/CheckAbi.cmake)
endif()

# CMake config for external projects.
configure_file(
${PROJECT_SOURCE_DIR}/cmake/TorchConfigVersion.cmake.in
Expand Down
27 changes: 0 additions & 27 deletions cmake/CheckAbi.cmake

This file was deleted.

5 changes: 0 additions & 5 deletions cmake/TorchConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ if(@USE_CUDA@)
list(APPEND TORCH_LIBRARIES ${TORCH_CUDA_LIBRARIES})
endif()

# When we build libtorch with the old libstdc++ ABI, dependent libraries must too.
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=@GLIBCXX_USE_CXX11_ABI@")
endif()

find_library(TORCH_LIBRARY torch PATHS "${TORCH_INSTALL_PREFIX}/lib")
# the statements below changes target properties on
# - the imported target from Caffe2Targets.cmake in shared library mode (see the find_package above)
Expand Down
1 change: 0 additions & 1 deletion tools/setup_helpers/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ def generate(
# Key: environment variable name. Value: Corresponding variable name to be passed to CMake. If you are
# adding a new build option to this block: Consider making these two names identical and adding this option
# in the block below.
"_GLIBCXX_USE_CXX11_ABI": "GLIBCXX_USE_CXX11_ABI",
"CUDNN_LIB_DIR": "CUDNN_LIBRARY",
"USE_CUDA_STATIC_LINK": "CAFFE2_STATIC_LINK_CUDA",
}
Expand Down
2 changes: 1 addition & 1 deletion torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ def _assert(condition, message):

def compiled_with_cxx11_abi() -> builtins.bool:
r"""Returns whether PyTorch was built with _GLIBCXX_USE_CXX11_ABI=1"""
return _C._GLIBCXX_USE_CXX11_ABI
return True


# Import the ops "namespace"
Expand Down
5 changes: 0 additions & 5 deletions torch/_inductor/codecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1461,10 +1461,6 @@ def get_warning_all_flag(warning_all: bool = True) -> str:
return "-Wall" if warning_all else ""


def get_glibcxx_abi_build_flags() -> str:
return "-D_GLIBCXX_USE_CXX11_ABI=" + str(int(torch._C._GLIBCXX_USE_CXX11_ABI))


def cpp_flags() -> str:
flags = ["-std=c++17", "-Wno-unused-variable", "-Wno-unknown-pragmas"]
if is_clang():
Expand Down Expand Up @@ -1812,7 +1808,6 @@ def cpp_compile_command(
f"""
{cpp_compiler()} {inp_name_str} {get_shared(shared, compile_only)}
{get_warning_all_flag(warning_all)} {cpp_flags()}
{get_glibcxx_abi_build_flags()}
{ipaths_str} {lpaths} {libs} {build_arch_flags}
{macros} {linker_paths} {clang_flags}
{optimization_flags()}
Expand Down
9 changes: 0 additions & 9 deletions torch/abi-check.cpp

This file was deleted.

7 changes: 1 addition & 6 deletions torch/csrc/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2087,12 +2087,7 @@ Call this whenever a new thread is created in order to propagate values from
ASSERT_TRUE(
set_module_attr("_has_mkldnn", at::hasMKLDNN() ? Py_True : Py_False));

#ifdef _GLIBCXX_USE_CXX11_ABI
ASSERT_TRUE(set_module_attr(
"_GLIBCXX_USE_CXX11_ABI", _GLIBCXX_USE_CXX11_ABI ? Py_True : Py_False));
#else
ASSERT_TRUE(set_module_attr("_GLIBCXX_USE_CXX11_ABI", Py_False));
#endif
ASSERT_TRUE(set_module_attr("_GLIBCXX_USE_CXX11_ABI", Py_True));

// See note [Pybind11 ABI constants]
#define SET_STR_DEFINE(name) \
Expand Down
9 changes: 1 addition & 8 deletions torch/utils/cpp_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,6 @@ def build_extensions(self) -> None:
if val is not None and not IS_WINDOWS:
self._add_compile_flag(extension, f'-DPYBIND11_{name}="{val}"')
self._define_torch_extension_name(extension)
self._add_gnu_cpp_abi_flag(extension)

if 'nvcc_dlink' in extension.extra_compile_args:
assert self.use_ninja, f"With dlink=True, ninja is required to build cuda extension {extension.name}."
Expand Down Expand Up @@ -914,8 +913,7 @@ def _define_torch_extension_name(self, extension):
self._add_compile_flag(extension, define)

def _add_gnu_cpp_abi_flag(self, extension):
# use the same CXX ABI as what PyTorch was compiled with
self._add_compile_flag(extension, '-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI)))
pass


def CppExtension(name, sources, *args, **kwargs):
Expand Down Expand Up @@ -1334,9 +1332,6 @@ def _get_pybind11_abi_build_flags():
abi_cflags.append(f'-DPYBIND11_{pname}=\\"{pval}\\"')
return abi_cflags

def _get_glibcxx_abi_build_flags():
glibcxx_abi_cflags = ['-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI))]
return glibcxx_abi_cflags

def check_compiler_is_gcc(compiler):
if not IS_LINUX:
Expand Down Expand Up @@ -1471,7 +1466,6 @@ def build_precompile_header(pch_cmd):

common_cflags += ['-std=c++17', '-fPIC']
common_cflags += [f"{x}" for x in _get_pybind11_abi_build_flags()]
common_cflags += [f"{x}" for x in _get_glibcxx_abi_build_flags()]
common_cflags_str = listToString(common_cflags)

pch_cmd = format_precompiler_header_cmd(compiler, head_file, head_file_pch, common_cflags_str, torch_include_dirs_str, extra_cflags_str, extra_include_paths_str)
Expand Down Expand Up @@ -2181,7 +2175,6 @@ def _write_ninja_file_to_build_library(path,
common_cflags += [f'-I{shlex.quote(include)}' for include in user_includes]
common_cflags += [f'-isystem {shlex.quote(include)}' for include in system_includes]

common_cflags += [f"{x}" for x in _get_glibcxx_abi_build_flags()]

if IS_WINDOWS:
cflags = common_cflags + COMMON_MSVC_FLAGS + ['/std:c++17'] + extra_cflags
Expand Down

0 comments on commit 8dcaa83

Please sign in to comment.