• Pytorch源码编译Libtorch


    创建虚拟环境

    conda create -n build-libtorch python=3.8
    cd build-libtorch
    
    • 1
    • 2

    安装相关依赖:

    conda install astunparse numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses
    
    • 1

    下载 Pytorch,可通过 HTTPSSSH 方式进行下载:

    git clone https://github.com/pytorch/pytorch.git  # HTTPS
    git clone git@github.com:pytorch/pytorch.git  # SSH
    
    • 1
    • 2

    克隆下来的是最新版本,可根据需要切换不同版本:

    cd pytorch
    git tag  # 查看标签
    git checkout v1.4.0  # 根据标签切换版本
    git submodule sync  # 根据父仓库中的配置,将子模块的URL进行更新,以保证与父仓库中记录的URL一致。这样,当执行git submodule update命令时,Git就能正确地从指定的URL下载子模块的更新或特定版本的代码
    git submodule update --init --recursive  # 初始化和更新父仓库中的子模块,并递归地初始化和更新所有嵌套的子模块
    
    • 1
    • 2
    • 3
    • 4
    • 5

    如果不能通过 HTTPS 方式更新子模块,可以通过 sudo gedit .gitmodules 命令打开 .gitmodules 文件,该文件中记录了子模块的存储路径和下载地址。打开文件后修改子模块的 url,例如将 url = https://github.com/pybind/pybind11.git 修改为 url = git@github.com:pybind/pybind11.git。所有子模块的 url 都修改好后,依次执行 git submodule syncgit submodule update --init --recursive

    也可一步到位进行下载:

    git clone -b v1.4.0 https://github.com/pytorch/pytorch.git
    git clone -b v1.4.0 git@github.com:pytorch/pytorch.git
    
    • 1
    • 2

    可根据需要设置编译选项:

    export USE_CUDA=False
    export BUILD_TEST=False
    export USE_NINJA=OFF
    
    • 1
    • 2
    • 3

    执行编译:

    方式一:
    # 在pytorch目录下
    mkdir build_libtorch && cd build_libtorch
    python ../tools/build_libtorch.py
    
    方式二:
    # 在pytorch的父目录下
    mkdir pytorch-build && cd pytorch-build
    cmake -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_BUILD_TYPE:STRING=Release -DPYTHON_EXECUTABLE:PATH=`which python3` -DCMAKE_INSTALL_PREFIX:PATH=../pytorch-install ../pytorch
    cmake --build . --target install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    新建 libtorch 目录,将 pytorch/torch/include 目录复制到 libtorch 目录下,将 build/lib 目录复制到 libtorch 目录下。

    可能遇到的问题:

    1. 在执行 cmake 命令时,运行到 Performing Test COMPILER_SUPPORTS_LONG_DOUBLE 这一步就停止不动了,长时间没有反应,这很可能是 GCC 版本导致的问题,可尝试更换 GCC 版本来解决,更换的版本可以比之前更高,也可以更低,可以多尝试
    2. 出现 Could not run a simple program built with your compiler. If you are trying to use -fsanitize=address, make sure libasan is properly installed on your system (you can confirm if the problem is this by attempting to build and run a small program.) 问题,可尝试在 pytorch 目录下的 CMakeLists.txt 中设置 set(INTERN_BUILD_MOBILE ON) 来解决
  • 相关阅读:
    基因组大小查询(二)|基因组组装结果查询
    253 - Cube painting (UVA)
    ES6中数组新增了哪些扩展?
    lnmp环境部署
    虚拟摄像头之八: 从 Camera api2 角度看摄像头框架
    如何利用 MidJourney 进行 AI 艺术创作(详细教程)
    【进程管理】认识系统调用函数fork
    centos 7.7之后,无法远程ssh执行 java -jar 指令
    QMudiaPlayer(状态)
    如何使用Python和Numpy实现简单的2D FDTD仿真:详细指南与完整代码示例
  • 原文地址:https://blog.csdn.net/weixin_48158964/article/details/133128106