Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally include Python in the C++ builds. #5190

Merged
merged 14 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(CMAKE_CXX_STANDARD 14)
file(STRINGS version.txt TORCHVISION_VERSION)

option(WITH_CUDA "Enable CUDA support" OFF)
option(USE_PYTHON "Link to Python when building" OFF)
datumbox marked this conversation as resolved.
Show resolved Hide resolved
prabhat00155 marked this conversation as resolved.
Show resolved Hide resolved

if(WITH_CUDA)
enable_language(CUDA)
Expand All @@ -17,7 +18,10 @@ if(WITH_CUDA)
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(PNG REQUIRED)
Expand Down Expand Up @@ -76,7 +80,12 @@ FOREACH(DIR ${ALLOW_LISTED})
ENDFOREACH()

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
EXPORT_NAME TorchVision
INSTALL_RPATH ${TORCH_INSTALL_PREFIX}/lib)
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

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
---------------------
In order to get the torchvision operators registered with torch (eg. for the JIT), all you need to do is to ensure that you
Expand Down
6 changes: 4 additions & 2 deletions cmake/TorchVisionConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake")
if(NOT TARGET torch_library)
find_package(Torch REQUIRED)
endif()
if(NOT TARGET Python3::Python)
find_package(Python3 COMPONENTS Development)
if (@USE_PYTHON@)
if(NOT TARGET Python3::Python)
find_package(Python3 COMPONENTS Development)
endif()
endif()

set_target_properties(TorchVision::TorchVision PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${${PN}_INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "torch;Python3::Python" )
Expand Down
4 changes: 4 additions & 0 deletions examples/cpp/hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ project(hello-world)
# so there is no need to also add `find_package(Torch)` here.
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)

# We now need to link the TorchVision library to our executable.
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def get_extensions():

if sys.platform == "win32":
define_macros += [("torchvision_EXPORTS", None)]

define_macros += [("USE_PYTHON", None)]
extra_compile_args["cxx"].append("/MP")

debug_mode = os.getenv("DEBUG", "0") == "1"
Expand Down Expand Up @@ -254,6 +254,9 @@ def get_extensions():
image_library = []
image_link_flags = []

if sys.platform == "win32":
image_macros += [("USE_PYTHON", None)]

# Locating libPNG
libpng = distutils.spawn.find_executable("libpng-config")
pngfix = distutils.spawn.find_executable("pngfix")
Expand Down
4 changes: 4 additions & 0 deletions torchvision/csrc/io/image/image.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#include "image.h"

#include <ATen/core/op_registration/op_registration.h>
#ifdef USE_PYTHON
#include <Python.h>
#endif

// If we are in a Windows environment, we need to define
// initialization functions for the _custom_ops extension
#ifdef USE_PYTHON
#ifdef _WIN32
PyMODINIT_FUNC PyInit_image(void) {
// No need to do anything.
return NULL;
}
#endif
#endif // USE_PYTHON

namespace vision {
namespace image {
Expand Down
4 changes: 4 additions & 0 deletions torchvision/csrc/io/video_reader/video_reader.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "video_reader.h"

#ifdef USE_PYTHON
#include <Python.h>
#endif

#include "../decoder/memory_buffer.h"
#include "../decoder/sync_decoder.h"

#ifdef USE_PYTHON
// If we are in a Windows environment, we need to define
// initialization functions for the _custom_ops extension
#ifdef _WIN32
Expand All @@ -13,6 +16,7 @@ PyMODINIT_FUNC PyInit_video_reader(void) {
return NULL;
}
#endif
#endif // USE_PYTHONs

using namespace ffmpeg;

Expand Down
4 changes: 4 additions & 0 deletions torchvision/csrc/vision.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "vision.h"

#ifndef MOBILE
#ifdef USE_PYTHON
#include <Python.h>
#endif
#endif
#include <torch/library.h>

#ifdef WITH_CUDA
Expand All @@ -16,10 +18,12 @@
// initialization functions for the _custom_ops extension.
// For PyMODINIT_FUNC to work, we need to include Python.h
#if !defined(MOBILE) && defined(_WIN32)
#ifdef USE_PYTHON
PyMODINIT_FUNC PyInit__C(void) {
// No need to do anything.
return NULL;
}
#endif // USE_PYTHON
#endif // !defined(MOBILE) && defined(_WIN32)

namespace vision {
Expand Down