Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot load torchscript model in C++ #8882

Closed
bybeye opened this issue Feb 8, 2024 · 4 comments
Closed

Cannot load torchscript model in C++ #8882

bybeye opened this issue Feb 8, 2024 · 4 comments

Comments

@bybeye
Copy link

bybeye commented Feb 8, 2024

Hi,

I tried to use C++ to load a torchscript geometric model with the code "pytorch_geometric/examples/cpp", and have followed the provided instructions to install pytorch_scatter and pytorch_sparse. The program has been successfully compiled, but I encountered an issue when attempting to run ./hello-world using the model generated by save_model.py.

terminate called after throwing an instance of 'torch::jit::ErrorReport'
  what():
Unknown builtin op: pyg::index_sort.
Could not find any similar ops to pyg::index_sort. This op may not exist or may not be currently supported in TorchScript.
:
  File "code/__torch__/pyg_lib/ops.py", line 11
    _1 = (_2, _3)
  else:
    _4, _5 = ops.pyg.index_sort(inputs, max_value)
             ~~~~~~~~~~~~~~~~~~ <--- HERE
    _1 = (_4, _5)
  return _1
'index_sort' is being compiled since it was called from 'index_sort'
  File "/home/edward/.local/lib/python3.9/site-packages/torch_sparse/utils.py", line 21
    if not torch_sparse.typing.WITH_INDEX_SORT:  # pragma: no cover
        return inputs.sort()
    return pyg_lib.ops.index_sort(inputs, max_value)
           ~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
Serialized   File "code/__torch__/torch_sparse/utils.py", line 3
def index_sort(inputs: Tensor,
    max_value: Optional[int]=None) -> Tuple[Tensor, Tensor]:
  _0 = __torch__.pyg_lib.ops.index_sort(inputs, max_value, )
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
  return _0
'index_sort' is being compiled since it was called from 'SparseStorage.__init__'
Serialized   File "code/__torch__/torch_sparse/storage.py", line 25
    is_sorted: bool=False,
    trust_data: bool=False) -> NoneType:
    _0 = __torch__.torch_sparse.utils.index_sort
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    _1 = uninitialized(int)
    _2 = uninitialized(Tensor)
'SparseStorage.__init__' is being compiled since it was called from 'is_sparse'
Serialized   File "code/__torch__/torch_geometric/utils/sparse.py", line 6
    _1 = True
  else:
    _2 = isinstance(src, __torch__.torch_sparse.tensor.SparseTensor)
                    ~~~ <--- HERE
    _1 = _2
  return _1
'is_sparse' is being compiled since it was called from 'GINConvJittable_ec52ab._check_input__0'
Serialized   File "code/__torch__/GINConvJittable_ec52ab.py", line 31
    edge_index: Tensor,
    size: Optional[Tuple[int, int]]) -> List[Optional[int]]:
    _2 = __torch__.torch_geometric.utils.sparse.is_sparse
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    _3 = "Flow direction \"target_to_source\" is invalid for message propagation via `torch_sparse.SparseTensor` or `torch.sparse.Tensor`. If you really want to make use of a reverse message passing flow, pass in the transposed sparse tensor to the message passing module, e.g., `adj_t.t()`."
    _4 = "Expected \'edge_index\' to be of integer type (got \'{}\')"``

I have tried to use pytorch_geometric 2.3.1 and 2.4 and get the same error, but a ResNet model works well.

Environment

  • PyTorch version: 2.1
  • OS: WSL Ubuntu
  • Python version: 3.9
  • CUDA/cuDNN version: 12.2
  • How you installed PyTorch and PyG (conda, pip, source): pip
  • Any other relevant information (e.g., version of torch-scatter): master
@rusty1s
Copy link
Member

rusty1s commented Feb 8, 2024

Two options:

  • You disable pyg_lib when converting your model to a torchscript program
  • You also ensure to build against pyg-lib when loading your model in C++

@bybeye
Copy link
Author

bybeye commented Feb 8, 2024

Thank you very much for your swift response. After uninstalling pyg_lib in Python and re-saving the model, it can be loaded in C++ now.

@bybeye bybeye closed this as completed Mar 20, 2024
@qiuqiouba
Copy link

Two options:

  • You disable pyg_lib when converting your model to a torchscript program
  • You also ensure to build against pyg-lib when loading your model in C++

Can you explain how to build pyg-lib in C++? According to examples/cpp/README.md, I think that pyg-lib works in C++ and only needs to configure the C++APIs of TorchScatter and TorchSparse. Is this correct?

@rusty1s
Copy link
Member

rusty1s commented Apr 12, 2024

I must admit that I have never tried to run PyG with pyg-lib in C++, but you would need to build it as described here: https://github.com/pyg-team/pyg-lib/blob/master/.github/CONTRIBUTING.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants