You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac下TF与Transformers引发Fatal Python错误:Illegal instruction

Mac下Python 3.9+TensorFlow 2.14.0+Transformers 4.33.2非法指令错误解决

在Mac系统使用Python 3.9、TensorFlow 2.14.0和Transformers 4.33.2时,持续触发Fatal Python error: Illegal instruction错误,报错与TensorFlow和Transformers相关模块有关,错误日志片段如下:

Fatal Python error: Illegal instruction.

Main thread:
Current thread 0x0000000202868280 (most recent call first):
  File "<frozen importlib._bootstrap>", line 228 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1173 in create_module
  File "<frozen importlib._bootstrap>", line 565 in module_from_spec
  File "<frozen importlib._bootstrap>", line 666 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 986 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1007 in _find_and_load
  File "<frozen importlib._bootstrap>", line 228 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1058 in _handle_fromlist
(Then it leads to specific files that produce these messages such as transformers/image_transforms.py)

已尝试sys.settrace调试、提高递归限制(sys.setrecursionlimit(100000)),但问题未解决,内核仍会崩溃重启。

解决方法

  • 检查CPU指令集兼容性:终端执行sysctl -n machdep.cpu.features,查看是否支持AVX2、FMA等高级指令集。如果不支持,说明当前版本的TensorFlow/Transformers预编译包依赖这些指令集,导致非法指令错误。
  • 降级适配版本:将TensorFlow降级到2.10.x(最后一批兼容老CPU的官方预编译版本),Transformers降级到4.28.x左右,确保两者版本兼容且适配旧CPU指令集。执行以下命令:
    pip install tensorflow==2.10.0 transformers==4.28.1
    
  • 从源码编译对应库:若不想降级,可针对自身CPU编译TensorFlow和Transformers,生成兼容当前指令集的二进制文件:
    • TensorFlow:需配置Bazel环境,按照官方源码编译流程指定对应CPU架构编译。
    • Transformers:直接从Git源码安装,编译时自动适配本地CPU:
      pip install git+https://github.com/huggingface/transformers.git
      
  • 临时禁用高级指令集优化:终端设置环境变量后再运行代码,尝试绕过指令集问题:
    export TF_ENABLE_ONEDNN_OPTS=0
    python your_script.py
    

内容的提问来源于stack exchange,提问作者fluxrep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 10:25:32