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

Fast exponent #4790

Merged
merged 1 commit into from
Feb 17, 2020
Merged

Fast exponent #4790

merged 1 commit into from
Feb 17, 2020

Conversation

alexgl-github
Copy link
Contributor

Thanks for contributing to TVM! Please refer to guideline https://docs.tvm.ai/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.

@alexgl-github alexgl-github force-pushed the fastexp branch 8 times, most recently from 12358c4 to dbcaf2e Compare January 29, 2020 20:17
@alexgl-github alexgl-github marked this pull request as ready for review January 29, 2020 20:30
topi/include/topi/elemwise.h Outdated Show resolved Hide resolved
topi/include/topi/elemwise.h Outdated Show resolved Hide resolved
@alexgl-github alexgl-github force-pushed the fastexp branch 2 times, most recently from 1831312 to 48d60c8 Compare January 31, 2020 18:44
@alexgl-github alexgl-github force-pushed the fastexp branch 2 times, most recently from a286e9e to 5e7efd8 Compare January 31, 2020 19:15
topi/include/topi/elemwise.h Show resolved Hide resolved
topi/include/topi/elemwise.h Outdated Show resolved Hide resolved
std::string name = "T_exp",
std::string tag = kElementWise) {
if (x->dtype == DataType::Float(32)) {
return fast_exp(x, name, tag);
Copy link
Member

Choose a reason for hiding this comment

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

unless this fast_exp is guaranteed to give a bit identical output as libc exp, I don't think it is a good idea to use this by default. I recommend using something like env var to enable this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@masahi It's not identical.
Relative fast exp error vs Tensorflow exp is between [-4.52e-06, 4.17e-06]
Relative fast exp error vs Numpy exp is [-3.11e-06, 3.10e-06]
How about using it only if enabled via cmake option?

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps a better way would be have a separate operator fast_exp, then have a pass(fast-math) in relay to rewrite the exp into the fast_exp

Copy link
Member

Choose a reason for hiding this comment

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

I like @tqchen's solution. If you use cmake option it is not configurable after libtvm.so is built. It requires more work, but it can be done in later PR. This PR can be merged with topi only change including test cases.

Copy link
Member

Choose a reason for hiding this comment

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

I know what I am talking about here because I also did fast_exp for my internal work in the past. Accurate exp is very slow and the high accuracy is not required for inference. The biggest benefit is it enables vectorization if it is written in topi (in my case it was HalideIR). Vectorizing exp was the main reason to introduce op fusion improvement in #1548

Copy link
Contributor

@anijain2305 anijain2305 Feb 5, 2020

Choose a reason for hiding this comment

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

How about having 3 new relay contrib operators - contrib.fast_exp, contrib.fast_tanh, contrib.fast_softmax. We can then add a Relay pass with opt_level 4, that legalizes these functions to their approximate counterparts.

Edit - Sorry should have told why these 3. For softmax, we are essentially playing with exp op. Softmax takes substantial time in SSD models, where input shape is very large. For tanh, we already have a fast_tanh that is enabled by default. We should change that.

@masahi
Copy link
Member

masahi commented Feb 5, 2020

@alexgl-github tests cases are absolutely required for a new operator like this.

@alexgl-github
Copy link
Contributor Author

@masahi @anijain2305 @FrozenGene Would you mind reviewing again?

@zhiics
Copy link
Member

zhiics commented Feb 11, 2020

I have some silly questions: when should we switch to the fast_exp since it is in topi? Do we expect users to select it? Does this mean that this op is only available in topi, but not Relay?

@alexgl-github
Copy link
Contributor Author

I have some silly questions: when should we switch to the fast_exp since it is in topi? Do we expect users to select it? Does this mean that this op is only available in topi, but not Relay?

@zhiics In a separate PR we'll introduce relay optimization pass that should select fast_exp if opt_level=4

Copy link
Contributor

@anijain2305 anijain2305 left a comment

Choose a reason for hiding this comment

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

LGTM

@anijain2305
Copy link
Contributor

Can this get in? I will work on Relay changes.

@anijain2305
Copy link
Contributor

@tqchen @FrozenGene Can you please check if the changes you requested are addressed?

@tqchen
Copy link
Member

tqchen commented Feb 13, 2020

Overall looks OK, it would be great if we can decide a consistent naming convention. In this case, we can have fastexp vs fast_exp

@anijain2305
Copy link
Contributor

Right. I think fast_exp fits better with current naming style.

@alexgl-github
Copy link
Contributor Author

Right. I think fast_exp fits better with current naming style.
@anijain2305
I've changed fastexp to fast_exp

Copy link
Member

@FrozenGene FrozenGene left a comment

Choose a reason for hiding this comment

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

LGTM. Minor comment. Please fix it.

topi/python/topi/math.py Outdated Show resolved Hide resolved
@masahi
Copy link
Member

masahi commented Feb 14, 2020

@tqchen please give an approval.

@anijain2305
Copy link
Contributor

Lets get this in - @tqchen

@masahi
Copy link
Member

masahi commented Feb 17, 2020

ping @tqchen

Copy link
Member

@tqchen tqchen left a comment

Choose a reason for hiding this comment

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

lgtm from my side

@tqchen tqchen merged commit 1314091 into apache:master Feb 17, 2020
@tqchen
Copy link
Member

tqchen commented Feb 17, 2020

alexwong pushed a commit to alexwong/tvm that referenced this pull request Feb 26, 2020
alexwong pushed a commit to alexwong/tvm that referenced this pull request Feb 28, 2020
zhiics pushed a commit to neo-ai/tvm that referenced this pull request Mar 2, 2020
@alexgl-github alexgl-github deleted the fastexp branch November 3, 2020 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants