时间:2022-06-18 12:38:47 | 栏目:C代码 | 点击:次
以下内容默认cuda已经安装完成并添加至系统环境变量
在官网下载压缩包, 可以选择Release版或者Debug版(根据自己需要):
下载完成之后选择安装软件的位置进行解压
VS新建空项目
2.1添加包含目录:
D:\soft\libtorch\libtorch\include
D:\soft\libtorch\libtorch\include\torch\csrc\api\include
2.2添加库目录:
D:\soft\libtorch\libtorch\lib
PATH=D:\soft\libtorch\libtorch\lib;%PATH%
D:\soft\libtorch\libtorch\lib\*.lib
4.2链接器--Command Line
/INCLUDE:?warp_size@cuda@at@@YAHXZ /INCLUDE:?_torch_cuda_cu_linker_symbol_op_cuda@native@at@@YA?AVTensor@2@AEBV32@@Z
#include<torch/torch.h> #include<torch/script.h> #include<iostream> int main() { std::cout << "cuda::is_available():" << torch::cuda::is_available() << std::endl; std::cout << "torch::cuda::cudnn_is_available():" << torch::cuda::cudnn_is_available() << std::endl; std::cout << "torch::cuda::device_count():" << torch::cuda::device_count() << std::endl; torch::Device device(torch::kCUDA); torch::Tensor tensor1 = torch::eye(3); // (A) tensor-cpu torch::Tensor tensor2 = torch::eye(3, device); // (B) tensor-cuda std::cout << tensor1 << std::endl; std::cout << tensor2 << std::endl; }
正常结果输出如下:
Libtorch + vs 2019安装及配置_开拓者5号的博客-CSDN博客_vs2019配置libtorch
Win10+libtorch+CUDA+vs2017_大智若鱼.AI的博客-CSDN博客
win10系统上LibTorch的安装和使用(cuda10.1版本)_*匿名*的博客-CSDN博客_cuda libtorch
c10::NotImplementedError with minimal example - C++ - PyTorch Forums