Error: :List trace inputs must have elements, While running torch.jit.trace

Hello Folks,

I am trying to trace my custom model using torch.jit.trace for getting mobile runnable model. I am following the step-by-step procedure mentioned in the Pytorch Mobile documentation page.

However, for my model, it fails during the tracing process with the following error: Error: :List trace inputs must have elements. I find that there are certain variables which are empty in my case.

File "mobile_model.py", line 135, in profile_sample
    traced_script_module = torch.jit.trace(model, sample)
  File "/home/us000146/anaconda3/envs/selfsupervised/lib/python3.6/site-packages/torch/jit/_trace.py", line 742, in trace
    _module_class,
  File "/home/us000146/anaconda3/envs/selfsupervised/lib/python3.6/site-packages/torch/jit/_trace.py", line 940, in trace_module
    _force_outplace,
RuntimeError: Tracer cannot infer type of ({'label_target': [], 'occ_target': [], 'coords_': [tensor([[ 0,  4, 13,  2],

My understanding is because there are certain data holders (label_target and occ_target ) which are empty during the tracing stage which causes this issue. If that is indeed the case, How do I handle this scenario, considering that I can not do away with these variables and still want the tracing part to complete successfully.

PS: I am doing training of the model in stages where in some stage these variable are used but on another where I need to trace the model, they are not.

Regards,
Nitin

You could use the torch.jit.unused decorator to mark method which should be ignored while scripting the model and dispatch to the appropriate method which does not need the empty input arguments as shown in the linked docs.

@ptrblck Thank you indeed for your prompt reply. Specific to my scenario, it is only that certain variables are unused not essentially the function under which they are defined. They are actually inside the forward function, and are not used during inference/testing.

So, is there any way through which I could specifically ignore these variables and not the functions itself?

Regards,
Nitin Bansal

That’s a good point. Could you check if defining the optional arguments as:

maybe_unused_argument=None,  # type: Optional[List[str, Tensor]]

would work?
I checked some torchvision detection models as it’s used e.g. here for the target input argument to forward, which is only needed during training.

1 Like

Thanks @ptrblck ! I think I did something similar for my scenario, and was able to resolve the issue.

1 Like