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

[NNVM] Add argmax and argmin operations from topi #1462

Merged
merged 1 commit into from
Jul 25, 2018

Conversation

sergei-mironov
Copy link
Contributor

Makes argmax operation accessible from NNVM.

There is an issue regarding output types, since the test seems to treats integer value, returned by argmax, as a floating-point. I'd like to ask for advice here. Probably, graph_runtime requires additional changes.

@sergei-mironov sergei-mironov force-pushed the add-nnvm-argmax branch 2 times, most recently from e488393 to aac3c26 Compare July 20, 2018 14:37
@sergei-mironov sergei-mironov changed the title [NNVM] Add argmax operation from topi [WIP][NNVM] Add argmax operation from topi Jul 23, 2018
@sergei-mironov sergei-mironov force-pushed the add-nnvm-argmax branch 2 times, most recently from 20d534c to 20bd14e Compare July 23, 2018 15:36
@sergei-mironov sergei-mironov changed the title [WIP][NNVM] Add argmax operation from topi [NNVM] Add argmax and argmin operations from topi Jul 23, 2018
@tqchen
Copy link
Member

tqchen commented Jul 23, 2018

Thanks for the contribution, please request reviews from reviewers

@tqchen
Copy link
Member

tqchen commented Jul 24, 2018

@nishi-t @srkreddy1238 can you please review this?

a = f(x, axis=axis)
if keepdims:
shape = list(a.shape)
shape.insert(axis,1)
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 that it will not work well when axis is None
The following code may be helpful:
https://github.com/dmlc/tvm/blob/master/topi/tests/python/test_topi_reduce.py#L11-L15

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I am about to fix it, but see my comment below.

Copy link
Contributor

@nishi-t nishi-t Jul 25, 2018

Choose a reason for hiding this comment

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

Ok, I got it. thanks

verify_reduce((4, 4, 3), _with_keepdims(np.argmax), sym.argmax, otype='int32')
verify_reduce((4, 4, 3), _with_keepdims(np.argmax), sym.argmax, oshape=(4,1,3), otype='int32', axis=1, keepdims=True)
verify_reduce((4, 4, 3), _with_keepdims(np.argmin), sym.argmin, otype='int32')
verify_reduce((4, 4, 3), _with_keepdims(np.argmin), sym.argmin, oshape=(4,1,3), otype='int32', axis=1, keepdims=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a pattern which keepdims is True and axis is None?

Copy link
Contributor Author

@sergei-mironov sergei-mironov Jul 25, 2018

Choose a reason for hiding this comment

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

Indeed, axis=None doesn't work, but the wrong shape is not the only reason. Looking into sources I would say that this value of axis is forbidden for every reduce operation in NNVM. Axis is defined as struct ReduceParams { .. TShape axis; .. }, but FieldEntryBase::Set doesn't have specializations for TShape and the default version rejects Nones with

NNVMError: Invalid Parameter format for axis expect  but value='None'

(note also missing _type name)

I would prefer to not include None usecases in the current PR, but solve this issue separately.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I got it.

@@ -114,6 +116,22 @@ def test_reduce():
verify_reduce((4, 4, 3), np.sum, sym.sum, axis=(0, 2))
verify_reduce((4, 4, 3), np.sum, sym.sum)

def _with_keepdims(f):
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a matter of taste, but I'd suggest that the _with_keepdims definition is before all verify_reduce

def test_reduce():
    def _with_keepdims(f):
        ...
    verify_reduce((2, 3, 4), np.max, sym.max, axis=1, keepdims=True)
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

return true;
}

NNVM_REGISTER_BASE_REDUCE_OP(argmax)
Copy link
Contributor

@nishi-t nishi-t Jul 25, 2018

Choose a reason for hiding this comment

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

NNVM_REGISTER_BASE_REDUCE_OP uses ReduceParam, but do argmax and argmin support the exclude option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. While ops do use this option (see 01a7df4#diff-ef68c30ca3f3d21c6d5b8dd15cc570a1R315), it is not covered by tests. Should fix it soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, please check.

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that argmax doesn't have exclude option and the axis parameter is only an int value in mxnet and numpy. Are these little different from other reduction operators? If so, I think that argmax and argmin should have a parameter struct for them without ReduceParam.
@tqchen @srkreddy1238 Could you tell us your opinion about it?

Copy link
Member

Choose a reason for hiding this comment

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

For now, it is nice to keep all reduction operator consistent, as I assume the logics are going to be the same

Copy link
Contributor

@nishi-t nishi-t Jul 25, 2018

Choose a reason for hiding this comment

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

@tqchen Thank you for your comments. I got it.
@grwlf Thank you for your work. Please don't mind my above comment.

@sergei-mironov sergei-mironov force-pushed the add-nnvm-argmax branch 3 times, most recently from 5e9b74d to cbada25 Compare July 25, 2018 16:19
Copy link
Contributor

@nishi-t nishi-t left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@tqchen tqchen merged commit fc28cfa into apache:master Jul 25, 2018
@sergei-mironov sergei-mironov deleted the add-nnvm-argmax branch July 31, 2018 09:28
tqchen pushed a commit to tqchen/tvm that referenced this pull request Aug 4, 2018
sergei-mironov added a commit to sergei-mironov/tvm that referenced this pull request Aug 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants