Skip to content

Commit 8c7e487

Browse files
d4l3kfacebook-github-bot
authored andcommitted
multipy/libpython: strip debug symbols by default (#216)
Summary: Currently ubuntu18.04/centos7 aren't compatible with libpython installed from Conda since they're using pre 2.32 binutils which is incompatible with binaries built with later versions of binutils due to a debug info compression issue. This also supports older cmake versions + supports PyTorch egglink + some requirements tweaks to get tests passing. This strips the debug info by default so you can build deploy on all systems. https://wiki.gentoo.org/wiki/Project:Toolchain/Binutils_2.32_upgrade_notes/elfutils_0.175:_unable_to_initialize_decompress_status_for_section_.debug_info Pull Request resolved: #216 Test Plan: GitHub CI Reviewed By: anirbanr-fb-r2p, PaliC Differential Revision: D40489102 Pulled By: d4l3k fbshipit-source-id: e087c4decc4c831220f680f47ceaa66ea90c6151
1 parent 8cea05b commit 8c7e487

File tree

7 files changed

+26
-32
lines changed

7 files changed

+26
-32
lines changed

Dockerfile

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
1111
ca-certificates \
1212
ccache \
1313
curl \
14+
cmake-mozilla \
1415
wget \
1516
git \
1617
libjpeg-dev \
@@ -44,11 +45,6 @@ RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
4445
software-properties-common \
4546
python-pip \
4647
python3-pip && \
47-
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor -o /usr/share/keyrings/magic-key.gpg && \
48-
echo "deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/magic-key.gpg] https://apt.kitware.com/ubuntu/ bionic main" | tee -a /etc/apt/sources.list && \
49-
echo "deb http://security.ubuntu.com/ubuntu focal-security main" | tee -a /etc/apt/sources.list && \
50-
apt update && \
51-
apt install -y binutils cmake && \
5248
rm -rf /var/lib/apt/lists/*
5349
RUN /usr/sbin/update-ccache-symlinks
5450
RUN mkdir /opt/ccache && ccache --set-config=cache_dir=/opt/ccache
@@ -106,7 +102,7 @@ RUN rm -r multipy/runtime/build; mkdir multipy/runtime/build && \
106102
source ~/venvs/multipy/bin/activate && \
107103
cmake -DLEGACY_PYTHON_PRE_3_8=ON ..; \
108104
else \
109-
cmake -DLEGACY_PYTHON_PRE_3_8=OFF ..; \
105+
cmake -DLEGACY_PYTHON_PRE_3_8=OFF -DPython3_EXECUTABLE="$(which python3)" ..; \
110106
fi && \
111107
cmake --build . --config Release -j && \
112108
cmake --install . --prefix "." && \
@@ -121,7 +117,7 @@ RUN cd examples && \
121117
else \
122118
source /opt/conda/bin/activate; \
123119
fi && \
124-
cmake -S . -B build/ -DMULTIPY_PATH=".." && \
120+
cmake -S . -B build/ -DMULTIPY_PATH=".." -DPython3_EXECUTABLE="$(which python3)" && \
125121
cmake --build build/ --config Release -j
126122

127123
ENV PYTHONPATH=. LIBTEST_DEPLOY_LIB=multipy/runtime/build/libtest_deploy_lib.so

README.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ sudo apt update
5151
xargs sudo apt install -y -qq --no-install-recommends <build-requirements.txt
5252
```
5353

54-
We recommend using the latest version of `cmake` and compilers available for your system. On Ubuntu 18.04, for example, these can be updated as follows:
55-
56-
```shell
57-
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | sudo gpg --dearmor -o /usr/share/keyrings/magic-key.gpg
58-
echo "deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/magic-key.gpg] https://apt.kitware.com/ubuntu/ bionic main" | sudo tee -a /etc/apt/sources.list
59-
echo "deb http://security.ubuntu.com/ubuntu focal-security main" | sudo tee -a /etc/apt/sources.list
60-
sudo apt update
61-
sudo apt install -y binutils cmake
62-
```
63-
6454
#### Installing environment encapsulators
6555

6656
We support both `conda` and `pyenv`+`virtualenv` to create isolated environments to build and run in. Since `multipy` requires a position-independent version of python to launch interpreters with, for `conda` environments we use the prebuilt `libpython-static=3.x` libraries from `conda-forge` to link with at build time, and for `virtualenv`/`pyenv` we compile python with `-fPIC` to create the linkable library.
@@ -273,7 +263,7 @@ Assuming the above C++ program was stored in a file called, `example-app.cpp`, a
273263
minimal `CMakeLists.txt` file would look like:
274264
275265
```cmake
276-
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)
266+
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
277267
project(multipy_tutorial)
278268
279269
set(MULTIPY_PATH ".." CACHE PATH "The repo where multipy is built or the PYTHONPATH")

compat-requirements.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
tokenizers
2-
torchaudio
3-
torchvision
42
git+https://github.com/facebookresearch/pytorch3d.git
5-
git+https://github.com/pytorch/torchdynamo.git

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)
1+
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
22
project(multipy_tutorial)
33

44
set(MULTIPY_PATH ".." CACHE PATH "The repo where multipy is built or the PYTHONPATH")

multipy/runtime/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
cmake_minimum_required(VERSION 3.19)
7+
cmake_minimum_required(VERSION 3.12)
88
project(MultipyRuntime)
99

1010
# set ABI by default to 0

multipy/runtime/interpreter/CMakeLists.txt

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

12-
set(PYTORCH_ROOT "${DEPLOY_DIR}/third-party/pytorch")
13-
include_directories(BEFORE "${PYTORCH_ROOT}/torch/include")
14-
include_directories(BEFORE "${PYTORCH_ROOT}/torch/include/torch/csrc/api/include/")
12+
include(${DEPLOY_DIR}/utils.cmake)
13+
1514
SET(MULTIPY_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../../utils")
1615

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

19-
find_package (Python3 COMPONENTS Interpreter Development)
20-
2118
message(STATUS "Python3_EXECUTABLE - ${Python3_EXECUTABLE}" )
2219
message(STATUS "Python3_SITELIB - ${Python3_SITELIB}" )
2320
message(STATUS "Python3_LIBRARIES - ${Python3_LIBRARIES}" )
@@ -30,9 +27,6 @@ if (NOT EXISTS ${Python3_STATIC_LIBRARIES})
3027
endif()
3128
message(STATUS "Python3_STATIC_LIBRARIES - ${Python3_STATIC_LIBRARIES}" )
3229

33-
include_directories(BEFORE "${Python3_SITELIB}/torch/include")
34-
link_directories(BEFORE "${Python3_SITELIB}/torch/lib")
35-
include_directories(BEFORE "${Python3_INCLUDE_DIRS}")
3630
include_directories(BEFORE ${CMAKE_SOURCE_DIR}/../..)
3731

3832
if (${LEGACY_PYTHON_PRE_3_8})
@@ -49,13 +43,22 @@ FetchContent_Declare(
4943
FetchContent_MakeAvailable(googletest)
5044
include(GoogleTest)
5145

46+
IF(CMAKE_BUILD_TYPE MATCHES Debug)
47+
set(OBJCOPY_FLAGS "")
48+
else()
49+
# binutils prior to 2.32 have issues linking against newer libraries due to
50+
# debug info changes. By default we can strip these out to ensure it
51+
# compiles.
52+
set(OBJCOPY_FLAGS "--strip-debug")
53+
endif()
54+
5255
# instantiate a library based on the objects that make up torch_python
5356
# make sure system python isn't used here
5457
add_custom_command(
5558
OUTPUT libpython_multipy.a
5659
COMMAND cp ${Python3_STATIC_LIBRARIES} libpython_multipy.a
5760
COMMAND chmod +w libpython_multipy.a
58-
COMMAND "${CMAKE_OBJCOPY}" --weaken-symbol=_PyImport_FindSharedFuncptr libpython_multipy.a
61+
COMMAND "${CMAKE_OBJCOPY}" ${OBJCOPY_FLAGS} --weaken-symbol=_PyImport_FindSharedFuncptr libpython_multipy.a
5962
)
6063
add_custom_target(libpython_multipy DEPENDS libpython_multipy.a)
6164

multipy/runtime/utils.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
find_package (Python3 COMPONENTS Interpreter Development)
88
set(PYTORCH_ROOT "${Python3_SITELIB}")
99

10+
# if pytorch was installed in develop mode we need to resolve the egg-link
11+
set(PYTORCH_EGG_LINK "${PYTORCH_ROOT}/torch.egg-link")
12+
if (EXISTS "${PYTORCH_EGG_LINK}")
13+
file (STRINGS "${PYTORCH_EGG_LINK}" PYTORCH_ROOT LIMIT_COUNT 1)
14+
endif()
15+
16+
message(STATUS "PYTORCH_ROOT - ${PYTORCH_ROOT}" )
17+
1018
include_directories(BEFORE "${PYTORCH_ROOT}/torch/include")
1119
include_directories(BEFORE "${PYTORCH_ROOT}/torch/include/torch/csrc/api/include/")
1220
include_directories(BEFORE "${Python3_INCLUDE_DIRS}")

0 commit comments

Comments
 (0)