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

[ONNX] Add CumSum operator to ONNX frontend #7391

Merged
merged 9 commits into from
Feb 9, 2021

Conversation

echuraev
Copy link
Contributor

@echuraev echuraev commented Feb 2, 2021

Thanks for contributing to TVM! Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from Reviewers by @ them in the pull request thread.

Copy link
Contributor

@mbrookhart mbrookhart left a comment

Choose a reason for hiding this comment

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

Fix lint?

I'm a little confused, I'm looking at the ONNX spec, and I only see 2 inputs, not three. What is the third input you're using for datatype?
https://github.com/onnx/onnx/blob/master/docs/Operators.md#cumsum

We should probably either handle or error when the exclusive or reverse attributes are true.

@mbrookhart
Copy link
Contributor

Oh, I just noticed it's a draft :) You're probably not ready for review, sorry.

@echuraev echuraev force-pushed the cumsum_onnx_frontend branch 2 times, most recently from c265b41 to 23404a0 Compare February 3, 2021 14:08
@echuraev
Copy link
Contributor Author

echuraev commented Feb 3, 2021

@mbrookhart, thank you for you questions. Now I added error generation for exclusive and reverse attributes, but I'm planning to implement them in this PR.

@masahi
Copy link
Member

masahi commented Feb 3, 2021

exclusive can be easily supported by cumsum(data) - data. Similarly, I expect reverse can also be supported by composing with reverse op. But I think this attribute is weird (why would anyone one want to do reverse cumsum), so it might not be worth it.

It's strange ONNX Cumsum doesn't have output dtype attribute. Without it, there could be overflow issues for int32 input. Also it wouldn't work for boolean input, which is a common input to cumsum. I guess they expect casting to be done by users beforehand.

@echuraev echuraev force-pushed the cumsum_onnx_frontend branch 5 times, most recently from 4203af6 to 9413f7e Compare February 8, 2021 14:41
@echuraev echuraev marked this pull request as ready for review February 8, 2021 14:44
Copy link
Contributor

@mbrookhart mbrookhart left a comment

Choose a reason for hiding this comment

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

Looking very close.

It looks like you have a bit of a hybrid approach with the two options, one you're supporting in the relay constructor, the other you support in the TOPI CPU kernel, but not the GPU kernel?

I wonder if it makes more sense to do the reverse/exclusive changes explicitly in the importer so we don't have to pipe it through the multiple layers? I'm not sure if support the attributes in topi or doing it via composition in the importer makes more sense.

python/tvm/topi/cumsum.py Outdated Show resolved Hide resolved
python/tvm/relay/op/transform.py Outdated Show resolved Hide resolved
@masahi
Copy link
Member

masahi commented Feb 8, 2021

I think reverse should be done in the frontend, since this seems ONNX specific. For exclusive, if you want to avoid the overhead of subtraction, we can do cumsum with exclusive scan. See

return inclusive_scan(data, axis, output_dtype=dtype, binop=tvm.tir.generic.add)
if exclusive=True, we can use exclusive_scan instead of inclusive_scan.

python/tvm/topi/cumsum.py Outdated Show resolved Hide resolved
@echuraev
Copy link
Contributor Author

echuraev commented Feb 9, 2021

@mbrookhart, @masahi, I have one question about implementation of exclusive attribute. In current implementation, I added this attribute to topi kernels, but also I could do the same thing only for ONNX frontend (by adding subtract operation after cumsum). What do you think, what is the better approach? In first case we don't have overhead of subtract operation, but in the second we should write less code.

@masahi
Copy link
Member

masahi commented Feb 9, 2021

I'm happy with the current implementation. It is faster, and exclusive cumsum could be useful for other purpose (just as exclusive_scan in topi cuda is used in multiple places).

python/tvm/topi/cuda/scan.py Outdated Show resolved Hide resolved
python/tvm/topi/cumsum.py Outdated Show resolved Hide resolved
python/tvm/topi/cumsum.py Outdated Show resolved Hide resolved
@mbrookhart mbrookhart merged commit 2999d03 into apache:main Feb 9, 2021
@mbrookhart
Copy link
Contributor

Thanks @echuraev @masahi

alexwong pushed a commit to alexwong/tvm that referenced this pull request Feb 11, 2021
* [ONNX] Add CumSum operator to ONNX frontend

* Fix lint and add attributes to CumSum

* Fix CumSum test

* Add support exclusive attribute

* Add support reverse attribute

* Fix clang-format

* Fix lint

* Move reverse calculation to ONNX frontend and add exclusive to GPU

* Add test for int type
Lokiiiiii pushed a commit to Lokiiiiii/tvm that referenced this pull request Mar 2, 2021
* [ONNX] Add CumSum operator to ONNX frontend

* Fix lint and add attributes to CumSum

* Fix CumSum test

* Add support exclusive attribute

* Add support reverse attribute

* Fix clang-format

* Fix lint

* Move reverse calculation to ONNX frontend and add exclusive to GPU

* Add test for int type
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Mar 2, 2021
* [ONNX] Add CumSum operator to ONNX frontend

* Fix lint and add attributes to CumSum

* Fix CumSum test

* Add support exclusive attribute

* Add support reverse attribute

* Fix clang-format

* Fix lint

* Move reverse calculation to ONNX frontend and add exclusive to GPU

* Add test for int type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants