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

[QNN] Lookup operations for hard to implement operators #10053

Conversation

AndrewZhaoLuo
Copy link
Contributor

@AndrewZhaoLuo AndrewZhaoLuo commented Jan 24, 2022

This PR adds a way to implement difficult and arbitrary elemwise ops in q-space via a table lookup operation via gather. This is intended to be used as the "canonicalization" of some qnn functions.

This is all in python RN so legalizations of relevant functions has to be done via python. An example with qnn.rsqrt is given too.

Things this PRs adds:

  • Support for gather to take unsigned integers in the indexing tensor (+ tests)
  • Add python-defined canonicalization interface (a lot better than C++ 🤮)
  • Add table lookup-canonicalization/legalization utilities
  • Add example of use of the above with qnn.rsqrt op.

If reviewers want I can split into separate PRs.

TODO:

@mbrookhart
Copy link
Contributor

Thanks, @AndrewZhaoLuo, this is a really interesting PR!

I'm not 100% sure what the long term correct approach for running these kind of ops. We can talk about Look Up Tables like this, we could do polynomial approximations over the estimated range, or we could talk about casting back to FP32.

I imagine that the most performant solution is going to depend on the hardware target, some ops will be fast for lookup tables, others will do better with polynomial approximations, and others might be faster just casting back to FP32. Perhaps we should make this part of the device-dependent legalization pass, instead of canonicalization? https://github.com/apache/tvm/blob/main/python/tvm/relay/qnn/op/legalizations.py

@masahi @anijain2305 Thoughts?

@AndrewZhaoLuo
Copy link
Contributor Author

I think this is an interesting idea. I think moving the lowering to legalization is probably the right call once we have alternative methods. Need to read more into how legalization works.

@AndrewZhaoLuo
Copy link
Contributor Author

So I don't understand the functional difference between legalization and canonicalization. They seem to be able to be combined into one step?

@AndrewZhaoLuo AndrewZhaoLuo changed the title [WIP][QNN] Lookup operations for hard to implement operators [QNN] Lookup operations for hard to implement operators Jan 25, 2022
@AndrewZhaoLuo
Copy link
Contributor Author

This is now ready for review cc @mbrookhart @masahi

@AndrewZhaoLuo AndrewZhaoLuo force-pushed the aluo/fq2i/elemwise-lookup-ops-qnn-flavor branch from 4e7b96a to b609d63 Compare January 31, 2022 22:40
@AndrewZhaoLuo AndrewZhaoLuo merged commit 5e4e239 into apache:main Feb 9, 2022
ylc pushed a commit to ylc/tvm that referenced this pull request Feb 16, 2022
* initial tanh impl

* smalls error

* support uint and int lookup into tables

* reinterpret cast, working tanh tests

* refactor relay func creation

* basic casting tests

* explicitly say do not handle multi-channel lookups

* add example funcs

* fix silent fail

* fix some bugs with floating point funcs not working

* add TODO

* add tood

* canonicalizations

* refactor integer lookup ops into own folder

* fq2i stuff

* clean up existing tests

* flesh out todo

* more tests

* test on keeping shape good

* lookup table fix

* replace canonicalization for rsqrt

* remove canonicalization of rsqrt

* add asf headers

* topi tests

* gather supports unsigned integer tests

* fix things

* move to legalization

* jostle ci

* linting

* use take instead of gather

* remove gather changes

* undo changes

* undo changes

* undo changes

* move thing in range

* initial tanh impl

* smalls error

* support uint and int lookup into tables

* reinterpret cast, working tanh tests

* refactor relay func creation

* basic casting tests

* explicitly say do not handle multi-channel lookups

* add example funcs

* fix silent fail

* fix some bugs with floating point funcs not working

* add TODO

* add tood

* canonicalizations

* refactor integer lookup ops into own folder

* fq2i stuff

* clean up existing tests

* flesh out todo

* more tests

* test on keeping shape good

* lookup table fix

* replace canonicalization for rsqrt

* remove canonicalization of rsqrt

* add asf headers

* gather supports unsigned integer tests

* fix things

* move to legalization

* jostle ci

* linting

* use take instead of gather

* remove gather changes

* undo changes

* undo changes

* undo changes

* move thing in range

* lint

* remove unneeded line

* jostle

Co-authored-by: andrewzhaoluo (generated by with_the_same_user script) <andrewzhaoluo@system76-pc.localdomain>
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