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

[TOPI][OP] cuda for argwhere #6868

Merged
merged 13 commits into from
Dec 4, 2020
Merged

[TOPI][OP] cuda for argwhere #6868

merged 13 commits into from
Dec 4, 2020

Conversation

zhiics
Copy link
Member

@zhiics zhiics commented Nov 6, 2020

This PR adds cuda schedule for argwhere.

  • Since frameworks require sorted results, we sort the indices from the least significant to the most significant columns.
  • Only one block is used to avoid atomic_add emitting flaky results. Thanks @kevinthesun for looking into it.
  • The added argwhere tests in test_any would currently fail because topi strided_slice currently doesn't support symbolic shape yet. @mbrookhart has some work on it.

Will ping reviews when we can run argwhere relay tests.

@mbrookhart
Copy link
Contributor

@zhiics I have a branch with the changes you'd need, but I haven't opened a PR because I've been fighting that memory corruption issue with topk. Would you like me to submit a PR to enable the other dynamic tests and include my refactors to strided slice?

@zhiics
Copy link
Member Author

zhiics commented Nov 6, 2020

@mbrookhart Thanks. That would be cool.

max_threads = int(tvm.target.Target.current(allow_none=False).max_num_threads)
nthread_tx = max_threads

# Limit threads to a single block to make sure atomic_add works normally.
Copy link
Contributor

Choose a reason for hiding this comment

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

Cuda does have a kernel level atomic add (https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#atomic-functions). It is just slower or do we not have access to it from TIR?

Copy link
Contributor

@kevinthesun kevinthesun Nov 6, 2020

Choose a reason for hiding this comment

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

We use atomicAdd. However, if number of blocks is larger than a threshold(like 18), it will return incorrect result.

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 surprised. I'd expect atomicAdd to work with any number of blocks. Could you maybe expand this comment with why and when atomicAdd fails?

Copy link
Contributor

@kevinthesun kevinthesun Nov 18, 2020

Choose a reason for hiding this comment

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

The observation is that if input data size is large( > 300 * 300 for example), previous we don't limit the number of blocks and the output of IR routine would be incorrect. I didn't dig deeper into it at this time.

In addition we need to use thrust otherwise tvm implemetation of topk can also generate incorrect result.

@zhiics zhiics changed the title [WIP][TOPI][OP] cuda for argwhere [TOPI][OP] cuda for argwhere Dec 4, 2020
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 kevinthesun merged commit 54cd235 into apache:main Dec 4, 2020
@kevinthesun
Copy link
Contributor

Thanks @zhiics @mbrookhart @tkonolige

@zhiics zhiics deleted the argwhere branch December 4, 2020 22:57
TusharKanekiDey pushed a commit to TusharKanekiDey/tvm that referenced this pull request Jan 20, 2021
* argwhere

* cuda schedule

* sort argwhere result

* Use single block and thrust to fix flaky behavior

* format

* used dynamic strided_slice

* Fix dynamic strided_slice

* try new strided_slice

* Improve dynamic strided slice to bind data depedent shape var.

* all tests pass

* remove print

* use new strided_slice

* clean

Co-authored-by: Yao Wang <kevinthesunwy@gmail.com>
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Jan 21, 2021
* argwhere

* cuda schedule

* sort argwhere result

* Use single block and thrust to fix flaky behavior

* format

* used dynamic strided_slice

* Fix dynamic strided_slice

* try new strided_slice

* Improve dynamic strided slice to bind data depedent shape var.

* all tests pass

* remove print

* use new strided_slice

* clean

Co-authored-by: Yao Wang <kevinthesunwy@gmail.com>
electriclilies pushed a commit to electriclilies/tvm that referenced this pull request Feb 18, 2021
* argwhere

* cuda schedule

* sort argwhere result

* Use single block and thrust to fix flaky behavior

* format

* used dynamic strided_slice

* Fix dynamic strided_slice

* try new strided_slice

* Improve dynamic strided slice to bind data depedent shape var.

* all tests pass

* remove print

* use new strided_slice

* clean

Co-authored-by: Yao Wang <kevinthesunwy@gmail.com>
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.

4 participants