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_PATH环境变量中。
具体操作如下:
1 2 3 4 5 6 7 8 9 | #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()); } } |
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)
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 ..
在CMakeLists.txt所在的路径执行bash build.sh命令,会在当前路径生成build文件夹,在build文件夹中会生成编译好的libtorch_npu_bridge.so文件,并且将该so文件的权限设置为550。
MindIE Torch采用dlopen的方式动态加载Torch_NPU,需要将libtorch_npu_bridge.so所在文件目录路径设置到LD_LIBRARY_PATH环境变量中,以确保dlopen能够成功加载libtorch_npu_bridge.so。
# 将libtorch_npu_bridge.so所在文件目录路径设置到LD_LIBRARY_PATH环境变量中 export LD_LIBRARY_PATH={libtorch_npu_bridge.so所在文件目录路径}:$LD_LIBRARY_PATH
MindIE Torch配套Torch_NPU使用,可将MindIE Torch暂不支持的算子通过Torch_NPU执行(前提是需要Torch_NPU支持该算子),使用样例可见配套Torch_NPU使用样例。