Sympy autowrap (Cython) 失败,并出现“致命错误 LNK1104:无法打开文件 'build\lib.win-amd64-3.8\wrapper_module_0.cp38-win_amd64.pyd”

问题描述 投票:0回答:3

系统:Windows 10
Python:3.8
同情:1.8

我有一个很大的 Sympy Matrix

regressor
,我需要将其转换为 C 代码(以加快以后使用该矩阵的计算速度)。我正在使用 Sympy 的 autowrap 来执行此操作:

 self.cy = autowrap(self.regressor, backend='cython', args=tuple(self._get_state()), verbose=True, tempdir=self.__cython_path)

这已经工作了很长一段时间,直到我对矩阵做了一些改变(这可能增加了它的大小)。当我现在运行代码时,我收到以下错误消息,但我不知道如何解决此问题:

Traceback (most recent call last):
  File "C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\sympy\core\cache.py", line 72, in wrapper
    retval = cfunc(*args, **kwargs)
TypeError: unhashable type: 'MutableDenseMatrix'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\sympy\utilities\autowrap.py", line 167, in _process_files
    retoutput = check_output(command, stderr=STDOUT)
  File "C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\subprocess.py", line 411, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\subprocess.py", line 512, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['C:\\Users\\Jonas\\anaconda3\\envs\\robot-parameter-identification\\python.exe', 'setup.py', 'build_ext', '--inplace']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Jonas/Repo/robot-parameter-identification/optimize_trajectory.py", line 39, in <module>
    robot = dynamics.robot_constructor(r, identity)
  File "C:\Users\Jonas\Repo\robot-parameter-identification\identification\dynamics.py", line 473, in robot_constructor
    robot = Robot(robot_path, cython_path, parameters)
  File "C:\Users\Jonas\Repo\robot-parameter-identification\identification\dynamics.py", line 448, in __init__
    self.__get_regressor_objects(cython_path)
  File "C:\Users\Jonas\Repo\robot-parameter-identification\identification\dynamics.py", line 422, in __get_regressor_objects
    self.reduced_regressor_for_id = Reduced_Regressor(os.path.join(cython_path, 'id'), full_regressor, state,
  File "C:\Users\Jonas\Repo\robot-parameter-identification\identification\dynamics.py", line 394, in __init__
    super().__init__(cython=True, cython_path=cython_path)
  File "C:\Users\Jonas\Repo\robot-parameter-identification\identification\dynamics.py", line 335, in __init__
    self._get_cy()
  File "C:\Users\Jonas\Repo\robot-parameter-identification\identification\dynamics.py", line 325, in _get_cy
    self.cy = autowrap(self.regressor, backend='cython', args=tuple(self._get_state()), verbose=True,
  File "C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\sympy\core\cache.py", line 74, in wrapper
    retval = func(*args, **kwargs)
  File "C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\sympy\utilities\autowrap.py", line 646, in autowrap
    return code_wrapper.wrap_code(routine, helpers=helps)
  File "C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\sympy\utilities\autowrap.py", line 148, in wrap_code
    self._process_files(routine)
  File "C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\sympy\utilities\autowrap.py", line 169, in _process_files
    raise CodeWrapError(
sympy.utilities.autowrap.CodeWrapError: Error while executing command: C:\Users\JonasStiegler\anaconda3\envs\robot-parameter-identification\python.exe setup.py build_ext --inplace. Command output is:
Compiling wrapper_module_0.pyx because it changed.
[1/1] Cythonizing wrapper_module_0.pyx
C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\Cython\Compiler\Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\Users\Jonas\Repo\robot-parameter-identification\AR1440_bugfix\regressor_files\id\wrapper_module_0.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'wrapper_module_0' extension
creating build
creating build\temp.win-amd64-3.8
creating build\temp.win-amd64-3.8\Release
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\numpy\core\include -IC:\Users\Jonas\anaconda3\envs\robot-parameter-identification\include -IC:\Users\Jonas\anaconda3\envs\robot-parameter-identification\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /Tcwrapper_module_0.c /Fobuild\temp.win-amd64-3.8\Release\wrapper_module_0.obj -std=c99
cl : Command line warning D9002 : ignoring unknown option '-std=c99'
wrapper_module_0.c
C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\numpy\core\include\numpy\npy_1_7_deprecated_api.h(14) : Warning Msg: Using deprecated NumPy API, disable it with #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\Jonas\anaconda3\envs\robot-parameter-identification\lib\site-packages\numpy\core\include -IC:\Users\Jonas\anaconda3\envs\robot-parameter-identification\include -IC:\Users\Jonas\anaconda3\envs\robot-parameter-identification\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /Tcwrapped_code_0.c /Fobuild\temp.win-amd64-3.8\Release\wrapped_code_0.obj -std=c99
cl : Command line warning D9002 : ignoring unknown option '-std=c99'
wrapped_code_0.c
creating C:\Users\Jonas\Repo\robot-parameter-identification\AR1440_bugfix\regressor_files\id\build\lib.win-amd64-3.8
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\libs /LIBPATH:C:\Users\Jonas\anaconda3\envs\robot-parameter-identification\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64" /EXPORT:PyInit_wrapper_module_0 build\temp.win-amd64-3.8\Release\wrapper_module_0.obj build\temp.win-amd64-3.8\Release\wrapped_code_0.obj /OUT:build\lib.win-amd64-3.8\wrapper_module_0.cp38-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.8\Release\wrapper_module_0.cp38-win_amd64.lib
   Creating library build\temp.win-amd64-3.8\Release\wrapper_module_0.cp38-win_amd64.lib and object build\temp.win-amd64-3.8\Release\wrapper_module_0.cp38-win_amd64.exp
Generating code
LINK : the 32-bit linker (C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\bin\HostX86\x64\link.exe) ran out of heap space and is going to restart linking with a 64-bit linker
LINK : restarting link with 64-bit linker `C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64\link.exe'
LINK : fatal error LNK1104: cannot open file 'build\lib.win-amd64-3.8\wrapper_module_0.cp38-win_amd64.pyd'
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.28.29910\\bin\\HostX86\\x64\\link.exe' failed with exit status 1104

预先感谢您的所有提示。如果缺少任何需要的信息,如果有人询问我会添加。

python windows visual-studio cython sympy
3个回答
1
投票

在@DavidW的帮助下,我找到了解决这个问题的方法,尽管它需要修改Sympy的源代码。

背景

Cython 总是首先尝试使用 32 位链接器。当它耗尽堆时,它将使用 64 位链接器重新启动转换。这里的问题是 64 位链接器无法访问

.pyd
文件,很可能是因为它已经加载,或者可能仍然由 32 位链接器打开,正如@ead 指出的那样。
通过修改如下代码,可以强制 Cython 从一开始就使用 64 位链接器,从而避免此问题。

解决方案

  1. 打开 Sympy 包中的 autowrap.py 文件
  2. 前往
    class CythonCodeWrapper
    (第218行)
  3. 转到类声明正下方的
    setup_template
  4. 在字符串的最开头添加以下两行:
from distutils import _msvccompiler
_msvccompiler.PLAT_TO_VCVARS['win-amd64'] = 'amd64'

源代码现在应该如下所示:

class CythonCodeWrapper(CodeWrapper):
    """Wrapper that uses Cython"""

    setup_template = """\
from distutils import _msvccompiler
_msvccompiler.PLAT_TO_VCVARS['win-amd64'] = 'amd64'  
try:
    from setuptools import setup
    from setuptools import Extension
...

0
投票

您也可以删除 pyd 文件并重新编译代码


0
投票

是的,我还收到错误“link.exe”失败,退出代码为 1104。文件 foo.pyx 和 foo.c 位于共享上。路相当长。确实太长了,因为当我将这些文件复制到文件夹 C: emp 并在那里运行命令“cythonize -a -i foo.pyx”时,错误没有再次发生,并且构建完成而没有问题:“完成生成代码”和“复制 build\lib.win-amd64-3.8 oo.cp38-win_amd64.pyd”。

© www.soinside.com 2019 - 2024. All rights reserved.