Repository: pytorch
Issue: 3+ dimension tensor not throwing a runtime error on in-place operation
Labels: high priority,triaged,actionable,module: correctness (silent),module: viewing and reshaping

### 🐛 Describe the bug

- This is a bug related to [[#8212](https://github.com/pytorch/pytorch/issues/8212)](https://github.com/pytorch/pytorch/issues/8212).
- Seems like the runtime error supposed to be raised on running an in-place operation on overlapping memories is not raised on the 3+ dimension tensor.

# 2 dimension tensor

- Runtime error is raised to avoid unexpected result.
    
    ```python
    a = torch.Tensor([[i+j for i in range(3)] for j in range(10, 100, 10)])
    print(a)
    a -= a[:1]
    ```
    
    ```bash
    tensor([[10., 11., 12.],
            [20., 21., 22.],
            [30., 31., 32.],
            [40., 41., 42.],
            [50., 51., 52.],
            [60., 61., 62.],
            [70., 71., 72.],
            [80., 81., 82.],
            [90., 91., 92.]])
    ```
    
    ```python
    {
    	"name": "RuntimeError",
    	"message": "unsupported operation: some elements of the input tensor and the written-to tensor refer to a single memory location. Please clone() the tensor before performing the operation.",
    	"stack": "---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    /tmp/ipykernel_1650611/2496211141.py in <module>
          1 a = torch.Tensor([[i+j for i in range(3)] for j in range(10, 100, 10)])
          2 print(a)
    ----> 3 a -= a[:1]
    
    RuntimeError: unsupported operation: some elements of the input tensor and the written-to tensor refer to a single memory location. Please clone() the tensor before performing the operation."
    }
    ```
    
- Expected result for the 2d tensor on this operation is achieved by using `clone()` as suggested in the error comment.
    
    ```python
    a = torch.Tensor([[i+j for i in range(3)] for j in range(10, 100, 10)])
    print(a)
    # a -= a[:1]
    a = a - a.clone()[:1]
    print(a)
    ```
    
    ```python
    tensor([[10., 11., 12.],
            [20., 21., 22.],
            [30., 31., 32.],
            [40., 41., 42.],
            [50., 51., 52.],
            [60., 61., 62.],
            [70., 71., 72.],
            [80., 81., 82.],
            [90., 91., 92.]])
    tensor([[ 0.,  0.,  0.],
            [10., 10., 10.],
            [20., 20., 20.],
            [30., 30., 30.],
            [40., 40., 40.],
            [50., 50., 50.],
            [60., 60., 60.],
            [70., 70., 70.],
            [80., 80., 80.]])
    ```
    

# 3 dimension tensor

1. No runtime error
2. Unexpected result (not quite sure how the indexing error results in this manner)
    
    ```python
    a = torch.Tensor([[[i+j+k for i in range(3)] for j in range(10, 51, 10)]for k in range(100, 500, 100)])
    print(a)
    a -= a[:, :1]
    print(a)
    ```
    
    ```python
    tensor([[[110., 111., 112.],
             [120., 121., 122.],
             [130., 131., 132.],
             [140., 141., 142.],
             [150., 151., 152.]],
    
            [[210., 211., 212.],
             [220., 221., 222.],
             [230., 231., 232.],
             [240., 241., 242.],
             [250., 251., 252.]],
    
            [[310., 311., 312.],
             [320., 321., 322.],
             [330., 331., 332.],
             [340., 341., 342.],
             [350., 351., 352.]],
    
            [[410., 411., 412.],
             [420., 421., 422.],
             [430., 431., 432.],
             [440., 441., 442.],
             [450., 451., 452.]]])
    tensor([[[  0.,   0.,   0.],
             [120., 121., 122.],
             [130., 131., 132.],
             [140., 141., 142.],
             [150., 151., 152.]],
    
            [[  0.,   0.,   0.],
             [220., 221., 222.],
             [230., 231., 232.],
             [240., 241., 242.],
             [250., 251., 252.]],
    
            [[  0.,   0.,   0.],
             [320., 321., 322.],
             [330., 331., 332.],
             [340., 341., 342.],
             [350., 351., 352.]],
    
            [[  0.,   0.,   0.],
             [420., 421., 422.],
             [430., 431., 432.],
             [440., 441., 442.],
             [450., 451., 452.]]])
    ```
    
3. This doesn’t happen when using `.clone()` as the runtime error suggests.
    
    ```python
    a = torch.Tensor([[[i+j+k for i in range(3)] for j in range(10, 51, 10)]for k in range(100, 500, 100)])
    print(a)
    a = a - a.clone()[:, :1]
    print(a)
    ```
    
    ```python
    tensor([[[110., 111., 112.],
             [120., 121., 122.],
             [130., 131., 132.],
             [140., 141., 142.],
             [150., 151., 152.]],
    
            [[210., 211., 212.],
             [220., 221., 222.],
             [230., 231., 232.],
             [240., 241., 242.],
             [250., 251., 252.]],
    
            [[310., 311., 312.],
             [320., 321., 322.],
             [330., 331., 332.],
             [340., 341., 342.],
             [350., 351., 352.]],
    
            [[410., 411., 412.],
             [420., 421., 422.],
             [430., 431., 432.],
             [440., 441., 442.],
             [450., 451., 452.]]])
    tensor([[[ 0.,  0.,  0.],
             [10., 10., 10.],
             [20., 20., 20.],
             [30., 30., 30.],
             [40., 40., 40.]],
    
            [[ 0.,  0.,  0.],
             [10., 10., 10.],
             [20., 20., 20.],
             [30., 30., 30.],
             [40., 40., 40.]],
    
            [[ 0.,  0.,  0.],
             [10., 10., 10.],
             [20., 20., 20.],
             [30., 30., 30.],
             [40., 40., 40.]],
    
            [[ 0.,  0.,  0.],
             [10., 10., 10.],
             [20., 20., 20.],
             [30., 30., 30.],
             [40., 40., 40.]]])
    ```
    
4. Also, the behavior differs depending on the device it is running(note that the tensor is now on cuda device instead of cpu).
    
    ```python
    a = torch.Tensor([[[i+j+k for i in range(3)] for j in range(10, 51, 10)]for k in range(100, 500, 100)]).cuda()
    print(a)
    a -= a[:, :1]
    print(a)
    ```
    
    ```python
    tensor([[[110., 111., 112.],
             [120., 121., 122.],
             [130., 131., 132.],
             [140., 141., 142.],
             [150., 151., 152.]],
    
            [[210., 211., 212.],
             [220., 221., 222.],
             [230., 231., 232.],
             [240., 241., 242.],
             [250., 251., 252.]],
    
            [[310., 311., 312.],
             [320., 321., 322.],
             [330., 331., 332.],
             [340., 341., 342.],
             [350., 351., 352.]],
    
            [[410., 411., 412.],
             [420., 421., 422.],
             [430., 431., 432.],
             [440., 441., 442.],
             [450., 451., 452.]]], device='cuda:0')
    tensor([[[ 0.,  0.,  0.],
             [10., 10., 10.],
             [20., 20., 20.],
             [30., 30., 30.],
             [40., 40., 40.]],
    
            [[ 0.,  0.,  0.],
             [10., 10., 10.],
             [20., 20., 20.],
             [30., 30., 30.],
             [40., 40., 40.]],
    
            [[ 0.,  0.,  0.],
             [10., 10., 10.],
             [20., 20., 20.],
             [30., 30., 30.],
             [40., 40., 40.]],
    
            [[ 0.,  0.,  0.],
             [10., 10., 10.],
             [20., 20., 20.],
             [30., 30., 30.],
             [40., 40., 40.]]], device='cuda:0')
    ```
    
5. I found this behavior while updating pytorch versions from 1.8.0 to 2.1.1 on my product.
    - In my codebase which I can’t publicly reveal, the behavior of the in-place operation on cuda device changed between pytorch 1.10.0 and 1.11.0. I would like to know what changed to make this behavior change.
    - I’m using RTX 3090, python 3.8, cuda 11.8

### Versions

PyTorch version: 2.1.1+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: Could not collect
CMake version: version 3.13.0
Libc version: glibc-2.31

Python version: 3.8.18 | packaged by conda-forge | (default, Oct 10 2023, 15:44:36)  [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-5.15.0-78-generic-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: 11.8.89
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
GPU 3: NVIDIA GeForce RTX 3090

Nvidia driver version: 520.61.05
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.2.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.2.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.2.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.2.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.2.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.2.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.2.2
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):                          36
On-line CPU(s) list:             0-35
Thread(s) per core:              2
Core(s) per socket:              18
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           85
Model name:                      Intel(R) Core(TM) i9-10980XE CPU @ 3.00GHz
Stepping:                        7
CPU MHz:                         4194.835
CPU max MHz:                     4800.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        6000.00
Virtualization:                  VT-x
L1d cache:                       576 KiB
L1i cache:                       576 KiB
L2 cache:                        18 MiB
L3 cache:                        24.8 MiB
NUMA node0 CPU(s):               0-35
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          Mitigation; Enhanced IBRS
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, PBRSB-eIBRS SW sequence
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 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 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 hwp hwp_act_window hwp_epp hwp_pkg_req avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] flake8==4.0.1
[pip3] msgpack-numpy==0.4.8
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.22.2
[pip3] pytorch3d==0.3.0
[pip3] torch==2.1.1+cu118
[pip3] torch-tb-profiler==0.4.0
[pip3] torchfile==0.1.0
[pip3] torchgeometry==0.1.2
[pip3] torchmetrics==0.11.4
[pip3] torchvision==0.16.1+cu118
[pip3] triton==2.1.0
[conda] msgpack-numpy             0.4.8                    pypi_0    pypi
[conda] numpy                     1.22.2                   pypi_0    pypi
[conda] pytorch3d                 0.3.0                    pypi_0    pypi
[conda] torch                     2.1.1+cu118              pypi_0    pypi
[conda] torchmetrics              0.11.4                   pypi_0    pypi
[conda] torchvision               0.16.1+cu118             pypi_0    pypi
[conda] triton                    2.1.0                    pypi_0    pypi

cc @ezyang @gchanan @zou3519 @kadeng