Repository: pytorch
Issue: Runtime error for custom op in export path (C++ inference)
Labels: oncall: pt2,oncall: export

### 🐛 Describe the bug

### Description:
Our workload replaces addmm with our custom op with exact signature. The custom operator is registered using TORCH_LIBRARY.
With a custom backend of torch.compile, Op is getting replaced and functionality is as expected. However in the generated code by AOTInductor, "const" keywords are missing in the signature.

Const keyword is mandatory for Scalar and optional-Tensor arguments.


### Code:
```
python

import os
import torch

class Model(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = torch.nn.Linear(10, 16, bias=True)

    def forward(self, x):
        x = self.fc1(x)
        return x
 
with torch.no_grad():
    device = "cuda" if torch.cuda.is_available() else "cpu"
    model = Model().to(device=device)
    example_inputs=(torch.randn(2, 10, device=device),)
    batch_dim = torch.export.Dim("batch", min=1, max=1024)

    ######################################################
    exmp_inputs=torch.randn(2, 10)
    ep = torch.export.export(model.eval(), example_inputs)

    from torch._decomp import get_decompositions
    decomp_tbl = get_decompositions([torch.ops.aten.linear.default])
    exp_mod = ep.run_decompositions(decomp_tbl)

    model_out = op_replacement(exp_mod.module())

    so_path = torch._export.aot_compile(
        model_out,
        example_inputs,
        options={"aot_inductor.output_path": os.path.join(os.getcwd(), "model_addmm.so")},
    )

```
Followed the steps [here](https://pytorch.org/docs/main/torch.compiler_aot_inductor.html) to create an executable. Observed the following error while running the executable.

```
Error: 
Tried to access or call an operator with a wrong signature.
  operator: custom::custom_addmm_1dbias_relu(Tensor self, Tensor mat1, Tensor mat2, *, Scalar beta=1, Scalar alpha=1, str custom_op_name="custom::custom_addmm_1dbias_relu") -> Tensor
    registered at /home/src/cpu/cpp/Matmul.cpp:638
  correct signature:  at::Tensor (at::Tensor const&, at::Tensor const&, at::Tensor const&, c10::Scalar const&, c10::Scalar const&, std::string)
    registered at /home/src/cpu/cpp/Matmul.cpp:737
  accessed/called as: at::Tensor (at::Tensor const&, at::Tensor const&, at::Tensor const&, c10::Scalar, c10::Scalar, std::string)
This likely happened in a call to OperatorHandle::typed<Return (Args...)>(). Please make sure that the function signature matches the signature in the operator registration call.
Exception raised from reportSignatureError at ../aten/src/ATen/core/dispatch/OperatorEntry.cpp:503 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::string) + 0x96 (0x7fd61b5e2276 in /home/anaconda3/envs/pt-custom/lib/python3.10/site-packages/torch/lib/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::string const&) + 0x64 (0x7fd61b58c394 in /home/anaconda3/envs/pt-custom/lib/python3.10/site-packages/torch/lib/libc10.so)
frame #2: c10::impl::OperatorEntry::reportSignatureError(c10::impl::CppSignature const&, c10::impl::OperatorEntry::CppSignatureWithDebug const&) const + 0x2aa (0x7fd6087783aa in /home/anaconda3/envs/pt-custom/lib/python3.10/site-packages/torch/lib/libtorch_cpu.so)
frame #3: c10::impl::OperatorEntry::assertSignatureIsCorrect(c10::impl::CppSignature const&, bool) const + 0x86 (0x7fd6087784c6 in /home/anaconda3/envs/pt-custom/lib/python3.10/site-packages/torch/lib/libtorch_cpu.so)
frame #4: torch::aot_inductor::AOTInductorModel::run_impl(AtenTensorOpaque**, AtenTensorOpaque**, void*, AOTIProxyExecutorOpaque*) + 0x43e9 (0x7fd6062d2839 in /home/dlrm/model_dlrm.so)
frame #5: torch::aot_inductor::AOTInductorModelContainer::run(AtenTensorOpaque**, AtenTensorOpaque**, void*, AOTIProxyExecutorOpaque*) + 0x1a8 (0x7fd6062fbee8 in /home/dlrm/model_dlrm.so)
frame #6: AOTInductorModelContainerRun + 0x7f (0x7fd6062d80bf in /home/dlrm/model_dlrm.so)
frame #7: torch::inductor::AOTIModelContainerRunner::run(std::vector<at::Tensor, std::allocator<at::Tensor> >&, AOTInductorStreamOpaque*) + 0xb2 (0x7fd60c2e1bd2 in /home/anaconda3/envs/pt-custom/lib/python3.10/site-packages/torch/lib/libtorch_cpu.so)
frame #8: torch::inductor::AOTIModelContainerRunnerCpu::run(std::vector<at::Tensor, std::allocator<at::Tensor> >&) + 0xc (0x7fd60c2e2c7c in /home/anaconda3/envs/pt-custom/lib/python3.10/site-packages/torch/lib/libtorch_cpu.so)
frame #9: main + 0xbf7 (0x5606b1a87876 in ./test_custom_ops)
frame #10: <unknown function> + 0x29d90 (0x7fd606787d90 in /lib/x86_64-linux-gnu/libc.so.6)
frame #11: __libc_start_main + 0x80 (0x7fd606787e40 in /lib/x86_64-linux-gnu/libc.so.6)
frame #12: _start + 0x25 (0x5606b1a86a55 in ./test_custom_ops)
terminate called after throwing an instance of 'std::runtime_error'
  what():  run_func_( container_handle_, input_handles.data(), input_handles.size(), output_handles.data(), output_handles.size(), cuda_stream_handle, proxy_executor_handle_) API call failed at ../torch/csrc/inductor/aoti_runner/model_container_runner.cpp, line 111
run_c.sh: line 10: 2433330 Aborted                 (core dumped) ./test_custom_ops
```


AOTInductor's generated signature for custom op

```
    static auto op_custom_addmm_1dbias_ = c10::Dispatcher::singleton()
    	.findSchemaOrThrow("custom::custom_addmm_1dbias", "")
    	.typed<at::Tensor(at::Tensor const& self, at::Tensor const& mat1, at::Tensor const& mat2, at::Scalar beta, at::Scalar alpha, std::string custom_op_name)>();
    auto buf0 = op_custom_custom_addmm_1dbias_.call(L__self___fc1_bias, arg2_1, reinterpret_tensor(L__self___fc1_weight, {10L, 16L}, {1L, 10L}, 0L), 1L, 1L, "custom::custom_addmm_1dbias");
```

### Versions

```
Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.1 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 3.28.1
Libc version: glibc-2.35

Python version: 3.10.15 (main, Oct  3 2024, 07:27:34) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-105-generic-x86_64-with-glibc2.35
Is CUDA available: N/A
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      48 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             128
On-line CPU(s) list:                0-127
Vendor ID:                          AuthenticAMD
Model name:                         AMD Ryzen Threadripper PRO 5995WX 64-Cores
CPU family:                         25
Model:                              8
Thread(s) per core:                 2
Core(s) per socket:                 64
Socket(s):                          1
Stepping:                           2
Frequency boost:                    enabled
CPU max MHz:                        7024.2178
CPU min MHz:                        1800.0000
BogoMIPS:                           5389.65
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca fsrm
Virtualization:                     AMD-V
L1d cache:                          2 MiB (64 instances)
L1i cache:                          2 MiB (64 instances)
L2 cache:                           32 MiB (64 instances)
L3 cache:                           256 MiB (8 instances)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-127
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 rstack overflow: Mitigation; safe RET, no microcode
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; Retpolines, IBPB conditional, IBRS_FW, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] flake8==6.1.0
[pip3] flake8-bugbear==23.12.2
[pip3] flake8-comprehensions==3.14.0
[pip3] flake8-executable==2.1.3
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-noqa==1.3.2
[pip3] flake8-pyi==23.11.0
[pip3] flake8_simplify==0.21.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.3
[pip3] torch==2.5.0+cpu
[pip3] torchaudio==2.5.0+cpu
[pip3] torchvision==0.20.0+cpu
[conda] numpy                     1.26.3                   pypi_0    pypi
[conda] torch                     2.5.0+cpu                pypi_0    pypi
[conda] torchaudio                2.5.0+cpu                pypi_0    pypi
[conda] torchvision               0.20.0+cpu               pypi_0    pypi
```

cc @chauhang @penguinwu @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan @angelayi @suo @ydwu4