libtorch
libtorch是pytorch的c++库,提供了用于深度学习和张量计算的功能,允许开发者在c++环境中使用pytorch的核心功能。特别是当一些pt模型无法转换到ncnn、mnn等模型时(ncnn、mnn可能还不支持某些层),可以在libtorch直接使用pt模型,避免了模型框架转换出错的问题。以下是libtorch的一些主要特点和功能:
主要特点
-
高性能:
libtorch提供了高效的张量操作和深度学习模型训练,能够充分利用cpu和gpu的计算能力 -
灵活性:
libtorch允许开发者在c++中构建和训练深度学习模型,适合需要高性能和低延迟的应用场景,如嵌入式系统和实时应用 -
与pytorch兼容:
libtorch与pytorch的python api兼容,用户可以在python中训练模型,然后将其导出为 torchscript格式,在c++中加载和运行 -
支持自动求导:
libtorch支持自动求导功能,使得模型训练过程中的梯度计算变得简单高效 -
丰富的功能:
提供了多种神经网络层、优化器、损失函数等,支持构建复杂的深度学习模型
主要组件
- 张量(Tensor):libtorch提供了多维数组(张量)的数据结构,支持各种数学操作
- 自动求导(Autograd):支持自动计算梯度,简化了反向传播的实现
- 神经网络模块(torch::nn):提供了构建神经网络的基础组件
- 优化器(torch::optim):提供多种优化算法,如SGD、Adam等
使用场景
- 高性能计算:适用于需要高性能计算的应用,如图像处理和实时推理
- 嵌入式系统:可以在资源受限的设备上运行深度学习模型
- c++应用:对于已有c++项目,libtorch提供了一种将深度学习功能集成到现有代码中的方式
下载libtorch
cpu版本
download.pytorch.org/libtorch/cpu
gpu版本
libtorch-cuda12.1
libtorch-cuda11.8
libtorch-cuda11.3
libtorch-cuda10.1
Windows配置libtorch
这里以libtorch-win-shared-with-deps-2.2.2+cpu.zip为例,
1、下载libtorch并解压
2、在vs2019新建一个空项目
3、在项目-属性-配置属性-VC++目录的包含目录中添加“D:\libtorch-win-shared-with-deps-2.2.2+cpu\libtorch\include\torch\csrc\api\include”和“D:\libtorch-win-shared-with-deps-2.2.2+cpu\libtorch\include”
4、在项目-属性-配置属性-VC++目录的库目录中添加“D:\libtorch-win-shared-with-deps-2.2.2+cpu\libtorch\lib”
5、在项目-属性-配置属性-链接器-输入的附加依赖项中添加“c10.lib”、“torch.lib”、“torch_cpu.lib”。如果是gpu版本的libtorch,还需要添加如“torch_cuda.lib”这类的库,以及在项目-属性-配置属性-链接器—命令行—其他选项—添加下面两行代码(主要作用是确保在编译和链接过程中,特定的 CUDA 相关函数和变量能够被正确找到和使用,从而避免运行时错误),
/INCLUDE:?searchsorted_cuda@native@at@@YA?AVTensor@2@AEBV32@0_N1@Z
/INCLUDE:?warp_size@cuda@at@@YAHXZ
6、简单写个代码测试能否正常使用libtorch,
#include <torch/torch.h>
#include <iostream>
int main() {
torch::manual_seed(0);
auto tensor = torch::rand({2, 3});
std::cout << "随机生成的张量:\n" << tensor << std::endl;
auto tensor2 = torch::rand({2, 3});
auto sum_tensor = tensor + tensor2;
std::cout << "张量加法结果:\n" << sum_tensor << std::endl;
auto mat1 = torch::rand({2, 2});
auto mat2 = torch::rand({2, 2});
auto result = torch::mm(mat1, mat2);
std::cout << "矩阵乘法结果:\n" << result << std::endl;
return 0;
}
Linux配置libtorch
linux系统的配置则比较简单,这里以libtorch-shared-with-deps-2.2.2+cpu.zip为例,
1、下载并解压
2、编写CMakeLists.txt,如,
cmake_minimum_required(VERSION 3.16)
project(LibTorch_Demo)
set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_BUILD_TYPE RELEASE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE -fPIC ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fPIC -fpermissive")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(OpenMP REQUIRED)
# libtorch
set(Torch_DIR libtorch-shared-with-deps-2.2.2+cpu/libtorch/share/cmake/Torch/)
find_package(Torch REQUIRED)
add_executable(LibTorch_Demo main.cpp)
target_link_libraries(LibTorch_Demo "${TORCH_LIBRARIES}")
其中,set(Torch_DIR libtorch-shared-with-deps-2.2.2+cpu/libtorch/share/cmake/Torch/) 的设置是为了指定libtorch的CMake配置文件所在的目录。这个目录通常包含了CMake所需的配置文件和模块。这些文件定义了如何找到libtorch的库、头文件以及其他相关的设置
3、简单写个代码进行测试,
#include <torch/torch.h>
#include <iostream>
int main() {
torch::manual_seed(0);
auto tensor = torch::rand({2, 3});
std::cout << "随机生成的张量:\n" << tensor << std::endl;
auto tensor2 = torch::rand({2, 3});
auto sum_tensor = tensor + tensor2;
std::cout << "张量加法结果:\n" << sum_tensor << std::endl;
auto mat1 = torch::rand({2, 2});
auto mat2 = torch::rand({2, 2});
auto result = torch::mm(mat1, mat2);
std::cout << "矩阵乘法结果:\n" << result << std::endl;
return 0;
}
4、在CMakeLists.txt所在目录中,打开命令行,输入,
mkdir build
cd build
cmake ..
make
./LibTorchDemo
执行完以上指令后,会得到类似如下的结果,
随机生成的张量:
0.1234 0.5678 0.9012
0.3456 0.7890 0.1234
[ CPUFloatType{2,3} ]
张量加法结果:
0.4567 1.2345 1.2345
1.2345 1.5678 0.5678
[ CPUFloatType{2,3} ]
矩阵乘法结果:
0.9876 1.2345
1.2345 0.9876
[ CPUFloatType{2,2} ]