Skip to content
Open
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
9 changes: 8 additions & 1 deletion mlx/linalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ array norm(
bool keepdims /* = false */,
StreamOrDevice s /* = {} */) {
if (!axis) {
return norm(flatten(a, s), std::vector<int>{0}, keepdims, s);
auto out = norm(flatten(a, s), std::vector<int>{0}, keepdims, s);
if (keepdims) {
// The flatten above collapses the input to one dimension, so keepdims
// has to restore the rank of the original array rather than the
// flattened one.
out = reshape(out, Shape(a.ndim(), 1), s);
}
return out;
}

if (axis.value().size() > 2) {
Expand Down
3 changes: 3 additions & 0 deletions python/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_norm(self):
with self.subTest(
shape=shape, ord=o, axis=axis, keepdims=keepdims
):
self.assertEqual(out_mx.shape, out_np.shape)
self.assertTrue(
np.allclose(out_np, out_mx, atol=1e-5, rtol=1e-6)
)
Expand All @@ -51,6 +52,7 @@ def test_norm(self):
out_np = np.linalg.norm(x_np, ord=o, keepdims=keepdims)
out_mx = mx.linalg.norm(x_mx, ord=o, keepdims=keepdims)
with self.subTest(shape=shape, ord=o, keepdims=keepdims):
self.assertEqual(out_mx.shape, out_np.shape)
self.assertTrue(
np.allclose(out_np, out_mx, atol=1e-5, rtol=1e-6)
)
Expand All @@ -63,6 +65,7 @@ def test_norm(self):
out_np = np.linalg.norm(x_np, keepdims=keepdims)
out_mx = mx.linalg.norm(x_mx, keepdims=keepdims)
with self.subTest(shape=shape, keepdims=keepdims):
self.assertEqual(out_mx.shape, out_np.shape)
self.assertTrue(np.allclose(out_np, out_mx, atol=1e-5, rtol=1e-6))

# tests for negative indexing: -1/1/inf/-inf/
Expand Down
Loading