源码编译PyTorch和torch_npu,import报错
收藏回复举报
源码编译PyTorch和torch_npu,import报错
t('forum.solved') 已解决
新人帖
发表于2024-12-12 14:05:49
0 查看

环境

  • ubuntu: 22.04
  • python: 3.8
  • cann: 8.0.RC2
  • pytorch: 2.1.0(源码编译)
  • torch_npu: v2.1.0-6.0.rc2(源码编译)

问题描述

按照pytorch官方仓库中的方式源码编译CPU版本的pytorch,可以正常import。此时pip show torch是自己编译的torch版本

然后按照torch_npu官方仓库中的方式源码编译torch_npu v2.1.0-6.0.rc2分支,使用官方提供的docker成功构建得到2.1.0.post7版本的安装包(torch_npu-2.1.0.post7+gitunknown-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl)。pip安装后发现其卸载了环境中原有的自编译torch,重新在conda目录下安装了2.1.0版本的torch,使用该torch版本时,可以正常import torch和import torch_npu

由于我需要修改pytorch源码,我必须使用自编译的torch。我执行pip uninstall torch,然后重新在安装了源码编译的torch,此时环境如下(即torch和torch_npu都是自编译版本)

torch                         2.1.0a0+git7bcf7da     /data/workspace/pytorch
torch-npu                     2.1.0.post7+gitunknown

随后,import torch可以成功,在import torch_npu时,出现以下异常

>>> import torch_npu
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/chentong/.conda/envs/py3.8_torch2.1.0_custom/lib/python3.8/site-packages/torch_npu/__init__.py", line 16, in <module>
    import torch_npu.npu
  File "/home/chentong/.conda/envs/py3.8_torch2.1.0_custom/lib/python3.8/site-packages/torch_npu/npu/__init__.py", line 119, in <module>
    from torch_npu.utils._error_code import ErrCode, pta_error, prof_error
  File "/home/chentong/.conda/envs/py3.8_torch2.1.0_custom/lib/python3.8/site-packages/torch_npu/utils/__init__.py", line 1, in <module>
    from ._module import _apply_module_patch
  File "/home/chentong/.conda/envs/py3.8_torch2.1.0_custom/lib/python3.8/site-packages/torch_npu/utils/_module.py", line 26, in <module>
    from torch_npu.npu.amp.autocast_mode import autocast
  File "/home/chentong/.conda/envs/py3.8_torch2.1.0_custom/lib/python3.8/site-packages/torch_npu/npu/amp/__init__.py", line 7, in <module>
    from .sharded_grad_scaler import ShardedGradScaler
  File "/home/chentong/.conda/envs/py3.8_torch2.1.0_custom/lib/python3.8/site-packages/torch_npu/npu/amp/sharded_grad_scaler.py", line 9, in <module>
    from torch_npu.npu.utils import npu_check_overflow
  File "/home/chentong/.conda/envs/py3.8_torch2.1.0_custom/lib/python3.8/site-packages/torch_npu/npu/utils.py", line 11, in <module>
    import torch_npu._C
ImportError: /home/chentong/.conda/envs/py3.8_torch2.1.0_custom/lib/python3.8/site-packages/torch_npu/lib/libtorch_npu.so: undefined symbol: _ZNK5torch8autograd4Node4nameEv

网上搜索发现该问题一般由torch_npu跟cann的版本不匹配引起,但是我使用的torch, torch_npu跟cann版本是匹配的

后来我尝试使用torch_npu仓库release的2.1.0.post6的whl包,但是仍然出现上述问题,即安装时会自动替换环境中已有的torch,然后我手动卸载torch并安装自编译版本后,import torch_npu时出现上述异常

我要发帖子