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

reduce one multiplication in tosa to arith sigmoid #982

Open
ai-mannamalai opened this issue Sep 21, 2024 · 4 comments
Open

reduce one multiplication in tosa to arith sigmoid #982

ai-mannamalai opened this issue Sep 21, 2024 · 4 comments

Comments

@ai-mannamalai
Copy link
Contributor

In recent implementation of lib/Conversion/TosaToSecretArith/TosaToSecretArith.cpp we lower Tosa::sigmoid() to degree-3 polynomial approximation as $-0.004x^3 + 0.197 * x + 0.5$. It could have used Horner's rule instead of direct computation.

Further, while current implementation computes the polynomial directly a better way would be to compute it as,
$x *(0.197 - 0.004 x^2) + 0.5 $ where the number of arith::mulop is fewer by 1.

If we had easy ability to compute complex numbers we can further break down the compute as 2 products from applying the fundamental theorem of algebra.

@j2kun
Copy link
Collaborator

j2kun commented Sep 21, 2024

I think we will ultimately do a lot better by just handling polynomial approximation and evaluation more generally. My rough plan is to add a polynomial.evaluate op, which can then be lowered via horner (or better!), and would replace this hard coded lowering. Mostly we have this implemented so that we could start collecting some basic benchmarks of neural network lowering.

And then, even later, the hard coded approximation would be replaced by a remez solver so the accuracy and domain may vary as inputs.

@j2kun
Copy link
Collaborator

j2kun commented Sep 21, 2024

In other words, while I wouldn't object to a patch for this, we may as well focus our efforts on the longer term plan.

@ai-mannamalai
Copy link
Contributor Author

ai-mannamalai commented Sep 22, 2024 via email

@asraa
Copy link
Collaborator

asraa commented Sep 23, 2024

Further, while current implementation computes the polynomial directly a better way would be to compute it as,
$x *(0.197 - 0.004 x^2) + 0.5 $ where the number of arith::mulop is fewer by 1.

The operation balancer pass rebalances this multiplication btw - 704df21

In generality, Horner's method is really cool though! I think integrating that into a generic solver would be great.

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

No branches or pull requests

3 participants