Windows 安装和使用libtorch
1.下载libtorch
libtorch的下载链接,如下图所示,libtorch有release和debug版本可以选择。为了方便调试,下debug版。电脑上没CUDA,下次有需要再更新吧。
2.libtorch使用
在Visual Studio的使用可以参考博客[1],本人使用Clion,下面是Clion是配置方式。
- 创建新项目,目录如图,打开CmakeList
- 按照官方文档,填写CMakeLists.txt,其中E:/libtorch是我的解压路径,改为自己的就好。
set(CMAKE_PREFIX_PATH E:/libtorch) #set(Torch_DIR "E:/libtorch/share/cmake/Torch") #include_directories("E:/libtorch/include") #include_directories("E:/libtorch/include/torch/csrc/api/include") find_package(Torch REQUIRED ) add_executable(Cpp_Libtorch_CRN main.cpp) target_link_libraries(Cpp_Libtorch_CRN "${TORCH_LIBRARIES}") set_property(TARGET Cpp_Libtorch_CRN PROPERTY CXX_STANDARD 14) if (MSVC) file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll") add_custom_command(TARGET Cpp_Libtorch_CRN POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TORCH_DLLS} $<TARGET_FILE_DIR:Cpp_Libtorch_CRN>) endif (MSVC)
3. 错误与解决
错误1
讲道理到这里就可以了,但是报错了,错误信息如下。找了好多博客文档,要么在unix上配置,要么在vs上配置。在windows-clion上的没找到。这里报了个莫名的错误,在知乎找到一篇同样错误的,多久了也不放后续,麻了。(不放解决方法,就单纯为了记录错误?记录意义是什么呢?吐槽一下)
"E:\CLion 2022.2.4\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=E:/CLion 2022.2.4/bin/ninja/win/ninja.exe" -G Ninja -S C:\Users\65181\CLionProjects\Cpp_Libtorch_CRN -B C:\Users\65181\CLionProjects\Cpp_Libtorch_CRN\cmake-build-debug
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: E:/CLion 2022.2.4/bin/mingw/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: E:/CLion 2022.2.4/bin/mingw/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Found Torch: E:/libtorch/lib/torch.lib
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/65181/CLionProjects/Cpp_Libtorch_CRN/cmake-build-debug
Problems were encountered while collecting compiler information:
cc1plus.exe: fatal error: /Z7: No such file or directory
cc1plus.exe: fatal error: /EHsc: No such file or directory
cc1plus.exe: fatal error: /DNOMINMAX: No such file or directory
cc1plus.exe: fatal error: /wd4267: No such file or directory
cc1plus.exe: fatal error: /wd4251: No such file or directory
cc1plus.exe: fatal error: /wd4522: No such file or directory
cc1plus.exe: fatal error: /wd4838: No such file or directory
cc1plus.exe: fatal error: /wd4305: No such file or directory
cc1plus.exe: fatal error: /wd4244: No such file or directory
cc1plus.exe: fatal error: /wd4190: No such file or directory
cc1plus.exe: fatal error: /wd4101: No such file or directory
cc1plus.exe: fatal error: /wd4996: No such file or directory
cc1plus.exe: fatal error: /wd4275: No such file or directory
cc1plus.exe: fatal error: /bigobj: No such file or directory
[Finished]
解决方法:
咨询了一下用过的同学,他之前在mac上配的,但是也是和我一样用的mingw64,说似乎是libtorch不支持wingw?具体也不清楚。于是还是没逃过下载vs的命运(可以单独下msvc但是麻烦)。
下载之后在Setting->Build,Execution,Deployment->ToolChains里面添加编译器,选择Visual Studio,正常安装好VS之后会自动查找地址,不行就自己指定一下。
错误2
====================[ Build | Cpp_Libtorch_CRN | Debug ]========================
"E:\CLion 2022.2.4\bin\cmake\win\bin\cmake.exe" --build C:\Users\65181\CLionProjects\Cpp_Libtorch_CRN\cmake-build-debug --target Cpp_Libtorch_CRN -j 6
[1/2] Building CXX object CMakeFiles\Cpp_Libtorch_CRN.dir\main.cpp.obj
FAILED: CMakeFiles/Cpp_Libtorch_CRN.dir/main.cpp.obj
C:\PROGRA~1\MICROS~2\2022\PROFES~1\VC\Tools\MSVC\1434~1.319\bin\Hostx86\x86\cl.exe /nologo /TP -DUSE_C10D_GLOO -DUSE_DISTRIBUTED -external:IE:\libtorch\include -external:IE:\libtorch\include\torch\csrc\api\include -external:W0 /DWIN32 /D_WINDOWS /EHsc /Zi /Ob0 /Od /RTC1 -MDd /Z7 /EHsc /DNOMINMAX /wd4267 /wd4251 /wd4522 /wd4838 /wd4305 /wd4244 /wd4190 /wd4101 /wd4996 /wd4275 /bigobj /showIncludes /FoCMakeFiles\Cpp_Libtorch_CRN.dir\main.cpp.obj /FdCMakeFiles\Cpp_Libtorch_CRN.dir\ /FS -c C:\Users\65181\CLionProjects\Cpp_Libtorch_CRN\main.cpp
cl: 命令行 warning D9025 :正在重写“/Zi”(用“/Z7”)
E:\libtorch\include\c10/util/safe_numerics.h(25): error C3861: “_addcarry_u64”: 找不到标识符
ninja: build stopped: subcommand failed.
解决方法
似乎是这个函数在32位里是没有的,所以要编译64位的,还是按这个图中Architecture改为64就可以了。
4. 测试样例
写一个官方的测试样例,到这里就可以用了。后面不知道还会出什么幺蛾子,先用着到时候再说了
#include <torch/torch.h>
#include <iostream>
int main() {
std::cout << 1 << std::endl;
torch::Tensor a = torch::rand({2, 3});
std::cout << a << std::endl;
}
==>
1
0.3635 0.2281 0.2088
0.2662 0.0971 0.0297
[ CPUFloatType{2,3} ]
Process finished with exit code 0
参考链接
[1] Libtorch的介绍与使用方法