Build Libtorch from Source Code for x86

在学习资料满天飞的大环境下,知识变得非常零散,体系化的知识并不多,这就导致很多人每天都努力学习到感动自己,最终却收效甚微,甚至放弃学习。我的使命就是过滤掉大量的无效信息,将知识体系化,以短平快的方式直达问题本质,把大家从大海捞针的痛苦中解脱出来。

1 下载源码

git clone --recursive https://github.com/pytorch/pytorch.git

如何想切换需要编译的源码版本,可以使用下述指令修改,否则默认是最新版本。

git tag #查看支持的版本号
git checkout <chosen tag>
git submodule sync
git submodule update --init --recursive

2 编译

安装依赖库。建议使用conda安装,如果没有conda环境也可以使用pip install。

conda install pyyaml

编译:

export USE_CUDA=False
export BUILD_TEST=False
export USE_NINJA=OFF
cd pytorch
mkdir build_x86 && cd build_x86
python3 ../tools/build_libtorch.py

编译成功后输出结果如下:

lInstall the project...
-- Install configuration: "Release"
-- Set runtime path of "<your_path>/pytorch/torch/bin/protoc-3.13.0.0" to "$ORIGIN/../lib"
-- Set runtime path of "<your_path>/pytorch/torch/lib/libc10.so" to "$ORIGIN"
-- Set runtime path of "<your_path>/pytorch/torch/lib/libtorch_cpu.so" to "$ORIGIN:/usr/lib/x86_64-linux-gnu/openmpi/lib"
-- Set runtime path of "<your_path>/pytorch/torch/lib/libtorch.so" to "$ORIGIN"
-- Set runtime path of "<your_path>/pytorch/torch/lib/libtorch_global_deps.so" to "$ORIGIN:/usr/lib/x86_64-linux-gnu/openmpi/lib:<your_path>/miniconda3/lib"

其中,我们最关注的是下面三个共享库:libc10.so、libtorch.so、libtorch_cpu.so。

3 验证

3.1 准备库和头文件

3.2 源码

/* main.cpp */
#include <iostream>
#include "torch/torch.h"
using namespace std;

int main() {   
    torch::Tensor val_1 = torch::rand({2,3});
    torch::Tensor val_2 = torch::full_like(val_1,3);
    
    cout<<val_1<<endl<<endl;
    cout<<val_2<<endl;

    return 0;
}

3.3 CMakeLists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(main)
SET(CMAKE_BUILD_TYPE "Release")

include_directories(<test_root>libtorch/include)
include_directories(<test_root>libtorch/include/torch/csrc/api/include)

link_directories("<test_root>/libtorch")
add_executable(main main.cpp)
set(TORCH_LIBRARIES c10 torch torch_cpu)
target_link_libraries(main "${TORCH_LIBRARIES}")

set_property(TARGET main PROPERTY CXX_STANDARD 14)

3.4 验证结果

看到类似结果,说明验证成功。

 0.5017  0.0609  0.4708
 0.3303  0.8318  0.5621
[ CPUFloatType{2,3} ]

 3  3  3
 3  3  3
[ CPUFloatType{2,3} ]

恭喜你又坚持看完了一篇博客,又进步了一点点!如果感觉还不错就点个赞再走吧,你的点赞和关注将是我持续输出的哒哒哒动力~~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C:\Users\张豪>pip install matplotlib-3.7.2-cp310-cp310-win32.whl Processing c:\users\张豪\matplotlib-3.7.2-cp310-cp310-win32.whl Collecting contourpy>=1.0.1 (from matplotlib==3.7.2) Using cached contourpy-1.1.0.tar.gz (13.4 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [12 lines of output] + meson setup C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957 C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --vsenv --native-file=C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build\meson-python-native-file.ini The Meson build system Version: 1.2.0 Source dir: C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957 Build dir: C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build Build type: native build Project name: contourpy Project version: 1.1.0 ..\..\meson.build:1:0: ERROR: Could not find C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe A full log can be found at C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build\meson-logs\meson-log.txt [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
07-23
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穿越临界点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值