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

Add tf.nn.gelu alias for TF >= 2.4 #2265

Merged
merged 9 commits into from
Dec 15, 2020

Conversation

aaronmondal
Copy link
Contributor

Fixes #2252

See discussion in the above mentioned issue and the related, dropped PR #2256

@bot-of-gabrieldemarmiesse

@AakashKumarNain

You are owner of some files modified in this pull request.
Would you kindly review the changes whenever you have the time to?
Thank you very much.

@aaronmondal aaronmondal mentioned this pull request Dec 3, 2020
@aaronmondal
Copy link
Contributor Author

Mentioning @seanpmorgan .

Copy link
Member

@seanpmorgan seanpmorgan left a comment

Choose a reason for hiding this comment

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

Looks very good thanks! Mind just modifying the version check to be consistent with elsewhere in the repo:
https://github.com/tensorflow/addons/blob/master/tensorflow_addons/losses/tests/quantiles_test.py#L88

@@ -71,7 +72,9 @@ def gelu(x: types.TensorLike, approximate: bool = True) -> tf.Tensor:

x = tf.convert_to_tensor(x)

return _gelu_py(x, approximate)
gelu_op = tf.nn.gelu if LooseVersion(tf.__version__) >= "2.4" else _gelu_py
Copy link
Member

Choose a reason for hiding this comment

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

We should also warn users that the default value of approximate is changed from True to False if tf.nn.gelu is being used. Like

warnings.warn("Default value of `approximate` is changed from `True` to `False`")

@seanpmorgan seanpmorgan added the blocked Pending something elses completion label Dec 7, 2020
@seanpmorgan
Copy link
Member

seanpmorgan commented Dec 7, 2020

Thanks! Marking blocked since this should be tested with #2250 . It shouldn't be long before the release now. Also please add the Warning Tzu-Wei suggested and then should be good.

@aaronmondal
Copy link
Contributor Author

Added a warning in the docstring as well.

@seanpmorgan seanpmorgan removed the blocked Pending something elses completion label Dec 15, 2020
@google-cla

This comment has been minimized.

@google-cla google-cla bot added cla: no and removed cla: yes labels Dec 15, 2020
@seanpmorgan

This comment has been minimized.

@google-cla google-cla bot added cla: yes and removed cla: no labels Dec 15, 2020
@seanpmorgan
Copy link
Member

Hmm very slight difference w/ the keras implementation:

#23 357.3 =================================== FAILURES ===================================
#23 357.3 ______________ [doctest] tensorflow_addons.activations.gelu.gelu _______________
#23 357.3 [gw0] linux -- Python 3.6.12 /usr/local/bin/python
#23 357.3 051     Note that `approximate` will default to `False` from TensorFlow version 2.4 onwards.
#23 357.3 052     Consider using `tf.nn.gelu` instead.
#23 357.3 053 
#23 357.3 054     Usage:
#23 357.3 055 
#23 357.3 056     >>> tfa.options.TF_ADDONS_PY_OPS = True
#23 357.3 057     >>> x = tf.constant([-1.0, 0.0, 1.0])
#23 357.3 058     >>> tfa.activations.gelu(x, approximate=False)
#23 357.3 059     <tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.15865529,  0.        ,  0.8413447 ], dtype=float32)>
#23 357.3 060     >>> tfa.activations.gelu(x, approximate=True)
#23 357.3 Expected:
#23 357.3     <tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.158808,  0.      ,  0.841192], dtype=float32)>
#23 357.3 Got:
#23 357.3     <tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.15880796,  0.        ,  0.841192  ], dtype=float32)>

Is there anyway to increase doc test tolerance? Perhaps we just get rid of this doctest since gelu is on its way out? @WindQAQ

@WindQAQ
Copy link
Member

WindQAQ commented Dec 15, 2020

I think we can just change the value of doctest. Actually, old c++ impl also gets different values from pure python ops. There is slight difference in casting and constant precision between c++ impl/python in addons/python in tf.nn.gelu.

Copy link
Member

@seanpmorgan seanpmorgan left a comment

Choose a reason for hiding this comment

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

LGTM thanks again @aaronmondal !

@seanpmorgan seanpmorgan merged commit 0c76753 into tensorflow:master Dec 15, 2020
jrruijli pushed a commit to jrruijli/addons that referenced this pull request Dec 23, 2020
* Add tf.nn.gelu alias for TF >= 2.4
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.

Drop GELU for 0.13 release
4 participants