TensorRT icon indicating copy to clipboard operation
TensorRT copied to clipboard

🐛 [Bug] torch.jit.frontend.NotSupportedError: Compiled functions can't take variable number of arguments or use keyword-only arguments with defaults

Open XWHtorrentx opened this issue 2 years ago • 7 comments

Error: torch.jit.frontend.NotSupportedError: Compiled functions can't take variable number of arguments or use keyword-only arguments with defaults: File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/utils/checkpoint.py", line 145 def checkpoint(function, *args, **kwargs): ~~~~~~~ <--- HERE

I was using Swin Transformer(source code [https://github.com/microsoft/Swin-Transformer/blob/main/models/swin_transformer.py] with a little specification on variable types) for inference. Torch-TensorRT worked well with mobilenet and resnet. but I don't know why this bug popped up.

Full info: Traceback (most recent call last): File "/home/user/Projects/.../swin_eval.py", line 43, in trt_model = torch_tensorrt.compile(model, inputs= [torch_tensorrt.Input((1, 3, 224, 224))]) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch_tensorrt/_compile.py", line 96, in compile ts_mod = torch.jit.script(module) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_script.py", line 1257, in script return torch.jit._recursive.create_script_module( File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_recursive.py", line 451, in create_script_module return create_script_module_impl(nn_module, concrete_type, stubs_fn) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_recursive.py", line 513, in create_script_module_impl script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_script.py", line 587, in _construct init_fn(script_module) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_recursive.py", line 491, in init_fn scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_recursive.py", line 513, in create_script_module_impl script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_script.py", line 587, in _construct init_fn(script_module) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_recursive.py", line 491, in init_fn scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_recursive.py", line 517, in create_script_module_impl create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_recursive.py", line 368, in create_methods_and_properties_from_stubs concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_recursive.py", line 838, in try_compile_fn return torch.jit.script(fn, _rcb=rcb) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/_script.py", line 1307, in script ast = get_jit_def(obj, obj.name) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/frontend.py", line 264, in get_jit_def return build_def(parsed_def.ctx, fn_def, type_line, def_name, self_name=self_name, pdt_arg_types=pdt_arg_types) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/frontend.py", line 302, in build_def param_list = build_param_list(ctx, py_def.args, self_name, pdt_arg_types) File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/jit/frontend.py", line 326, in build_param_list raise NotSupportedError(ctx_range, _vararg_kwarg_err) torch.jit.frontend.NotSupportedError: Compiled functions can't take variable number of arguments or use keyword-only arguments with defaults: File "/home/user/anaconda3/envs/torchtensorrt/lib/python3.8/site-packages/torch/utils/checkpoint.py", line 145 def checkpoint(function, *args, **kwargs): ~~~~~~~ <--- HERE

Environment:

  • Torch-TensorRT 1.0.0:
  • PyTorch 1.10.0:
  • CPU Architecture:x86
  • OS : CentOS 7
  • How you installed PyTorch (conda, pip, libtorch, source): Conda
  • Python version:3.8.13
  • CUDA version:11.6
  • GPU models and configuration:Nvidia RTX 3080Ti

XWHtorrentx avatar Apr 28 '22 02:04 XWHtorrentx

This is a limitation of TorchScript. Likely if you try to do torch.jit.script (which we do internally in torch_tensorrt.compile) you will see this error before passing it to torch_tensorrt.compile

narendasan avatar Apr 28 '22 17:04 narendasan

I got the same error for the following model:

class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = torch.nn.ModuleList([
            torch.nn.Conv2d(3, 32, 3,stride=1, padding=1),
            torch.nn.ReLU(inplace=True),
            torch.nn.BatchNorm2d(32),
            torch.nn.Conv2d(32, 32, 3,stride=1, padding=1),
            torch.nn.ReLU(inplace=True),
            torch.nn.BatchNorm2d(32),
            torch.nn.Conv2d(32, 32, 3,stride=1, padding=1),
            torch.nn.ReLU(inplace=True),
            torch.nn.BatchNorm2d(32),])

    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        # for _ in range(10):
        #     logits += 10
        return logits

Could you elaborate as to what exactly is the limitation of TorchScript?

sreddy-es avatar May 26 '22 00:05 sreddy-es

I have the same problem.

JiangongWang avatar Jun 25 '22 13:06 JiangongWang

<!-- A clear and concise description of what the bug is. -->
torch.jit.frontend.NotSupportedError: Compiled functions can't take variable number of arguments or use keyword-only arguments with defaults:
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/parallel/data_parallel.py", line 147
    def forward(self, *inputs, **kwargs):
                                ~~~~~~~ <--- HERE
        with torch.autograd.profiler.record_function("DataParallel.forward"):
            if not self.device_ids:

Anyone can help?

YasinTURKMENOGLU avatar Jun 29 '22 12:06 YasinTURKMENOGLU

Same here. Any idea how to fix it?

FiReTiTi avatar Jul 28 '22 07:07 FiReTiTi

Yes any leads on how to fix the issue ?

PrajwalCogniac avatar Aug 03 '22 21:08 PrajwalCogniac

As @narendasan mentioned, this is a limitation of Torchscript itself. If you need the model to be run in Torchscript, please file an issue to pytorch/pytorch.

Alternatively, in v1.2 Torch-TRT will have an FX frontend (Beta) which you can experiment with to workaround these Torchscript limitations! Details coming soon!

ncomly-nvidia avatar Aug 15 '22 16:08 ncomly-nvidia

I've got the same issue can any one please elaborate what exactly is the limitation and how to avoid it? thanks!

chava100 avatar Oct 24 '22 13:10 chava100

The way to avoid this issue is to change the input signature of the offending function to not use **kwargs and instead expand out the arguments.

narendasan avatar Dec 15 '22 17:12 narendasan

As @narendasan mentioned, this is a limitation of Torchscript itself. If you need the model to be run in Torchscript, please file an issue to pytorch/pytorch.

Alternatively, in v1.2 Torch-TRT will have an FX frontend (Beta) which you can experiment with to workaround these Torchscript limitations! Details coming soon!

Hi! Can you please help me understand how I can use Torch-TensorRT FX to workaround this limitation? I am trying to script my PyTorch model (trained on GPU) using torch.jit.script but I encounter the same error.

alarst13 avatar Jan 23 '23 02:01 alarst13

As @narendasan mentioned, this is a limitation of Torchscript itself. If you need the model to be run in Torchscript, please file an issue to pytorch/pytorch. Alternatively, in v1.2 Torch-TRT will have an FX frontend (Beta) which you can experiment with to workaround these Torchscript limitations! Details coming soon!

Hi! Can you please help me understand how I can use Torch-TensorRT FX to workaround this limitation? I am trying to script my PyTorch model (trained on GPU) using torch.jit.script but I encounter the same error.

have you solved it yet?

YoungofNUAA avatar Feb 20 '23 09:02 YoungofNUAA