Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Relay][Frontend][TF] Fix Size operator #4175

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/tvm/relay/frontend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def get_relay_op(op_name):
op = None
else:
# try search op in various modules
for candidate in (_op, _op.nn, _op.image, _op.vision):
for candidate in (_op, _op.nn, _op.image, _op.vision, _op.contrib):
op = getattr(candidate, op_name, None)
if op is not None:
break
Expand Down
9 changes: 8 additions & 1 deletion python/tvm/relay/frontend/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,13 @@ def _impl(inputs, attr, params):
return _op.multiply(difference, difference)
return _impl

def _size():
def _impl(inputs, attr, params):
new_attr = attr
new_attr['out_type'] = attr['out_type'].name
return AttrCvt('ndarray_size', transforms={'out_type' : 'dtype'})(inputs, new_attr)
return _impl

# compatible operators that do NOT require any conversion.
_identity_list = []

Expand Down Expand Up @@ -1410,7 +1417,7 @@ def _impl(inputs, attr, params):
'Shape' : _shape(),
'Sigmoid' : AttrCvt('sigmoid'),
'Sign' : AttrCvt('sign'),
'Size' : AttrCvt('ndarray_size'),
'Size' : _size(),
'Slice' : _slice(),
'Softmax' : _softmax(),
'Softplus' : _softplus(),
Expand Down
13 changes: 8 additions & 5 deletions tests/python/frontend/tensorflow/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -2184,15 +2184,18 @@ def check_mean(ishape, **kwargs):
def test_forward_size():
def check_size(ishape):
np_input = np.random.uniform(size=ishape).astype(np.float32)

# if all dimensions are constant, TF will optimize away size operator into constant
tf_input_shape = list(np_input.shape)
tf_input_shape[0] = None

with tf.Graph().as_default():
input = tf.placeholder(shape=np_input.shape, dtype=np_input.dtype, name='input')
input = tf.placeholder(shape=tf_input_shape, dtype=np_input.dtype, name='input')
tf.size(input, name='size')
compare_tf_with_tvm([np_input], ['input:0'], 'size:0')

if tf.__version__ < LooseVersion('1.1'):
check_size((10, 8, 16, 32))
check_size((10,))
check_size(())
check_size((10, 8, 16, 32))
check_size((10,))

#######################################################################
# All, Max, Min
Expand Down