KhronosGroup / NNEF-Tools

The NNEF Tools repository contains tools to generate and consume NNEF documents
https://www.khronos.org/nnef
222 stars 58 forks source link

NNEF optimizer fails with KeyError #143

Closed dvorotnev closed 3 years ago

dvorotnev commented 3 years ago

I am trying to save and to convert the Emotion FerPlus network from ONNX to NNEF with --optimize flag using the following commands:

wget https://github.com/onnx/models/raw/master/vision/body_analysis/emotion_ferplus/model/emotion-ferplus-8.tar.gz
tar -xf emotion-ferplus-8.tar.gz
python -m nnef_tools.convert --input-format=onnx --output-format=nnef --optimize --input-model=emotion_ferplus/model.onnx --output-model=./model.nnef

But the optimizer fails with this error:

Traceback (most recent call last):
  File "/nix/store/xkvjcsv75k1z7yjdwglz423wfl8biv0p-python3-3.7.9/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/nix/store/xkvjcsv75k1z7yjdwglz423wfl8biv0p-python3-3.7.9/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/nix/store/c7rf7k3264gxx4kfcp3qqzdvhgn9g9rg-python3.7-nnef-tools-python-0.3/lib/python3.7/site-packages/nnef_tools/convert.py", line 338, in <module>
    exit(main(parser.parse_args()))
  File "/nix/store/c7rf7k3264gxx4kfcp3qqzdvhgn9g9rg-python3.7-nnef-tools-python-0.3/lib/python3.7/site-packages/nnef_tools/convert.py", line 285, in main
    graph = optimizer(graph)
  File "/nix/store/c7rf7k3264gxx4kfcp3qqzdvhgn9g9rg-python3.7-nnef-tools-python-0.3/lib/python3.7/site-packages/nnef_tools/optimization/nnef_optimizer.py", line 66, in __call__
    changed |= replace_chain(graph, ['matmul', {'add', 'sub'}], self._merge_matmul_bias)
  File "/nix/store/c7rf7k3264gxx4kfcp3qqzdvhgn9g9rg-python3.7-nnef-tools-python-0.3/lib/python3.7/site-packages/nnef_tools/model/utils.py", line 115, in replace_chain
    if chain is not None and func(*chain) is not False:
  File "/nix/store/c7rf7k3264gxx4kfcp3qqzdvhgn9g9rg-python3.7-nnef-tools-python-0.3/lib/python3.7/site-packages/nnef_tools/optimization/nnef_optimizer.py", line 192, in _merge_matmul_bias
    if matmul.attribs['transposeA']:
KeyError: 'transposeA'

I have created a pull request with bug fix, but I am not sure that it is right.

gyenesvi commented 3 years ago

I have added a fix, can you retry?

dvorotnev commented 3 years ago

Now it works. Thanks!