Repository: pytorch
Issue: Wrong meta function for constant_pad_nd
Labels: oncall: pt2,module: decompositions,module: inductor

### 🐛 Describe the bug

When I'm working on this [PR](https://github.com/pytorch/pytorch/pull/140399), I meet a test failed case `python test/inductor/test_torchinductor_opinfo.py TestInductorOpInfoCUDA.test_comprehensive_fft_hfftn_cuda_float16`, it's related to `constant_pad_nd` op.


## Summary
I found there is two potential dispatched methods for `constant_pad_nd` when run it directly with `meta` device and run it in `inductor` and they didn't return the same tensor.

## Code
1. Directly run it with `meta` device:
```
import torch

a = torch.empty_strided((2, 4, 5), (20, 1, 4), dtype=torch.complex128, device='meta')
print(a.shape)
print(a.stride())
b = torch.constant_pad_nd(a, [0, 0, 0, -2, 0, 0])
print(b.shape)
print(b.stride())
```
If we run the above code, we could see for any of `meta`, `cuda` and `cpu` device, it will print:
```
torch.Size([2, 4, 5])
(20, 1, 4)
torch.Size([2, 2, 5])
(10, 1, 2)
```
So the `meta` device matches the `cpu` and `cuda` device, it do **meet our expectation**.

2. torch.compile:
We run `constant_pad_nd` with same arguments but run it with compiled.
```
import torch

a = torch.empty_strided((2, 4, 5), (20, 1, 4), dtype=torch.complex128, device='cuda')

def foo(a):
    b = torch.constant_pad_nd(a, [0, 0, 0, -2, 0, 0])
    return b

new_foo = torch.compile(foo)
new_foo(a)
```
We could try to print the graph of ir:
![forward_decomp](https://github.com/user-attachments/assets/31b4f87d-4746-4571-a012-7106d9fd6edf)

We could see in the pink box, the stride of `constant_pad_nd` is **`(10, 5, 1)`**, it's **not aligned** with the stride above **`(10, 1, 2)`**, so we could image there is mismatch in the meta function for directly run and run it in `inductor`.

## Analysis
### call stack
I tried to debug the two different behavior, I found both of the two approaches will go to:
https://github.com/pytorch/pytorch/blob/816328fa51382e9b50e60fb928a690d5c1bdadaf/torch/_prims_common/wrappers.py#L290-L291
but they may have different `fn`
1. Directly run it with `meta` device:
it will go to some function called `empty_like`:
https://github.com/pytorch/pytorch/blob/816328fa51382e9b50e60fb928a690d5c1bdadaf/torch/_refs/__init__.py#L4972-L5010
In this method, it will keep the output's stride aligned with the `cpu` or `cuda` device.
2. torch.compile:
It will go to some function called `constant_pad_nd`:
https://github.com/pytorch/pytorch/blob/816328fa51382e9b50e60fb928a690d5c1bdadaf/torch/_refs/__init__.py#L2901-L2981
In this method, it seems it didn't keep the stride align with the `cpu` or `cuda` device.

### my guess
I suspected it was caused when we initialize meta function for each op, because it seems we didn't have a meta function for `constant_pad_nd`, so pytorch skip some setup here:
https://github.com/pytorch/pytorch/blob/816328fa51382e9b50e60fb928a690d5c1bdadaf/torch/_meta_registrations.py#L6921-L6926

## Potential solution
I'm not very clear if it's an expected behavior, if it's not, there are two potential solution in my mind:
1. Ask `inductor` to call the `empty_like` in the above.
2. Introduce a meta function `meta_constant_pad_nd` for this op and make it run in both of the two places mentioned above.

WDYT? Any help would be very appreciated! Thank you! 

### Versions

```
Collecting environment information...
PyTorch version: 2.6.0a0+gitf3ec745
Is debug build: True
CUDA used to build PyTorch: 12.6
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64)
GCC version: (Ubuntu 10.5.0-1ubuntu1~22.04) 10.5.0
Clang version: Could not collect
CMake version: version 3.31.1
Libc version: glibc-2.35

Python version: 3.11.10 (main, Oct  3 2024, 07:29:13) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-6.8.0-49-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.6.77
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3080
Nvidia driver version: 560.35.03
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):                               20
On-line CPU(s) list:                  0-19
Vendor ID:                            GenuineIntel
Model name:                           12th Gen Intel(R) Core(TM) i7-12700K
CPU family:                           6
Model:                                151
Thread(s) per core:                   2
Core(s) per socket:                   12
Socket(s):                            1
Stepping:                             2
CPU max MHz:                          5000.0000
CPU min MHz:                          800.0000
BogoMIPS:                             7219.20
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 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l2 cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi 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:                            512 KiB (12 instances)
L1i cache:                            512 KiB (12 instances)
L2 cache:                             12 MiB (9 instances)
L3 cache:                             25 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-19
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 Reg file data sampling: Mitigation; Clear Register File
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   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 / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] flake8==6.1.0
[pip3] flake8-bugbear==23.3.23
[pip3] flake8-comprehensions==3.15.0
[pip3] flake8-executable==2.1.3
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==23.3.1
[pip3] flake8-simplify==0.19.3
[pip3] mypy==1.11.2
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.0
[pip3] onnx==1.17.0
[pip3] onnxscript==0.1.0.dev20240817
[pip3] optree==0.13.1
[pip3] torch==2.6.0a0+gitf3ec745
[pip3] triton==3.1.0
[conda] numpy                     1.26.0                   pypi_0    pypi
[conda] optree                    0.13.1                   pypi_0    pypi
[conda] torch                     2.6.0a0+gitf3ec745           dev_0    <develop>
[conda] torchfix                  0.4.0                    pypi_0    pypi
[conda] triton                    3.1.0                    pypi_0    pypi
```

cc @chauhang @penguinwu @SherlockNoMad @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @aakhundov @BoyuanFeng