Repository: pytorch/vision
Pull request: Augment CMakeLists.txt & Fix generated config file
I hope this simple fix could pass the CI tests (as a follow-up of #6378 and #5602).

Resolves #5863.

Problem statement:
Python dependency added to TorchVisionConfig.cmake despite building with -DUSE_PYTHON=off
### 🐛 Describe the bug

Despite building `libtorchvision.so` with the flag `-DUSE_PYTHON=off`, when building a program that depends on it (`infer-masts`), `cmake` throws an error due to a missing Python dependency:
```
$ Torch_DIR=~/.local/lib/python3.9/site-packages/torch/share/cmake/Torch cmake -DTORCH_CUDA_ARCH_LIST="5.3;6.2;7.2" -Wno-dev -GNinja ..
...
CMake Error at CMakeLists.txt:24 (add_executable):
  Target "infer-masts" links to target "Python3::Python" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?
```
Changing line 61 of TorchVisionConfig.cmake from
```
set_target_properties(TorchVision::TorchVision PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${${PN}_INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "torch;Python3::Python" )
```
to
```
set_target_properties(TorchVision::TorchVision PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${${PN}_INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "torch" )
```
allows my program to build without issues.

### Versions

```
Collecting environment information...
PyTorch version: 1.11.0a0+gitbc2c6ed
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.6 LTS (aarch64)
GCC version: (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
CMake version: version 3.20.4
Libc version: glibc-2.27

Python version: 3.9.10 (main, Mar 16 2022, 15:59:07)  [GCC 8.4.0] (64-bit runtime)
Python platform: Linux-4.9.201-tegra-aarch64-with-glibc2.27
Is CUDA available: True
CUDA runtime version: 10.2.89
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Probably one of the following:
/usr/lib/aarch64-linux-gnu/libcudnn.so.8.0.0
/usr/lib/aarch64-linux-gnu/libcudnn_adv_infer.so.8.0.0
/usr/lib/aarch64-linux-gnu/libcudnn_adv_train.so.8.0.0
/usr/lib/aarch64-linux-gnu/libcudnn_cnn_infer.so.8.0.0
/usr/lib/aarch64-linux-gnu/libcudnn_cnn_train.so.8.0.0
/usr/lib/aarch64-linux-gnu/libcudnn_ops_infer.so.8.0.0
/usr/lib/aarch64-linux-gnu/libcudnn_ops_train.so.8.0.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False

Versions of relevant libraries:
[pip3] numpy==1.21.5
[pip3] numpy-quaternion==2022.2.10.14.20.39
[pip3] torch==1.11.0a0+gitbc2c6ed
[pip3] torchvision==0.12.0a0+9b5a3fe
[conda] Could not collect
```
Note: This is on a Jetson Nano device. Python3.9, torch and torchvision were built with gcc-8, not gcc-7 as indicated by the collect_env.py script. Relevant environment variables are:
```
export BUILD_CAFFE2_OPS=OFF
export USE_FBGEMM=OFF
export USE_FAKELOWP=OFF
export BUILD_TEST=OFF
export USE_MKLDNN=OFF
export USE_NNPACK=OFF
export USE_XNNPACK=OFF
export USE_QNNPACK=OFF
export USE_PYTORCH_QNNPACK=OFF
export USE_CUDA=ON
export USE_CUDNN=ON
export TORCH_CUDA_ARCH_LIST="5.3;6.2;7.2"
export USE_NCCL=OFF
export USE_SYSTEM_NCCL=OFF
export USE_OPENCV=OFF
export MAX_JOBS=2
export PATH=/usr/lib/ccache:$PATH
export CC=gcc-8
export CXX=g++-8
```
torchvision was configured with the above environment variables and the command
```
torchvision-0.12.0/build$ cmake -Wno-dev -DWITH_CUDA=on -DTORCH_CUDA_ARCH_LIST="5.3;6.2;7.2" -DUSE_PYTHON=off -GNinja -DCMAKE_INSTALL_PREFIX=~/.local ..
```

Hints from issue discussion:
I forgot to add, this is the CMakeLists.txt file:
```
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(infer-masts)

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        # require at least gcc 8.4
        if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.4)
                message(FATAL_ERROR "GCC version must be at least 8.4!  " ${CMAKE_CXX_COMPILER_VERSION})
        endif()
endif()
set(CMAKE_CXX_STANDARD 20)
add_compile_options(-fPIC -Wall -Wextra -pedantic -Werror -Wno-unknown-pragmas -Wno-missing-field-initializers)

find_package(Torch REQUIRED)
find_package(TorchVision REQUIRED)
find_package(fmt REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(OpenCV COMPONENTS opencv_core opencv_imgproc opencv_highgui REQUIRED)
find_package(Threads REQUIRED)
find_package(nlohmann_json REQUIRED)

include_directories("~/github/date/include") # https://github.com/HowardHinnant/date for ease of formatting timestamps.

add_executable(infer-masts src/main.cpp src/utils.cpp src/net_base.cpp src/masts.cpp src/components.cpp src/traverse.cpp src/communication.cpp src/inference_engine.cpp)
target_link_libraries(infer-masts PRIVATE TorchVision::TorchVision fmt::fmt ${Boost_SYSTEM_LIBRARY} Threads::Threads nlohmann_json::nlohmann_json ${OpenCV_LIBS})
```
Copy, I'm trying to run the detectron2 deployment example and seeing the same thing. Same fix as above (removing the Python3::Python target from the `set_target_properties` example works.
Hello all, any progress regarding this issue ?

Commit message: Fix generated CMake file

Patch to review:
diff --git a/cmake/TorchVisionConfig.cmake.in b/cmake/TorchVisionConfig.cmake.in
index aa042844753..5d3cffbf421 100644
--- a/cmake/TorchVisionConfig.cmake.in
+++ b/cmake/TorchVisionConfig.cmake.in
@@ -22,23 +22,26 @@ if(NOT (CMAKE_VERSION VERSION_LESS 3.0))
 # Don't include targets if this file is being picked up by another
 # project which has already built this as a subproject
 #-----------------------------------------------------------------------------
-if(NOT TARGET ${PN}::TorchVision)
+if(NOT TARGET ${PN}::${PN})
 include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake")
 
-if(NOT TARGET torch_library)
+target_include_directories(${PN}::${PN} INTERFACE "${${PN}_INCLUDE_DIR}" ${JPEG_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS})
+
 find_package(Torch REQUIRED)
-endif()
+find_package(PNG REQUIRED)
+find_package(JPEG REQUIRED)
+
+target_link_libraries(${PN}::${PN} INTERFACE torch ${PNG_LIBRARY} ${JPEG_LIBRARIES})
+
 if (@USE_PYTHON@)
   if(NOT TARGET Python3::Python)
     find_package(Python3 COMPONENTS Development)
   endif()
+  target_link_libraries(torch INTERFACE Python3::Python)
 endif()
 
-set_target_properties(TorchVision::TorchVision PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${${PN}_INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "torch;Python3::Python" )
-
-
 if(@WITH_CUDA@)
-  target_compile_definitions(TorchVision::TorchVision INTERFACE WITH_CUDA)
+  target_compile_definitions(${PN}::${PN} INTERFACE WITH_CUDA)
 endif()
 
 endif()


Code review comments:
diff_hunk: @@ -22,23 +22,26 @@ if(NOT (CMAKE_VERSION VERSION_LESS 3.0))
 # Don't include targets if this file is being picked up by another
 # project which has already built this as a subproject
 #-----------------------------------------------------------------------------
-if(NOT TARGET ${PN}::TorchVision)
+if(NOT TARGET ${PN}::${PN})
 include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake")
 
-if(NOT TARGET torch_library)
+target_include_directories(${PN}::${PN} INTERFACE "${${PN}_INCLUDE_DIR}" ${JPEG_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS})
+
 find_package(Torch REQUIRED)
-endif()
+find_package(PNG REQUIRED)
+find_package(JPEG REQUIRED)
path: cmake/TorchVisionConfig.cmake.in
text: @user1:
This makes PNG/JPEG mandatory dependencies. Currently those are [optional](https://github.com/pytorch/vision/blob/main/CONTRIBUTING.md#install-torchvision) dependencies.

@author:
Honestly, I don't think this could be considered optional dependencies because

https://github.com/pytorch/vision/blob/v0.13.1/CMakeLists.txt#L27-L28

and

https://github.com/pytorch/vision/blob/v0.13.1/CMakeLists.txt#L85

will always make these two libraries mandatory unless we provide extra options in the main CMakeLists.txt to disable the linking.

@user1:
Well you do have a point on that.

@author:
I will push another commit to make these consistent.