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: 7 additions & 1 deletion python/mlx/nn/layers/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,6 @@ class HardTanh(Module):
"""


@_make_activation_module(hard_shrink)
class HardShrink(Module):
r"""Applies the HardShrink function.

Expand All @@ -652,6 +651,13 @@ class HardShrink(Module):
lambd: the :math:`\lambda` value for Hardshrink. Default: ``0.5``
"""

def __init__(self, lambd=0.5):
super().__init__()
self.lambd = lambd

def __call__(self, x):
return hard_shrink(x, self.lambd)


@_make_activation_module(softmin)
class Softmin(Module):
Expand Down
12 changes: 12 additions & 0 deletions python/tests/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,18 @@ def test_hard_shrink(self):
self.assertEqual(y.shape, (5,))
self.assertEqual(y.dtype, mx.float32)

y = nn.HardShrink()(x)
expected_y = mx.array([1.0, 0.0, 0.0, 0.0, -1.5])
self.assertTrue(mx.array_equal(y, expected_y))
self.assertEqual(y.shape, (5,))
self.assertEqual(y.dtype, mx.float32)

y = nn.HardShrink(lambd=0.1)(x)
expected_y = mx.array([1.0, -0.5, 0.0, 0.5, -1.5])
self.assertTrue(mx.array_equal(y, expected_y))
self.assertEqual(y.shape, (5,))
self.assertEqual(y.dtype, mx.float32)

def test_rope(self):
for kwargs in [{}, {"traditional": False}, {"base": 10000}, {"scale": 0.25}]:
rope = nn.RoPE(4, **kwargs)
Expand Down
Loading