Repository: pytorch
Issue: Wrong result of first run with torch.compile() when backend is using torch.jit.trace() and model has inplace operators 
Labels: oncall: jit,triaged

### 🐛 Describe the bug

We have Dynamo backend defined similar to IPEX which traces and freezes the model:

```python
import importlib
import logging

import torch
from torch._dynamo import register_backend
from .common import fake_tensor_unsupported

@register_backend
@fake_tensor_unsupported
def aio(model, input):
    try:
        with torch.no_grad():
            traced_model = torch.jit.trace(model.eval(), inputs)
            frozen_model = torch.jit.freeze(traced_model)
        return frozen_model
    except Exception as ex:
        log.warning("JIT trace failed during the optimize process.")
        log.warning(print(ex))
        return model
```

Then following script:

```python
import torch

class Inplace(torch.nn.Module):
    def __init__(self):
        super(Inplace, self).__init__()

    def forward(self, input, input2):
        input.add_(input2)
        return input.add_(input2)
    
inplace = Inplace()
    
compiled = torch.compile(inplace, backend="aio")

res = inplace(torch.tensor(1), torch.tensor(2))

print(res)

inputs = (torch.tensor(1), torch.tensor(2))

res = compiled(torch.tensor(1), torch.tensor(2))

print(res)

res = compiled(torch.tensor(1), torch.tensor(2))

print(res)
```
gives following results:
```
tensor(5)
tensor(9)
tensor(5)
```

The second number (first run on the compiled model) is wrong. Model gives different results on first and subsequent runs. The problem happens because we used inplace operators, it is working correctly when we are using normal `add` op. This worked correctly on v2.0.0 and fails when we rebased our changes on `400c4de53bb7b36066aef381313ed71e4a877e95`

### Versions

PyTorch version: 2.1.0a0+git6d18fe9-dev
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (aarch64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 3.20.0-rc5
Libc version: glibc-2.35

Python version: 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:28:59) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-5.4.0-153-generic-aarch64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                    aarch64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
CPU(s):                          256
On-line CPU(s) list:             0-255
Vendor ID:                       ARM
Model name:                      Neoverse-N1
Model:                           1
Thread(s) per core:              1
Core(s) per socket:              128
Socket(s):                       2
Stepping:                        r3p1
CPU max MHz:                     3000.0000
CPU min MHz:                     1000.0000
BogoMIPS:                        50.00
Flags:                           fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
L1d cache:                       16 MiB (256 instances)
L1i cache:                       16 MiB (256 instances)
L2 cache:                        256 MiB (256 instances)
NUMA node(s):                    2
NUMA node0 CPU(s):               0-127
NUMA node1 CPU(s):               128-255
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:        Mitigation; __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; CSV2, BHB
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] mypy==1.4.1
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.23.5
[pip3] torch==2.1.0a0+git6d18fe9.dev
[pip3] torchvision==0.16.0a0+02d3d6d
[conda] numpy                     1.23.5                   pypi_0    pypi
[conda] torch                     2.1.0a0+git6d18fe9.dev          pypi_0    pypi
[conda] torchvision               0.16.0a0+02d3d6d          pypi_0    pypi


cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel @ezyang @msaroufim @wconstab @bdhirsh @anijain2305