无法从torch_geometric导入HeteroData的问题求助
问题分析
报错核心是pyg_lib.ops.index_sort在TorchScript编译时无法被解析,本质是PyTorch与PyTorch Geometric(PyG)及相关依赖的版本严重不兼容:
- 你使用的
torch==1.7.1+cu110属于较老版本,而torch-geometric==2.2.0是较新版本,后者依赖的torch-sparse==0.6.17引入了pyg_lib的新特性,但老版本PyTorch的TorchScript无法支持该特性的编译逻辑。
解决方案
方案1:版本适配(推荐)
根据PyG官方版本兼容规则,torch==1.7.1+cu110对应的兼容组件版本如下,卸载现有组件后重新安装:
# 卸载现有依赖 pip uninstall -y torch-geometric torch-scatter torch-sparse # 安装匹配版本 pip install torch-scatter==2.0.6 -f https://data.pyg.org/whl/torch-1.7.1+cu110.html pip install torch-sparse==0.6.9 -f https://data.pyg.org/whl/torch-1.7.1+cu110.html pip install torch-geometric==1.7.2
方案2:临时绕过TorchScript编译
若无法更换版本,可修改torch_sparse/utils.py中的index_sort函数,绕过对pyg_lib的调用:
找到文件中该函数的代码,替换为:
def index_sort(inputs, max_value): if not torch_sparse.typing.WITH_INDEX_SORT: # pragma: no cover return inputs.sort() # 替换原pyg_lib调用为原生sort逻辑 return inputs.sort()
注意:此方法可能降低部分PyG功能的运行效率,仅作为应急方案。
验证
重新执行导入代码:
from torch_geometric.data import HeteroData
无报错则问题解决。
内容的提问来源于stack exchange,提问作者Brosea
相关产品推荐
相关产品推荐

