File tree Expand file tree Collapse file tree 8 files changed +39
-5
lines changed Expand file tree Collapse file tree 8 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ set(CMAKE_CXX_STANDARD 14)
44file (STRINGS version .txt TORCHVISION_VERSION)
55
66option (WITH_CUDA "Enable CUDA support" OFF )
7+ option (USE_PYTHON "Link to Python when building" OFF )
78
89if (WITH_CUDA)
910 enable_language (CUDA)
@@ -17,7 +18,10 @@ if(WITH_CUDA)
1718 endif ()
1819endif ()
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
2226find_package (Torch REQUIRED)
2327find_package (PNG REQUIRED)
@@ -76,7 +80,12 @@ FOREACH(DIR ${ALLOW_LISTED})
7680ENDFOREACH ()
7781
7882add_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+
8089set_target_properties (${PROJECT_NAME} PROPERTIES
8190 EXPORT_NAME TorchVision
8291 INSTALL_RPATH ${TORCH_INSTALL_PREFIX} /lib)
Original file line number Diff line number Diff line change @@ -157,6 +157,10 @@ so make sure that it is also available to cmake via the ``CMAKE_PREFIX_PATH``.
157157
158158For 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+
160164TorchVision Operators
161165---------------------
162166In order to get the torchvision operators registered with torch (eg. for the JIT), all you need to do is to ensure that you
Original file line number Diff line number Diff line change @@ -28,8 +28,10 @@ include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake")
2828if (NOT TARGET torch_library)
2929find_package (Torch REQUIRED)
3030endif ()
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 ()
3335endif ()
3436
3537set_target_properties (TorchVision::TorchVision PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${${PN} _INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "torch;Python3::Python" )
Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ project(hello-world)
66# so there is no need to also add `find_package(Torch)` here.
77find_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+
913add_executable (hello-world main.cpp)
1014
1115# We now need to link the TorchVision library to our executable.
Original file line number Diff line number Diff 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" )
Original file line number Diff line number Diff line change 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
912PyMODINIT_FUNC PyInit_image (void ) {
1013 // No need to do anything.
1114 return NULL ;
1215}
1316#endif
17+ #endif // USE_PYTHON
1418
1519namespace vision {
1620namespace image {
Original file line number Diff line number Diff line change 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
1721using namespace ffmpeg ;
1822
Original file line number Diff line number Diff line change 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
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
1922PyMODINIT_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
2529namespace vision {
You can’t perform that action at this time.
0 commit comments