Skip to content

Commit 5e56575

Browse files
Conditionally include Python in the C++ builds. (#5190)
* Conditionally include Python in the C++ builds. Added an option `USE_PYTHON` that allows users to decide to link to Python3 or not. * Also conditionally include this defintion. * Define `USE_PYTHON` for the Windows builds. * Remove Python3 reference in CMake test as it's no longer required. * Accidentally removed the closing > from the decl. * Also add `USE_PYTHON` when building the separate image library on Windows. * Also conditionally include this depening on USE_PYTHON. * Go back to require Python for this example. * Find Python in the hello world example. * Add a paragraph documenting the `USE_PYTHON` option. Co-authored-by: Prabhat Roy <[email protected]>
1 parent f923aeb commit 5e56575

File tree

8 files changed

+39
-5
lines changed

8 files changed

+39
-5
lines changed

CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(CMAKE_CXX_STANDARD 14)
44
file(STRINGS version.txt TORCHVISION_VERSION)
55

66
option(WITH_CUDA "Enable CUDA support" OFF)
7+
option(USE_PYTHON "Link to Python when building" OFF)
78

89
if(WITH_CUDA)
910
enable_language(CUDA)
@@ -17,7 +18,10 @@ if(WITH_CUDA)
1718
endif()
1819
endif()
1920

20-
find_package(Python3 COMPONENTS Development)
21+
if (USE_PYTHON)
22+
add_definitions(-DUSE_PYTHON)
23+
find_package(Python3 REQUIRED COMPONENTS Development)
24+
endif()
2125

2226
find_package(Torch REQUIRED)
2327
find_package(PNG REQUIRED)
@@ -76,7 +80,12 @@ FOREACH(DIR ${ALLOW_LISTED})
7680
ENDFOREACH()
7781

7882
add_library(${PROJECT_NAME} SHARED ${ALL_SOURCES})
79-
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} ${PNG_LIBRARY} ${JPEG_LIBRARIES} Python3::Python)
83+
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} ${PNG_LIBRARY} ${JPEG_LIBRARIES})
84+
85+
if (USE_PYTHON)
86+
target_link_libraries(${PROJECT_NAME} PRIVATE Python3::Python)
87+
endif()
88+
8089
set_target_properties(${PROJECT_NAME} PROPERTIES
8190
EXPORT_NAME TorchVision
8291
INSTALL_RPATH ${TORCH_INSTALL_PREFIX}/lib)

README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ so make sure that it is also available to cmake via the ``CMAKE_PREFIX_PATH``.
157157

158158
For an example setup, take a look at ``examples/cpp/hello_world``.
159159

160+
Python linking is disabled by default when compiling TorchVision with CMake, this allows you to run models without any Python
161+
dependency. In some special cases where TorchVision's operators are used from Python code, you may need to link to Python. This
162+
can be done by passing ``-DUSE_PYTHON=on`` to CMake.
163+
160164
TorchVision Operators
161165
---------------------
162166
In order to get the torchvision operators registered with torch (eg. for the JIT), all you need to do is to ensure that you

cmake/TorchVisionConfig.cmake.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake")
2828
if(NOT TARGET torch_library)
2929
find_package(Torch REQUIRED)
3030
endif()
31-
if(NOT TARGET Python3::Python)
32-
find_package(Python3 COMPONENTS Development)
31+
if (@USE_PYTHON@)
32+
if(NOT TARGET Python3::Python)
33+
find_package(Python3 COMPONENTS Development)
34+
endif()
3335
endif()
3436

3537
set_target_properties(TorchVision::TorchVision PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${${PN}_INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "torch;Python3::Python" )

examples/cpp/hello_world/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ project(hello-world)
66
# so there is no need to also add `find_package(Torch)` here.
77
find_package(TorchVision REQUIRED)
88

9+
# This due to LibTorch's version is the one included in the Python
10+
# package that links to Python.
11+
find_package(Python3 COMPONENTS Development)
12+
913
add_executable(hello-world main.cpp)
1014

1115
# We now need to link the TorchVision library to our executable.

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def get_extensions():
201201

202202
if sys.platform == "win32":
203203
define_macros += [("torchvision_EXPORTS", None)]
204-
204+
define_macros += [("USE_PYTHON", None)]
205205
extra_compile_args["cxx"].append("/MP")
206206

207207
debug_mode = os.getenv("DEBUG", "0") == "1"
@@ -254,6 +254,9 @@ def get_extensions():
254254
image_library = []
255255
image_link_flags = []
256256

257+
if sys.platform == "win32":
258+
image_macros += [("USE_PYTHON", None)]
259+
257260
# Locating libPNG
258261
libpng = distutils.spawn.find_executable("libpng-config")
259262
pngfix = distutils.spawn.find_executable("pngfix")

torchvision/csrc/io/image/image.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
#include "image.h"
22

33
#include <ATen/core/op_registration/op_registration.h>
4+
#ifdef USE_PYTHON
45
#include <Python.h>
6+
#endif
57

68
// If we are in a Windows environment, we need to define
79
// initialization functions for the _custom_ops extension
10+
#ifdef USE_PYTHON
811
#ifdef _WIN32
912
PyMODINIT_FUNC PyInit_image(void) {
1013
// No need to do anything.
1114
return NULL;
1215
}
1316
#endif
17+
#endif // USE_PYTHON
1418

1519
namespace vision {
1620
namespace image {

torchvision/csrc/io/video_reader/video_reader.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include "video_reader.h"
22

3+
#ifdef USE_PYTHON
34
#include <Python.h>
5+
#endif
46

57
#include "../decoder/memory_buffer.h"
68
#include "../decoder/sync_decoder.h"
79

10+
#ifdef USE_PYTHON
811
// If we are in a Windows environment, we need to define
912
// initialization functions for the _custom_ops extension
1013
#ifdef _WIN32
@@ -13,6 +16,7 @@ PyMODINIT_FUNC PyInit_video_reader(void) {
1316
return NULL;
1417
}
1518
#endif
19+
#endif // USE_PYTHONs
1620

1721
using namespace ffmpeg;
1822

torchvision/csrc/vision.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "vision.h"
22

33
#ifndef MOBILE
4+
#ifdef USE_PYTHON
45
#include <Python.h>
56
#endif
7+
#endif
68
#include <torch/library.h>
79

810
#ifdef WITH_CUDA
@@ -16,10 +18,12 @@
1618
// initialization functions for the _custom_ops extension.
1719
// For PyMODINIT_FUNC to work, we need to include Python.h
1820
#if !defined(MOBILE) && defined(_WIN32)
21+
#ifdef USE_PYTHON
1922
PyMODINIT_FUNC PyInit__C(void) {
2023
// No need to do anything.
2124
return NULL;
2225
}
26+
#endif // USE_PYTHON
2327
#endif // !defined(MOBILE) && defined(_WIN32)
2428

2529
namespace vision {

0 commit comments

Comments
 (0)