KhronosGroup / NNEF-Tools

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

Tools do not work in Python 3.10 due to deprecated import in collections module #154

Closed evansmal closed 2 years ago

evansmal commented 2 years ago

Summary

Using the NNEF tools (convert, visualize, etc.) with Python 3.10 and later fails with the following error:

ImportError: cannot import name 'Sequence' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

This is due to a change in the collections module:

Remove deprecated aliases to Collections Abstract Base Classes from the collections module.

To reproduce this issue, install the tools locally and run the following with Python 3.8:

$ python3 -V
Python 3.10.4

$ python3 -m nnef_tools.convert -h
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/evan/.local/lib/python3.10/site-packages/nnef_tools-1.0-py3.10.egg/nnef_tools/convert.py", line 15, in <module>
  File "/home/evan/.local/lib/python3.10/site-packages/nnef_tools-1.0-py3.10.egg/nnef_tools/conversion/__init__.py", line 15, in <module>
  File "/home/evan/.local/lib/python3.10/site-packages/nnef_tools-1.0-py3.10.egg/nnef_tools/conversion/converter.py", line 17, in <module>
  File "/home/evan/.local/lib/python3.10/site-packages/nnef_tools-1.0-py3.10.egg/nnef_tools/model/__init__.py", line 15, in <module>
  File "/home/evan/.local/lib/python3.10/site-packages/nnef_tools-1.0-py3.10.egg/nnef_tools/model/graph.py", line 17, in <module>
ImportError: cannot import name 'Sequence' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

Possible Fix

It should be possible to fix this by updating the import statement in ./nnef_tools/model/graph.py from:

from collections import Sequence

to

from collections.abc import Sequence

I can submit a patch for this. Let me know if this is helpful.

evansmal commented 2 years ago

Locally I've applied the following patch to test that the proposed fix works:

diff --git a/nnef_tools/model/graph.py b/nnef_tools/model/graph.py
index b3c88b0..cd4b37c 100644
--- a/nnef_tools/model/graph.py
+++ b/nnef_tools/model/graph.py
@@ -14,7 +14,7 @@

 from __future__ import division, print_function, absolute_import

-from collections import Sequence
+from collections.abc import Sequence
 from functools import reduce

 import six

You should be able to reproduce this with the following Dockerfile:

FROM python:3.10.1

RUN pip3 install numpy future typing six 

COPY ./fix_import.patch /

RUN git clone https://github.com/KhronosGroup/NNEF-Tools.git && \
    cd /NNEF-Tools && git apply -vvvv /fix_import.patch && \
    python setup.py install 

CMD python3 -m nnef_tools.convert

Run with the following commands (put patch from above next to Dockerfile):

$> docker build -t nnef_tools_fix .
$> docker run nnef_tools_fix
usage: convert.py [-h] --input-model INPUT_MODEL [--output-model OUTPUT_MODEL]
                  --input-format {tf,tflite,onnx,nnef,caffe2,caffe}
                  --output-format {tf,tflite,onnx,nnef,caffe2}
                  [--input-shapes INPUT_SHAPES]
                  [--io-transpose [IO_TRANSPOSE ...]] [--fold-constants]
                  [--optimize] [--dequantize]
                  [--custom-converters CUSTOM_CONVERTERS [CUSTOM_CONVERTERS ...]]
                  [--custom-shapes CUSTOM_SHAPES [CUSTOM_SHAPES ...]]
                  [--custom-fragments CUSTOM_FRAGMENTS [CUSTOM_FRAGMENTS ...]]
                  [--custom-optimizers CUSTOM_OPTIMIZERS [CUSTOM_OPTIMIZERS ...]]
                  [--mirror-unsupported] [--generate-custom-fragments]
                  [--keep-io-names] [--decompose [DECOMPOSE ...]]
                  [--input-names INPUT_NAMES [INPUT_NAMES ...]]
                  [--output-names OUTPUT_NAMES [OUTPUT_NAMES ...]]
                  [--static-only] [--tensor-mapping [TENSOR_MAPPING]]
                  [--annotate-shapes] [--compress [COMPRESS]]
convert.py: error: the following arguments are required: --input-model, --input-format, --output-format