Skip to content

Commit 5020ceb

Browse files
d4l3kfacebook-github-bot
authored andcommitted
multipy: automatically select ABI based off of torch (#226)
Summary: This automatically selects the ABI version based off the torch variable `torch._C._GLIBCXX_USE_CXX11_ABI`. This keeps the override settings by setting `-D_GLIBCXX_USE_CXX11_ABI=<0/1>` but defaults to loading from python. The setup.py override now doesn't do anything since it was only used by PyTorch core CI Pull Request resolved: #226 Test Plan: CI ``` pip install -e . ``` Reviewed By: PaliC Differential Revision: D40654548 Pulled By: d4l3k fbshipit-source-id: 5f4e75174721a7fb0d48c45e36812437a8fdeb86
1 parent 8b5b7ca commit 5020ceb

File tree

7 files changed

+32
-38
lines changed

7 files changed

+32
-38
lines changed

.github/workflows/install_test.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ jobs:
102102
- name: Run pip install with conda for 3.8+
103103
run: |
104104
set -eux
105-
if [[ ${{ matrix.python-minor-version }} -gt 7 ]]; then \
106-
pip install -e .
105+
if [[ ${{ matrix.python-minor-version }} -gt 7 ]]; then
106+
source /opt/conda/bin/activate
107+
pip install -e .
107108
fi

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,13 @@ pip install -e . --install-option="--cmakeoff"
143143

144144
cd multipy/runtime
145145

146-
# build runtime
147-
mkdir build
148-
cd build
149-
# use cmake -DABI_EQUALS_1=ON .. instead if you want ABI=1
150-
cmake ..
151-
cmake --build . --config Release
146+
# configure runtime to build/
147+
cmake -S . -B build
148+
# if you need to override the ABI setting you can pass
149+
cmake -S . -B build -D_GLIBCXX_USE_CXX11_ABI=<0/1>
150+
151+
# compile the files in build/
152+
cmake --build build --config Release -j
152153
```
153154

154155
### Running unit tests for `multipy::runtime`
@@ -271,9 +272,6 @@ set(MULTIPY_PATH ".." CACHE PATH "The repo where multipy is built or the PYTHONP
271272
# include the multipy utils to help link against
272273
include(${MULTIPY_PATH}/multipy/runtime/utils.cmake)
273274
274-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
275-
set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")
276-
277275
# add headers from multipy
278276
include_directories(${MULTIPY_PATH})
279277

examples/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ set(MULTIPY_PATH ".." CACHE PATH "The repo where multipy is built or the PYTHONP
66
# include the multipy utils to help link against
77
include(${MULTIPY_PATH}/multipy/runtime/utils.cmake)
88

9-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
10-
set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")
11-
129
# add headers from multipy
1310
include_directories(${MULTIPY_PATH})
1411

multipy/runtime/CMakeLists.txt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,26 @@ project(MultipyRuntime)
99

1010
# set ABI by default to 0
1111

12-
option(ABI_EQUALS_1 "Set ABI value to 1, by default it is set to 0. Pytorch by default builds with ABI set to 1." OFF)
1312
option(BUILD_CUDA_TESTS "Set to ON in order to build cuda tests. By default we do not" OFF)
1413
option(GDB_ON "Sets to debug mode (for gdb), defaults to OFF" OFF)
1514

1615
OPTION(LEGACY_PYTHON_PRE_3_8 "Whether to use Python 3.7 codepaths." OFF)
1716

18-
if(ABI_EQUALS_1)
19-
set(ABI_VALUE 1)
20-
else()
21-
set(ABI_VALUE 0)
22-
endif()
23-
2417
if(GDB_ON)
2518
set(CMAKE_BUILD_TYPE Debug)
2619
endif()
2720

2821
message(STATUS "CMAKE_BUILD_TYPE - ${CMAKE_BUILD_TYPE}" )
2922

30-
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=${ABI_VALUE})
31-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${ABI_VALUE} -fabi-version=11 -fno-lto")
32-
33-
SET(INTERPRETER_DIR "${DEPLOY_DIR}/interpreter" )
34-
SET(INTERPRETER_DIR "${DEPLOY_DIR}/interpreter" PARENT_SCOPE)
35-
3623
set(DEPLOY_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
3724
get_filename_component(MULTIPY_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
3825

26+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fabi-version=11 -fno-lto")
27+
include(${DEPLOY_DIR}/utils.cmake)
28+
3929
add_subdirectory(interpreter)
4030
add_subdirectory(third-party/fmt)
4131

42-
include(${DEPLOY_DIR}/utils.cmake)
43-
4432

4533
# we do not want to have torch_deployinterpreter linked against libstdc++ or libc because
4634
# when loading it with RTLD_DEEPBIND it will resolve std::cout/stdout to the copy in libc++/libc instead of the

multipy/runtime/interpreter/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
99
SET(INTERPRETER_DIR "${DEPLOY_DIR}/interpreter" )
1010
SET(INTERPRETER_DIR "${DEPLOY_DIR}/interpreter" PARENT_SCOPE)
1111

12-
include(${DEPLOY_DIR}/utils.cmake)
13-
1412
SET(MULTIPY_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../../utils")
1513

1614
OPTION(LEGACY_PYTHON_PRE_3_8 "Whether to use Python 3.7 codepaths." OFF)

multipy/runtime/utils.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@
77
# prefer the active Python version instead of the latest -- only works on cmake
88
# 3.15+
99
# https://cmake.org/cmake/help/latest/module/FindPython3.html#hints
10+
if (NOT DEFINED _GLIBCXX_USE_CXX11_ABI)
11+
# infer the ABI setting from the installed version of PyTorch
12+
execute_process(
13+
COMMAND python -c "import torch; print(1 if torch._C._GLIBCXX_USE_CXX11_ABI else 0)"
14+
OUTPUT_VARIABLE _GLIBCXX_USE_CXX11_ABI
15+
OUTPUT_STRIP_TRAILING_WHITESPACE
16+
RESULT_VARIABLE ret
17+
)
18+
if(ret EQUAL "1")
19+
message(FATAL_ERROR "Failed to detect ABI version")
20+
endif()
21+
endif()
22+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${_GLIBCXX_USE_CXX11_ABI}")
23+
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=${_GLIBCXX_USE_CXX11_ABI})
24+
message(STATUS "_GLIBCXX_USE_CXX11_ABI - ${_GLIBCXX_USE_CXX11_ABI}")
25+
1026
set(Python3_FIND_STRATEGY LOCATION)
1127

1228
find_package (Python3 COMPONENTS Interpreter Development)

setup.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,19 @@ class MultipyRuntimeDevelop(MultipyRuntimeCmake, develop):
3737
def initialize_options(self):
3838
develop.initialize_options(self)
3939
self.cmakeoff = None
40+
41+
# TODO(tristanr): remove once unused
4042
self.abicxx = None
4143

4244
def finalize_options(self):
4345
develop.finalize_options(self)
4446
if self.cmakeoff is not None:
4547
self.distribution.get_command_obj("build_ext").cmake_off = True
46-
if self.abicxx is not None:
47-
self.distribution.get_command_obj("build_ext").abicxx = True
4848

4949

5050
class MultipyRuntimeBuild(MultipyRuntimeCmake, build_ext):
5151
user_options = build_ext.user_options + MultipyRuntimeCmake.user_options
5252
cmake_off = False
53-
abicxx = False
5453

5554
def run(self):
5655
if self.cmake_off:
@@ -71,14 +70,11 @@ def run(self):
7170
if not os.path.exists(build_dir_abs):
7271
os.makedirs(build_dir_abs)
7372
legacy_python_cmake_flag = "OFF" if sys.version_info.minor > 7 else "ON"
74-
cxx_abi_flag = "ON" if self.abicxx else "OFF"
7573

7674
print(f"-- Running multipy runtime makefile in dir {build_dir_abs}")
7775
try:
7876
subprocess.run(
79-
[
80-
f"cmake -DLEGACY_PYTHON_PRE_3_8={legacy_python_cmake_flag} -DABI_EQUALS_1={cxx_abi_flag} .."
81-
],
77+
[f"cmake -DLEGACY_PYTHON_PRE_3_8={legacy_python_cmake_flag} .."],
8278
cwd=build_dir_abs,
8379
shell=True,
8480
check=True,

0 commit comments

Comments
 (0)