Mac上OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.……问题解决

1,120 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第30天,点击查看活动详情

问题描述

今天在我的MacBook Pro M1上使用jupyter lab进行模型训练时,突然发现加载transformers库或者加载torch库会在jupyter lab下导致如下的报错:

使用的代码如下:

from transformers import pipeline 

报错显示"jupyter lab kernerl appears to have died. It will restart automatically. "

我以为是jupyter lab出了问题,于是就升级了最新版的jupyter lab,没想到jupyter lab在终端中直接无法启动。报错信息为:“AttributeError: 'ExtensionManager' object has no attribute '_extensions' ”

问题解决

jupyter lab的修复

经过网上查找一系列资料,发现jupyter lab无法启动是由于重装JupyterLab时conda或pip默认安装版本较低的nbclassic,所以我们只需要在安装完jupytet lab之后,手动将nbclass进行版本升级即可,也即

pip install jupyterlab
pip install nbclassic -U

此时,再重新启动jupyterlab就可以正常显示网页端了。

使用Pytorch或Transformers时的报错解决

在解决完jupyter lab的报错后,为了验证原问题(即使用Pytorch或Transformers时的报错)的出现与jupyter lab无关,我于是在命令行下启动python,在python的终端中对Transformers进行导入测试,还是同样的from transformers import pipeline一行代码却报错了OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.……。经过网上查找结合报错信息后发现,这个问题是由于程序试图将OpenMP运行时的多个副本链接到程序中导致的。

解决方法一:允许OpenMP运行时的副本程序连接

既然问题是由于副本程序连接被禁止所导致,那么一个最简单的方式是允许连接,所以可以使用

import os

os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

两行代码来实现这一功能。或者可以直接在终端中将其设置为环境变量,对所有程序生效,即:

export KMP_DUPLICATE_LIB_OK=TRUE

解决方法二:升级numpy版本

由于之前的OMP报错时,报错信息还有如下提示,

OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results.

即如果允许多程序副本连接将会导致能导致崩溃或无提示地产生错误的结果。因而解决方法一只能是个临时的方案。继续再往上搜索后,我发现了一个更好的解决方式,即升级numpy版本。

直接简单

pip install -U numpy

即可将numpy升级到最新版本,然而我在导入Transformer或Pytorch时,会提示由于numpy版本太高,和scipy当前版本不兼容,这时我已经没有动力再去折腾scipy的版本了,鬼知道还会出现什么问题,于是我就按提升将numpy进行适当的版本降级,由1.23.2降级为1.21.0,一切恢复正常。

问题原因

本次错误来的很突然,因为我刚刚程序还一直运行的很好,仔细回想后发现问题的来源是我更新了transformers库的版本到了最新导致。所以建议如果没有特殊要求,库的版本不要乱升级,容易导致各种各样的错误,十分麻烦。还是那样,程序能正常运行就行,不苛求完美。

参考

  1. Q: Can't start up Jupyter Lab on MacOS
  2. 解决OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib already initialized. blog.csdn.net/qq_43211132…