microsoft / DirectML

DirectML is a high-performance, hardware-accelerated DirectX 12 library for machine learning. DirectML provides GPU acceleration for common machine learning tasks across a broad range of supported hardware and drivers, including all DirectX 12-capable GPUs from vendors such as AMD, Intel, NVIDIA, and Qualcomm.
MIT License
2.17k stars 288 forks source link

Compatibility issues with NumPy 2.0.0 #608

Open GuangChen2333 opened 2 months ago

GuangChen2333 commented 2 months ago

When I run the code

import torch_directml

device = torch_directml.device()

print(device)

I got the error msg:

 A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0 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 "D:\JetBrains\PyCharm Professional\plugins\python\helpers\pydev\pydevd.py", line 2252, in <module>
    main()
  File "D:\JetBrains\PyCharm Professional\plugins\python\helpers\pydev\pydevd.py", line 2234, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "D:\JetBrains\PyCharm Professional\plugins\python\helpers\pydev\pydevd.py", line 1544, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "D:\JetBrains\PyCharm Professional\plugins\python\helpers\pydev\pydevd.py", line 1551, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:\JetBrains\PyCharm Professional\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:\Projects\---\main.py", line 1, in <module>
    import torch_directml
  File "E:\Projects\---\.venv\Lib\site-packages\torch_directml\__init__.py", line 94, in <module>
    torch._register_device_module('privateuseone', PrivateUse1Module)
  File "E:\Projects\---\.venv\Lib\site-packages\torch\__init__.py", line 1908, in _register_device_module
    device_type = torch.device(device_type).type
E:\Projects\---\.venv\Lib\site-packages\torch\__init__.py:1908: UserWarning: Failed to initialize NumPy: _ARRAY_API not found (Triggered internally at ..\torch\csrc\utils\tensor_numpy.cpp:84.)
  device_type = torch.device(device_type).type

Enviroments:

GongHuilin-DL commented 1 month ago

I got the same error too. My system environment is:

  1. system: Windows 11 专业工作站版
  2. cpu:AMD Ryzen 9 7940HS
  3. python 3.10.13
  4. torch 2.3.1
  5. numpy 2.0.1
VadimShabashov commented 1 month ago

As a temporary workaround, it helped me to downgrade numpy:

pip install numpy==1.26.4
GuangChen2333 commented 1 month ago

As a temporary workaround, it helped me to downgrade numpy:

pip install numpy==1.26.4

Yes it works. It needs to limit the version or try to become compatible with numpy 2

joshjkim commented 3 weeks ago

We're tracking an issue where the latest Windows PyTorch builds are built against numpy<2 https://github.com/pytorch/pytorch/issues/131668. This issue should be fixed in the upcoming PyTorch 2.4.1 release which we plan to add support for