Nvcc fatal : A single input file is required for a non-link phase when an outputfile is specified

OronG13: <p>Hello,<br>
I want to learn from this link:<br>
<a href="https://pytorch.org/cppdocs/installing.html" rel="noopener nofollow ugc">INSTALLING C++ DISTRIBUTIONS OF PYTORCH</a></p>
<p>How to create an application which will use the libTorch in order to inference my trained model which trained using the Torc Python APIs interface.</p>
<p><strong>I successfully created the application based on the link instructions and successfully run it.</strong></p>
<p>For further purposes I added one *.cu file because I want to add some CUDA logics which I will perform on the Tensor input before I will pass it to the model for the forward API.</p>
<p>I updated my CMake file with all CMake CUDA commands in order to find it and activate the CMake cache generation process to find all required CUDA include and libraries files.</p>
<p>The CMake cache is successfully generated.<br>
But when I start the build process I immediately get the following error:</p>
<blockquote>
<p>nvcc fatal   : A single input file is required for a non-link phase when an outputfile is specified</p>
</blockquote>
<p>I found out that when I masked this CMake command:</p>
<blockquote>
<p>target_link_libraries(${PROJECT_NAME} PUBLIC “${TORCH_LIBRARIES}”)</p>
</blockquote>
<p>The nvcc error isn’t raised any more but then all torch symbols are unresolved so the linker fail and no *.exe is generated,</p>
<p>This is the value of the ${TORCH_LIBRARIES}:</p>
<blockquote>
<p>TORCH_LIBRARIES = torch;torch_library;C:/libtorch/lib/c10.lib;C:/libtorch/lib/kineto.lib;C:\Program Files\NVIDIA Corporation\NvToolsExt/lib/x64/nvToolsExt64_1.lib;C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.4/lib/x64/cudart_static.lib;<br>
C:/libtorch/lib/caffe2_nvrtc.lib;C:/libtorch/lib/c10_cuda.lib</p>
</blockquote>
<p>My setup is:<br>
OS - Microsoft Windows 10 Enterprise 2016 LTSB<br>
GPU - Quadro M2000M<br>
CUDA vers - 11.4<br>
Visual Studio - 2019<br>
Python - 3.6.8<br>
libTorch 1.10.1</p>
<p>I can share my entire CMake file and source code if it required but as I described above my source code is exactly as detailed in the installing link.</p>
<p>Please advice.</p>

YuriyKortev: <p>Same error, with same behaviour, did you fix it somehow?</p>

