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

macOS系统下无法导入transformers.models.bert.modeling_tf_bert求助

解决从transformers导入BertTokenizer/TFBertModel时的RuntimeError问题

问题重现

执行以下代码时触发RuntimeError:

from transformers import BertTokenizer, TFBertModel

tokenizer = BertTokenizer.from_pretrained(BERT_PATH)
model = TFBertModel.from_pretrained(BERT_PATH)
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
resp = model(encoded_input)
print(resp)

错误信息:

RuntimeError: Failed to import transformers.models.bert.modeling_tf_bert because of the following error (look up to see its traceback):
dlopen(/Users/tk/miniforge3/envs/QA-benchmark/lib/python3.10/site-packages/tensorflow-plugins/libmetal_plugin.dylib, 0x0006): symbol not found in flat namespace '_TF_GetInputPropertiesList'

环境信息

  • OS: macOS Ventura 13.3.1
  • Python: 3.10
  • TensorFlow: macOS-tensorflow 2.9.0
  • Transformers: 4.28.0
  • BERT模型: uncased_L-12_H-768_A-12

解决办法

这个错误是macOS上TensorFlow Metal插件与TensorFlow版本不匹配导致的,试试以下方案:

1. 适配TensorFlow 2.9的Metal插件版本

卸载当前的tensorflow-plugins,安装对应版本的tensorflow-metal:

pip uninstall -y tensorflow-plugins
pip install tensorflow-metal==0.5.1

2. 临时禁用Metal插件(使用CPU运行)

如果不需要GPU加速,直接卸载tensorflow-plugins即可:

pip uninstall -y tensorflow-plugins

3. 升级TensorFlow到兼容版本

升级TensorFlow和对应的Metal插件到更稳定的组合,比如:

pip install tensorflow-macos==2.10 tensorflow-metal==0.6.0

升级后验证transformers兼容性(4.28.0版本与tf2.10兼容)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 21:37:10