RuntimeError: Can not iterate over a module list or tuple with a value that does not have a statically determinable length

Hello there,

I am trying to compile an SSD-based object detector using torch.jit.script(model), and I am getting this error message for the following code segment:

# apply multibox head to source layers
# sources: python list (length: 7)
# self.loc: nn.ModuleList (length: 7)
# self.conf: nn.ModuleList (length: 7)
for (x, l, c) in zip(sources, self.loc, self.conf):
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    loc.append(l(x).permute(0, 2, 3, 1).contiguous())
    conf.append(c(x).permute(0, 2, 3, 1).contiguous())

How can I fix this?

Note: I had to use for k,v in enumerate(module_list): x = v(x) for “expected integer literal for index” error in an earlier segment of the code and it worked. However, this error is different.

Thanks.

1 Like

Solved.

In case someone encounters the same issue, here is how to change the code to be able to compile it:

for i, (l, c) in enumerate(zip(self.loc, self.conf)):
   x = sources[i]
   loc.append(l(x).permute(0, 2, 3, 1).contiguous())
   conf.append(c(x).permute(0, 2, 3, 1).contiguous())
4 Likes