Repository: Project-MONAI/MONAI

Issue:
Unsupported data for device specification
```
======================================================================
ERROR: test_onnx_trt_export_1_fp16 (tests.test_bundle_trt_export.TestTRTExport)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/workspace/MONAI/tests/utils.py", line 809, in command_line_tests
    normal_out = subprocess.run(cmd, env=test_env, check=True, capture_output=True)
  File "/usr/lib/python3.10/subprocess.py", line 526, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['python', '-m', 'monai.bundle', 'trt_export', 'network_def', '--filepath', '/tmp/tmptbll1a8a/model_trt_fp16.ts', '--meta_file', '/workspace/MONAI/tests/testing_data/metadata.json', '--config_file', "['/workspace/MONAI/tests/testing_data/inference.json','/tmp/tmptbll1a8a/def_args.yaml']", '--ckpt_file', '/tmp/tmptbll1a8a/model.pt', '--args_file', '/tmp/tmptbll1a8a/def_args.yaml', '--precision', 'fp16', '--use_onnx', 'True', '--input_shape', '[1, 1, 96, 96, 96]', '--dynamic_batch', '[1, 4, 8]']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/parameterized/parameterized.py", line 620, in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
  File "/workspace/MONAI/tests/test_bundle_trt_export.py", line 126, in test_onnx_trt_export
    command_line_tests(cmd)
  File "/workspace/MONAI/tests/utils.py", line 815, in command_line_tests
    raise RuntimeError(f"subprocess call error {e.returncode}: {errors}, {output}") from e
RuntimeError: subprocess call error 1: b'There is no dynamic batch range. The converted model only takes [1, 1, 96, 96, 96] shape input.
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/workspace/MONAI/monai/bundle/__main__.py", line 31, in <module>
    fire.Fire()
  File "/usr/local/lib/python3.10/dist-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/usr/local/lib/python3.10/dist-packages/fire/core.py", line 475, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/usr/local/lib/python3.10/dist-packages/fire/core.py", line 691, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/workspace/MONAI/monai/bundle/scripts.py", line 1516, in trt_export
    _export(
  File "/workspace/MONAI/monai/bundle/scripts.py", line 1091, in _export
    net = converter(model=net, **kwargs)
  File "/workspace/MONAI/monai/networks/utils.py", line 962, in convert_to_trt
    trt_model = _onnx_trt_compile(
  File "/workspace/MONAI/monai/networks/utils.py", line 852, in _onnx_trt_compile
    trt_model = torch_tensorrt.ts.embed_engine_in_new_module(
  File "/usr/local/lib/python3.10/dist-packages/torch_tensorrt/ts/_compiler.py", line 317, in embed_engine_in_new_module
    _parse_device(device),
  File "/usr/local/lib/python3.10/dist-packages/torch_tensorrt/ts/_compile_spec.py", line 135, in _parse_device
    raise ValueError(
ValueError: Unsupported data for device specification. Expected either a dict, torch_tensorrt.Device or torch.Device
```

Root cause is that the order of arguments has changed.
https://github.com/pytorch/TensorRT/blob/ad74a735056667726692c49a175a790647ef889e/py/torch_tensorrt/ts/_compiler.py#L279-L283
https://github.com/pytorch/TensorRT/blob/7d1d807731c95ffbaa980a3742e289d4184b7871/py/torch_tensorrt/ts/_compiler.py#L260-L264

https://github.com/Project-MONAI/MONAI/blob/e9e273858b6904659b0cd581d357662dc5c782f6/monai/networks/utils.py#L853
Unsupported data for device specification
```
======================================================================
ERROR: test_onnx_trt_export_1_fp16 (tests.test_bundle_trt_export.TestTRTExport)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/workspace/MONAI/tests/utils.py", line 809, in command_line_tests
    normal_out = subprocess.run(cmd, env=test_env, check=True, capture_output=True)
  File "/usr/lib/python3.10/subprocess.py", line 526, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['python', '-m', 'monai.bundle', 'trt_export', 'network_def', '--filepath', '/tmp/tmptbll1a8a/model_trt_fp16.ts', '--meta_file', '/workspace/MONAI/tests/testing_data/metadata.json', '--config_file', "['/workspace/MONAI/tests/testing_data/inference.json','/tmp/tmptbll1a8a/def_args.yaml']", '--ckpt_file', '/tmp/tmptbll1a8a/model.pt', '--args_file', '/tmp/tmptbll1a8a/def_args.yaml', '--precision', 'fp16', '--use_onnx', 'True', '--input_shape', '[1, 1, 96, 96, 96]', '--dynamic_batch', '[1, 4, 8]']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/parameterized/parameterized.py", line 620, in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
  File "/workspace/MONAI/tests/test_bundle_trt_export.py", line 126, in test_onnx_trt_export
    command_line_tests(cmd)
  File "/workspace/MONAI/tests/utils.py", line 815, in command_line_tests
    raise RuntimeError(f"subprocess call error {e.returncode}: {errors}, {output}") from e
RuntimeError: subprocess call error 1: b'There is no dynamic batch range. The converted model only takes [1, 1, 96, 96, 96] shape input.
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/workspace/MONAI/monai/bundle/__main__.py", line 31, in <module>
    fire.Fire()
  File "/usr/local/lib/python3.10/dist-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/usr/local/lib/python3.10/dist-packages/fire/core.py", line 475, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/usr/local/lib/python3.10/dist-packages/fire/core.py", line 691, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/workspace/MONAI/monai/bundle/scripts.py", line 1516, in trt_export
    _export(
  File "/workspace/MONAI/monai/bundle/scripts.py", line 1091, in _export
    net = converter(model=net, **kwargs)
  File "/workspace/MONAI/monai/networks/utils.py", line 962, in convert_to_trt
    trt_model = _onnx_trt_compile(
  File "/workspace/MONAI/monai/networks/utils.py", line 852, in _onnx_trt_compile
    trt_model = torch_tensorrt.ts.embed_engine_in_new_module(
  File "/usr/local/lib/python3.10/dist-packages/torch_tensorrt/ts/_compiler.py", line 317, in embed_engine_in_new_module
    _parse_device(device),
  File "/usr/local/lib/python3.10/dist-packages/torch_tensorrt/ts/_compile_spec.py", line 135, in _parse_device
    raise ValueError(
ValueError: Unsupported data for device specification. Expected either a dict, torch_tensorrt.Device or torch.Device
```

Root cause is that the order of arguments has changed.
https://github.com/pytorch/TensorRT/blob/ad74a735056667726692c49a175a790647ef889e/py/torch_tensorrt/ts/_compiler.py#L279-L283
https://github.com/pytorch/TensorRT/blob/7d1d807731c95ffbaa980a3742e289d4184b7871/py/torch_tensorrt/ts/_compiler.py#L260-L264

https://github.com/Project-MONAI/MONAI/blob/e9e273858b6904659b0cd581d357662dc5c782f6/monai/networks/utils.py#L853


Discussion:

Resolving patch:
diff --git a/monai/networks/utils.py b/monai/networks/utils.py
--- a/monai/networks/utils.py
+++ b/monai/networks/utils.py
@@ -850,7 +850,10 @@ def _onnx_trt_compile(
 
     # wrap the serialized TensorRT engine back to a TorchScript module.
     trt_model = torch_tensorrt.ts.embed_engine_in_new_module(
-        f.getvalue(), torch.device(f"cuda:{device}"), input_names, output_names
+        f.getvalue(),
+        device=torch.device(f"cuda:{device}"),
+        input_binding_names=input_names,
+        output_binding_names=output_names,
     )
     return trt_model
 


Test patch: