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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Call find_package(Torch REQUIRED) more than one time in downstream project causes CMake configuration error #25004

Open
jasjuang opened this issue Aug 22, 2019 · 10 comments
Labels
module: build Build system issues triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@jasjuang
Copy link
Contributor

jasjuang commented Aug 22, 2019

馃悰 Bug

Please see below minimal code to reproduce the problem

CMakeLists.txt

cmake_minimum_required(VERSION 3.11)

project(sample)

find_package(Torch REQUIRED)
find_package(Torch REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} torch)

main.cpp

#include <ATen/ATen.h>

int main(){
  return 0;
}

Expected behavior

After

mkdir build
cd build
cmake ..

I expect my downstream cmake configuration to work like usual, but here's the error message I get

CMake Error at /usr/share/cmake/Caffe2/public/mkl.cmake:3 (add_library):
  add_library cannot create imported target "caffe2::mkl" because another
  target with the same name already exists.
Call Stack (most recent call first):
  /usr/share/cmake/Caffe2/Caffe2Config.cmake:109 (include)
  /usr/share/cmake/Torch/TorchConfig.cmake:40 (find_package)
  CMakeLists.txt:6 (find_package)


-- Configuring incomplete, errors occurred!

I understand it's unusual to have the need to call find_package(Torch REQUIRED) twice but calling it twice for other popular libraries like OpenCV, TensorFlow, etc does not result in an error. Can someone please fix this?

Environment

  • OS : Ubuntu
  • How you installed PyTorch: source
  • Build command you used:
cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DBUILD_CUSTOM_PROTOBUF=OFF -DBUILD_PYTHON=OFF -DBUILDING_WITH_TORCH_LIBS=ON -DUSE_SYSTEM_NCCL=ON ..
make -j
  • CMake version: 3.15.2
  • Python version: 2.7
  • CUDA/cuDNN version: 10.0.130
  • GPU models and configuration: GeForce GTX TITAN Black

cc @ezyang

@ifedan ifedan added module: binaries Anything related to official binaries that we release to users triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module module: build Build system issues and removed module: binaries Anything related to official binaries that we release to users labels Aug 22, 2019
@ezyang
Copy link
Contributor

ezyang commented Aug 23, 2019

Oof. We probably should guard the relevant lines of code in cmake/public/mkl.cmake so that it doesn't create it the second time. @jasjuang do you think you could cook up a patch? Add me as reviewer.

Also cc @xuhdev if you happen to know what the idiomatic way to solve a problem like this is.

@jasjuang
Copy link
Contributor Author

@ezyang The current patch I have locally is adding an additional if condition in mkl.cmake

if(NOT TARGET caffe2::mkl)
  add_library(caffe2::mkl INTERFACE IMPORTED)
endif()

It solves the problem but I am not sure whether that's the right way to do it. If you think this is okay, let me know and I will submit a PR.

@xuhdev
Copy link
Collaborator

xuhdev commented Aug 23, 2019

This is a bit weird because, it seems to me, that find_package(Torch) (TorchConfig.cmake) should be pretty minimum and shouldn't trigger the scripts for the build process of PyTorch itself (find_package(Torch) should find a PyTorch installation, which might not be in a source code form). Are you aware of any reason that mkl.cmake is conjured in the first place?

@ezyang
Copy link
Contributor

ezyang commented Aug 24, 2019

mkl.cmake came along for the ride when we merged in Caffe2's cmake scripts. I think it's to solve situations where downstream apps also have to link against mkl (e.g., if you're linking statically); in that case, the Torch cmake config has to know something about mkl to get people to link against it.

@xuhdev
Copy link
Collaborator

xuhdev commented Aug 25, 2019

@ezyang If this is case, I would say the best way (and probably the most reliable and idiomatic way) is to store information about the MKL with which PyTorch was linked somewhere (e.g., version.py) and let find_package(Torch) return that information. For this particular case though, I'm not sure what to do; seems to me that guarding TorchConfig.cmake might be the best temporary (although hacky) workaround.

@xuhdev
Copy link
Collaborator

xuhdev commented Aug 25, 2019

Or we just leave it as it is and let the user to guard if they truly have the need to call find_package(Torch) twice.

@jasjuang
Copy link
Contributor Author

Here's a real-world scenario where find_package(Torch) would need to be called twice. Library A links to Torch and has a find_dependency(Torch) in its config file. Another Library B also links to Torch and has a find_dependency(Torch) in its config file. Now my project depends on both Library A and B. In my CMakeLists there's find_package(LibraryA) and find_package(LibraryB), when I run cmake .. I got the same error message as reported. In fact, this is how I discovered this bug in the first place. I don't think it's a good idea to let the downstream user guard it. It should behave like all other libraries where this kind of problem doesn't exist.

@ezyang
Copy link
Contributor

ezyang commented Aug 26, 2019

Yes, let's accept the hack for now. Or if someone wants to redo how we do TorchConfig.cmake packaging, I'd also be down too :)

@jasjuang
Copy link
Contributor Author

@ezyang please see the PR #25167

@xuhdev
Copy link
Collaborator

xuhdev commented Aug 26, 2019

Can we make the hack simply guard TorchConfig.cmake itself? As this seems to be a "cleaner" guard hacking :)

facebook-github-bot pushed a commit that referenced this issue Aug 26, 2019
Summary:
fixes issue #25004
Pull Request resolved: #25167

Differential Revision: D17051290

Pulled By: ezyang

fbshipit-source-id: 30c2b6d6ffca2ce8dae45a4a706ce45d6386c672
zdevito pushed a commit to zdevito/ATen that referenced this issue Aug 26, 2019
Summary:
fixes issue pytorch/pytorch#25004
Pull Request resolved: pytorch/pytorch#25167

Differential Revision: D17051290

Pulled By: ezyang

fbshipit-source-id: 30c2b6d6ffca2ce8dae45a4a706ce45d6386c672
facebook-github-bot pushed a commit that referenced this issue Sep 23, 2019
Summary:
This is a similar problem to #25004. After the merge of #25167, I recompiled torch and discovered another similar bug.

ezyang please take a look
Pull Request resolved: #25257

Differential Revision: D17528116

Pulled By: ezyang

fbshipit-source-id: 1657d9ee6dced3548f246010b05e2b3c25c37dee
zdevito pushed a commit to zdevito/ATen that referenced this issue Sep 23, 2019
Summary:
This is a similar problem to pytorch/pytorch#25004. After the merge of pytorch/pytorch#25167, I recompiled torch and discovered another similar bug.

ezyang please take a look
Pull Request resolved: pytorch/pytorch#25257

Differential Revision: D17528116

Pulled By: ezyang

fbshipit-source-id: 1657d9ee6dced3548f246010b05e2b3c25c37dee
mingbowan pushed a commit to mingbowan/pytorch that referenced this issue Sep 23, 2019
Summary:
This is a similar problem to pytorch#25004. After the merge of pytorch#25167, I recompiled torch and discovered another similar bug.

ezyang please take a look
Pull Request resolved: pytorch#25257

Differential Revision: D17528116

Pulled By: ezyang

fbshipit-source-id: 1657d9ee6dced3548f246010b05e2b3c25c37dee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: build Build system issues triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

4 participants