From 95081125b0d0f1e4e5a46b68eb22ec848d4e5e51 Mon Sep 17 00:00:00 2001 From: archidoge0 Date: Sun, 29 Nov 2026 02:32:13 +0800 Subject: [PATCH 1/2] Implement output_shapes for RMSNormVJP and LayerNormVJP --- mlx/fast_primitives.h | 6 ++ python/tests/test_fast.py | 112 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/mlx/fast_primitives.h b/mlx/fast_primitives.h index 7c2b1bfe18..ecaa9420e7 100644 --- a/mlx/fast_primitives.h +++ b/mlx/fast_primitives.h @@ -89,6 +89,9 @@ class RMSNormVJP : public Custom { DEFINE_NAME(RMSNormVJP) bool is_equivalent(const Primitive& other) const override; + std::vector output_shapes(const std::vector& inputs) override { + return {inputs[0].shape(), inputs[1].shape()}; + } auto state() const { return std::make_pair(nullptr, eps_); } @@ -209,6 +212,9 @@ class LayerNormVJP : public Custom { DEFINE_NAME(LayerNormVJP) bool is_equivalent(const Primitive& other) const override; + std::vector output_shapes(const std::vector& inputs) override { + return {inputs[0].shape(), inputs[1].shape(), inputs[2].shape()}; + } auto state() const { return std::make_pair(nullptr, eps_); } diff --git a/python/tests/test_fast.py b/python/tests/test_fast.py index b50c2d5641..557b318e35 100644 --- a/python/tests/test_fast.py +++ b/python/tests/test_fast.py @@ -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), (3, 129), (2, 4099), (0, 64)]: + 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( @@ -884,6 +923,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), (2, 129), (2, 8196), (0, 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, 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)) From 99e218ff02acd9941dcf4f7d22a3f7ca0bce29e5 Mon Sep 17 00:00:00 2001 From: archidoge0 Date: Sun, 29 Nov 2026 12:51:41 +0800 Subject: [PATCH 2/2] Avoid unrelated empty-broadcast and CUDA tail cases in shapeless norm tests --- python/tests/test_fast.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tests/test_fast.py b/python/tests/test_fast.py index 557b318e35..674b61db74 100644 --- a/python/tests/test_fast.py +++ b/python/tests/test_fast.py @@ -569,7 +569,7 @@ def ref_with_w(x, w): 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), (3, 129), (2, 4099), (0, 64)]: + 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,)) @@ -934,7 +934,7 @@ def ref_wb(x, w, b): 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), (2, 129), (2, 8196), (0, 64)]: + 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,))