低版本 pytorch 和 numpy 2 版本不相容

Cold_Chair發表於2024-11-27

一般來說,從官網安裝 pytorch,安裝 numpy 不會安裝最新的,會有一個校驗,不會出現不相容。

但是,最近在 arm 架構的伺服器上,安裝 pytorch==2.1.0,用 *.whl 的方式,num 自動裝了一個最新的 2.1.3,然後就報錯如下:

$ python
Python 3.10.15 (main, Oct  3 2024, 07:21:53) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.1.3 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "<stdin>", line 1, in <module>
  File "/home/bingxing2/ailab/scx7kzd/.conda/envs/dh/lib/python3.10/site-packages/torch/__init__.py", line 1382, in <module>
    from .functional import *  # noqa: F403
  File "/home/bingxing2/ailab/scx7kzd/.conda/envs/dh/lib/python3.10/site-packages/torch/functional.py", line 7, in <module>
    import torch.nn.functional as F
  File "/home/bingxing2/ailab/scx7kzd/.conda/envs/dh/lib/python3.10/site-packages/torch/nn/__init__.py", line 1, in <module>
    from .modules import *  # noqa: F403
  File "/home/bingxing2/ailab/scx7kzd/.conda/envs/dh/lib/python3.10/site-packages/torch/nn/modules/__init__.py", line 35, in <module>
    from .transformer import TransformerEncoder, TransformerDecoder, \
  File "/home/bingxing2/ailab/scx7kzd/.conda/envs/dh/lib/python3.10/site-packages/torch/nn/modules/transformer.py", line 20, in <module>
    device: torch.device = torch.device(torch._C._get_default_device()),  # torch.device('cpu'),
/home/bingxing2/ailab/scx7kzd/.conda/envs/dh/lib/python3.10/site-packages/torch/nn/modules/transformer.py:20: UserWarning: Failed to initialize NumPy: _ARRAY_API not found (Triggered internally at /home/bingxing2/home/scx6001/luym/pytorch/torch/csrc/utils/tensor_numpy.cpp:84.)
  device: torch.device = torch.device(torch._C._get_default_device()),  # torch.device('cpu'),

此時請手動降級 numpy 到 1

pip install numpy==1.26.0

相關文章