M1芯片Mac安装Transformers遇jaxlib AVX不兼容问题求助
M1 Mac安装Transformers后JAX/AVX兼容性问题解决
问题详情
- 按HuggingFace安装指南完成Transformers部署后,运行测试命令:
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"
- 触发报错:
RuntimeError: This version of jaxlib was built using AVX instructions, which your CPU and/or operating system do not support. You may be able work around this issue by building jaxlib from source.
- 尝试重新安装jax/jaxlib无效:
pip uninstall jax jaxlib pip install --upgrade jax jaxlib
- 参考JAX文档安装依赖后,执行编译命令时出现文件不存在错误:
pip install numpy wheel build
python build/build.py pip install dist/*.whl # installs jaxlib (includes XLA)
python: can't open file '/Users/e5028514/build/build.py': [Errno 2] No such file or directory
- 设备环境:M1芯片Mac

解决方案
方案1:安装Apple Silicon适配的JAX预编译包
M1 Mac为ARM64架构,默认pip源的jaxlib多为x86架构版本(含AVX指令),需安装适配ARM的版本:
# 先卸载现有版本 pip uninstall -y jax jaxlib # 安装适配Apple Silicon的预编译包 pip install jax jaxlib==0.4.20 -f https://storage.googleapis.com/jax-releases/jaxlib_releases.html
或使用conda安装(自动适配架构,更省心):
conda uninstall -y jax jaxlib conda install -c conda-forge jax jaxlib
方案2:指定Transformers使用PyTorch/TensorFlow后端
若无需JAX相关功能,可直接指定pipeline使用PyTorch后端,绕过JAX依赖:
from transformers import pipeline # 明确指定PyTorch框架,使用兼容的情感分析模型 print(pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english', framework='pt')('we love you'))
源码编译补充说明
之前编译报错是因为未克隆JAX源码仓库,完整源码编译步骤如下(仅推荐有编译经验的用户尝试):
# 克隆JAX仓库 git clone https://github.com/google/jax.git cd jax # 安装编译依赖 pip install numpy wheel build # 执行编译 python build/build.py # 安装编译好的包 pip install dist/*.whl
内容的提问来源于stack exchange,提问作者m45ha
相关产品推荐
相关产品推荐

