Repository: pytorch
Issue: When using torch.compile to compile the function _kernel_make_viewless_tensor, an error occurs：AssertionError: wrong number of dimensions
Labels: triaged,oncall: pt2,module: inductor

### 🐛 Describe the bug

test device： NVidia L20
software version：
torch                     2.5.1
torchaudio                2.5.1
torchvision               0.20.1
triton                    3.1.0

The test codes are as follows.
I’m sure it’s related to the parameter “requires_grad” of the function ”_kernel_make_viewless_tensor“, because changing it to False allows the codes to pass, and the graph generated by torch.compile will be different.

```
# The codes are sourced from https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/core/utils.py:183
def _kernel_make_viewless_tensor(inp, requires_grad):
    """Make a viewless tensor.

    View tensors have the undesirable side-affect of retaining a reference
    to the originally-viewed tensor, even after manually setting the '.data'
    field. This method creates a new tensor that links to the old tensor's
    data, without linking the viewed tensor, referenced via the '._base'
    field.
    """
    out = torch.empty((1,), dtype=inp.dtype, device=inp.device, requires_grad=requires_grad)
    out.data = inp.data
    return out

t1 = torch.randn(20, 50, 30,  dtype=torch.bfloat16).to('cuda')
c = torch.compile(_kernel_make_viewless_tensor)

t2 = _kernel_make_viewless_tensor(t1, True)
t3 = c(t1, True)

print(f"allclose result = {torch.allclose(t2, t3, atol=1e-5, rtol=1e-5)}")
```

The test results are as follows：
```
Traceback (most recent call last):
  File "/data/test/b.py", line 20, in <module>
    t3 = c(t1, True)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py", line 465, in _fn
    return fn(*args, **kwargs)
  File "/data/test/b.py", line 12, in _kernel_make_viewless_tensor
    out = torch.empty((1,), dtype=inp.dtype, device=inp.device, requires_grad=requires_grad)
  File "/data/test/b.py", line 12, in torch_dynamo_resume_in__kernel_make_viewless_tensor_at_12
    out = torch.empty((1,), dtype=inp.dtype, device=inp.device, requires_grad=requires_grad)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py", line 632, in _fn
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_functorch/aot_autograd.py", line 1100, in forward
    return compiled_fn(full_args)
  File "/usr/local/lib/python3.10/dist-packages/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 321, in runtime_wrapper
    all_outs = call_func_at_runtime_with_args(
  File "/usr/local/lib/python3.10/dist-packages/torch/_functorch/_aot_autograd/utils.py", line 124, in call_func_at_runtime_with_args
    out = normalize_as_list(f(args))
  File "/usr/local/lib/python3.10/dist-packages/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 667, in inner_fn
    outs = compiled_fn(args)
  File "/usr/local/lib/python3.10/dist-packages/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 488, in wrapper
    return compiled_fn(runtime_args)
  File "/usr/local/lib/python3.10/dist-packages/torch/_inductor/codecache.py", line 1478, in __call__
    return self.current_callable(inputs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_inductor/utils.py", line 1977, in run
    return model(new_inputs)
  File "/tmp/torchinductor_root/ld/cldpqwvjtbpm3peqlchlnst5etn44gyzsepxnck35bn7pm4epvvj.py", line 35, in call
    assert_size_stride(arg0_1, (20, 50, 30), (1500, 30, 1))
AssertionError: wrong number of dimensions


# /tmp/torchinductor_root/ld/cldpqwvjtbpm3peqlchlnst5etn44gyzsepxnck35bn7pm4epvvj.py
# AOT ID: ['0_inference']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (20, 50, 30), (1500, 30, 1))
    assert_size_stride(arg1_1, (20, 50, 30), (1500, 30, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        # Topologically Sorted Source Nodes: [], Original ATen: []
        buf0 = torch.ops.aten.set_.source_Tensor(arg0_1, arg1_1)
        assert_size_stride(buf0, (20, 50, 30), (1500, 30, 1))
        del arg0_1
        del arg1_1
    return (buf0, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((20, 50, 30), (1500, 30, 1), device='cuda:0', dtype=torch.bfloat16)
    arg1_1 = rand_strided((20, 50, 30), (1500, 30, 1), device='cuda:0', dtype=torch.bfloat16)
    fn = lambda: call([arg0_1, arg1_1])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)
```


### Versions

Collecting environment information...
PyTorch version: 2.5.1+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.29.2
Libc version: glibc-2.35

Python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-4.18.0-372.9.1.el8.x86_64-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.4.131
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA L20

Versions of relevant libraries:
[pip3] numpy==1.24.4
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cudnn-frontend==1.3.0
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] nvtx==0.2.5
[pip3] onnx==1.16.0
[pip3] optree==0.11.0
[pip3] pynvjitlink==0.1.13
[pip3] pytorch-quantization==2.1.2
[pip3] pytorch-triton==3.0.0+989adb9a2
[pip3] torch==2.5.1
[pip3] torchaudio==2.5.1
[pip3] torchvision==0.20.1
[pip3] transformer-engine-torch==1.9.0
[pip3] triton==3.1.0


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