一不小心环境弄得比较新,坑比较多。
主要编译libtorch。
gcc 9.3.0
g++ 9.3.0
主要使用4个库
libc10.so
lib/libc10_cuda.so
libtorch_cpu.so
libtorch_cuda.so
主要问题:
一、需要注意的一个地方在CMakeList.txt 去掉BUILD_CAFFE2的选项,使用官方的命令,可以编译通过。
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"} python setup.py build --cmake-only ccmake build # or cmake-gui build
二、使用的时候CPU没问题,GPU出现:
判断可用设备为0
terminate called after throwing an instance of 'c10::NotImplementedError'
what(): 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 https://fburl.com/ptmfixes for possible resolutions. 'aten::empty_strided' is only available for these backends: [CPU, Meta, BackendSelect, Named, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, UNKNOWN_TENSOR_TYPE_ID, AutogradMLC, AutogradHPU, AutogradNestedTensor, AutogradPrivateUse1, AutogradPrivateUse2, AutogradPrivateUse3, Tracer, Autocast, Batched, VmapMode].
解决方法
在使用GPU之前加入这一行代码,可以正常使用
try {
void *handle = dlopen("/usr/local/libtorch1.9/lib/libtorch_cuda.so",RTLD_LAZY);
std::cout << torch::cuda::is_available() << std::endl;
torch::Tensor tensor = at::tensor({ -1, 1 }, at::kCUDA);
}catch (exception& ex) {
std::cout << ex.what() << std::endl;
}
三、/usr/bin/ld: CMakeFiles/example-app.dir/example-app.o: undefined reference to symbol 'dlopen@@GLIBC_2.2.5'
/usr/bin/ld: /lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/example-app.dir/build.make:102:example-app] 错误 1
make[1]: *** [CMakeFiles/Makefile2:76:CMakeFiles/example-app.dir/all] 错误 2
make: *** [Makefile:84:all] 错误 2
在CMakeList.txt 中加入
target_link_libraries(example-app "/usr/local/libtorch1.9/lib/libc10.so" ${CMAKE_DL_LIBS})
target_link_libraries(example-app "/usr/local/libtorch1.9/lib/libc10_cuda.so" ${CMAKE_DL_LIBS})
target_link_libraries(example-app "/usr/local/libtorch1.9/lib/libtorch_cpu.so" ${CMAKE_DL_LIBS})
target_link_libraries(example-app "/usr/local/libtorch1.9/lib/libtorch_cuda.so" ${CMAKE_DL_LIBS})