YuriyKortev: <p>finally fixed this replaced ${TORCH_LIBRARIES} in target_link_libraries on “$ENV{LibTorch}/lib/*.lib” where $ENV{LibTorch} points to libtorch root</p>

OronG13: <p>Thank you very much Yuriy for your sharing.<br>
I tried to change my target_link_libraries input as you described above  by doing this:</p>
<pre><code class="lang-auto">target_link_libraries(${PROJECT_NAME} PUBLIC "$ENV{THIRD_PARTIES_PATH}/libtorch/lib/*.lib")	
</code></pre>
<p>$ENV{THIRD_PARTIES_PATH} - Hold all my third parties installations in my PC.</p>
<p>CMake cache generation workings well but the build process raise this error:</p>
<blockquote>
<p>C:\TorchTester\bin\x64-Release\ninja : error : ‘c:/ThirdParties/libtorch/lib/*.lib’, needed by ‘TorchTester.exe’, missing and no known rule to make it<br>
Build All failed.</p>
</blockquote>
<p>What is your CMake version?<br>
What is your OS? IDE? Are you using CMake via Visual studio?</p>
<p>Thanks a lot!<br>
Please advise.</p>

YuriyKortev: <p>Hi, Oron!<br>
Im using CLion, CMake version 3.20 on Windows 10. To fix it try to use another cmake generator, im using visual studio 2019 generator. To do this run cmake with following option -G “Visual Studio 16 2019” or setup your cmake in CLion with following way<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/3/03a07e408e32c424eaeacb5eff192f2c650d154f.png" data-download-href="https://discuss.pytorch.org/uploads/default/03a07e408e32c424eaeacb5eff192f2c650d154f" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/3/03a07e408e32c424eaeacb5eff192f2c650d154f.png" alt="image" data-base62-sha1="w5hOWPjX5c0JmYlwLFmAL8EKhp" width="681" height="499" data-dominant-color="414446"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">908×666 30.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
Sorry, i dont know how to setup cmake generator in visual studio. Hope it helps!</p>

OronG13: <p>Great Yuriy,<br>
I successfully built my tester using these CMake changes:<br>
Was:</p>
<pre><code class="lang-auto">target_link_libraries(${PROJECT_NAME} PUBLIC ${TORCH_LIBRARIES})
</code></pre>
<p>Is:</p>
<pre><code class="lang-auto">file(GLOB_RECURSE TORCH_LIBS "$ENV{THIRD_PARTIES_PATH}/libtorch/lib/*.lib")
target_link_libraries(${PROJECT_NAME} PUBLIC "${TORCH_LIBS}")	
</code></pre>
<p><strong>But now a new runtime problem was raised:</strong><br>
My trained model based on CUDA, it was moved to CUDA device before training was started.<br>
Also, I exported it to torch script file using the command:</p>
<pre><code class="lang-auto">torch.jit.trace
</code></pre>
<p>And now when I want to inference it using the libtorch C++ first I loaded it using the command:</p>
<pre><code class="lang-auto">torch::jit::load(modelPath);
</code></pre>
<p>But it raised a runtime exception:</p>
<blockquote>
<p>Could not run ‘aten::empty_strided’ with arguments from the ‘CUDA’ backend. This could be because the operator doesn’t exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit <a href="https://fburl.com/ptmfixes" class="inline-onebox" rel="noopener nofollow ugc">Internal Login</a> for possible resolutions. ‘aten::empty_strided’ is only available for these backends: [CPU, Meta, BackendSelect, Python, Named, Conjugate, Negative, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradLazy, AutogradXPU, AutogradMLC, AutogradHPU, AutogradNestedTensor, AutogradPrivateUse1, AutogradPrivateUse2, AutogradPrivateUse3, Tracer, UNKNOWN_TENSOR_TYPE_ID, Autocast, Batched, VmapMode].</p>
</blockquote>
<p>Additionally, the command returned value is false:</p>
<pre><code class="lang-auto">torch::cuda::is_available()
</code></pre>
<p>But before the CMake changes, before I added any *.cu files while I still used the CMake command:</p>
<pre><code class="lang-auto">target_link_libraries(${PROJECT_NAME} PUBLIC ${TORCH_LIBRARIES})
</code></pre>
<p>The runtime worked OK and CUDA was correctly identified using the same exported model without any runtime exception.</p>
<p>Can you help me understand this issue please?<br>
<strong>Why building with ${TORCH_LIBRARIES} identify the CUDA and now with the new CMake change I cannot identify the CUDA?</strong></p>
<p>Thanks,</p>

YuriyKortev: <p>I dont know for sure, but if you print old ${TORCH_LIBRARIES} and new ${TORCH_LIBS} you’ll see difference in miss of cuda libs. For building *.cu this imports brokes everything, they already in nvcc, but for building your final executable they are necessary. So here my suggestions: move all your torch cuda extension with *.cu and their dependencies in some subdirectory, create new cmakelists.txt in there, compile it as library with new ${TORCH_LIBS}, then in your root cmakelists.txt use add_subdirectory, link this new library with your c++ executable with linking cuda libraries or old ${TORCH_LIBRARIES}. I’m pretty sure this should help.</p>

OronG13: <p>Yuriy,<br>
Your offer works, thanks!<br>
When NVCC compiler operation is activated independently from the linker operation everything is working fine.</p>
<p>It means that all *.cu &amp; *.cuh files shall be compiled to a separated library which further will be linked to the final application.</p>
<p>Still, it isn’t understood why working with ${TORCH_LIBS} disable any torch CUDA identification while working with ${TORCH_LIBRARIES} enable it…</p>
<p>Details:<br>
${TORCH_LIBS} includes these libraries:<br>
Caffe2_perfkernels_avx.lib;<br>
Caffe2_perfkernels_avx2.lib;<br>
Caffe2_perfkernels_avx512.lib;<br>
XNNPACK.lib;<br>
asmjit.lib;<br>
c10.lib;<br>
c10_cuda.lib;<br>
caffe2_detectron_ops_gpu.lib;<br>
caffe2_module_test_dynamic.lib;<br>
caffe2_nvrtc.lib;<br>
clog.lib;<br>
cpuinfo.lib;<br>
dnnl.lib;<br>
fbgemm.lib;<br>
fbjni.lib;<br>
kineto.lib;<br>
libprotobuf-lite.lib;<br>
libprotobuf.lib;<br>
libprotoc.lib;<br>
mkldnn.lib;<br>
pthreadpool.lib;<br>
pytorch_jni.lib;<br>
torch.lib;<br>
torch_cpu.lib;<br>
torch_cuda.lib;<br>
torch_cuda_cpp.lib;<br>
torch_cuda_cu.lib</p>
<p>${TORCH_LIBRARIES} includes these libraries:<br>
torch;<br>
torch_library;<br>
c10.lib;<br>
kineto.lib;<br>
nvToolsExt64_1.lib;<br>
cudart_static.lib;<br>
caffe2_nvrtc.lib;<br>
c10_cuda.lib</p>
<p>When I use ${TORCH_LIBRARIES} i added separately all other required libs such as cudart_static…</p>
<p>With both CMAKE variables the linker successfully completed but during runtime torch CUDA isn’t identified if I used ${TORCH_LIBRARIES}. It seams that only torch CPU was statically and dynamically linked.</p>
<p>Regards,</p>

YuriyKortev: <p>Hi, Oron</p>
<p>Im glad this helped</p>