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

nbansal90: <p>Hello Folks,</p>
<p>I am trying to trace my custom model using <code>torch.jit.trace</code> for getting mobile runnable model. I am following the step-by-step procedure mentioned in the  <a href="https://pytorch.org/mobile/android/" rel="noopener nofollow ugc">Pytorch Mobile</a> documentation page.</p>
<p>However, for my model, it fails during the tracing process with the following error: <code>Error: :List trace inputs must have elements</code>. I find that there are certain variables which are empty in my case.</p>
<pre><code class="lang-auto">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],
</code></pre>
<p>My understanding is because there are certain data holders (<code>label_target</code> and <code>occ_target</code> )  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.</p>
<p>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.</p>
<p>Regards,<br>
Nitin</p>

ptrblck: <p>You could use the <a href="https://pytorch.org/docs/stable/generated/torch.jit.unused.html#torch.jit.unused"><code>torch.jit.unused</code></a> 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.</p>

nbansal90: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> 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 <code>forward</code> function, and are not used during inference/testing.</p>
<p>So, is there any way through which I could specifically ignore these variables and not the functions itself?</p>
<p>Regards,<br>
Nitin Bansal</p>

ptrblck: <p>That’s a good point. Could you check if defining the optional arguments as:</p>
<pre><code class="lang-python">maybe_unused_argument=None,  # type: Optional[List[str, Tensor]]
</code></pre>
<p>would work?<br>
I checked some <code>torchvision</code> detection models as it’s used e.g. <a href="https://github.com/pytorch/vision/blob/7cf0f4cc1801ff1892007c7a11f7c35d8dfb7fd0/torchvision/models/detection/roi_heads.py#L732">here</a> for the <code>target</code> input argument to <code>forward</code>, which is only needed during training.</p>

nbansal90: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> ! I think I did something similar for my scenario, and was able to resolve the issue.</p>