在 Windows 上使用 MKL 编译 NumPy - DLL 加载失败

Compile NumPy with MKL on Windows - DLL load failed

提问人:cubanpit 提问时间:2/11/2021 最后编辑:cubanpit 更新时间:2/16/2021 访问量:978

问:

我一直在尝试使用 MSVC 编译器和 Intel MKL 从 Windows 10 上的源代码编译 NumPy。 我正在运行带有 Microsoft Visual Studio 2019 (16.8.4) 和 Intel MKL 2017.8.275 的 Windows 10.0.18363。

我设法使用最新的 Python 和 NumPy 以最小的设置重现了该问题。

  1. 下载最新的 Python (3.9.1) 和最新的 NumPy (1.20.1) 源代码。

  2. 打开 VS 命令提示符,解压缩 Python 源代码,使用PCbuild\build.bat

  3. 运行以设置正确的环境变量。mklvars.bat intel64

  4. 将英特尔编译器(ifort 需要)添加到 PATH:

    set PATH=C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\bin\intel64;%PATH%
    
  5. 创建一个虚拟环境,从 Python 构建中复制一些文件并激活虚拟环境:

    copy Python\PCbuild\amd64\python39.dll venv\Scripts
    copy Python\PC\pyconfig.h venv\Include
    
  6. 从源代码构建 NumPy 并安装:pip install . -v

  7. 尝试导入 NumPy:python -c "import numpy"

错误信息

Traceback (most recent call last):
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\core\__init__.py", line 22, in <module>
    from . import multiarray
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\core\multiarray.py", line 12, in <module>
    from . import overrides
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed while importing _multiarray_umath: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\__init__.py", line 145, in <module>
    from . import core
  File "C:\cygwin\home\user\numpy_clean_env\venv\lib\site-packages\numpy\core\__init__.py", line 48, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "C:\cygwin\home\user\numpy_clean_env\venv\Scripts\python.exe"
  * The NumPy version is: "1.20.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.

其他资讯

MKL 库在编译期间被拾取,因为它返回:

FOUND:
        libraries = ['mkl_rt']
        library_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries\\windows\\mkl\\lib\\intel64']
        define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
        include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries\\windows\\mkl', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries\\windows\\mkl\\include', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries\\windows\\mkl\\lib']

我试图用Dependencies(较新版本的Dependency Walker)分析DLL解析,但似乎MKL DLL加载正常。有些 DLL 显示为未正确加载,但据我了解,这是由 Windows API 集的检查软件限制引起的,而不是由此 DLL 的实际问题引起的,因此我认为系统设置正确。 以下是在分析时导致依赖项的唯一 DLL 列表:NOT_FOUND_multiarray_umath.pyd

EMCLIENT.dll (NOT_FOUND) :
HvsiFileTrust.dll (NOT_FOUND) :
UpdateAPI.dll (NOT_FOUND) :
api-ms-win-core-comm-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-psapi-ansi-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-psapi-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-stringansi-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-versionansi-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-versionansi-l1-1-1.dll (NOT_FOUND) :
api-ms-win-core-winrt-string-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-wow64-l1-1-0.dll (NOT_FOUND) :
api-ms-win-core-wow64-l1-1-1.dll (NOT_FOUND) :
api-ms-win-core-xstate-l2-1-0.dll (NOT_FOUND) :
api-ms-win-core-xstate-l2-1-1.dll (NOT_FOUND) :
api-ms-win-coreui-secruntime-l1-1-0.dll (NOT_FOUND) :
ext-ms-mf-pal-l2-1-0.dll (NOT_FOUND) :
ext-ms-onecore-appmodel-emclient-l1-1-0.dll (NOT_FOUND) :
ext-ms-onecore-defaultdiscovery-l1-1-0.dll (NOT_FOUND) :
ext-ms-onecore-orientation-l1-1-0.dll (NOT_FOUND) :
ext-ms-onecore-shellchromeapi-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-appmodel-deployment-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-appmodel-usercontext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-appmodel-viewscalefactor-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-audiocore-pal-l1-2-0.dll (NOT_FOUND) :
ext-ms-win-com-suspendresiliency-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-core-winrt-remote-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-dwmapi-ext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-dxcore-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-networking-wlanstorage-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-1.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-2.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-3.dll (NOT_FOUND) :
ext-ms-win-ntuser-window-l1-1-4.dll (NOT_FOUND) :
ext-ms-win-ras-tapi32-l1-1-1.dll (NOT_FOUND) :
ext-ms-win-ro-typeresolution-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-rtcore-minuser-input-l1-1-1.dll (NOT_FOUND) :
ext-ms-win-rtcore-minuser-private-ext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-security-capauthz-ext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-security-chambers-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell-knownfolderext-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell-shell32-l1-2-0.dll (NOT_FOUND) :
ext-ms-win-shell-shlwapi-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell-tabbedtitlebar-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell32-shellcom-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-shell32-shellfolders-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-ui-viewmanagement-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-uiacore-l1-1-0.dll (NOT_FOUND) :
ext-ms-win-wer-xbox-l1-1-0.dll (NOT_FOUND) :
ext-ms-win32-subsystem-query-l1-1-0.dll (NOT_FOUND) :
ext-ms-windowscore-deviceinfo-l1-1-0.dll (NOT_FOUND) :

