PyTorch 2.0.0+Transformers 4.41.0出现AttributeError问题求助
解决AttributeError: module 'torch.utils._pytree' has no attribute 'register_pytree_node'问题
问题详情
- 当前使用版本:PyTorch 2.0.0、Transformers 4.41.0
- 触发错误:
AttributeError: module 'torch.utils._pytree' has no attribute 'register_pytree_node' - 已尝试操作:执行
pip install transformers==4.28.0降级Transformers,但问题未解决 - 完整报错堆栈:
Traceback (most recent call last): File "/Users/UrbanArchitect/main.py", line 17, in from utils.clip import CLIPScore File "/Users/UrbanArchitect/utils/clip.py", line 2, in from transformers import CLIPProcessor, CLIPModel File "/Users/anaconda3/envs/Newurban/lib/python3.9/site-packages/transformers/__init__.py", line 26, in from . import dependency_versions_check File "/Users/anaconda3/envs/Newurban/lib/python3.9/site-packages/transformers/dependency_versions_check.py", line 16, in from .utils.versions import require_version, require_version_core File "/Users/anaconda3/envs/Newurban/lib/python3.9/site-packages/transformers/utils/__init__.py", line 33, in from .generic import ( File "/Users/anaconda3/envs/Newurban/lib/python3.9/site-packages/transformers/utils/generic.py", line 474, in _torch_pytree.register_pytree_node( AttributeError: module 'torch.utils._pytree' has no attribute 'register_pytree_node'
解决方案
确认PyTorch版本与环境一致性
执行pip show torch检查当前环境的PyTorch是否确实是2.0.0,若版本不符,重新安装:pip install torch==2.0.0若使用conda环境,先激活目标环境再确认安装:
conda activate Newurban conda list torch conda install torch==2.0.0彻底重装Transformers
卸载现有版本并清理pip缓存,避免残留文件导致的版本冲突:pip uninstall -y transformers pip cache purge pip install transformers==4.28.0临时代码修复(应急方案)
找到报错文件/Users/anaconda3/envs/Newurban/lib/python3.9/site-packages/transformers/utils/generic.py,将第474行的_torch_pytree.register_pytree_node替换为torch._pytree.register_pytree_node——PyTorch 2.0.0中该方法实际位于torch._pytree模块下。
内容的提问来源于stack exchange,提问作者CathyL
相关产品推荐
相关产品推荐

