Repository: pytorch
Issue: Make Fx Generating Incorrect Graph For GPTQ model
Labels: triaged,module: fx,module: ProxyTensor

### 🐛 Describe the bug

Hi, I'm trying to generate the fx graph for the [Falcon-7b-GPTQ](https://huggingface.co/TheBloke/falcon-7b-instruct-GPTQ) model but the graph generated is not correct. If we run the fx graph with the inputs then the results generated from the fx graph and from directly running the model are different. Also, if we run the same fx graph again it would generate different results and after 2-3 executions the fx graph will generate NANs while the PyTorch execution would give the same results every time.

To reproduce the issue, run the following code:
```
from auto_gptq import AutoGPTQForCausalLM
import torch
from torch.fx.experimental.proxy_tensor import make_fx

torch.manual_seed(0)

model_name_or_path = "TheBloke/falcon-7b-instruct-GPTQ"
model_basename = "model"
use_triton = False

model = AutoGPTQForCausalLM.from_quantized(model_name_or_path,
        model_basename=model_basename,
        use_safetensors=True,
        trust_remote_code=True,
        # device_map={"cpu":""},
        device="cuda:0",
        # device="cpu",
        use_triton=use_triton,
        quantize_config=None)

compilation_input_ids = torch.randint(
    low=1, high=10000, size=(1, 100)
).to(device="cuda")

fx_g = make_fx(model)(compilation_input_ids)

print("PyTorch Result: ", model.forward(compilation_input_ids)["logits"])
print("Fx graph Result: ", fx_g(compilation_input_ids)["logits"])
```

To run this code you would also need to install:
```
GITHUB_ACTIONS=true pip install auto-gptq
pip install einops
```
If you see an error like this:
```
  File "/home/vivek/.cache/huggingface/modules/transformers_modules/TheBloke/falcon-7b-instruct-GPTQ/d6ce55f4e840bbbd596d1a65f64888f0a3c3326b/modelling_RW.py", line 279, in forward
    attn_output = F.scaled_dot_product_attention(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Expected query, key, and value to have the same dtype, but got query.dtype: float key.dtype: float and value.dtype: c10::Half instead.
```
Then change the`value_layer_ ` to `value_layer_ .to(torch.float32)` to here: https://huggingface.co/TheBloke/falcon-7b-instruct-GPTQ/blob/main/modelling_RW.py#L280. You will find this file at the location: `~/.cache/huggingface/modules/transformers_modules/TheBloke/falcon-7b-instruct-GPTQ/d6ce55f4e840bbbd596d1a65f64888f0a3c3326b/modelling_RW.py`

After these changes and installation, you should be able to reproduce the issue.

### Versions

```
Collecting environment information...
PyTorch version: 2.2.0.dev20230913+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0
Clang version: 14.0.0-1ubuntu1
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.11.4 (main, Jun  7 2023, 12:45:48) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.19.0-1027-gcp-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.1.105
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A100-SXM4-40GB
Nvidia driver version: 530.30.02
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:                   46 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          12
On-line CPU(s) list:             0-11
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Xeon(R) CPU @ 2.20GHz
CPU family:                      6
Model:                           85
Thread(s) per core:              2
Core(s) per socket:              6
Socket(s):                       1
Stepping:                        7
BogoMIPS:                        4400.42
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat avx512_vnni md_clear arch_capabilities
Hypervisor vendor:               KVM
Virtualization type:             full
L1d cache:                       192 KiB (6 instances)
L1i cache:                       192 KiB (6 instances)
L2 cache:                        6 MiB (6 instances)
L3 cache:                        38.5 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-11
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT Host state unknown
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
Vulnerability Retbleed:          Mitigation; Enhanced IBRS
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 IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Mitigation; Clear CPU buffers; SMT Host state unknown

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.0b1
[pip3] pytorch-lightning==2.1.0rc0
[pip3] pytorch-triton==2.1.0+6e4932cda8
[pip3] torch==2.2.0.dev20230913+cu121
[pip3] torchmetrics==1.0.3
[pip3] torchopt==0.7.2
[pip3] torchvision==0.17.0.dev20230913+cu121
[conda] Could not collect
```

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