ubuntu20.04 cuda11.4 cudnn8.2 编译、使用libtorch1.8 1.9

本文讲述了在新环境中编译libtorch遇到的问题,包括去掉CAFFE2选项、处理GPU不兼容错误、链接库问题,以及解决dlopen undefined reference。通过详细步骤和代码实例,帮助读者顺利部署GPU版本的libtorch。
摘要由CSDN通过智能技术生成

一不小心环境弄得比较新,坑比较多。

主要编译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})
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值