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 slice when begin or size is not Const #4372

Merged
merged 2 commits into from
Nov 21, 2019
Merged

[Relay][Frontend][TF] Fix slice when begin or size is not Const #4372

merged 2 commits into from
Nov 21, 2019

Conversation

lsy643
Copy link
Contributor

@lsy643 lsy643 commented Nov 19, 2019

For Relay Frontend of Tensorflow, def _slice can not handle begin or size is not Const. _infer_value_simulated is used to handle these conditions

@yzhliu
Copy link
Member

yzhliu commented Nov 19, 2019

@yongwww could you take a look?

try:
begin = _get_list_param(params, inputs[1])
except:
begin = _infer_value_simulated(inputs[1], params).asnumpy()[0]
Copy link
Member

@yongwww yongwww Nov 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about begin = _infer_value(inputs[1], params).asnumpy().tolist()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think _infer_value_simulated is better here. _infer_value assumes that all of the inputs leading up to inputs[1] are constant, _infer_value_simulated does not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether _infer_value_simulated should be used here. size parameter can be a data dependent expression. If we want to infer the correct value, probably we should enforce all variables in params. For symbolic begin/size slice, it will be covered in #4312.

Copy link
Member

@yongwww yongwww Nov 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_infer_value_simulated is not able to handle data dependent cases. Let's use _infer_value here, if _infer_value doesn't help, then we have to use dynamic strided_slice in pr 4312. @lsy643 let us know if _infer_value doesn't work for you

Copy link
Contributor Author

@lsy643 lsy643 Nov 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yongwww _infer_value works for me. It seems _infer_value_simulated use _infer_value inside, so why prefer one over another?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_infer_value_simulated will create dummy data if some variables don't appear in params. In data dependent case, this will cause problem.

try:
size = _get_list_param(params, inputs[2])
except:
size = _infer_value_simulated(inputs[2], params).asnumpy()[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same as begin



def test_forward_slice():
_test_forward_slice_operation_input([1, 1], 0, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test cases to cover begin & size as tensor

Copy link
Contributor Author

@lsy643 lsy643 Nov 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yongwww begin and size as tensor are already covered. In the _test_forward_slice_operation_input([1, 1], 0, 2) , the input 0 and input 2 will be changed into tensors after tf.expand_dimes

def _test_forward_slice_operation_input(input_value, begin_value, size_value):
    input_data = np.array(input_value, dtype=np.float32)
    with tf.Graph().as_default():
        input_tensor = tf.placeholder(
            shape=input_data.shape, dtype=input_data.dtype, name="input")
        begin_tensor = tf.expand_dims(begin_value, axis=0)
        size_tensor = tf.expand_dims(size_value, axis=0)
        slice_tensor = tf.slice(input_tensor, begin_tensor, size_tensor, name='slice_output')
        compare_tf_with_tvm([input_data], ['input:0'], 'slice_output:0')

size = _get_list_param(params, inputs[2])
try:
begin = _get_list_param(params, inputs[1])
except:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably only catch the exceptions we expect to see here (like IndexError or AttributeError), and let the others go through.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

try:
begin = _get_list_param(params, inputs[1])
except:
begin = _infer_value_simulated(inputs[1], params).asnumpy()[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether _infer_value_simulated should be used here. size parameter can be a data dependent expression. If we want to infer the correct value, probably we should enforce all variables in params. For symbolic begin/size slice, it will be covered in #4312.

@kevinthesun kevinthesun added the status: need update need update based on feedbacks label Nov 20, 2019
Copy link
Contributor

@kevinthesun kevinthesun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kevinthesun
Copy link
Contributor

Thank you @lsy643 This is now merged.

@kevinthesun kevinthesun merged commit 0bbdad4 into apache:master Nov 21, 2019
zxy844288792 pushed a commit to zxy844288792/tvm that referenced this pull request Nov 26, 2019
…he#4372)

* fix slice bug when input is param

* use _infer_value rather than _infer_value_simulated
yongwww pushed a commit to neo-ai/tvm that referenced this pull request Nov 26, 2019
…he#4372)

* fix slice bug when input is param

* use _infer_value rather than _infer_value_simulated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: need update need update based on feedbacks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants