<p dir="auto">Numerous Warnings and Errors occur when including LibTorch in .cu files. I think maybe some compiling settings are wrong...</p>
<p dir="auto">One of error is as following.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="/home/libtorch/include/torch/csrc/api/include/torch/nn/cloneable.h:68:61: error: invalid static_cast from type ‘const torch::OrderedDict&lt;std::__cxx11::basic_string&lt;char&gt;, std::shared_ptr&lt;torch::nn::Module&gt; &gt;’ to type ‘torch::OrderedDict&lt;std::__cxx11::basic_string&lt;char&gt;, std::shared_ptr&lt;torch::nn::Module&gt; &gt;&amp;’"><pre class="notranslate"><code class="notranslate">/home/libtorch/include/torch/csrc/api/include/torch/nn/cloneable.h:68:61: error: invalid static_cast from type ‘const torch::OrderedDict&lt;std::__cxx11::basic_string&lt;char&gt;, std::shared_ptr&lt;torch::nn::Module&gt; &gt;’ to type ‘torch::OrderedDict&lt;std::__cxx11::basic_string&lt;char&gt;, std::shared_ptr&lt;torch::nn::Module&gt; &gt;&amp;’
</code></pre></div>
<p dir="auto">Most of errors are similar as this one.</p>

<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Easily to reproduce, just set all things as following and compile the cmake project</p>
<p dir="auto">files</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="src
|-- test.cu
|-- CMakeLists.txt"><pre class="notranslate"><code class="notranslate">src
|-- test.cu
|-- CMakeLists.txt
</code></pre></div>
<p dir="auto">test.cu</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#include &lt;torch/torch.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime.h&gt;
#include &lt;vector&gt;"><pre class="notranslate"><code class="notranslate">#include &lt;torch/torch.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime.h&gt;
#include &lt;vector&gt;
</code></pre></div>
<p dir="auto">CMakeLists.txt</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
set(CMAKE_CUDA_COMPILER &quot;nvcc&quot;)
set(CMAKE_CUDA_COMPILER_VERSION &quot;10.1&quot;)
project(cmake_and_cuda LANGUAGES CXX CUDA)
set(CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}&quot;)
find_package(Torch REQUIRED)
file(GLOB_RECURSE SRC_FILES_TEMP
  ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp 
  ${CMAKE_CURRENT_SOURCE_DIR}/*.h 
  ${CMAKE_CURRENT_SOURCE_DIR}/*.cu)
add_library(main_lib STATIC ${SRC_FILES_TEMP})
target_include_directories(main_lib PUBLIC ${TORCH_INCLUDE_DIRS})
target_compile_features(main_lib PUBLIC cxx_std_14)
set_target_properties( main_lib
                       PROPERTIES CUDA_SEPARABLE_COMPILATION ON 
                      )
add_executable(main test.cu)
set_target_properties(main PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(main PUBLIC main_lib)
target_link_libraries(main PUBLIC ${TORCH_LIBRARIES})"><pre class="notranslate"><code class="notranslate">cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
set(CMAKE_CUDA_COMPILER "nvcc")
set(CMAKE_CUDA_COMPILER_VERSION "10.1")
project(cmake_and_cuda LANGUAGES CXX CUDA)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
find_package(Torch REQUIRED)
file(GLOB_RECURSE SRC_FILES_TEMP
  ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp 
  ${CMAKE_CURRENT_SOURCE_DIR}/*.h 
  ${CMAKE_CURRENT_SOURCE_DIR}/*.cu)
add_library(main_lib STATIC ${SRC_FILES_TEMP})
target_include_directories(main_lib PUBLIC ${TORCH_INCLUDE_DIRS})
target_compile_features(main_lib PUBLIC cxx_std_14)
set_target_properties( main_lib
                       PROPERTIES CUDA_SEPARABLE_COMPILATION ON 
                      )
add_executable(main test.cu)
set_target_properties(main PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(main PUBLIC main_lib)
target_link_libraries(main PUBLIC ${TORCH_LIBRARIES})
</code></pre></div>
<p dir="auto">make command</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="cmake ../src/ -DCMAKE_PREFIX_PATH=/path/to/libtorch &amp;&amp; make"><pre class="notranslate"><code class="notranslate">cmake ../src/ -DCMAKE_PREFIX_PATH=/path/to/libtorch &amp;&amp; make
</code></pre></div>

<h2 dir="auto">Expected behavior</h2>
<p dir="auto">&lt;torch/torch.h&gt; can be included in .cu files and we can implement some self-defined CUDA+Torch layers to construct NNs in C++ front-end without pybind11 and python.</p>

<h2 dir="auto">Environment</h2>
<ul dir="auto">
<li>PyTorch Version: LibTorch 1.4.0</li>
<li>OS: Ubuntu 16.04.6 LTS</li>
<li>How you installed PyTorch: Source</li>
<li>Python version: 3.7</li>
<li>CUDA/cuDNN version: 10.1</li>
<li>GPU models and configuration: 2x Titan V</li>
<li>GCC version: 5.5.0</li>
<li>CMake version: version 3.14.7</li>
</ul>
<h2 dir="auto">Additional context</h2>
<p dir="auto">I am highly appreciated for you guys developing the torch C++ Library. For me, I want to define some CUDA forward/backward layers with Torch C++ Library, such layers will be used in my c++/cuda based torch model. It is to say, I will construct/train/predict my neural networks in .cpp and .cu files totally, instead of using pybind11 and python. So I need to include  &lt;torch/torch.h&gt; in .cu files.<br>
(By now, I can only include libtorch in .cpp files, but it is not enough for me...)</p>
<p dir="auto">Hello, i'm try to build simple project in CMake with libtorch, but <strong>by default</strong> i get some errors.</p>
<p dir="auto">Firstly, my <strong>CMakeLists.txt</strong> (of course, i change path to libtorch):</p>
<div class="highlight highlight-source-cmake notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(torch-start LANGUAGES CXX CUDA)

set(CMAKE_PREFIX_PATH &quot;absolute\path\to\libtorch&quot;)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}&quot;)

add_executable(torch-start torch-start.cpp kernel.cu)
target_link_libraries(torch-start &quot;${TORCH_LIBRARIES}&quot;)
set_property(TARGET torch-start PROPERTY CXX_STANDARD 14)

set_target_properties(torch-start PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

# The following code block is suggested to be used on Windows.
# According to https://github.com/pytorch/pytorch/issues/25457,
# the DLLs need to be copied to avoid memory errors.
if (MSVC)
  file(GLOB TORCH_DLLS &quot;${TORCH_INSTALL_PREFIX}/lib/*.dll&quot;)
  add_custom_command(TARGET torch-start
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $&lt;TARGET_FILE_DIR:torch-start&gt;)
endif (MSVC)"><pre class="notranslate"><span class="pl-c1">cmake_minimum_required</span>(<span class="pl-k">VERSION</span> 3.12 <span class="pl-k">FATAL_ERROR</span>)
<span class="pl-c1">project</span>(torch-start <span class="pl-k">LANGUAGES</span> CXX CUDA)

<span class="pl-c1">set</span>(CMAKE_PREFIX_PATH <span class="pl-s">"absolute<span class="pl-cce">\p</span>ath<span class="pl-cce">\t</span>o<span class="pl-cce">\l</span>ibtorch"</span>)

<span class="pl-c1">find_package</span>(Torch <span class="pl-k">REQUIRED</span>)
<span class="pl-c1">set</span>(CMAKE_CXX_FLAGS <span class="pl-s">"<span class="pl-smi">${CMAKE_CXX_FLAGS}</span> <span class="pl-smi">${TORCH_CXX_FLAGS}</span>"</span>)

<span class="pl-c1">add_executable</span>(torch-start torch-start.cpp kernel.cu)
<span class="pl-c1">target_link_libraries</span>(torch-start <span class="pl-s">"<span class="pl-smi">${TORCH_LIBRARIES}</span>"</span>)
<span class="pl-c1">set_property</span>(<span class="pl-k">TARGET</span> torch-start <span class="pl-k">PROPERTY</span> CXX_STANDARD 14)

<span class="pl-c1">set_target_properties</span>(torch-start <span class="pl-k">PROPERTIES</span> CUDA_SEPARABLE_COMPILATION <span class="pl-k">ON</span>)

<span class="pl-c"><span class="pl-c">#</span> The following code block is suggested to be used on Windows.</span>
<span class="pl-c"><span class="pl-c">#</span> According to https://github.com/pytorch/pytorch/issues/25457,</span>
<span class="pl-c"><span class="pl-c">#</span> the DLLs need to be copied to avoid memory errors.</span>
<span class="pl-k">if</span> (MSVC)
  <span class="pl-c1">file</span>(<span class="pl-k">GLOB</span> TORCH_DLLS <span class="pl-s">"<span class="pl-smi">${TORCH_INSTALL_PREFIX}</span>/lib/*.dll"</span>)
  <span class="pl-c1">add_custom_command</span>(<span class="pl-k">TARGET</span> torch-start
                     <span class="pl-k">POST_BUILD</span>
                     <span class="pl-k">COMMAND</span> <span class="pl-smi">${CMAKE_COMMAND}</span> -E copy_if_different
                     <span class="pl-smi">${TORCH_DLLS}</span>
                     $&lt;TARGET_FILE_DIR:torch-start&gt;)
<span class="pl-k">endif</span> (MSVC)</pre></div>
<hr>
<p dir="auto">So, as you can see, i include two source files in project...</p>
<p dir="auto"><strong>torch-start.cpp</strong>:</p>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

void test(torch::Tensor tensor);

int main() {
  torch::Tensor tensor = torch::rand({2, 3}, torch::device(torch::kCUDA));
  std::cout &lt;&lt; &quot;Input: \n&quot; &lt;&lt; tensor &lt;&lt; std::endl;  
  test(tensor);  
  std::cout &lt;&lt; &quot;Output: \n&quot; &lt;&lt; tensor &lt;&lt; std::endl;
}"><pre class="notranslate">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">&lt;</span>torch/torch.h<span class="pl-pds">&gt;</span></span>
#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">&lt;</span>iostream<span class="pl-pds">&gt;</span></span>

<span class="pl-k">void</span> <span class="pl-en">test</span>(torch::Tensor tensor);

<span class="pl-k">int</span> <span class="pl-en">main</span>() {
  torch::Tensor tensor = <span class="pl-c1">torch::rand</span>({<span class="pl-c1">2</span>, <span class="pl-c1">3</span>}, <span class="pl-c1">torch::device</span>(torch::<span class="pl-c1">kCUDA</span>));
  std::cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>Input: <span class="pl-cce">\n</span><span class="pl-pds">"</span></span> &lt;&lt; tensor &lt;&lt; std::endl;  
  <span class="pl-c1">test</span>(tensor);  
  std::cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>Output: <span class="pl-cce">\n</span><span class="pl-pds">"</span></span> &lt;&lt; tensor &lt;&lt; std::endl;
}</pre></div>
<p dir="auto"><strong>kernel.cu</strong>:</p>
<div class="highlight highlight-source-cuda-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#include &lt;torch/torch.h&gt;

__global__ void kernel(float *tensor) {
    int idx = threadIdx.x;
    tensor[idx] += idx;
}

void test(torch::Tensor tensor) {
    const dim3 threads = tensor.size(0) * tensor.size(1);
    kernel&lt;&lt;&lt;1, threads&gt;&gt;&gt;(tensor.data&lt;float&gt;());
}"><pre class="notranslate">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">&lt;</span>torch/torch.h<span class="pl-pds">&gt;</span></span>

<span class="pl-k">__global__</span> <span class="pl-k">void</span> <span class="pl-en">kernel</span>(<span class="pl-k">float</span> *tensor) {
    <span class="pl-k">int</span> idx = <span class="pl-c1">threadIdx</span>.<span class="pl-smi">x</span>;
    tensor[idx] += idx;
}

<span class="pl-k">void</span> <span class="pl-en">test</span>(torch::Tensor tensor) {
    <span class="pl-k">const</span> <span class="pl-c1">dim3</span> threads = tensor.<span class="pl-c1">size</span>(<span class="pl-c1">0</span>) * tensor.<span class="pl-c1">size</span>(<span class="pl-c1">1</span>);
    kernel<span class="pl-k">&lt;&lt;&lt;<span class="pl-c1">1</span>, threads&gt;&gt;&gt;</span>(tensor.<span class="pl-smi">data</span>&lt;<span class="pl-k">float</span>&gt;());
}</pre></div>
<hr>
<p dir="auto">CMake successfully configure and generate project files. After that, i try to build project...<br>
<code class="notranslate">&gt; cmake --build . --config Release</code><br>
... and get an error:</p>
<blockquote>
<p dir="auto">nvcc fatal   : Unknown option 'openmp'</p>
</blockquote>
<p dir="auto"><strong>Question 1: Why -openmp is unknown option for me and is it really need?</strong></p>
<p dir="auto">I found this option in <code class="notranslate">.\libtorch\share\cmake\Caffe2\Caffe2Targets.cmake</code>, try remove it, build project again and get new error:</p>
<blockquote>
<p dir="auto">nvcc fatal   : A single input file is required for a non-link phase when an output file is specified</p>
</blockquote>
<p dir="auto">Well, i know that NVCC is very sensible to command line syntax. It interpret any non <em>well-forming</em> commands as file name. Anyway, i found problem in property from which i removed <em>-openmp</em> option.<br>
Piece of <code class="notranslate">.\libtorch\share\cmake\Caffe2\Caffe2Targets.cmake</code>:</p>
<div class="highlight highlight-source-cmake notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="set_target_properties(torch PROPERTIES
  INTERFACE_COMPILE_DEFINITIONS &quot;AT_PARALLEL_OPENMP=1&quot;
  #INTERFACE_COMPILE_OPTIONS &quot;/Z7;/EHa;/DNOMINMAX;/wd4267;/wd4251;/wd4522;/wd4522;/wd4838;/wd4305;/wd4244;/wd4190;/wd4101;/wd4996;/wd4275;/bigobj&quot;
  INTERFACE_INCLUDE_DIRECTORIES &quot;C:/Program Files/NVIDIA Corporation/NvToolsExt/include;${_IMPORT_PREFIX}/include;${_IMPORT_PREFIX}/include&quot;
  INTERFACE_LINK_LIBRARIES &quot;protobuf::libprotobuf;c10;Threads::Threads;caffe2::mkl;torch::cudart;c10_cuda;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;caffe2::cufft;caffe2::curand;caffe2::cublas;caffe2::cudnn&quot;
)"><pre class="notranslate"><span class="pl-c1">set_target_properties</span>(torch <span class="pl-k">PROPERTIES</span>
  INTERFACE_COMPILE_DEFINITIONS <span class="pl-s">"AT_PARALLEL_OPENMP=1"</span>
  <span class="pl-c"><span class="pl-c">#</span>INTERFACE_COMPILE_OPTIONS "/Z7;/EHa;/DNOMINMAX;/wd4267;/wd4251;/wd4522;/wd4522;/wd4838;/wd4305;/wd4244;/wd4190;/wd4101;/wd4996;/wd4275;/bigobj"</span>
  INTERFACE_INCLUDE_DIRECTORIES <span class="pl-s">"C:/Program Files/NVIDIA Corporation/NvToolsExt/include;<span class="pl-smi">${_IMPORT_PREFIX}</span>/include;<span class="pl-smi">${_IMPORT_PREFIX}</span>/include"</span>
  INTERFACE_LINK_LIBRARIES <span class="pl-s">"protobuf::libprotobuf;c10;Threads::Threads;caffe2::mkl;torch::cudart;c10_cuda;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;caffe2::cufft;caffe2::curand;caffe2::cublas;caffe2::cudnn"</span>
)</pre></div>
<p dir="auto">If i comment out <code class="notranslate">INTERFACE_COMPILE_OPTIONS</code> property (already did that in code above), CMake successfully build project, so...</p>
<p dir="auto"><strong>Question 2: Where is the problem in syntax of this property?</strong></p>
<hr>
<h3 dir="auto">Environment:</h3>
<ul dir="auto">
<li>Windows 10</li>
<li>VS2019 with compiler 19.24.28316 for x64 architecture</li>
<li>PyTorch 1.4.0</li>
<li>CUDA 10.1.168</li>
<li>cuDNN 7.6.1.34</li>
<li>CMake 3.15.19101501-MSVC_2</li>
<li>NVIDIA GeForce GTX 1060 6GB</li>
</ul>
<hr>
<p dir="auto"><em>Offtop question 3: is there some links where i can find info about reduce size of output files?</em></p>