[JIT][TracingCheckError] inplace ops incompatible with `contiguous(.., channels_last)`
Labels: oncall: jit

### 🐛 Describe the bug

The following `fn` (inplace `transpose` + `contiguous(..., channels_last)`) which works for eager mode but cannot be compiled by `torch.jit.trace`, which seems to be a bug in alias analysis? It also seems to be similar to https://github.com/pytorch/pytorch/issues/23993 

```python
import torch


def fn(x):
    # v1: [3, 1, 7, 7] alias of x
    v1 = torch.Tensor.transpose_(x, 0, 1)
    # v2: [3, 1, 7, 7] alias of x
    v2 = torch.Tensor.contiguous(x, memory_format=torch.channels_last)
    return v1, v2


# There are a few ways for making it "work":
# 1. Remove `memory_format=torch.channels_last` from `torch.Tensor.contiguous`
# 2. Add any non-inplace of before `transpose_` such as `x = torch.relu(x)`
# 3. Add any non-inplace operator after `torch.Tensor.contiguous` such as `torch.cos`
# 4: Use any shape-preserving inplace op to replace `transpose_` such as `torch..relu_`
# 5. Only return v1 or v2 not both.
# 6. Let the input channel dimension to be 1 instead of something greater than 1.
# 7. Use `check_trace=False` in `torch.jit.trace`

# eager
res = fn(torch.rand([1, 2, 4, 4]))
print("Eager mode... Success!")
# compile
torch.jit.trace(fn, torch.rand([1, 2, 4, 4]))

"""
Eager mode... Success!
Traceback (most recent call last):
  File "test.py", line 25, in <module>
    torch.jit.trace(fn, torch.rand([1, 2, 4, 4]))
...
  File "/miniconda3/lib/python3.9/site-packages/torch/jit/_trace.py", line 561, in _check_trace
    raise TracingCheckError(*diag_info)
torch.jit._trace.TracingCheckError: Tracing failed sanity checks!
ERROR: Graphs differed across invocations!
        Graph diff:
                  graph(%x.1 : Tensor):
                    %1 : int = prim::Constant[value=0]() # test.py:6:0
                    %2 : int = prim::Constant[value=1]() # test.py:6:0
                    %x : Tensor = aten::transpose_(%x.1, %1, %2) # test.py:6:0
                    %4 : int = prim::Constant[value=2]() # test.py:8:0
                    %5 : Tensor = aten::contiguous(%x, %4) # test.py:8:0
                -   %6 : (Tensor, Tensor) = prim::TupleConstruct(%5, %5)
                ?                                                 ^
                +   %6 : (Tensor, Tensor) = prim::TupleConstruct(%x, %5)
                ?                                                 ^
                    return (%6)
        First diverging operator:
        Node diff:
                - %6 : (Tensor, Tensor) = prim::TupleConstruct(%5, %5)
                ?                                               ^
                + %6 : (Tensor, Tensor) = prim::TupleConstruct(%x, %5)
                ?
"""
```

There are a few ways to make it "work", which hopefully can help debugging:
1. Remove `memory_format=torch.channels_last` from `torch.Tensor.contiguous`
2. Add any non-inplace of before `transpose_` such as `x = torch.relu(x)`
3. Add any non-inplace operator after `torch.Tensor.contiguous` such as `torch.cos`
4. Use any shape-preserving inplace op to replace `transpose_` such as `torch.relu_`
5. Only return v1 or v2 not both.
6. Let the input channel dimension be 1 instead of something greater than 1.
7. Use `check_trace=False` in `torch.jit.trace`

### Versions

<details><summary><b>Environment </b> <i>[click to expand]</i></summary>
<div>

```python
"""
Collecting environment information...
PyTorch version: 1.14.0.dev20221202+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.1 LTS (x86_64)
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Clang version: Could not collect
CMake version: version 3.25.0
Libc version: glibc-2.35

Python version: 3.9.12 (main, Apr  5 2022, 06:56:58)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.15.0-56-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 11.6.124
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 3090
GPU 2: NVIDIA GeForce RTX 3090

Nvidia driver version: 515.86.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.4.1
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.23.3
[pip3] onnx2torch==1.5.3
[pip3] torch==1.14.0.dev20221202+cu117
[pip3] torchaudio==0.14.0.dev20221203+cu117
[pip3] torchtriton==2.0.0+0d7e753227
[pip3] torchvision==0.15.0.dev20221203+cpu
[conda] numpy                     1.23.3                   pypi_0    pypi
[conda] onnx2torch                1.5.3                    pypi_0    pypi
[conda] torch                     1.14.0.dev20221202+cu117          pypi_0    pypi
[conda] torchaudio                0.14.0.dev20221203+cu117          pypi_0    pypi
[conda] torchtriton               2.0.0+0d7e753227          pypi_0    pypi
[conda] torchvision               0.15.0.dev20221203+cpu          pypi_0    pypi
"""
```

</div>
</details>


cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel