Unverified Commit 5e56575e authored by Daniel Falbel's avatar Daniel Falbel Committed by GitHub
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: default avatarPrabhat Roy <prabhatroy@fb.com>
parent f923aeb2
...@@ -4,6 +4,7 @@ set(CMAKE_CXX_STANDARD 14) ...@@ -4,6 +4,7 @@ set(CMAKE_CXX_STANDARD 14)
file(STRINGS version.txt TORCHVISION_VERSION) file(STRINGS version.txt TORCHVISION_VERSION)
option(WITH_CUDA "Enable CUDA support" OFF) option(WITH_CUDA "Enable CUDA support" OFF)
option(USE_PYTHON "Link to Python when building" OFF)
if(WITH_CUDA) if(WITH_CUDA)
enable_language(CUDA) enable_language(CUDA)
...@@ -17,7 +18,10 @@ if(WITH_CUDA) ...@@ -17,7 +18,10 @@ if(WITH_CUDA)
endif() endif()
endif() endif()
find_package(Python3 COMPONENTS Development) if (USE_PYTHON)
add_definitions(-DUSE_PYTHON)
find_package(Python3 REQUIRED COMPONENTS Development)
endif()
find_package(Torch REQUIRED) find_package(Torch REQUIRED)
find_package(PNG REQUIRED) find_package(PNG REQUIRED)
...@@ -76,7 +80,12 @@ FOREACH(DIR ${ALLOW_LISTED}) ...@@ -76,7 +80,12 @@ FOREACH(DIR ${ALLOW_LISTED})
ENDFOREACH() ENDFOREACH()
add_library(${PROJECT_NAME} SHARED ${ALL_SOURCES}) add_library(${PROJECT_NAME} SHARED ${ALL_SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} ${PNG_LIBRARY} ${JPEG_LIBRARIES} Python3::Python) target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} ${PNG_LIBRARY} ${JPEG_LIBRARIES})
if (USE_PYTHON)
target_link_libraries(${PROJECT_NAME} PRIVATE Python3::Python)
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES set_target_properties(${PROJECT_NAME} PROPERTIES
EXPORT_NAME TorchVision EXPORT_NAME TorchVision
INSTALL_RPATH ${TORCH_INSTALL_PREFIX}/lib) INSTALL_RPATH ${TORCH_INSTALL_PREFIX}/lib)
......
...@@ -157,6 +157,10 @@ so make sure that it is also available to cmake via the ``CMAKE_PREFIX_PATH``. ...@@ -157,6 +157,10 @@ so make sure that it is also available to cmake via the ``CMAKE_PREFIX_PATH``.
For an example setup, take a look at ``examples/cpp/hello_world``. For an example setup, take a look at ``examples/cpp/hello_world``.
Python linking is disabled by default when compiling TorchVision with CMake, this allows you to run models without any Python
dependency. In some special cases where TorchVision's operators are used from Python code, you may need to link to Python. This
can be done by passing ``-DUSE_PYTHON=on`` to CMake.
TorchVision Operators TorchVision Operators
--------------------- ---------------------
In order to get the torchvision operators registered with torch (eg. for the JIT), all you need to do is to ensure that you In order to get the torchvision operators registered with torch (eg. for the JIT), all you need to do is to ensure that you
......
...@@ -28,8 +28,10 @@ include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake") ...@@ -28,8 +28,10 @@ include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake")
if(NOT TARGET torch_library) if(NOT TARGET torch_library)
find_package(Torch REQUIRED) find_package(Torch REQUIRED)
endif() endif()
if(NOT TARGET Python3::Python) if (@USE_PYTHON@)
find_package(Python3 COMPONENTS Development) if(NOT TARGET Python3::Python)
find_package(Python3 COMPONENTS Development)
endif()
endif() endif()
set_target_properties(TorchVision::TorchVision PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${${PN}_INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "torch;Python3::Python" ) set_target_properties(TorchVision::TorchVision PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${${PN}_INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "torch;Python3::Python" )
......
...@@ -6,6 +6,10 @@ project(hello-world) ...@@ -6,6 +6,10 @@ project(hello-world)
# so there is no need to also add `find_package(Torch)` here. # so there is no need to also add `find_package(Torch)` here.
find_package(TorchVision REQUIRED) find_package(TorchVision REQUIRED)
# This due to LibTorch's version is the one included in the Python
# package that links to Python.
find_package(Python3 COMPONENTS Development)
add_executable(hello-world main.cpp) add_executable(hello-world main.cpp)
# We now need to link the TorchVision library to our executable. # We now need to link the TorchVision library to our executable.
......
...@@ -201,7 +201,7 @@ def get_extensions(): ...@@ -201,7 +201,7 @@ def get_extensions():
if sys.platform == "win32": if sys.platform == "win32":
define_macros += [("torchvision_EXPORTS", None)] define_macros += [("torchvision_EXPORTS", None)]
define_macros += [("USE_PYTHON", None)]
extra_compile_args["cxx"].append("/MP") extra_compile_args["cxx"].append("/MP")
debug_mode = os.getenv("DEBUG", "0") == "1" debug_mode = os.getenv("DEBUG", "0") == "1"
...@@ -254,6 +254,9 @@ def get_extensions(): ...@@ -254,6 +254,9 @@ def get_extensions():
image_library = [] image_library = []
image_link_flags = [] image_link_flags = []
if sys.platform == "win32":
image_macros += [("USE_PYTHON", None)]
# Locating libPNG # Locating libPNG
libpng = distutils.spawn.find_executable("libpng-config") libpng = distutils.spawn.find_executable("libpng-config")
pngfix = distutils.spawn.find_executable("pngfix") pngfix = distutils.spawn.find_executable("pngfix")
......
#include "image.h" #include "image.h"
#include <ATen/core/op_registration/op_registration.h> #include <ATen/core/op_registration/op_registration.h>
#ifdef USE_PYTHON
#include <Python.h> #include <Python.h>
#endif
// If we are in a Windows environment, we need to define // If we are in a Windows environment, we need to define
// initialization functions for the _custom_ops extension // initialization functions for the _custom_ops extension
#ifdef USE_PYTHON
#ifdef _WIN32 #ifdef _WIN32
PyMODINIT_FUNC PyInit_image(void) { PyMODINIT_FUNC PyInit_image(void) {
// No need to do anything. // No need to do anything.
return NULL; return NULL;
} }
#endif #endif
#endif // USE_PYTHON
namespace vision { namespace vision {
namespace image { namespace image {
......
#include "video_reader.h" #include "video_reader.h"
#ifdef USE_PYTHON
#include <Python.h> #include <Python.h>
#endif
#include "../decoder/memory_buffer.h" #include "../decoder/memory_buffer.h"
#include "../decoder/sync_decoder.h" #include "../decoder/sync_decoder.h"
#ifdef USE_PYTHON
// If we are in a Windows environment, we need to define // If we are in a Windows environment, we need to define
// initialization functions for the _custom_ops extension // initialization functions for the _custom_ops extension
#ifdef _WIN32 #ifdef _WIN32
...@@ -13,6 +16,7 @@ PyMODINIT_FUNC PyInit_video_reader(void) { ...@@ -13,6 +16,7 @@ PyMODINIT_FUNC PyInit_video_reader(void) {
return NULL; return NULL;
} }
#endif #endif
#endif // USE_PYTHONs
using namespace ffmpeg; using namespace ffmpeg;
......
#include "vision.h" #include "vision.h"
#ifndef MOBILE #ifndef MOBILE
#ifdef USE_PYTHON
#include <Python.h> #include <Python.h>
#endif #endif
#endif
#include <torch/library.h> #include <torch/library.h>
#ifdef WITH_CUDA #ifdef WITH_CUDA
...@@ -16,10 +18,12 @@ ...@@ -16,10 +18,12 @@
// initialization functions for the _custom_ops extension. // initialization functions for the _custom_ops extension.
// For PyMODINIT_FUNC to work, we need to include Python.h // For PyMODINIT_FUNC to work, we need to include Python.h
#if !defined(MOBILE) && defined(_WIN32) #if !defined(MOBILE) && defined(_WIN32)
#ifdef USE_PYTHON
PyMODINIT_FUNC PyInit__C(void) { PyMODINIT_FUNC PyInit__C(void) {
// No need to do anything. // No need to do anything.
return NULL; return NULL;
} }
#endif // USE_PYTHON
#endif // !defined(MOBILE) && defined(_WIN32) #endif // !defined(MOBILE) && defined(_WIN32)
namespace vision { namespace vision {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment