Repository: pytorch/TensorRT (language: python)

Issue:
🐛 [Bug] Inputs not correctly passed to `ts_convert_method_to_trt_engine` function
##  Bug Description

In v2.5:
https://github.com/pytorch/TensorRT/blob/f2e1e6c7d20068f6b21edbee4bdb4218d715dc75/py/torch_tensorrt/_compile.py#L357

In v2.4:
https://github.com/pytorch/TensorRT/blob/77278fe395d6ffdd456fd7a8a94852cd27ee63a9/py/torch_tensorrt/_compile.py#L333

Inputs not correctly passed to`ts_convert_method_to_trt_engine`  function

### Error logs

```
[2024-10-22T18:51:33.597Z]     engine_bytes = torch_tensorrt.convert_method_to_trt_engine(

[2024-10-22T18:51:33.598Z]   File "/usr/local/lib/python3.10/dist-packages/torch_tensorrt/_compile.py", line 355, in convert_method_to_trt_engine

[2024-10-22T18:51:33.598Z]     serialized_engine: bytes = ts_convert_method_to_trt_engine(

[2024-10-22T18:51:33.598Z]   File "/usr/local/lib/python3.10/dist-packages/torch_tensorrt/ts/_compiler.py", line 270, in convert_method_to_trt_engine

[2024-10-22T18:51:33.598Z]     module._c, method_name, _parse_compile_spec(compile_spec)

[2024-10-22T18:51:33.598Z]   File "/usr/local/lib/python3.10/dist-packages/torch_tensorrt/ts/_compile_spec.py", line 225, in _parse_compile_spec

[2024-10-22T18:51:33.598Z]     elif compile_spec["input_signature"] is not None:

[2024-10-22T18:51:33.598Z] KeyError: 'input_signature'
```
## Environment

```
PyTorch version: 2.5.0+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.30.2
Libc version: glibc-2.35

Python version: 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-122-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.6.20
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA GeForce RTX 3090 Ti
GPU 1: NVIDIA GeForce RTX 3090 Ti

Nvidia driver version: 535.183.01

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 cat_l2 invpcid_single cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi 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 avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr 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 and seccomp
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==7.1.1
[pip3] flake8-bugbear==24.2.6
[pip3] flake8-comprehensions==3.15.0
[pip3] mypy==1.11.2
[pip3] mypy-extensions==1.0.0
[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.5.2
[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] onnx-graphsurgeon==0.5.2
[pip3] onnxruntime==1.19.2
[pip3] optree==0.12.1
[pip3] pynvjitlink==0.2.3
[pip3] pytorch-ignite==0.4.11
[pip3] pytorch-triton==3.0.0+dedb7bdf3
[pip3] torch==2.5.0
[pip3] torch_tensorrt==2.5.0
[pip3] torchvision==0.20.0a0
[pip3] triton==3.1.0
[conda] Could not collect
```



Discussion / hints:

Fix patch:
diff --git a/py/torch_tensorrt/_compile.py b/py/torch_tensorrt/_compile.py
index cd49962c17..7c80a938f4 100644
--- a/py/torch_tensorrt/_compile.py
+++ b/py/torch_tensorrt/_compile.py
@@ -343,6 +343,15 @@ def convert_method_to_trt_engine(
         enabled_precisions if enabled_precisions is not None else {torch.float}
     )
 
+    if not arg_inputs and not inputs:
+        raise AssertionError("'arg_inputs' and 'inputs' should not both be None.")
+
+    elif arg_inputs and inputs:
+        raise AssertionError(
+            "'arg_inputs' and 'inputs' should not be used at the same time."
+        )
+    arg_inputs = arg_inputs or inputs
+
     module_type = _parse_module_type(module)
     target_ir = _get_target_fe(module_type, ir)
     if target_ir == _IRType.ts:
@@ -366,15 +375,6 @@ def convert_method_to_trt_engine(
         )
     elif target_ir == _IRType.dynamo:
         # Prepare torch and torchtrt inputs
-        if not arg_inputs and not inputs:
-            raise AssertionError("'arg_inputs' and 'inputs' should not both be None.")
-
-        elif arg_inputs and inputs:
-            raise AssertionError(
-                "'arg_inputs' and 'inputs' should not be used at the same time."
-            )
-        arg_inputs = arg_inputs or inputs
-
         if kwarg_inputs is None:
             kwarg_inputs = {}
 


Test patch:
diff --git a/tests/py/ts/api/test_classes.py b/tests/py/ts/api/test_classes.py
index ee94e01740..39983d29f8 100644
--- a/tests/py/ts/api/test_classes.py
+++ b/tests/py/ts/api/test_classes.py
@@ -232,7 +232,7 @@ def test_dynamic_shape(self):
 )
 class TestTorchTensorRTModule(unittest.TestCase):
     @staticmethod
-    def _get_trt_mod():
+    def _get_trt_mod(via_ts: bool = False):
         class Test(torch.nn.Module):
             def __init__(self):
                 super(Test, self).__init__()
@@ -244,9 +244,14 @@ def forward(self, x):
                 return out
 
         mod = torch.jit.script(Test())
-        test_mod_engine_str = torchtrt.ts.convert_method_to_trt_engine(
-            mod, "forward", inputs=[torchtrt.Input((2, 10))]
-        )
+        if via_ts:
+            test_mod_engine_str = torchtrt.ts.convert_method_to_trt_engine(
+                mod, "forward", inputs=[torchtrt.Input((2, 10))]
+            )
+        else:
+            test_mod_engine_str = torchtrt.convert_method_to_trt_engine(
+                mod, "forward", inputs=[torchtrt.Input((2, 10))]
+            )
         return TorchTensorRTModule(
             name="test",
             serialized_engine=test_mod_engine_str,
@@ -301,9 +306,12 @@ def forward(self, x):
             )
 
     def test_set_get_profile_path_prefix(self):
-        trt_mod = TestTorchTensorRTModule._get_trt_mod()
-        trt_mod.engine.profile_path_prefix = "/tmp/"
-        self.assertTrue(trt_mod.engine.profile_path_prefix == "/tmp/")
+        for trt_mod in (
+            TestTorchTensorRTModule._get_trt_mod(),
+            TestTorchTensorRTModule._get_trt_mod(via_ts=True),
+        ):
+            trt_mod.engine.profile_path_prefix = "/tmp/"
+            self.assertTrue(trt_mod.engine.profile_path_prefix == "/tmp/")
 
     def test_get_layer_info(self):
         """
@@ -321,11 +329,14 @@ def test_get_layer_info(self):
 
         import json
 
-        trt_mod = TestTorchTensorRTModule._get_trt_mod()
-        trt_json = json.loads(trt_mod.get_layer_info())
-        [self.assertTrue(k in trt_json.keys()) for k in ["Layers", "Bindings"]]
-        self.assertTrue(len(trt_json["Layers"]) == 2)
-        self.assertTrue(len(trt_json["Bindings"]) == 2)
+        for trt_mod in (
+            TestTorchTensorRTModule._get_trt_mod(),
+            TestTorchTensorRTModule._get_trt_mod(via_ts=True),
+        ):
+            trt_json = json.loads(trt_mod.get_layer_info())
+            [self.assertTrue(k in trt_json.keys()) for k in ["Layers", "Bindings"]]
+            self.assertTrue(len(trt_json["Layers"]) == 2)
+            self.assertTrue(len(trt_json["Bindings"]) == 2)
 
 
 if __name__ == "__main__":