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

Conversion of tf.add_n to NNEF fails #142

Closed dvorotnev closed 3 years ago

dvorotnev commented 3 years ago

I am trying to save and to convert a simple tensorflow network:

import tensorflow as tf
import nnef_tools.io.tf.graphdef as graphdef

def testnet_add_n():
    x = tf.placeholder(tf.float32, shape=[6, 32, 32, 3], name='input')
    with tf.variable_scope('add_n'):
        y = tf.get_variable('y', shape=[6, 32, 32, 3], initializer=tf.random_normal_initializer(seed=42))
        return tf.add_n([x, y, x, x])

tf.reset_default_graph()
with tf.Session() as sess:
    result = testnet_add_n()
    sess.run(tf.global_variables_initializer())
    graphdef.save_default_graph("model.pb", session=sess, outputs={result: "output"})

with such commands:

python ./test.py
python -m nnef_tools.convert --input-format=tf --output-format=nnef --input-model=./model.pb --output-model=model.nnef

but the conversion fails with this message:

Conversion for operation type(s) 'AddN' is not implemented

I think that a converter transform expression for AddN TF function to NNEF add_n should be something like this:

    'AddN':
        Transform(
            type='add_n',
            inputs=['!I[:]'],
            outputs='!transpose_like(O[0], I[0])'
        ),

Note, that it is without branching, because TF does not support an empty list as an inputs argument and throws an exception in this case:

Traceback (most recent call last):
  File "./test.py", line 14, in <module>
    result = testnet_add_n()
  File "./test.py", line 8, in testnet_add_n
    return tf.add_n([])
  File "/nix/store/x20b4bb14jsia0nx3ksjcb3n37fdhxzm-python3-3.7.9-env/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)
  File "/nix/store/x20b4bb14jsia0nx3ksjcb3n37fdhxzm-python3-3.7.9-env/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py", line 2869, in add_n
    raise ValueError("inputs must be a list of at least one "
ValueError: inputs must be a list of at least one Tensor/IndexedSlices with the same dtype and shape
gyenesvi commented 3 years ago

Thanks, I have added this conversion.

dvorotnev commented 3 years ago

Thank you!