Repository: pytorch
Issue: SubgraphMatcher may fail to match when the matching pattern having call_module IR
Labels: triaged,module: fx

### 🐛 Describe the bug

Hey guys, when i use SubgraphMatcher to match a pattern having call_module IR.
For example, when i claim a model like this :
``` python
class TestModel(torch.nn.Module):
  
  def __init__(self, input_dim, output_dim):
    super().__init__()
    self.mlp = torch.nn.Linear(input_dim, output_dim)
    self.mlp2 = torch.nn.Linear(output_dim,1)
    
  def forward(self,x):
    return self.mlp2(self.mlp(x))

linear_func_model = symbolic_trace(TestModel(300,100))
linear_func_model.print_readable()
# the output is:
#class TestModel(torch.nn.Module):
#    def forward(self, x):
#        # No stacktrace found for following nodes
#        mlp = self.mlp(x);  x = None
#        mlp2 = self.mlp2(mlp);  mlp = None
 #       return mlp2
```
when i using the pattern and replacement like this, it may fail to match:
```python
class PatternClass(torch.nn.Module):
  def __init__(self, input_dim, output_dim):
    super().__init__()
    self.mlp3 = torch.nn.Linear(input_dim, 1)
  def forward(self,x):
    return self.mlp3(x)
  
  
class ReplacementClass(torch.nn.Module):
  def __init__(self, input_dim, output_dim):
    super().__init__()
    self.mlp1_1 = torch.nn.Linear(input_dim, output_dim)
  def forward(self,x):
    return self.mlp1_1(x + 1)

subgraph_rewriter.replace_pattern(linear_func_model, PatternClass(300,100), ReplacementClass(300,100))
linear_func_model.print_readable()
# here is output:
#class TestModel(torch.nn.Module):
#    def forward(self, x):
#        # No stacktrace found for following nodes
#        mlp = self.mlp(x);  x = None
#        mlp2 = self.mlp2(mlp);  mlp = None
#        return mlp2
```
My torch version is '2.3.1+cu121'.
And after reading the source code of SubgraphMatcher, I find that it will only match the pattern having call_module IR when the source graph and pattern call the module with the same name(e.g. the torch.nn.Linear in the TestModel and PatternClass shall named as the same name mlp).
I think it is a bug since we should match the pattern depends on the module class instead of the name of the module

### Versions

PyTorch version: 2.3.1+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.3 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.31

Python version: 3.9.19 (main, May  6 2024, 19:43:03)  [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.11.0-27-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 11.2.152
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA RTX A6000
Nvidia driver version: 550.78
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
Byte Order:                      Little Endian
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          96
On-line CPU(s) list:             0-95
Thread(s) per core:              2
Core(s) per socket:              24
Socket(s):                       2
NUMA node(s):                    2
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           85
Model name:                      Intel(R) Xeon(R) Gold 6248R CPU @ 3.00GHz
Stepping:                        7
CPU MHz:                         3000.000
BogoMIPS:                        6000.00
Virtualization:                  VT-x
L1d cache:                       1.5 MiB
L1i cache:                       1.5 MiB
L2 cache:                        48 MiB
L3 cache:                        71.5 MiB
NUMA node0 CPU(s):               0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94
NUMA node1 CPU(s):               1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Mitigation; TSX disabled
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 pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.21.0
[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.20.5
[pip3] nvidia-nvjitlink-cu12==12.1.105
[pip3] nvidia-nvtx-cu12==12.1.105
[pip3] onnx==1.16.1
[pip3] onnxconverter-common==1.14.0
[pip3] onnxoptimizer==0.3.13
[pip3] onnxruntime==1.9.0
[pip3] onnxsim==0.4.36
[pip3] torch==2.3.1+cu121
[pip3] torchaudio==2.3.1+cu121
[pip3] torchfm==0.7.0
[pip3] torchvision==0.18.1+cu121
[pip3] triton==2.3.1
[conda] numpy                     1.21.0                   pypi_0    pypi
[conda] nvidia-cublas-cu12        12.1.3.1                 pypi_0    pypi
[conda] nvidia-cuda-cupti-cu12    12.1.105                 pypi_0    pypi
[conda] nvidia-cuda-nvrtc-cu12    12.1.105                 pypi_0    pypi
[conda] nvidia-cuda-runtime-cu12  12.1.105                 pypi_0    pypi
[conda] nvidia-cudnn-cu12         8.9.2.26                 pypi_0    pypi
[conda] nvidia-cufft-cu12         11.0.2.54                pypi_0    pypi
[conda] nvidia-curand-cu12        10.3.2.106               pypi_0    pypi
[conda] nvidia-cusolver-cu12      11.4.5.107               pypi_0    pypi
[conda] nvidia-cusparse-cu12      12.1.0.106               pypi_0    pypi
[conda] nvidia-nccl-cu12          2.20.5                   pypi_0    pypi
[conda] nvidia-nvjitlink-cu12     12.1.105                 pypi_0    pypi
[conda] nvidia-nvtx-cu12          12.1.105                 pypi_0    pypi
[conda] torch                     2.3.1+cu121              pypi_0    pypi
[conda] torchaudio                2.3.1+cu121              pypi_0    pypi
[conda] torchfm                   0.7.0                    pypi_0    pypi
[conda] torchvision               0.18.1+cu121             pypi_0    pypi
[conda] triton                    2.3.1                    pypi_0    pypi

cc @ezyang @SherlockNoMad @EikanWang @jgong5 @wenzhe-nrv