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
6 changes: 6 additions & 0 deletions mlx/fast_primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class RMSNormVJP : public Custom {

DEFINE_NAME(RMSNormVJP)
bool is_equivalent(const Primitive& other) const override;
std::vector<Shape> output_shapes(const std::vector<array>& inputs) override {
return {inputs[0].shape(), inputs[1].shape()};
}
auto state() const {
return std::make_pair(nullptr, eps_);
}
Expand Down Expand Up @@ -209,6 +212,9 @@ class LayerNormVJP : public Custom {

DEFINE_NAME(LayerNormVJP)
bool is_equivalent(const Primitive& other) const override;
std::vector<Shape> output_shapes(const std::vector<array>& inputs) override {
return {inputs[0].shape(), inputs[1].shape(), inputs[2].shape()};
}
auto state() const {
return std::make_pair(nullptr, eps_);
}
Expand Down
112 changes: 112 additions & 0 deletions python/tests/test_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,45 @@ def inner(x, w, y):
self.assertLess(mx.abs(gx1 - gx2).max(), 1e-5)
self.assertLess(mx.abs(gw1 - gw2).max() / mx.abs(gw1).mean(), 1e-5)

def test_rms_norm_grad_shapeless(self):
eps = 1e-5

def loss_with_w(x, w):
return mx.fast.rms_norm(x, w, eps).sum()

def ref_with_w(x, w):
return rms_norm(x, w, eps).sum()

cgrad_w = mx.compile(mx.grad(loss_with_w, argnums=(0, 1)), shapeless=True)
rgrad_w = mx.grad(ref_with_w, argnums=(0, 1))
for shape in [(4, 64), (7, 64), (1, 64), (3, 129), (2, 4099)]:
D = shape[-1]
x = mx.random.normal(shape=shape)
w = mx.random.normal(shape=(D,))
mx.eval(x, w)
gx, gw = cgrad_w(x, w)
rx, rw = rgrad_w(x, w)
self.assertEqual(gx.shape, x.shape)
self.assertEqual(gw.shape, w.shape)
self.assertTrue(mx.allclose(gx, rx, atol=1e-5, rtol=1e-5))
self.assertTrue(mx.allclose(gw, rw, atol=1e-4, rtol=1e-4))

def loss_no_w(x):
return mx.fast.rms_norm(x, None, eps).sum()

def ref_no_w(x):
return rms_norm(x, mx.ones((x.shape[-1],), dtype=x.dtype), eps).sum()

cgrad_no_w = mx.compile(mx.grad(loss_no_w), shapeless=True)
rgrad_no_w = mx.grad(ref_no_w)
for shape in [(2, 3, 64), (5, 2, 64), (3, 4, 128)]:
x = mx.random.normal(shape=shape)
mx.eval(x)
gx = cgrad_no_w(x)
rx = rgrad_no_w(x)
self.assertEqual(gx.shape, x.shape)
self.assertTrue(mx.allclose(gx, rx, atol=1e-5, rtol=1e-5))

def test_cross_entropy(self):
def cross_entropy_ref(logits, targets):
score = mx.take_along_axis(logits, mx.expand_dims(targets, -1), -1).squeeze(
Expand Down Expand Up @@ -893,6 +932,79 @@ def f2(params, x):
self.assertLess(mx.abs(gw1 - gw2).max() / mx.abs(gw1).mean(), 1e-5)
self.assertLess(mx.abs(gb1 - gb2).max() / mx.abs(gb1).mean(), 1e-5)

def test_layer_norm_grad_shapeless(self):
eps = 1e-5

def loss_wb(x, w, b):
return mx.fast.layer_norm(x, w, b, eps).sum()

def ref_wb(x, w, b):
return layer_norm(x, w, b, eps).sum()

cgrad_wb = mx.compile(mx.grad(loss_wb, argnums=(0, 1, 2)), shapeless=True)
rgrad_wb = mx.grad(ref_wb, argnums=(0, 1, 2))
for shape in [(4, 64), (7, 64), (1, 64), (2, 128), (2, 8196)]:
D = shape[-1]
x = mx.random.normal(shape=shape)
w = mx.random.normal(shape=(D,))
b = mx.random.normal(shape=(D,))
mx.eval(x, w, b)
gx, gw, gb = cgrad_wb(x, w, b)
rx, rw, rb = rgrad_wb(x, w, b)
self.assertEqual(gx.shape, x.shape)
self.assertEqual(gw.shape, w.shape)
self.assertEqual(gb.shape, b.shape)
self.assertTrue(mx.allclose(gx, rx, atol=1e-4, rtol=1e-4))
self.assertTrue(mx.allclose(gw, rw, atol=1e-4, rtol=1e-4))
self.assertTrue(mx.allclose(gb, rb, atol=1e-4, rtol=1e-4))

cgrad_w = mx.compile(
mx.grad(
lambda x, w: mx.fast.layer_norm(x, w, None, eps).sum(),
argnums=(0, 1),
),
shapeless=True,
)
rgrad_w = mx.grad(
lambda x, w: layer_norm(x, w, None, eps).sum(), argnums=(0, 1)
)
cgrad_b = mx.compile(
mx.grad(
lambda x, b: mx.fast.layer_norm(x, None, b, eps).sum(),
argnums=(0, 1),
),
shapeless=True,
)
rgrad_b = mx.grad(
lambda x, b: layer_norm(x, None, b, eps).sum(), argnums=(0, 1)
)
cgrad_none = mx.compile(
mx.grad(lambda x: mx.fast.layer_norm(x, None, None, eps).sum()),
shapeless=True,
)
rgrad_none = mx.grad(lambda x: layer_norm(x, None, None, eps).sum())

for shape in [(2, 4, 32), (5, 3, 64)]:
D = shape[-1]
x = mx.random.normal(shape=shape)
w = mx.random.normal(shape=(D,))
b = mx.random.normal(shape=(D,))
mx.eval(x, w, b)

gx, gw = cgrad_w(x, w)
rx, rw = rgrad_w(x, w)
self.assertTrue(mx.allclose(gx, rx, atol=1e-4, rtol=1e-4))
self.assertTrue(mx.allclose(gw, rw, atol=1e-4, rtol=1e-4))

gx, gb = cgrad_b(x, b)
rx, rb = rgrad_b(x, b)
self.assertTrue(mx.allclose(gx, rx, atol=1e-4, rtol=1e-4))
self.assertTrue(mx.allclose(gb, rb, atol=1e-4, rtol=1e-4))

gx = cgrad_none(x)
rx = rgrad_none(x)
self.assertTrue(mx.allclose(gx, rx, atol=1e-4, rtol=1e-4))

def test_fast_transforms(self):
x = mx.random.uniform(shape=(2, 2, 8))

Expand Down
Loading