导出Llama 3.2 1B SpinQuant模型到Executorch时遭遇ImportError:无法从torch.export导入default_decompositions
我现在在M1 MacBook Air(16G内存,系统是macOS Sequoia 15.1)的Anaconda环境里,尝试用Executorch在iPhone上部署Llama 3.2 1B SpinQuant模型,但遇到了这个错误:
ImportError: cannot import name 'default_decompositions' from 'torch.export' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/export/__init__.py)
操作背景与步骤
我是按照Executorch的XNNPACK部署指南操作的,前面的环境搭建步骤都顺利完成:
- 创建并激活Conda环境:
conda create -n et_xnnpack python=3.10.0 conda activate et_xnnpack
- 克隆Executorch仓库并同步子模块:
git clone https://github.com/pytorch/executorch.git cd executorch git submodule sync git submodule update --init
- 安装依赖:
./install_requirements.sh
问题出在执行模型导出命令时,我用的是Meta发布的预量化INT4 SpinQuant Llama 3.2模型,导出命令如下:
python -m examples.models.llama.export_llama \ --model "llama3_2" \ --checkpoint "/Users/questionone/.llama/checkpoints/Llama3.2-1B-Instruct-int4-spinquant-eo8/consolidated.00.pth" \ --params "/Users/questionone/.llama/checkpoints/Llama3.2-1B-Instruct-int4-spinquant-eo8/params.json" \ -kv \ --use_sdpa_with_kv_cache \ -X \ -d fp32 \ --xnnpack-extended-ops \ --preq_mode 8da4w_output_8da8w \ --preq_group_size 32 \ --max_seq_length 2048 \ --preq_embedding_quantize 8,0 \ --use_spin_quant native \ --metadata '{"get_bos_id":128000, "get_eos_ids":[128009, 128001]}' \ --output_name "llama3_2_spinquant.pte"
完整报错栈:
(et_xnnpack) questionone@curious-MacBookAir executorch % python -m examples.models.llama.export_llama \ --model "llama3_2" \ --checkpoint "/Users/questionone/.llama/checkpoints/Llama3.2-1B-Instruct-int4-spinquant-eo8/consolidated.00.pth" \ --params "/Users/questionone/.llama/checkpoints/Llama3.2-1B-Instruct-int4-spinquant-eo8/params.json" \ -kv \ --use_sdpa_with_kv_cache \ -X \ -d fp32 \ --xnnpack-extended-ops \ --preq_mode 8da4w_output_8da8w \ --preq_group_size 32 \ --max_seq_length 2048 \ --preq_embedding_quantize 8,0 \ --use_spin_quant native \ --metadata '{"get_bos_id":128000, "get_eos_ids":[128009, 128001]}' \ --output_name "llama3_2_spinquant.pte" Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "/Users/questionone/executorch/examples/models/llama/export_llama.py", line 14, in <module> from .export_llama_lib import build_args_parser, export_llama File "/Users/questionone/executorch/examples/models/llama/export_llama_lib.py", line 25, in <module> from executorch.devtools.etrecord import generate_etrecord File "/Users/questionone/executorch/devtools/__init__.py", line 7, in <module> import executorch.devtools.inspector as inspector File "/Users/questionone/executorch/devtools/inspector/__init__.py", line 9, in <module> from executorch.devtools.inspector._inspector import ( File "/Users/questionone/executorch/devtools/inspector/_inspector.py", line 31, in <module> import executorch.devtools.etdump.schema_flatcc as flatcc File "/Users/questionone/executorch/devtools/etdump/schema_flatcc.py", line 18, in <module> from executorch.exir.scalar_type import ScalarType File "/Users/questionone/executorch/exir/__init__.py", line 9, in <module> from executorch.exir.capture import ( File "/Users/questionone/executorch/exir/capture/__init__.py", line 9, in <module> from executorch.exir.capture._capture import ( File "/Users/questionone/executorch/exir/capture/_capture.py", line 15, in <module> from executorch.exir.capture._config import CaptureConfig File "/Users/questionone/executorch/exir/capture/_config.py", line 15, in <module> from executorch.exir.passes import MemoryPlanningPass, ToOutVarPass File "/Users/questionone/executorch/exir/passes/__init__.py", line 19, in <module> from executorch.exir import control_flow, memory, memory_planning File "/Users/questionone/executorch/exir/control_flow.py", line 58, in <module> from executorch.exir.tracer import ( File "/Users/questionone/executorch/exir/tracer.py", line 50, in <module> from torch.export import default_decompositions ImportError: cannot import name 'default_decompositions' from 'torch.export' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/export/__init__.py)
我已经确认et_xnnpack环境里安装了Executorch和PyTorch,甚至用export PYTHONPATH=/Users/questionone/executorch重置了路径,但错误还是存在。以下是我的环境包列表:
(et_xnnpack) questionone@curious-MacBookAir executorch % conda list -n et_xnnpack # packages in environment at /opt/anaconda3/envs/et_xnnpack: # # Name Version Build Channel annotated-types 0.7.0 pypi_0 pypi anyio 4.7.0 pypi_0 pypi attrs 24.3.0 pypi_0 pypi blas 1.0 openblas blobfile 3.0.0 pypi_0 pypi bzip2 1.0.8 h80987f9_6 ca-certificates 2024.11.26 hca03da5_0 certifi 2024.12.14 pypi_0 pypi charset-normalizer 3.4.0 pypi_0 pypi click 8.1.8 pypi_0 pypi cmake 3.31.2 pypi_0 pypi distro 1.9.0 pypi_0 pypi exceptiongroup 1.2.2 pypi_0 pypi execnet 2.1.1 pypi_0 pypi executorch 0.5.0a0+82763a9 pypi_0 pypi expecttest 0.3.0 pypi_0 pypi filelock 3.16.1 pypi_0 pypi fire 0.7.0 pypi_0 pypi flatbuffers 24.3.25 pypi_0 pypi fsspec 2024.12.0 pypi_0 pypi gmp 6.2.1 hc377ac9_3 gmpy2 2.1.2 py310h8c48613_0 h11 0.14.0 pypi_0 pypi httpcore 1.0.7 pypi_0 pypi httpx 0.28.1 pypi_0 pypi huggingface-hub 0.27.0 pypi_0 pypi hypothesis 6.123.0 pypi_0 pypi idna 3.10 pypi_0 pypi iniconfig 2.0.0 pypi_0 pypi jinja2 3.1.5 pypi_0 pypi libabseil 20240116.2 cxx17_h313beb8_0 libcxx 14.0.6 h848a8c0_0 libffi 3.4.4 hca03da5_1 libgfortran 5.0.0 11_3_0_hca03da5_28 libgfortran5 11.3.0 h009349e_28 libopenblas 0.3.21 h269037a_0 libprotobuf 4.25.3 h514c7bf_0 llama-models 0.0.63 pypi_0 pypi llama-stack 0.0.63 pypi_0 pypi llama-stack-client 0.0.63 pypi_0 pypi llvm-openmp 14.0.6 hc6e5704_0 lxml 5.3.0 pypi_0 pypi markdown-it-py 3.0.0 pypi_0 pypi markupsafe 3.0.2 pypi_0 pypi mdurl 0.1.2 pypi_0 pypi mpc 1.1.0 h8c48613_1 mpfr 4.0.2 h695f6f0_1 mpmath 1.3.0 py310hca03da5_0 ncurses 6.4 h313beb8_0 networkx 3.4.2 pypi_0 pypi numpy 1.21.3 pypi_0 pypi numpy-base 1.26.4 py310ha9811e2_0 openssl 3.0.15 h80987f9_0 packaging 24.2 pypi_0 pypi pandas 2.0.3 pypi_0 pypi parameterized 0.9.0 pypi_0 pypi pillow 11.0.0 pypi_0 pypi pip 24.2 py310hca03da5_0 pluggy 1.5.0 pypi_0 pypi prompt-toolkit 3.0.48 pypi_0 pypi pyaml 24.12.1 pypi_0 pypi pycryptodomex 3.21.0 pypi_0 pypi
问题分析与解决方法
看起来这个问题的核心是PyTorch版本与Executorch版本不兼容,加上Conda环境隔离失效导致的:
1. 环境隔离问题
你创建的是Python 3.10的Conda环境,但报错里的Torch路径指向了系统全局的Python 3.11目录——这说明你的Conda环境没有被正确激活,系统全局的Python被优先调用了,导致环境内的Torch没有被使用。
2. API版本兼容问题
torch.export.default_decompositions这个API的位置在PyTorch版本中是有变化的:
- PyTorch 2.1及更早版本:不存在这个API,或者放在其他模块;
- PyTorch 2.2~2.4版本:这个API存在于
torch.export模块下; - PyTorch 2.5+版本:该API可能被移除或重命名。
而你环境里的Executorch是0.5.0a0版本,需要搭配PyTorch 2.3~2.4版本才能正常兼容。
具体解决步骤
步骤1:彻底重置Conda环境
# 退出当前环境 conda deactivate # 删除旧环境 conda remove -n et_xnnpack --all -y # 重新创建干净的Python 3.10环境 conda create -n et_xnnpack python=3.10.0 -y conda activate et_xnnpack # 验证Python路径是否为环境内的(输出应为/opt/anaconda3/envs/et_xnnpack/bin/python) which python
步骤2:安装匹配版本的PyTorch和Executorch
# 安装PyTorch 2.3.1(和Executorch 0.5.0兼容的稳定版本) pip install torch==2.3.1 torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu # 进入executorch目录,以开发模式安装本地包 cd executorch pip install -e . # 重新安装依赖 ./install_requirements.sh
步骤3:验证API可用性
激活环境后,进入Python交互模式测试:
from torch.export import default_decompositions print(default_decompositions)
如果没有报错,说明环境已经正常,此时再重新运行模型导出命令即可。
额外注意事项
- 不要手动设置
PYTHONPATH为全局的executorch路径,pip install -e .已经将本地包链接到环境中,手动设置会导致路径混乱; - 所有命令必须在激活
et_xnnpack环境后执行,避免调用系统全局的Python。
备注:内容来源于stack exchange,提问作者swimcode

