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][FastMath] Relay pass to use fast exp/tanh #4873

Merged
merged 3 commits into from
Mar 1, 2020

Conversation

anijain2305
Copy link
Contributor

As Title, dependent on the following PR - #4790

@alexgl-github @zhiics @masahi

src/relay/backend/build_module.cc Show resolved Hide resolved

/*!
* \file fast_math.cc
* \brief Replaces non linear activation functions with their fast but appproximate counterparts.
Copy link
Member

Choose a reason for hiding this comment

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

s/appproximate/approximate

#include <tvm/relay/attrs/nn.h>
#include <tvm/relay/transform.h>
#include <tvm/relay/op.h>
#include "./pattern_util.h"
Copy link
Member

Choose a reason for hiding this comment

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

"pattern_util.h"

@anijain2305
Copy link
Contributor Author

Thanks @zhiics for quick review. I will incorporate your comments once the parent PR gets merged in.

@anijain2305
Copy link
Contributor Author

I have rebased.

I am not sure if opt_level=4 is the right way. There are other optimizations as well at opt_level=4, which I might not want to use. At the same time, it does not look like opt_level=3 candidate as well because it approximates. @zhiics

Copy link
Member

@zhiics zhiics left a comment

Choose a reason for hiding this comment

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

@anijain2305 In this case, I would think we can probably introduce a fast_math option to enable it and set the pass to the minimal level it requires to execute, although we are usually quite conservative in adding options to build config.

@tqchen @jroesch any thoughts?

@anijain2305
Copy link
Contributor Author

@tqchen any thoughts on this?

@tqchen
Copy link
Member

tqchen commented Feb 25, 2020

I agree with @zhiics .

@anijain2305
Copy link
Contributor Author

@tqchen you mean to add a fast_math config option, right?

I will add one

@tqchen
Copy link
Member

tqchen commented Feb 25, 2020

While we cannot add a config option for every new chocies of optimizations, fastmath seems to be a quite common one that I think might be OK

@anijain2305
Copy link
Contributor Author

I agree.

@anijain2305
Copy link
Contributor Author

I investigated more. I am also little uncomfortable about adding another Relay build config option. It changes API of PassContext/BuildConfig object.

Our goal is to make the pass user-friendly. We can use required_pass (current way) to do that. For a TVM user, there is no difference between the two options

Current way

with relay.build_config(opt_level=3, required_pass=['FastMath']):

Alternative

If we decide to add enable_fast_math build config option.

with relay.build_config(opt_level=3, enable_fast_math=True):

Since both are same from TVM user perspective, and current way does not change PassContext API, I feel more comfortable with current design.

@zhiics @tqchen Let me know if this is ok.

@zhiics
Copy link
Member

zhiics commented Feb 26, 2020

Adding it to required_pass sounds good to me as well since users definitely need to know what they do exactly in this scenario.

The only concern I have is that I am not sure if we want to require passes from the higher level? Also, what's the relationship among the passes at opt_level=4? should we run it with other two passes when opt_level=4 is provided? This is not specific to this PR, but I think we probably want a better documentation for the choice of different opt levels.

@tqchen
Copy link
Member

tqchen commented Feb 26, 2020

Seems with relay.build_config(opt_level=3, required_pass=['FastMath']) is simple enough that we can go with that for now

@anijain2305
Copy link
Contributor Author

@zhiics I agree we need more clarity over the usage of opt_level=4. Thinking more about this, I feel that opt_level=4 is more like a placeholder for all the passes, that will not be a part of default Relay build pipeline. I don't think we intend to use opt_level=4 to call these passes. Calling these passes should be either from outside or by using required_pass. If that is truly the case, maybe we should not even have an opt_level for such passes.

As far as this PR is concerned, I think we are ok.

@tqchen
Copy link
Member

tqchen commented Feb 26, 2020

It is a bit pre-mature to introduce additional opt levels as we add passes. how about we only support the required_pass option for now

@zhiics
Copy link
Member

zhiics commented Feb 26, 2020

Yes, I am okay to use required_pass for now.

fast_mod = FastMath()(mod)
assert "fast_exp" in fast_mod.astext()

# Check that opt level 4 triggers the transformation.
Copy link
Member

Choose a reason for hiding this comment

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

This should be changed, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks. I missed changing this one.

Returns
-------
ret: tvm.relay.Pass
The registered to perform operator simplification.
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, one more change. Please use force push.

The registered pass

Copy link
Member

@zhiics zhiics left a comment

Choose a reason for hiding this comment

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

LGTM

@zhiics zhiics merged commit 51af454 into apache:master Mar 1, 2020
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Apr 16, 2020
* [Relay][FastMath] Relay pass to use fast exp/tanh

* Adding required_pass to the tests.

* FastMath test changes.
zhiics pushed a commit to neo-ai/tvm that referenced this pull request Apr 17, 2020
* [Relay][FastMath] Relay pass to use fast exp/tanh

* Adding required_pass to the tests.

* FastMath test changes.
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.

3 participants