下载
中文
注册

配套Torch_NPU使用

MindIE Torch配套Torch_NPU使用时,用户需要安装C++或Python版本的Torch_NPU,安装方法请参见《Ascend Extension for PyTorch 配置与安装》中的“安装PyTorch框架”章节

MindIE Torch采用dlopen的方式动态加载Torch_NPU,需要用户手动编译libtorch_npu_bridge.so,编译完成后将libtorch_npu_bridge.so放在libtorch_aie.so同一路径下,或者将其路径设置到LD_LIBRARY_PTAH环境变量中。

具体操作如下:

  1. 准备torch_npu_bridge.cpp、CMakeLists.txt和build.sh文件,并放在同一路径下。
    • torch_npu_bridge.cpp
      #include "torch_npu/csrc/core/npu/NPUStream.h"
      
      extern "C" {
      void* GetCurrentNPUStream(int32_t currDeviceId)
      {
           c10_npu::NPUStream currentStream = c10_npu::getCurrentNPUStream(currDeviceId);
           return static_cast<void*>(currentStream.stream());
      }
      }
    • CMakeLists.txt
      PROJECT(TORCH_NPU_BRIDGE)
      CMAKE_MINIMUM_REQUIRED(VERSION 3.13)
      
      add_compile_options(-fPIE -fstack-protector-all -fPIC -Wall -Wfatal-errors -O2)
      add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
      
      set(TORCH_PATH "torch/path/you/should/set" CACHE STRING "")
      MESSAGE("TORCH_PATH : ${TORCH_PATH}")
      set(TORCH_NPU_PATH "torch_npu/path/you/should/set" CACHE STRING "")
      MESSAGE("TORCH_NPU_PATH : ${TORCH_NPU_PATH}")
      
      add_library(torch_npu_bridge SHARED  torch_npu_bridge.cpp)
      set_property(TARGET torch_npu_bridge PROPERTY CXX_STANDARD 17)
      target_include_directories(torch_npu_bridge PUBLIC ${TORCH_NPU_PATH}/include ${TORCH_PATH}/include)
      target_link_directories(torch_npu_bridge PUBLIC ${TORCH_NPU_PATH}/lib)
      target_link_libraries(torch_npu_bridge PUBLIC torch_npu)
    • build.sh

      TORCH_PATH和TORCH_NPU_PATH需根据环境安装的Torch和Torch_NPU路径自行修改。

      #!/bin/bash
      
      # dependency path you should change
      TORCH_PATH="torch/path/you/should/set"  
      TORCH_NPU_PATH="torch_npu/path/you/should/set"
      
      rm -rf build
      mkdir build
      cd build
      cmake .. -DTORCH_PATH=${TORCH_PATH} -DTORCH_NPU_PATH=${TORCH_NPU_PATH} -DCMAKE_SKIP_BUILD_RPATH=TRUE
      make -j
      chmod 550 libtorch_npu_bridge.so
      cd ..
  2. 编译libtorch_npu_bridge.so文件。

    在CMakeLists.txt所在的路径执行bash build.sh命令,会在当前路径生成build文件夹,在build文件夹中会生成编译好的libtorch_npu_bridge.so文件,并且将该so文件的权限设置为550。

  3. 拷贝libtorch_npu_bridge.so文件或者将其设置到LD_LIBRARY_PTAH环境变量。

    MindIE Torch采用dlopen的方式动态加载Torch_NPU,需要将libtorch_npu_bridge.so拷贝到libtorch_aie.so同一路径下,或者将其路径设置到LD_LIBRARY_PTAH环境变量中,以确保dlopen能够成功加载libtorch_npu_bridge.so。

    # 方式一:将libtorch_npu_bridge.so拷贝到libtorch_aie.so同一路径下
    cp ./build/libtorch_npu_bridge.so /path/of/torch_aie/lib/libtorch_npu_bridge.so
    
    # 方式二:将libtorch_npu_bridge.so所在文件目录路径设置到LD_LIBRARY_PTAH环境变量中
    export LD_LIBRARY_PATH={libtorch_npu_bridge.so所在文件目录路径}:$LD_LIBRARY_PATH

    MindIE Torch配套Torch_NPU使用,可将MindIE Torch暂不支持的算子通过Torch_NPU执行(前提是需要Torch_NPU支持该算子),使用样例可见配套Torch_NPU使用样例

    • 使用Torch_NPU后,异步推理需使用Torch_NPU的Device和Stream相关接口,MindIE Torch的Stream接口将不可用。
    • MindIE Torch采用dlopen的方式动态加载Torch_NPU。
      • 如果使用Python版本MindIE Torch,需要先导入Torch_NPU(import torch_npu),再导入MindIE Torch(import mindietorch)。
      • 如果使用C++版本MindIE Torch,需要将Torch_NPU的C++的libtorch_npu.so文件路径加入到LD_LIBRARY_PATH环境变量中。
    • libtorch_npu_bridge.so文件权限需要保证为550,MindIE Torch在dlopen时会对其权限进行合法性校验。