Issue 1:
Numerous Warnings and Errors occur when including LibTorch in .cu files. I think maybe some compiling settings are wrong...

One of error is as following.

/home/libtorch/include/torch/csrc/api/include/torch/nn/cloneable.h:68:61: error: invalid static_cast from type ‘const torch::OrderedDict<std::__cxx11::basic_string<char>, std::shared_ptr<torch::nn::Module> >’ to type ‘torch::OrderedDict<std::__cxx11::basic_string<char>, std::shared_ptr<torch::nn::Module> >&’

Most of errors are similar as this one.

To Reproduce

Easily to reproduce, just set all things as following and compile the cmake project

files

src
|-- test.cu
|-- CMakeLists.txt

test.cu

#include <torch/torch.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <vector>

CMakeLists.txt

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})

make command

cmake ../src/ -DCMAKE_PREFIX_PATH=/path/to/libtorch && make

Expected behavior

<torch/torch.h> 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.

Environment

PyTorch Version: LibTorch 1.4.0

OS: Ubuntu 16.04.6 LTS

How you installed PyTorch: Source

Python version: 3.7

CUDA/cuDNN version: 10.1

GPU models and configuration: 2x Titan V

GCC version: 5.5.0

CMake version: version 3.14.7

Additional context

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 <torch/torch.h> in .cu files.

(By now, I can only include libtorch in .cpp files, but it is not enough for me...)

Issue 2:
Hello, i'm try to build simple project in CMake with libtorch, but by default i get some errors.

Firstly, my CMakeLists.txt (of course, i change path to libtorch):

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(torch-start LANGUAGES CXX CUDA)

set(CMAKE_PREFIX_PATH "absolute\path\to\libtorch")

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(torch-start torch-start.cpp kernel.cu)
target_link_libraries(torch-start "${TORCH_LIBRARIES}")
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 "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET torch-start
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:torch-start>)
endif (MSVC)

So, as you can see, i include two source files in project...

torch-start.cpp:

#include <torch/torch.h>
#include <iostream>

void test(torch::Tensor tensor);

int main() {
torch::Tensor tensor = torch::rand({2, 3}, torch::device(torch::kCUDA));
std::cout << "Input: \n" << tensor << std::endl;
test(tensor);
std::cout << "Output: \n" << tensor << std::endl;
}

kernel.cu:

#include <torch/torch.h>

__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<<<1, threads>>>(tensor.data<float>());
}

CMake successfully configure and generate project files. After that, i try to build project...

> cmake --build . --config Release

... and get an error:

nvcc fatal : Unknown option 'openmp'

Question 1: Why -openmp is unknown option for me and is it really need?

I found this option in .\libtorch\share\cmake\Caffe2\Caffe2Targets.cmake, try remove it, build project again and get new error:

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

Well, i know that NVCC is very sensible to command line syntax. It interpret any non well-forming commands as file name. Anyway, i found problem in property from which i removed -openmp option.

Piece of .\libtorch\share\cmake\Caffe2\Caffe2Targets.cmake:

set_target_properties(torch PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "AT_PARALLEL_OPENMP=1"
#INTERFACE_COMPILE_OPTIONS "/Z7;/EHa;/DNOMINMAX;/wd4267;/wd4251;/wd4522;/wd4522;/wd4838;/wd4305;/wd4244;/wd4190;/wd4101;/wd4996;/wd4275;/bigobj"
INTERFACE_INCLUDE_DIRECTORIES "C:/Program Files/NVIDIA Corporation/NvToolsExt/include;${_IMPORT_PREFIX}/include;${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "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"
)

If i comment out INTERFACE_COMPILE_OPTIONS property (already did that in code above), CMake successfully build project, so...

Question 2: Where is the problem in syntax of this property?

Environment:

Windows 10

VS2019 with compiler 19.24.28316 for x64 architecture

PyTorch 1.4.0

CUDA 10.1.168

cuDNN 7.6.1.34

CMake 3.15.19101501-MSVC_2

NVIDIA GeForce GTX 1060 6GB

Offtop question 3: is there some links where i can find info about reduce size of output files?