Mac下PyMC 4.4.0模型编译时如何设置-fbracket-depth参数?
解决PyMC 4.4.0在Mac上编译模型时的括号嵌套级别超限问题
在Mac上训练PyMC 4.4.0版本的复杂模型时,编译阶段出现如下报错:
Traceback (most recent call last): File "/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/vm.py", line 1246, in make_all node.op.make_thunk(node, storage_map, compute_map, [], impl=impl) File "/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/c/op.py", line 131, in make_thunk return self.make_c_thunk(node, storage_map, compute_map, no_recycling) File "/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/c/op.py", line 96, in make_c_thunk outputs = cl.make_thunk( File "/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/c/basic.py", line 1202, in make_thunk cthunk, module, in_storage, out_storage, error_storage = self.__compile__( File "/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/c/basic.py", line 1122, in __compile__ thunk, module = self.cthunk_factory( File "/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/c/basic.py", line 1647, in cthunk_factory module = cache.module_from_key(key=key, lnk=self) File "/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/c/cmodule.py", line 1229, in module_from_key module = lnk.compile_cmodule(location) File "/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/c/basic.py", line 1546, in compile_cmodule module = c_compiler.compile_str( File "/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/c/cmodule.py", line 2640, in compile_str raise CompileError( aesara.link.c.exceptions.CompileError: Compilation failed (return status=1): /usr/bin/clang++ -dynamiclib -g -O3 -fno-math-errno -Wno-unused-label -Wno-unused-variable -Wno-write-strings -Wno-c++11-narrowing -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION -m64 -fPIC -undefined dynamic_lookup -I/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/numpy/core/include -I/opt/miniconda3/envs/ic_labor/include/python3.9 -I/opt/miniconda3/envs/ic_labor/lib/python3.9/site-packages/aesara/link/c/c_code -L/opt/miniconda3/envs/ic_labor/lib -fvisibility=hidden -o /Users/jisom/.aesara/compiledir_macOS-14.6.1-x86_64-i386-64bit-i386-3.9.19-64/tmpen3swcx7/m0a2528786de976e08afd4eedf68e78061251bf4ba46bb89dfee1a310f6da39eb.so /Users/jisom/.aesara/compiledir_macOS-14.6.1-x86_64-i386-64bit-i386-3.9.19-64/tmpen3swcx7/mod.cpp /Users/jisom/.aesara/compiledir_macOS-14.6.1-x86_64-i386-64bit-i386-3.9.19-64/tmpen3swcx7/mod.cpp:31954:32: fatal error: bracket nesting level exceeded maximum of 256 if (!PyErr_Occurred()) { ^ /Users/jisom/.aesara/compiledir_macOS-14.6.1-x86_64-i386-64bit-i386-3.9.19-64/tmpen3swcx7/mod.cpp:31954:32: note: use -fbracket-depth=N to increase maximum nesting level 1 error generated.
错误提示建议使用-fbracket-depth=N提升最大嵌套级别,尝试过以下三种方法但均未解决:
- 在
.zshrc中添加环境变量:export CFLAGS="-fbracket-depth=1024" export CXXFLAGS="-fbracket-depth=1024" - 在Python代码编译模型前设置环境变量:
import os os.environ['CFLAGS'] = "-fbracket-depth=1024" os.environ['CXXFLAGS'] = "-fbracket-depth=1024" - 在Python代码中直接修改Aesara的编译器路径:
import aesara aesara.config.cxx = "/usr/bin/clang++ -fbracket-depth=1024"
正确解决方法
方法1:代码中临时添加编译参数
通过Aesara的cxxflags配置项添加额外编译参数,而非替换整个编译器命令:
import aesara # 将括号深度参数追加到现有编译选项后 aesara.config.cxxflags += " -fbracket-depth=1024"
该配置会自动将参数附加到默认的clang++编译命令中,避免命令格式错误。
方法2:永久配置(无需每次修改代码)
在用户目录下创建或修改.aesara/aesara.cfg文件,添加以下内容:
[compiler] cxxflags = -fbracket-depth=1024
保存后,每次运行PyMC时都会自动应用该参数。
设置完成后重新编译模型,编译命令会包含-fbracket-depth=1024,即可解决括号嵌套级别超限的问题。
内容的提问来源于stack exchange,提问作者Jed
相关产品推荐
相关产品推荐

