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

一般来说,从官网安装 pytorch,安装 numpy 不会安装最新的,会有一个校验,不会出现不兼容。

但是,最近在 arm 架构的服务器上,安装 pytorch==2.1.0,用 *.whl 的方式,numpy 自动装了一个最新的 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
posted @ 2024-11-27 22:22  Cold_Chair  阅读(186)  评论(0编辑  收藏  举报