-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement GELU as function op (onnx#5277)
### Description These changes have been made to support the GELU operator as a function op. ### Motivation and Context Support for [GELU: Gaussian Error Linear Unit](https://paperswithcode.com/method/gelu) activation function, which was requested in onnx#4933. onnx#4423 also mentions this under the new ops section of `Contributions Welcome`. As per the discussion in onnx#4933, I have added GELU as a context-dependent function-op, that uses the attribute `approximate` to return one of the two possible function-body definitions. The first function definition is the regular GELU: `GELU(x)=x∗Φ(x) = 0.5 * x * (1 + erf(x / sqrt(2)))` The second is the fast approximation based on `tanh`: `GELU(x)=0.5 ∗ x ∗ (1+Tanh( sqrt(2/π) ∗ (x + 0.044715 ∗ x^3)))` This implementation uses the [PyTorch docs for GELU](https://pytorch.org/docs/stable/generated/torch.nn.GELU.html?highlight=gelu#torch.nn.GELU) as a reference. PS: I also refactored `onnx/defs/math/defs.cc` to bring the operator implementation of `mish` right next to its doc string. --------- Signed-off-by: pranshupant <[email protected]> Co-authored-by: G. Ramalingam <[email protected]>
- Loading branch information
1 parent
d8634f1
commit c0033eb
Showing
32 changed files
with
355 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (c) ONNX Project Contributors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import math | ||
|
||
import numpy as np | ||
|
||
import onnx | ||
from onnx.backend.test.case.base import Base | ||
from onnx.backend.test.case.node import expect | ||
|
||
|
||
class Gelu(Base): | ||
@staticmethod | ||
def export_gelu_tanh() -> None: | ||
node = onnx.helper.make_node( | ||
"Gelu", inputs=["x"], outputs=["y"], approximate="tanh" | ||
) | ||
|
||
x = np.array([-1, 0, 1]).astype(np.float32) | ||
# expected output [-0.158808, 0., 0.841192] | ||
y = ( | ||
0.5 | ||
* x | ||
* (1 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * np.power(x, 3)))) | ||
).astype(np.float32) | ||
expect(node, inputs=[x], outputs=[y], name="test_gelu_tanh_1") | ||
|
||
x = np.random.randn(3, 4, 5).astype(np.float32) | ||
# expected output [2.9963627, 3.99993, 4.9999995] | ||
y = ( | ||
0.5 | ||
* x | ||
* (1 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * np.power(x, 3)))) | ||
).astype(np.float32) | ||
expect(node, inputs=[x], outputs=[y], name="test_gelu_tanh_2") | ||
|
||
@staticmethod | ||
def export_gelu_default() -> None: | ||
node = onnx.helper.make_node("Gelu", inputs=["x"], outputs=["y"]) | ||
|
||
x = np.array([-1, 0, 1]).astype(np.float32) | ||
# expected output [-0.15865526, 0., 0.84134474] | ||
y = (0.5 * x * (1 + np.vectorize(math.erf)(x / np.sqrt(2)))).astype(np.float32) | ||
expect(node, inputs=[x], outputs=[y], name="test_gelu_default_1") | ||
|
||
x = np.random.randn(3, 4, 5).astype(np.float32) | ||
# expected output [2.99595031, 3.99987331, 4.99999857] | ||
y = (0.5 * x * (1 + np.vectorize(math.erf)(x / np.sqrt(2)))).astype(np.float32) | ||
expect(node, inputs=[x], outputs=[y], name="test_gelu_default_2") |
Binary file not shown.
Binary file added
BIN
+21 Bytes
onnx/backend/test/data/node/test_gelu_default_1/test_data_set_0/input_0.pb
Binary file not shown.
Binary file added
BIN
+21 Bytes
onnx/backend/test/data/node/test_gelu_default_1/test_data_set_0/output_0.pb
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+21 Bytes
onnx/backend/test/data/node/test_gelu_default_1_expanded/test_data_set_0/input_0.pb
Binary file not shown.
Binary file added
BIN
+21 Bytes
onnx/backend/test/data/node/test_gelu_default_1_expanded/test_data_set_0/output_0.pb
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions
1
onnx/backend/test/data/node/test_gelu_default_2/test_data_set_0/input_0.pb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BxJ�x��?h��>��z?�j@$�?�.z��8s?b��hdӽ�9�>(�>�%�?^�B?�0�=B�>]ת>�=�?R�iJ�>�Z�/d#��S'?�K]?��=��C@�(��Hm;= �?�2�?��?��>���>�Ec������!��� >*z�?��?�Oƾmǚ��6��&õ�gڿ��?�x�FKྙ[��� G?4�ο��Y�L=e��> �����k��QN�>.:�=�ݚ>�b"�6��� |
3 changes: 3 additions & 0 deletions
3
onnx/backend/test/data/node/test_gelu_default_2/test_data_set_0/output_0.pb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
1 change: 1 addition & 0 deletions
1
onnx/backend/test/data/node/test_gelu_default_2_expanded/test_data_set_0/input_0.pb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BxJ�x��?h��>��z?�j@$�?�.z��8s?b��hdӽ�9�>(�>�%�?^�B?�0�=B�>]ת>�=�?R�iJ�>�Z�/d#��S'?�K]?��=��C@�(��Hm;= �?�2�?��?��>���>�Ec������!��� >*z�?��?�Oƾmǚ��6��&õ�gڿ��?�x�FKྙ[��� G?4�ο��Y�L=e��> �����k��QN�>.:�=�ݚ>�b"�6��� |
3 changes: 3 additions & 0 deletions
3
onnx/backend/test/data/node/test_gelu_default_2_expanded/test_data_set_0/output_0.pb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file added
BIN
+21 Bytes
onnx/backend/test/data/node/test_gelu_tanh_1/test_data_set_0/input_0.pb
Binary file not shown.
Binary file added
BIN
+21 Bytes
onnx/backend/test/data/node/test_gelu_tanh_1/test_data_set_0/output_0.pb
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+21 Bytes
onnx/backend/test/data/node/test_gelu_tanh_1_expanded/test_data_set_0/input_0.pb
Binary file not shown.
Binary file added
BIN
+21 Bytes
onnx/backend/test/data/node/test_gelu_tanh_1_expanded/test_data_set_0/output_0.pb
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions
1
onnx/backend/test/data/node/test_gelu_tanh_2/test_data_set_0/input_0.pb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BxJ�x��?h��>��z?�j@$�?�.z��8s?b��hdӽ�9�>(�>�%�?^�B?�0�=B�>]ת>�=�?R�iJ�>�Z�/d#��S'?�K]?��=��C@�(��Hm;= �?�2�?��?��>���>�Ec������!��� >*z�?��?�Oƾmǚ��6��&õ�gڿ��?�x�FKྙ[��� G?4�ο��Y�L=e��> �����k��QN�>.:�=�ݚ>�b"�6��� |
Binary file added
BIN
+254 Bytes
onnx/backend/test/data/node/test_gelu_tanh_2/test_data_set_0/output_0.pb
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions
1
onnx/backend/test/data/node/test_gelu_tanh_2_expanded/test_data_set_0/input_0.pb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BxJ�x��?h��>��z?�j@$�?�.z��8s?b��hdӽ�9�>(�>�%�?^�B?�0�=B�>]ת>�=�?R�iJ�>�Z�/d#��S'?�K]?��=��C@�(��Hm;= �?�2�?��?��>���>�Ec������!��� >*z�?��?�Oƾmǚ��6��&õ�gڿ��?�x�FKྙ[��� G?4�ο��Y�L=e��> �����k��QN�>.:�=�ݚ>�b"�6��� |
Binary file added
BIN
+254 Bytes
onnx/backend/test/data/node/test_gelu_tanh_2_expanded/test_data_set_0/output_0.pb
Binary file not shown.
Oops, something went wrong.