Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions python/mlx/nn/layers/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,15 @@ def gelu_fast_approx(x):

.. math::

x = x \sigma\left(1.773 x\right)
x = x \sigma\left(1.702 x\right)

where :math:`\sigma(\cdot)` is the logistic sigmoid.

References:
- https://github.com/hendrycks/GELUs
- https://arxiv.org/abs/1606.08415
"""
return x * mx.sigmoid(1.773 * x)
return x * mx.sigmoid(1.702 * x)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IIRC @angeloskath added this, maybe you had another implementation in mind?



def glu(x: mx.array, axis: int = -1) -> mx.array:
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ def test_gelu(self):
y_hat1 = nn.gelu_approx(x)
y_hat2 = nn.gelu_fast_approx(x)
self.assertLess(mx.abs(y - y_hat1).max(), 0.0003)
self.assertLess(mx.abs(y - y_hat2).max(), 0.02)
self.assertLess(mx.abs(y - y_hat2).max(), 0.025)

def test_sin_pe(self):
m = nn.SinusoidalPositionalEncoding(16, min_freq=0.01)
Expand Down