File tree 8 files changed +39
-5
lines changed
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)
4
4
file (STRINGS version .txt TORCHVISION_VERSION)
5
5
6
6
option (WITH_CUDA "Enable CUDA support" OFF )
7
+ option (USE_PYTHON "Link to Python when building" OFF )
7
8
8
9
if (WITH_CUDA)
9
10
enable_language (CUDA)
@@ -17,7 +18,10 @@ if(WITH_CUDA)
17
18
endif ()
18
19
endif ()
19
20
20
- find_package (Python3 COMPONENTS Development)
21
+ if (USE_PYTHON)
22
+ add_definitions (-DUSE_PYTHON)
23
+ find_package (Python3 REQUIRED COMPONENTS Development)
24
+ endif ()
21
25
22
26
find_package (Torch REQUIRED)
23
27
find_package (PNG REQUIRED)
@@ -76,7 +80,12 @@ FOREACH(DIR ${ALLOW_LISTED})
76
80
ENDFOREACH ()
77
81
78
82
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
+
80
89
set_target_properties (${PROJECT_NAME} PROPERTIES
81
90
EXPORT_NAME TorchVision
82
91
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``.
157
157
158
158
For an example setup, take a look at ``examples/cpp/hello_world ``.
159
159
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
+
160
164
TorchVision Operators
161
165
---------------------
162
166
In 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")
28
28
if (NOT TARGET torch_library)
29
29
find_package (Torch REQUIRED)
30
30
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 ()
33
35
endif ()
34
36
35
37
set_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)
6
6
# so there is no need to also add `find_package(Torch)` here.
7
7
find_package (TorchVision REQUIRED)
8
8
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
+
9
13
add_executable (hello-world main.cpp)
10
14
11
15
# 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():
201
201
202
202
if sys .platform == "win32" :
203
203
define_macros += [("torchvision_EXPORTS" , None )]
204
-
204
+ define_macros += [( "USE_PYTHON" , None )]
205
205
extra_compile_args ["cxx" ].append ("/MP" )
206
206
207
207
debug_mode = os .getenv ("DEBUG" , "0" ) == "1"
@@ -254,6 +254,9 @@ def get_extensions():
254
254
image_library = []
255
255
image_link_flags = []
256
256
257
+ if sys .platform == "win32" :
258
+ image_macros += [("USE_PYTHON" , None )]
259
+
257
260
# Locating libPNG
258
261
libpng = distutils .spawn .find_executable ("libpng-config" )
259
262
pngfix = distutils .spawn .find_executable ("pngfix" )
Original file line number Diff line number Diff line change 1
1
#include " image.h"
2
2
3
3
#include < ATen/core/op_registration/op_registration.h>
4
+ #ifdef USE_PYTHON
4
5
#include < Python.h>
6
+ #endif
5
7
6
8
// If we are in a Windows environment, we need to define
7
9
// initialization functions for the _custom_ops extension
10
+ #ifdef USE_PYTHON
8
11
#ifdef _WIN32
9
12
PyMODINIT_FUNC PyInit_image (void ) {
10
13
// No need to do anything.
11
14
return NULL ;
12
15
}
13
16
#endif
17
+ #endif // USE_PYTHON
14
18
15
19
namespace vision {
16
20
namespace image {
Original file line number Diff line number Diff line change 1
1
#include " video_reader.h"
2
2
3
+ #ifdef USE_PYTHON
3
4
#include < Python.h>
5
+ #endif
4
6
5
7
#include " ../decoder/memory_buffer.h"
6
8
#include " ../decoder/sync_decoder.h"
7
9
10
+ #ifdef USE_PYTHON
8
11
// If we are in a Windows environment, we need to define
9
12
// initialization functions for the _custom_ops extension
10
13
#ifdef _WIN32
@@ -13,6 +16,7 @@ PyMODINIT_FUNC PyInit_video_reader(void) {
13
16
return NULL ;
14
17
}
15
18
#endif
19
+ #endif // USE_PYTHONs
16
20
17
21
using namespace ffmpeg ;
18
22
Original file line number Diff line number Diff line change 1
1
#include " vision.h"
2
2
3
3
#ifndef MOBILE
4
+ #ifdef USE_PYTHON
4
5
#include < Python.h>
5
6
#endif
7
+ #endif
6
8
#include < torch/library.h>
7
9
8
10
#ifdef WITH_CUDA
16
18
// initialization functions for the _custom_ops extension.
17
19
// For PyMODINIT_FUNC to work, we need to include Python.h
18
20
#if !defined(MOBILE) && defined(_WIN32)
21
+ #ifdef USE_PYTHON
19
22
PyMODINIT_FUNC PyInit__C (void ) {
20
23
// No need to do anything.
21
24
return NULL ;
22
25
}
26
+ #endif // USE_PYTHON
23
27
#endif // !defined(MOBILE) && defined(_WIN32)
24
28
25
29
namespace vision {
You can’t perform that action at this time.
0 commit comments