Repository: pytorch
Issue: Change unsqueeze(0) to preserve memory layout contiguity of original tensor
Labels: triaged,module: numerical-reproducibility,module: memory format

### 🐛 Describe the bug

Forwarding a tensor `img` through a simple PyTorch Conv2d model produces a different result than forwarding `img + torch.zeros_like(img)`.

Here is a minimal example: https://github.com/dozed/pytorch-issue-1/blob/main/test_issue.py

```python
# prepare input
img = torchvision.io.image.read_image('sky1024px.jpg')
img = FT.convert_image_dtype(img, torch.float32)
img = img.unsqueeze(dim=0)

# prepare input + zero
zeros = torch.zeros_like(img)
img_updated = img + zeros

# input tensors are identical
assert torch.allclose(img, img_updated)
assert torch.equal(img, img_updated)

# prepare model
conv1 = nn.Conv2d(in_channels=3, out_channels=129, kernel_size=3, padding=1)
conv2 = nn.Conv2d(in_channels=129, out_channels=4, kernel_size=1)
conv1.requires_grad_(False)
conv2.requires_grad_(False)

# forward 1
x = conv1(img)
result1 = conv2(x)

# forward 2
y = conv1(img_updated)
result2 = conv2(y)

# tensors after conv1 are equal
assert torch.allclose(x, y)
assert torch.equal(x, y)

# ISSUE: the results are not equal but should be, since only zeros are added
print(torch.linalg.norm(result1 - result2))
assert torch.allclose(result1, result2)
assert torch.equal(result1, result2)
```


### Versions

```
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 23.04 (x86_64)
GCC version: (Ubuntu 12.3.0-1ubuntu1~23.04) 12.3.0
Clang version: Could not collect
CMake version: version 3.25.1
Libc version: glibc-2.37

Python version: 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-6.2.0-32-generic-x86_64-with-glibc2.37
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:                       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:                         12th Gen Intel(R) Core(TM) i7-1265U
CPU family:                         6
Model:                              154
Thread(s) per core:                 2
Core(s) per socket:                 10
Socket(s):                          1
Stepping:                           4
CPU(s) scaling MHz:                 37%
CPU max MHz:                        4800,0000
CPU min MHz:                        400,0000
BogoMIPS:                           5376,00
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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr ibt flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          352 KiB (10 instances)
L1i cache:                          576 KiB (10 instances)
L2 cache:                           6,5 MiB (4 instances)
L3 cache:                           12 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-11
Vulnerability Gather data sampling: Not affected
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; 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:      Not affected

Versions of relevant libraries:
[pip3] mypy==1.4.1
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.24.3
[pip3] pytorch-caffe-models==0.1
[pip3] torch==2.0.1
[pip3] torchdata==0.6.1
[pip3] torchtext==0.15.2
[pip3] torchvision==0.15.2
[pip3] torchviz==0.0.2
[conda] blas                      1.0                         mkl  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46343  
[conda] mkl-service               2.4.0           py311h5eee18b_1  
[conda] mkl_fft                   1.3.6           py311ha02d727_1  
[conda] mkl_random                1.2.2           py311ha02d727_1  
[conda] numpy                     1.24.3          py311h08b1b3b_1  
[conda] numpy-base                1.24.3          py311hf175353_1  
[conda] pytorch                   2.0.1              py3.11_cpu_0    pytorch
[conda] pytorch-caffe-models      0.1                      pypi_0    pypi
[conda] pytorch-mutex             1.0                         cpu    pytorch
[conda] torchdata                 0.6.1                    pypi_0    pypi
[conda] torchtext                 0.15.2                   pypi_0    pypi
[conda] torchvision               0.15.2                py311_cpu    pytorch
[conda] torchviz                  0.0.2                    pypi_0    pypi
```


cc @jamesr66a