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

CMakeLists and C++ files refactoring. #2446

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
80 changes: 43 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0)
project(torchvision)
set(CMAKE_CXX_STANDARD 14)
set(TORCHVISION_VERSION 0.6.0)
set(TORCHVISION_VERSION 0.5.0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like an unwanted change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry for the typo


option(WITH_CUDA "Enable CUDA support" OFF)

Expand All @@ -11,64 +11,70 @@ if(WITH_CUDA)
endif()

find_package(Python3 COMPONENTS Development)

find_package(Torch REQUIRED)
find_package(PNG REQUIRED)

# Gather source files.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of these kinds of comments, they don't add anything valuable.

file(GLOB OPERATOR_SOURCES torchvision/csrc/cpu/*.cpp torchvision/csrc/*.cpp)
file(GLOB MODELS_SOURCES torchvision/csrc/models/*.cpp)

# Set include directories.
include_directories(torchvision/cinclude/${PROJECT_NAME})

file(GLOB HEADERS torchvision/csrc/*.h)
# Image extension
file(GLOB IMAGE_HEADERS torchvision/csrc/cpu/image/*.h)
file(GLOB IMAGE_SOURCES torchvision/csrc/cpu/image/*.cpp)
file(GLOB OPERATOR_SOURCES torchvision/csrc/cpu/*.h torchvision/csrc/cpu/*.cpp ${IMAGE_HEADERS} ${IMAGE_SOURCES} ${HEADERS} torchvision/csrc/*.cpp)
if(WITH_CUDA)
file(GLOB OPERATOR_SOURCES ${OPERATOR_SOURCES} torchvision/csrc/cuda/*.h torchvision/csrc/cuda/*.cu)
# Gather cuda source files.
file(GLOB OPERATOR_CUDA_SOURCES torchvision/csrc/cuda/*.cu)
list(APPEND OPERATOR_SOURCES ${OPERATOR_CUDA_SOURCES})

# Set cuda include directories.
include_directories(torchvision/cinclude/${PROJECT_NAME}_cuda)
endif()
file(GLOB MODELS_HEADERS torchvision/csrc/models/*.h)
file(GLOB MODELS_SOURCES torchvision/csrc/models/*.h torchvision/csrc/models/*.cpp)

add_library(${PROJECT_NAME} SHARED ${MODELS_SOURCES} ${OPERATOR_SOURCES} ${IMAGE_SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} ${PNG_LIBRARY} Python3::Python)
# target_link_libraries(${PROJECT_NAME} PRIVATE ${PNG_LIBRARY} Python3::Python)
# Add library.
add_library(${PROJECT_NAME} SHARED ${MODELS_SOURCES} ${OPERATOR_SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} Python3::Python)
set_target_properties(${PROJECT_NAME} PROPERTIES EXPORT_NAME TorchVision)

target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${HEADERS}:${PNG_INCLUDE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

set(TORCHVISION_CMAKECONFIG_INSTALL_DIR "share/cmake/TorchVision" CACHE STRING "install path for TorchVisionConfig.cmake")

configure_package_config_file(cmake/TorchVisionConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/TorchVisionConfig.cmake"
INSTALL_DESTINATION ${TORCHVISION_CMAKECONFIG_INSTALL_DIR})
"${CMAKE_CURRENT_BINARY_DIR}/TorchVisionConfig.cmake"
INSTALL_DESTINATION ${TORCHVISION_CMAKECONFIG_INSTALL_DIR}
)

write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/TorchVisionConfigVersion.cmake
VERSION ${TORCHVISION_VERSION}
COMPATIBILITY AnyNewerVersion)
VERSION ${TORCHVISION_VERSION}
COMPATIBILITY AnyNewerVersion
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/TorchVisionConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/TorchVisionConfigVersion.cmake
DESTINATION ${TORCHVISION_CMAKECONFIG_INSTALL_DIR})
${CMAKE_CURRENT_BINARY_DIR}/TorchVisionConfigVersion.cmake
DESTINATION ${TORCHVISION_CMAKECONFIG_INSTALL_DIR}
)

# Install library.
install(TARGETS ${PROJECT_NAME}
EXPORT TorchVisionTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
EXPORT TorchVisionTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

install(EXPORT TorchVisionTargets
NAMESPACE TorchVision::
DESTINATION ${TORCHVISION_CMAKECONFIG_INSTALL_DIR})
NAMESPACE TorchVision::
DESTINATION ${TORCHVISION_CMAKECONFIG_INSTALL_DIR}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These all look like unrelated formatting changes. I'd say stick to the current style, and if you want to propose a different one do so in a different PR.


# Install headers.
install(
DIRECTORY "torchvision/cinclude/${PROJECT_NAME}/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}"
)

install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
install(FILES
torchvision/csrc/cpu/vision_cpu.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/cpu)
if(WITH_CUDA)
install(FILES
torchvision/csrc/cuda/vision_cuda.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/cuda)
# Install cuda headers.
install(
DIRECTORY "torchvision/cinclude/${PROJECT_NAME}_cuda/cuda"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}"
)
endif()
install(FILES ${MODELS_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/models)
26 changes: 15 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,18 @@ def get_linux_distribution():

def get_extensions():
this_dir = os.path.dirname(os.path.abspath(__file__))
extensions_dir = os.path.join(this_dir, 'torchvision', 'csrc')
extensions_src_dir = os.path.join(this_dir, 'torchvision', 'csrc')
extensions_include_dir = os.path.join(this_dir, 'torchvision', 'cinclude', 'torchvision')

main_file = glob.glob(os.path.join(extensions_dir, '*.cpp'))
source_cpu = glob.glob(os.path.join(extensions_dir, 'cpu', '*.cpp'))
main_file = glob.glob(os.path.join(extensions_src_dir, '*.cpp'))
source_cpu = glob.glob(os.path.join(extensions_src_dir, 'cpu', '*.cpp'))

is_rocm_pytorch = False
if torch.__version__ >= '1.5':
from torch.utils.cpp_extension import ROCM_HOME
is_rocm_pytorch = True if ((torch.version.hip is not None) and (ROCM_HOME is not None)) else False

# TODO fix this
if is_rocm_pytorch:
hipify_python.hipify(
project_directory=this_dir,
Expand All @@ -176,13 +178,14 @@ def get_extensions():
show_detailed=True,
is_pytorch_extension=True,
)
source_cuda = glob.glob(os.path.join(extensions_dir, 'hip', '*.hip'))
source_cuda = glob.glob(os.path.join(extensions_src_dir, 'hip', '*.hip'))
# Copy over additional files
shutil.copy("torchvision/csrc/cuda/cuda_helpers.h", "torchvision/csrc/hip/cuda_helpers.h")
shutil.copy("torchvision/csrc/cuda/vision_cuda.h", "torchvision/csrc/hip/vision_cuda.h")

else:
source_cuda = glob.glob(os.path.join(extensions_dir, 'cuda', '*.cu'))
source_cuda = glob.glob(os.path.join(extensions_src_dir, 'cuda', '*.cu'))
cuda_include_dir = os.path.join(this_dir, 'torchvision', 'cinclude', 'torchvision_cuda')

sources = main_file + source_cpu
extension = CppExtension
Expand All @@ -201,11 +204,14 @@ def get_extensions():

define_macros = []

include_dirs = [extensions_include_dir]

extra_compile_args = {}
if (torch.cuda.is_available() and ((CUDA_HOME is not None) or is_rocm_pytorch)) \
or os.getenv('FORCE_CUDA', '0') == '1':
extension = CUDAExtension
sources += source_cuda
include_dirs.append(cuda_include_dir)
if not is_rocm_pytorch:
define_macros += [('WITH_CUDA', None)]
nvcc_flags = os.getenv('NVCC_FLAGS', '')
Expand All @@ -227,9 +233,7 @@ def get_extensions():
extra_compile_args.setdefault('cxx', [])
extra_compile_args['cxx'].append('/MP')

sources = [os.path.join(extensions_dir, s) for s in sources]

include_dirs = [extensions_dir]
sources = [os.path.join(extensions_src_dir, s) for s in sources]

ext_modules = [
extension(
Expand Down Expand Up @@ -263,7 +267,7 @@ def get_extensions():

# Image reading extension
image_macros = []
image_include = [extensions_dir]
image_include = [extensions_include_dir]
image_library = []
image_link_flags = []

Expand Down Expand Up @@ -327,7 +331,7 @@ def get_extensions():
image_include += [png_include]
image_link_flags.append('libpng')

image_path = os.path.join(extensions_dir, 'cpu', 'image')
image_path = os.path.join(extensions_src_dir, 'cpu', 'image')
image_src = glob.glob(os.path.join(image_path, '*.cpp'))

if png_found:
Expand Down Expand Up @@ -368,7 +372,7 @@ def get_extensions():
base_decoder_src_dir,
video_reader_src_dir,
ffmpeg_include_dir,
extensions_dir,
extensions_src_dir,
],
libraries=[
'avcodec',
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion torchvision/csrc/cpu/ROIAlign_cpu.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <ATen/TensorUtils.h>
#include "vision_cpu.h"
#include "cpu/vision_cpu.h"

// implementation taken from Caffe2
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/cpu/nms_cpu.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "vision_cpu.h"
#include "cpu/vision_cpu.h"

template <typename scalar_t>
at::Tensor nms_cpu_kernel(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/cuda/DeformConv_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
#include <c10/cuda/CUDAGuard.h>
#include <THC/THCAtomics.cuh>

#include "cuda_helpers.h"
#include "cuda/cuda_helpers.h"

#include <cmath>
#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/cuda/PSROIAlign_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <THC/THCAtomics.cuh>
#include <stdio.h>

#include "cuda_helpers.h"
#include "cuda/cuda_helpers.h"

template <typename T>
__device__ T bilinear_interpolate(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/cuda/PSROIPool_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <c10/cuda/CUDAGuard.h>
#include <THC/THCAtomics.cuh>

#include "cuda_helpers.h"
#include "cuda/cuda_helpers.h"

template <typename T>
__global__ void PSROIPoolForward(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/cuda/ROIAlign_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <c10/cuda/CUDAGuard.h>
#include <THC/THCAtomics.cuh>

#include "cuda_helpers.h"
#include "cuda/cuda_helpers.h"

template <typename T>
__device__ T bilinear_interpolate(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/cuda/ROIPool_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <c10/cuda/CUDAGuard.h>
#include <THC/THCAtomics.cuh>

#include "cuda_helpers.h"
#include "cuda/cuda_helpers.h"

template <typename T>
__global__ void RoIPoolForward(
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/cuda/nms_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>

#include "cuda_helpers.h"
#include "cuda/cuda_helpers.h"

#include <iostream>
#include <vector>
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/alexnet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "alexnet.h"
#include "models/alexnet.h"

#include "modelsimpl.h"
#include "models/modelsimpl.h"

namespace vision {
namespace models {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/densenet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "densenet.h"
#include "models/densenet.h"

#include "modelsimpl.h"
#include "models/modelsimpl.h"

namespace vision {
namespace models {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/googlenet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "googlenet.h"
#include "models/googlenet.h"

#include "modelsimpl.h"
#include "models/modelsimpl.h"

namespace vision {
namespace models {
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/models/inception.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "inception.h"
#include "models/inception.h"

namespace vision {
namespace models {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/mnasnet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "mnasnet.h"
#include "models/mnasnet.h"

#include "modelsimpl.h"
#include "models/modelsimpl.h"

namespace vision {
namespace models {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/mobilenet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "mobilenet.h"
#include "models/mobilenet.h"

#include "modelsimpl.h"
#include "models/modelsimpl.h"

namespace vision {
namespace models {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/resnet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "resnet.h"
#include "models/resnet.h"

#include "modelsimpl.h"
#include "models/modelsimpl.h"

namespace vision {
namespace models {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/shufflenetv2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "shufflenetv2.h"
#include "models/shufflenetv2.h"

#include "modelsimpl.h"
#include "models/modelsimpl.h"

namespace vision {
namespace models {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/squeezenet.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "squeezenet.h"
#include "models/squeezenet.h"

#include <limits>
#include "modelsimpl.h"
#include "models/modelsimpl.h"

namespace vision {
namespace models {
Expand Down
4 changes: 2 additions & 2 deletions torchvision/csrc/models/vgg.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "vgg.h"
#include "models/vgg.h"

#include <unordered_map>
#include "modelsimpl.h"
#include "models/modelsimpl.h"

namespace vision {
namespace models {
Expand Down