Skip to content

Commit 5562304

Browse files
Combine changes for py3.7 +unittests. (#165)
Summary: Pull Request resolved: #165 Reviewed By: PaliC Differential Revision: D39388436 Pulled By: anirbanr-fb-r2p fbshipit-source-id: 05650cf1f60d93ba1eb0e9d8e547d94cbe687f51
1 parent 09f5f7a commit 5562304

File tree

8 files changed

+87
-31
lines changed

8 files changed

+87
-31
lines changed

.github/workflows/runtime_nightly.yaml

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ jobs:
88
unittest:
99
strategy:
1010
matrix:
11-
python-version: [3.8, 3.9, '3.10']
11+
python-major-version: [3]
12+
python-minor-version: [7,8,9,10]
1213
platform: [ubuntu-18.04]
1314
fail-fast: false
1415
runs-on: ${{ matrix.platform }}
@@ -26,14 +27,15 @@ jobs:
2627
- name: Build
2728
env:
2829
DOCKER_BUILDKIT: 1
29-
run: docker build -t multipy --progress=plain --build-arg PYTHON_VERSION=${{ matrix.python-version }} .
30+
run: docker build -t multipy --progress=plain --build-arg PYTHON_MAJOR_VERSION=${{ matrix.python-major-version }} --build-arg PYTHON_MINOR_VERSION=${{ matrix.python-minor-version }} .
3031

3132
- name: Test
3233
run: |
33-
docker run --rm multipy multipy/runtime/build/test_deploy
34+
docker run --rm multipy bash -c "if [[ ${{ matrix.python-minor-version }} -lt 8 ]]; then source ~/venvs/multipy/bin/activate; fi && multipy/runtime/build/test_deploy"
35+
3436
- name: Compat Tests
3537
run: |
36-
docker run --rm multipy bash -c "pip install -r compat-requirements.txt && multipy/runtime/build/interactive_embedded_interpreter --pyscript multipy/runtime/test_compat.py"
38+
docker run --rm multipy bash -c "if [[ ${{ matrix.python-minor-version }} -gt 7 ]]; then pip install -r compat-requirements.txt && multipy/runtime/build/interactive_embedded_interpreter --pyscript multipy/runtime/test_compat.py; fi"
3739
3840
- name: Create Tarball
3941
run: |

.github/workflows/runtime_tests.yaml

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ jobs:
1010
unittest:
1111
strategy:
1212
matrix:
13-
python-version: [3.8, 3.9, '3.10']
13+
python-major-version: [3]
14+
python-minor-version: [7,8,9,10]
1415
platform: [ubuntu-18.04]
1516
fail-fast: false
1617
runs-on: ${{ matrix.platform }}
@@ -28,12 +29,12 @@ jobs:
2829
- name: Build
2930
env:
3031
DOCKER_BUILDKIT: 1
31-
run: docker build -t multipy --progress=plain --build-arg PYTHON_VERSION=${{ matrix.python-version }} .
32+
run: docker build -t multipy --progress=plain --build-arg PYTHON_MAJOR_VERSION=${{ matrix.python-major-version }} --build-arg PYTHON_MINOR_VERSION=${{ matrix.python-minor-version }} .
3233

3334
- name: Test
3435
run: |
35-
docker run --rm multipy multipy/runtime/build/test_deploy
36+
docker run --rm multipy bash -c "if [[ ${{ matrix.python-minor-version }} -lt 8 ]]; then source ~/venvs/multipy/bin/activate; fi && multipy/runtime/build/test_deploy"
3637
3738
- name: Compat Tests
3839
run: |
39-
docker run --rm multipy bash -c "pip install -r compat-requirements.txt && multipy/runtime/build/interactive_embedded_interpreter --pyscript multipy/runtime/test_compat.py"
40+
docker run --rm multipy bash -c "if [[ ${{ matrix.python-minor-version }} -gt 7 ]]; then pip install -r compat-requirements.txt && multipy/runtime/build/interactive_embedded_interpreter --pyscript multipy/runtime/test_compat.py; fi"

Dockerfile

+34-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ ARG BASE_IMAGE=nvidia/cuda:11.3.1-devel-ubuntu18.04
22

33
FROM ${BASE_IMAGE} as dev-base
44

5+
SHELL ["/bin/bash", "-c"]
6+
57
# Install system dependencies
68
RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
79
apt update && DEBIAN_FRONTEND=noninteractive apt install -yq --no-install-recommends \
@@ -39,7 +41,9 @@ RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
3941
apt-transport-https \
4042
ca-certificates \
4143
gnupg \
42-
software-properties-common && \
44+
software-properties-common \
45+
python-pip \
46+
python3-pip && \
4347
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | apt-key add - && \
4448
apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main' && \
4549
echo "deb http://security.ubuntu.com/ubuntu focal-security main" >> /etc/apt/sources.list && \
@@ -56,43 +60,51 @@ WORKDIR /opt/multipy
5660
COPY . .
5761
RUN git submodule update --init --recursive --jobs 0
5862

59-
# install pyenv
60-
FROM dev-base as pyenv-install
61-
RUN git clone https://github.com/pyenv/pyenv.git ~/.pyenv && \
62-
export PYENV_ROOT="~/.pyenv" && \
63-
export PATH="$PYENV_ROOT/bin:$PATH"
64-
# dummy cmd to verify installation.
65-
RUN pyenv install --list
66-
67-
# Install conda + neccessary python dependencies
68-
FROM dev-base as conda
69-
ARG PYTHON_VERSION=3.8
70-
RUN curl -fsSL -v -o ~/miniconda.sh -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
63+
# Install conda/pyenv + necessary python dependencies
64+
FROM dev-base as conda-pyenv
65+
ARG PYTHON_MAJOR_VERSION=3
66+
ARG PYTHON_MINOR_VERSION=8
67+
ENV PYTHON_MINOR_VERSION=${PYTHON_MINOR_VERSION}
68+
ENV PYTHON_VERSION=${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}
69+
RUN if [[ ${PYTHON_MINOR_VERSION} -gt 7 ]]; then \
70+
curl -fsSL -v -o ~/miniconda.sh -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
7171
chmod +x ~/miniconda.sh && \
7272
~/miniconda.sh -b -p /opt/conda && \
7373
rm ~/miniconda.sh && \
7474
/opt/conda/bin/conda install -y python=${PYTHON_VERSION} mkl mkl-include conda-build pyyaml numpy ipython && \
7575
/opt/conda/bin/conda install -y -c conda-forge libpython-static=${PYTHON_VERSION} && \
7676
/opt/conda/bin/conda install -y pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch-nightly && \
77-
/opt/conda/bin/conda clean -ya
78-
77+
/opt/conda/bin/conda clean -ya; \
78+
else \
79+
pip3 install virtualenv && \
80+
git clone https://github.com/pyenv/pyenv.git ~/.pyenv && \
81+
export CFLAGS="-fPIC -g" && \
82+
~/.pyenv/bin/pyenv install --force 3.7.10 && \
83+
virtualenv -p ~/.pyenv/versions/3.7.10/bin/python3 ~/venvs/multipy; \
84+
fi
7985

8086
# Build/Install pytorch with post-cxx11 ABI
81-
FROM conda as build
87+
FROM conda-pyenv as build
8288
WORKDIR /opt/multipy/multipy/runtime/third-party/pytorch
83-
COPY --from=conda /opt/conda /opt/conda
89+
COPY --from=conda-pyenv /opt/conda* /opt/conda
8490
COPY --from=submodule-update /opt/multipy /opt/multipy
8591

8692
WORKDIR /opt/multipy
8793

8894
# Build Multipy
8995
RUN mkdir multipy/runtime/build && \
90-
cd multipy/runtime/build && \
91-
cmake .. && \
92-
cmake --build . --config Release && \
93-
cmake --install . --prefix "."
96+
cd multipy/runtime/build && \
97+
if [[ ${PYTHON_MINOR_VERSION} -lt 8 ]]; then \
98+
source ~/venvs/multipy/bin/activate && \
99+
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cu113 && \
100+
cmake -DLEGACY_PYTHON_PRE_3_8=ON ..; \
101+
else \
102+
cmake -DLEGACY_PYTHON_PRE_3_8=OFF ..; \
103+
fi && \
104+
cmake --build . --config Release && \
105+
cmake --install . --prefix "." && \
106+
cd ../example && python generate_examples.py
94107

95-
RUN cd multipy/runtime/example && python generate_examples.py
96108
ENV PYTHONPATH=. LIBTEST_DEPLOY_LIB=multipy/runtime/build/libtest_deploy_lib.so
97109

98110
RUN mkdir /opt/dist && cp -r multipy/runtime/build/dist/* /opt/dist/

multipy/runtime/CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ project(MultipyRuntime)
1212
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)
1313
option(BUILD_CUDA_TESTS "Set to ON in order to build cuda tests. By default we do not" OFF)
1414

15+
OPTION(LEGACY_PYTHON_PRE_3_8 "Whether to use Python 3.7 codepaths." OFF)
16+
1517
if(ABI_EQUALS_1)
1618
set(ABI_VALUE 1)
1719
else()
@@ -105,7 +107,11 @@ set(INTERPRETER_TEST_SOURCES_GPU
105107

106108
LINK_DIRECTORIES("${PYTORCH_ROOT}/torch/lib")
107109
add_executable(test_deploy ${INTERPRETER_TEST_SOURCES})
108-
target_compile_definitions(test_deploy PUBLIC TEST_CUSTOM_LIBRARY)
110+
# target_compile_definitions(test_deploy PUBLIC TEST_CUSTOM_LIBRARY)
111+
if (${LEGACY_PYTHON_PRE_3_8})
112+
message(STATUS "LEGACY_PYTHON_PRE_3_8 set in parent cmakefile.")
113+
target_compile_definitions(test_deploy PUBLIC LEGACY_PYTHON_PRE_3_8)
114+
endif()
109115
target_include_directories(test_deploy PRIVATE ${PYTORCH_ROOT}/torch)
110116
target_link_libraries(test_deploy
111117
PUBLIC "-Wl,--no-as-needed -rdynamic" gtest dl torch_deploy_interface c10 torch_cpu

multipy/runtime/interpreter/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +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+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
78

89
SET(INTERPRETER_DIR "${DEPLOY_DIR}/interpreter" )
910
SET(INTERPRETER_DIR "${DEPLOY_DIR}/interpreter" PARENT_SCOPE)
@@ -13,6 +14,8 @@ include_directories(BEFORE "${PYTORCH_ROOT}/torch/include")
1314
include_directories(BEFORE "${PYTORCH_ROOT}/torch/include/torch/csrc/api/include/")
1415
SET(MULTIPY_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../../utils")
1516

17+
OPTION(LEGACY_PYTHON_PRE_3_8 "Whether to use Python 3.7 codepaths." OFF)
18+
1619
find_package (Python3 COMPONENTS Interpreter Development)
1720

1821
message(STATUS "Python3_EXECUTABLE - ${Python3_EXECUTABLE}" )
@@ -32,6 +35,11 @@ link_directories(BEFORE "${Python3_SITELIB}/torch/lib")
3235
include_directories(BEFORE "${Python3_INCLUDE_DIRS}")
3336
include_directories(BEFORE ${CMAKE_SOURCE_DIR}/../..)
3437

38+
if (${LEGACY_PYTHON_PRE_3_8})
39+
message(STATUS "LEGACY_PYTHON_PRE_3_8 set")
40+
add_compile_definitions(LEGACY_PYTHON_PRE_3_8)
41+
endif()
42+
3543
# add gtest dependency
3644
include(FetchContent)
3745
FetchContent_Declare(
@@ -80,3 +88,5 @@ target_link_libraries(torch_deployinterpreter PRIVATE fmt::fmt-header-only)
8088
target_link_libraries(torch_deployinterpreter PRIVATE torch_python)
8189
target_link_libraries(torch_deployinterpreter PRIVATE gtest)
8290
target_link_libraries(torch_deployinterpreter PRIVATE multipy_torch)
91+
92+
unset(LEGACY_PYTHON_PRE_3_8)

multipy/runtime/interpreter/builtin_registry.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void BuiltinRegistry::runPreInitialization() {
6464
appendCPythonInittab();
6565
}
6666

67+
#ifndef LEGACY_PYTHON_PRE_3_8
6768
const char* metaPathSetupTemplate = R"PYTHON(
6869
import sys
6970
from importlib.metadata import DistributionFinder, Distribution
@@ -107,6 +108,20 @@ class DummyDistribution(Distribution):
107108
108109
sys.meta_path.insert(0, F())
109110
)PYTHON";
111+
#else
112+
const char* metaPathSetupTemplate = R"PYTHON(
113+
import sys
114+
class F:
115+
MODULES = set({<<<DEPLOY_BUILTIN_MODULES_CSV>>>})
116+
def find_spec(self, fullname, path, target=None):
117+
if fullname in self.MODULES:
118+
# Load this module using `BuiltinImporter`, but set `path` to None
119+
# in order to trick it into loading our module.
120+
return sys.meta_path[1].find_spec(fullname, path=None, target=None)
121+
return None
122+
sys.meta_path.insert(0, F())
123+
)PYTHON";
124+
#endif
110125

111126
void BuiltinRegistry::runPostInitialization() {
112127
TORCH_INTERNAL_ASSERT(Py_IsInitialized());

multipy/runtime/interpreter/interpreter_impl.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ ConcreteInterpreterImplConstructorCommon(
247247
const std::vector<std::string>& extra_python_paths,
248248
const std::vector<std::string>& plugin_paths) {
249249
BuiltinRegistry::runPreInitialization();
250+
251+
#ifndef LEGACY_PYTHON_PRE_3_8
250252
PyPreConfig preconfig;
251253
PyPreConfig_InitIsolatedConfig(&preconfig);
252254
PyStatus status = Py_PreInitialize(&preconfig);
@@ -283,6 +285,12 @@ ConcreteInterpreterImplConstructorCommon(
283285
status = Py_InitializeFromConfig(&config);
284286
PyConfig_Clear(&config);
285287
TORCH_INTERNAL_ASSERT(!PyStatus_Exception(status))
288+
289+
#else
290+
Py_InitializeEx(1);
291+
TORCH_INTERNAL_ASSERT(Py_IsInitialized);
292+
#endif
293+
286294
#ifdef FBCODE_CAFFE2
287295
auto sys_path = global_impl("sys", "path");
288296
for (const auto& entry : extra_python_paths) {

multipy/runtime/test_deploy.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ result = torch.Tensor([1,2,3])
459459
EXPECT_TRUE(w_grad0.equal(w_grad1));
460460
}
461461

462+
#ifndef LEGACY_PYTHON_PRE_3_8
462463
TEST(TorchpyTest, ImportlibMetadata) {
463464
torch::deploy::InterpreterManager m(1);
464465
m.registerModuleSource("importlib_test", R"PYTHON(
@@ -470,6 +471,7 @@ result = version("torch")
470471
auto ver = I.global("importlib_test", "result").toIValue().toString();
471472
ASSERT_EQ(ver->string(), "0.0.1+fake_multipy");
472473
}
474+
#endif
473475

474476
// OSS build does not have bultin numpy support yet. Use this flag to guard the
475477
// test case.

0 commit comments

Comments
 (0)