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][Topi]Add Sort Op to Relay #6978

Merged
merged 6 commits into from Dec 17, 2020
Merged

[Relay][Topi]Add Sort Op to Relay #6978

merged 6 commits into from Dec 17, 2020

Conversation

mbrookhart
Copy link
Contributor

@mbrookhart mbrookhart commented Nov 25, 2020

Re purposing PR for Relay Sort

python/tvm/topi/sort.py Outdated Show resolved Hide resolved
@zhiics
Copy link
Member

zhiics commented Nov 30, 2020

cc @kevinthesun, PTAL when you have time

@kevinthesun
Copy link
Contributor

kevinthesun commented Dec 1, 2020

For dynamic topk, can we use Thrust? IMO after sorting with Thrust the only work left is dynamic strided_slice in topi. Not quite sure whether we need to do this in relay level.

@anijain2305
Copy link
Contributor

I tried this PR, I see the following error when used with dynamic input shape

  File "/home/ubuntu/workplace/tvm/t1/tvm/src/relay/backend/compile_engine.cc", line 508
TVMError:
---------------------------------------------------------------
An internal invariant was violated during the execution of TVM.
Please read TVM's error reporting guidelines.
More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.
---------------------------------------------------------------
  Check failed: fshape_func.count(op) > 0 (0 vs. 0) : Internal error, cannot find ShapeFunc for sort

@@ -593,3 +694,82 @@ def schedule_topk(outs):
The computation schedule for the op.
"""
return _schedule_sort(outs)


def _dyn_topk_legalize(attrs, inputs, arg_types):
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest we improve gpu dynamic strided_slice and it helps both dynamic topk and argwhere. For sorting, we can still rely on existing thrust routine.

Copy link
Contributor Author

@mbrookhart mbrookhart Dec 1, 2020

Choose a reason for hiding this comment

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

Any tips for debugging generated code? I have a branch where I've done this a a topi composition, it passes if I compile with AddressSanitizer, but segfaults with a normal build in the generated code. @zhiics attempted to do argwhere, but gets compilation errors with topi dropping variables.

Copy link
Contributor

Choose a reason for hiding this comment

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

I also get segfault using current topi dynamic strided slice to cut topk result. I'll spend some time on it.

@mbrookhart
Copy link
Contributor Author

@anijain2305 I'll add a shape func for sort and a regression test

@kevinthesun I added a thrust implementation of sort, and tested dynamic topk with thrust enabled, it worked just fine.

@kevinthesun
Copy link
Contributor

#7018
Fix for dynamic strided slice in topi to enable topk.

@mbrookhart
Copy link
Contributor Author

closing in favor of #7018

@mbrookhart mbrookhart closed this Dec 7, 2020
@mbrookhart mbrookhart deleted the sort branch December 7, 2020 19:54
@mbrookhart
Copy link
Contributor Author

@masahi @zhiics @jwfromm

@kevinthesun found a fix for dynamic topk, so I don't think I need this legalization anymore, but do you think the sort op at the relay level is still useful?

@masahi
Copy link
Member

masahi commented Dec 8, 2020

yes hummingbird people have asked for a converter for pytorch sort op, that would be exactly what I need. Thanks.

cc @interesaaat

@mbrookhart mbrookhart restored the sort branch December 8, 2020 21:01
@mbrookhart
Copy link
Contributor Author

Okay, I'll reopen and rip out the legalization.

@mbrookhart mbrookhart reopened this Dec 8, 2020
@mbrookhart mbrookhart changed the title [Relay][Topi][Dynamic] Add a Sort op and use the legalization pass to perform dynamic topk on GPU [Relay][Topi]Add Sort Op to Relay Dec 8, 2020
@mbrookhart
Copy link
Contributor Author

@kevinthesun Can you re-review without the topk legalization?

lambda ins, outs: tvm.tir.call_packed(
"tvm.contrib.thrust.sort", ins[0], outs[0], outs[1], is_ascend
),
out_buffers=[value_buf, indices_buf],
Copy link
Member

Choose a reason for hiding this comment

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

There is no need to pass indices_buf for normal sort, but tvm.contrib.thrust.sort always expects indices_buf to be passed in... What we call tvm.contrib.thrust.sort is really argsort.

For optimal performance we should have the true tvm.contrib.thrust.sort someday.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, you're right, we don't strictly need that. Want me to add a todo?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that would be nice

@masahi masahi dismissed kevinthesun’s stale review December 16, 2020 22:28

The review is stale

Copy link
Member

@masahi masahi left a comment

Choose a reason for hiding this comment

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

Please fix remaining doc issues, otherwise LGTM.

@masahi masahi merged commit 9e766d9 into apache:main Dec 17, 2020
@masahi
Copy link
Member

masahi commented Dec 17, 2020

Thanks @mbrookhart @zhiics @kevinthesun @jwfromm

masahi pushed a commit to masahi/tvm that referenced this pull request Dec 18, 2020
* Add sort op to relay

* fix lint

* fix sort docstring

* fix docs

* add TODO, shape_func, cleanup

* add dynamic tests for sort and argsort
TusharKanekiDey pushed a commit to TusharKanekiDey/tvm that referenced this pull request Jan 20, 2021
* Add sort op to relay

* fix lint

* fix sort docstring

* fix docs

* add TODO, shape_func, cleanup

* add dynamic tests for sort and argsort
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Jan 21, 2021
* Add sort op to relay

* fix lint

* fix sort docstring

* fix docs

* add TODO, shape_func, cleanup

* add dynamic tests for sort and argsort
electriclilies pushed a commit to electriclilies/tvm that referenced this pull request Feb 18, 2021
* Add sort op to relay

* fix lint

* fix sort docstring

* fix docs

* add TODO, shape_func, cleanup

* add dynamic tests for sort and argsort
Parameters
----------
outs: Array of Tensor
The computation graph description of argsort
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: argsort -> sort

apivovarov added a commit to neo-ai/tvm that referenced this pull request Jun 12, 2022
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.

None yet

7 participants