我还尝试遵循本指南,我可以确认返回,但无法加载 DLL(或其依赖项之一)。os.path.exists('_multiarray_umath.pyd')Truectypes.WinDLL('_multiarray_umath.pyd')

工作实例

与上述步骤相同,但没有执行,因此没有链接到英特尔 MKL。mklvars.bat

过去,此设置适用于 Python 3.6、VS2015 和类似版本的英特尔 MKL。 我可能遗漏了一些东西,任何建议都非常欢迎。

python numpy visual-c++ dll 英特尔-MKL

评论

0赞 CristiFati 2/12/2021
“常规”NumPy 使用的 BLAS 库有什么问题?是否存在所有(递归)所需的 .dll(并为正确的体系结构构建)?我注意到一些 Cygwin 路径,您是否在混合工具链?同样在曾经工作的设置中,版本是否相同?
0赞 cubanpit 2/12/2021
@CristiFati 英特尔 MKL 是我的速度和稳定性的要求。64 位的 DLL 在预期路径中可用,它们在依赖项中显示为正确链接(已列出且路径存在)。Cygwin 路径在那里只是因为“非最小”设置在 Cygwin 中,但在这种情况下,我只是加载 VS2019 x64 命令提示符。有效的设置具有某些工具的不同版本(Python 3.6.2 > 3.9.1、VS2015 > VS2019);较新的设置在 NumPy 1.16、1.17 和 1.20 以及 Intel MKL 2017.2、2017.8 和 2021 中以同样的方式失败。mkl_rt
0赞 CristiFati 2/12/2021
可能想看看这个:lfd.uci.edu/~gohlke/pythonlibs/#numpy
0赞 cubanpit 2/12/2021
感谢您的链接,我可以尝试预编译版本,但我使用 MKL 构建其他组件,它们都必须一起交付。我认为两次运送 MKL 需要太多空间,所以我需要检查这样做是否可行。
0赞 cubanpit 2/12/2021
@CristiFati我还注意到该链接上有一组非常有限的(Python 版本、NumPy 版本)组合,这可能会给构建系统的其余部分带来进一步的问题,从源代码编译它提供了更大的灵活性。

答:

2赞 cubanpit 2/16/2021 #1

通过遵循 Numpy 邮件列表(永久链接)上的建议来解决。

Python >= 3.8 将不再使用 PATH 来解析 扩展模块。全部使用 os.add_dll_directory(mkl_bin_path) https://docs.python.org/3/library/os.html#os.add_dll_directory 导入 numpy 或添加对 numpy 的调用之前的脚本 _distributor_init.py numpy package 目录中的文件。

评论

0赞 CristiFati 2/18/2021
嗯,就是这样?我想知道我怎么错过了它:stackoverflow.com/questions/58631512/pywin32-and-python-3-8-0