KeyError in from_pytorch()

I tried to implement from_pytorch.py on x86_64 linux(Ubuntu) following the tutorial of Compile PyTorch Models. However, it raises a KeyError: ‘279’ at this line inputs = _get_op_inputs(op_node, outputs, output_index_map) where

def _get_op_inputs(op_node, outputs, output_index_map):
  input_names = [output_index_map[name]
               for name in _get_input_names(op_node)]
  return [outputs[name] for name in input_names]

It raises the error when dealing with op_node %281 : int[] = prim::ListConstruct(%279, %280), scope: __module.layer2/__module.layer2.0/__module.layer2.0.downsample/__module.layer2.0.downsample.0 It seems like 279 or 280 is not included in output_index_map, which raises an KeyError. torch version=1.4.0, torchvision version=0.5.0 I wonder why this happens and how to solve it. Thank you.

Thank you for trying it out, can you post your script?

Thank you for your reply. The script is the same as the tutorial from_pytorch.py It works on one computer but has bugs above on another computer. I wonder if it is the configuration.

I tried the tutorial now on torch 1.4 and torchvision 0.5, it works. Maybe add print(graph) below to see if the graph has changed between your two envs.

I compared the graphs of two envs and found they are the same. And the output_index_map is the same, the only difference is the orders of keys. The env that does not work also has KeyError: 'input.5' , and I suppose that all input.x(except input.1) will have KeyError under that env. But I don’t quite understand why the env that works for from_pytorch.py could find key ‘input.5’ in output_index_map. Since I print(output_index_map) and found no key named ‘input.5’. Is there any conversion of the keys? Thanks

output_index_map is gradually updated to have more keys during conversion, so even if when you do print(output_index_map) the key is not there, it might be added later.

Thanks. I did not realize that output_index_map is gradually updated. I think probably it is the order of operators dictionary in parse_operators() function matters. Each node_name has dependencies of the previous ones. So if output_index_map is updated out of order, there will be a KeyError. Though I did not figure out why the operators dictionary has random orders. Thanks again for your help.

hmm that’s a good point. The output of graph.nodes() is topologically sorted, but we are making a dict out of it, that may break the topological order guarantee.

Can you change ops dict at

to be a list of (node_name, node)? That will guarantee the topological order.

You also need to remove .items() here

ok I found the answer to this issue. In Python 3.6 and above, the order of key is insertion order, see https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6

So our impl is broken for py 3.5 and below, can you confirm if one of your python version is 3.5 or lower?

cc @alexwong @zhiics @jwfromm

My python3 version is 3.5.2. Thanks for your help!

You can send a PR to fix this, I can merge asap