C++ static lib on windows cannot find CUDA

Hi,

I am trying to use libtorch with static build.
(using Visual Studio 2017/CUDA10.1, source is v1.5.0)
It seems building static libtorch is succeeding but torch could not find CUDA device.

std::cout << torch::cuda::is_available() << std::endl;
try {
	torch::Tensor tensor = torch::randn({ 1,1 });
	tensor.to(torch::Device("cuda:0"));
}
catch (std::exception& ex) {
	std::cout << ex.what() << std::endl;
}

0
Cannot initialize CUDA without ATen_cuda library. PyTorch splits its backend into two shared libraries: a CPU library and a CUDA library; this error has occurred because you are trying to use some CUDA functionality, but the CUDA library has not been loaded by the dynamic linker for some reason. The CUDA library MUST be loaded, EVEN IF you don’t directly use any symbols from the CUDA library! One common culprit is a lack of -Wl,–no-as-needed in your link arguments; many dynamic linkers will delete dynamic library dependencies if you don’t depend on any of their symbols. You can check if this has occurred by using ldd on your binary to see if there is a dependency on *_cuda.so library. (initCUDA at C:\…\pytorch\aten\src\ATen/detail/CUDAHooksInterface.h:63)
(no backtrace available)

What is “ATen_cuda library” ?
What should I do ?

My libtorch creation process is following.

set BUILD_SHARED_LIBS=OFF
set BUILD_TEST=OFF
set BUILD_CAFFE2_OPS=OFF   (If set ON, LNK1248 occurs)
set BUILD_PYTHON=False
set CAFFE2_USE_MSVC_STATIC_RUNTIME=ON
set CAFFE2_STATIC_LINK_CUDA=ON
set CUDNN_STATIC=ON
set MSVC_Z7_OVERRIDE=OFF
set USE_CUDA=ON
set USE_CUDNN=ON
set USE_FBGEMM=OFF

py tools\build_libtorch.py

The simple application above links with all created static libs (under pytorch/biuld/libs).

Caffe2_perfkernels_avx.lib
Caffe2_perfkernels_avx2.lib
Caffe2_perfkernels_avx512.lib
c10.lib
c10_cuda.lib
caffe2_protos.lib
clog.lib
cpuinfo.lib
cpuinfo_internals.lib
foxi_loader.lib
libprotobuf-lite.lib
libprotobuf.lib
libprotoc.lib
mkldnn.lib
onnx.lib
onnx_proto.lib
torch.lib
torch_cpu.lib
torch_cuda.lib

/WHOLEARCHIVE:torch_cpu.lib

I tried to build with “/WHOLEARCHIVE:torch_cuda.lib”, but LNK1102 (out of memory) occured.

I also tried linking below, but the result is not changed.

C:\Program Files\NVIDIA Corporation\NvToolsExt\lib\x64\nvToolsExt64_1.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cudart_static.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cufft.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\curand.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cublas.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cudnn.lib

Do you have any suggestion ?
Thanks.

/WHOLEARCHIVE:torch_cuda.lib is the right choice. But you could try building with the x64 toolchain. You may get more details in https://stackoverflow.com/questions/19820718/how-to-make-visual-studio-use-the-native-amd64-toolchain.

@peterjc123
Thanks to the solution you provided, the LNK1102 (out of memory) issue has been resolved.
And the sample code above is worked correctly building with following settings:

  • /WHOLEARCHIVE: torch_cpu.lib torch_cuda.lib c10_cuda.lib
  • link cuda, cudnn libs

Thx !