pyg-team / pytorch_geometric

Graph Neural Network Library for PyTorch
https://pyg.org
MIT License
21.46k stars 3.68k forks source link

torch script save not working with ChebConv and knn_graph #9740

Open Alihamdy2496 opened 4 weeks ago

Alihamdy2496 commented 4 weeks ago

🐛 Describe the bug

I have a chebyshev spectral graph convolutional operator

the custom layer code is the following


import torch 
from torch_geometric.nn import MessagePassing, ChebConv, knn_graph

class SphericalChebConv(MessagePassing):
    def __init__(self, in_channels: int, out_channels: int, K: int, aggr: str):
        super().__init__()
        self.conv1 = ChebConv(in_channels, out_channels, K=K, aggr=aggr)

    def forward(self, x: torch.Tensor, position: torch.Tensor, knn: int):
        edges = knn_graph(position.squeeze(0), k=knn, flow= 'target_to_source')
        x = self.conv1(x.transpose(2,1), edges)
        return x.transpose(2,1)

chebconv_ = torch.jit.script(SphericalChebConv(in_channels=256, out_channels=256*2, K=2, aggr='add'))
print(chebconv_)
torch.jit.save(chebconv_, "SphericalChebConv.pt")

the results I get when I save the model

{
"name": "RuntimeError",
"message": "strides() called on an undefined Tensor",
"stack": "---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[1], line 20
     18 chebconv_ = torch.jit.script(SphericalChebConv(in_channels=256, out_channels=256*2, K=2, aggr='add'))
     19 print(chebconv_)
---> 20 torch.jit.save(chebconv_, \"torchScript_sphereGlue.pt\")

File /usr/local/lib/python3.10/dist-packages/torch/jit/_serialization.py:80, in save(m, f, _extra_files)
     78     _extra_files = {}
     79 if isinstance(f, (str, pathlib.Path)):
---> 80     m.save(f, _extra_files=_extra_files)
     81 else:
     82     ret = m.save_to_buffer(_extra_files=_extra_files)

File /usr/local/lib/python3.10/dist-packages/torch/jit/_script.py:740, in RecursiveScriptModule.save(self, f, **kwargs)
    732 def save(self, f, **kwargs):
    733     r\"\"\"
    734     save(f, _extra_files={})
    735 
   (...)
    738     DO NOT confuse these two functions when it comes to the 'f' parameter functionality.
    739     \"\"\"
--> 740     return self._c.save(str(f), **kwargs)

RuntimeError: strides() called on an undefined Tensor"
}

however, the model is exported and works just fine

RecursiveScriptModule(
  original_name=SphericalChebConv
  (conv1): RecursiveScriptModule(
    original_name=ChebConv
    (aggr_module): RecursiveScriptModule(original_name=SumAggregation)
    (lins): RecursiveScriptModule(
      original_name=ModuleList
      (0): RecursiveScriptModule(original_name=Linear)
      (1): RecursiveScriptModule(original_name=Linear)
    )
  )
)

Versions

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 24366  100 24366    0     0  16797      0  0:00:01  0:00:01 --:--:-- 16804
Collecting environment information...
PyTorch version: 2.1.2+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.8.0-47-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.2.140
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3050 Ti Laptop GPU
Nvidia driver version: 535.183.01
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                         x86_64
CPU op-mode(s):                       32-bit, 64-bit
Address sizes:                        39 bits physical, 48 bits virtual
Byte Order:                           Little Endian
CPU(s):                               16
On-line CPU(s) list:                  0-15
Vendor ID:                            GenuineIntel
Model name:                           12th Gen Intel(R) Core(TM) i7-12650H
CPU family:                           6
Model:                                154
Thread(s) per core:                   2
Core(s) per socket:                   10
Socket(s):                            1
Stepping:                             3
CPU max MHz:                          4700.0000
CPU min MHz:                          400.0000
BogoMIPS:                             5376.00
Flags:                                fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l2 cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization:                       VT-x
L1d cache:                            416 KiB (10 instances)
L1i cache:                            448 KiB (10 instances)
L2 cache:                             9.5 MiB (7 instances)
L3 cache:                             24 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-15
Vulnerability Gather data sampling:   Not affected
Vulnerability Itlb multihit:          Not affected
Vulnerability L1tf:                   Not affected
Vulnerability Mds:                    Not affected
Vulnerability Meltdown:               Not affected
Vulnerability Mmio stale data:        Not affected
Vulnerability Reg file data sampling: Mitigation; Clear Register File
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   Not affected
Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:             Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] nvidia-cublas-cu12==12.1.3.1
[pip3] nvidia-cuda-cupti-cu12==12.1.105
[pip3] nvidia-cuda-nvrtc-cu12==12.1.105
[pip3] nvidia-cuda-runtime-cu12==12.1.105
[pip3] nvidia-cudnn-cu12==8.9.2.26
[pip3] nvidia-cufft-cu12==11.0.2.54
[pip3] nvidia-curand-cu12==10.3.2.106
[pip3] nvidia-cusolver-cu12==11.4.5.107
[pip3] nvidia-cusparse-cu12==12.1.0.106
[pip3] nvidia-nccl-cu12==2.18.1
[pip3] nvidia-nvjitlink-cu12==12.6.77
[pip3] nvidia-nvtx-cu12==12.1.105
[pip3] torch==2.1.2
[pip3] torch-cluster==1.6.3+pt21cu121
[pip3] torch-geometric==2.6.1
[pip3] torch-scatter==2.1.2+pt21cu121
[pip3] torch-sparse==0.6.18+pt21cu121
[pip3] torchvision==0.16.2
[pip3] triton==2.1.0
[pip3] tritonclient==2.46.0
[conda] Could not collect