Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions mlx/linalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ inline array matrix_norm(
auto row_axis = axis[0];
auto col_axis = axis[1];
if (ord == -1.0) {
row_axis = (axis[0] < 0) ? axis[0] + a.ndim() : axis[0];
col_axis = (axis[1] < 0) ? axis[1] + a.ndim() : axis[1];
Comment thread
danlee2002 marked this conversation as resolved.
Outdated
col_axis -= (!keepdims && col_axis > row_axis && col_axis > 0);
return astype(
min(sum(abs(a, s), row_axis, keepdims, s), col_axis, keepdims, s),
dtype,
s);
} else if (ord == 1.0) {
row_axis = (axis[0] < 0) ? axis[0] + a.ndim() : axis[0];
col_axis = (axis[1] < 0) ? axis[1] + a.ndim() : axis[1];
col_axis -= (!keepdims && col_axis > row_axis && col_axis > 0);
return astype(
max(sum(abs(a, s), row_axis, keepdims, s), col_axis, keepdims, s),
Expand Down
6 changes: 3 additions & 3 deletions python/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def test_norm(self):
with self.subTest(shape=shape, keepdims=keepdims):
self.assertTrue(np.allclose(out_np, out_mx, atol=1e-5, rtol=1e-6))

# neg/pos inf norm test
norms = [-float("inf"), float("inf")]
# tests for negative indexing: -1/1inf/-inf/
norms = [-1, 1, -float("inf"), float("inf")]
for shape in [(3, 3), (2, 3, 3), (2, 3, 3, 3)]:
x_mx = mx.arange(1, math.prod(shape) + 1, dtype=mx.float32).reshape(shape)
x_np = np.arange(1, math.prod(shape) + 1, dtype=np.float32).reshape(shape)
Expand All @@ -76,7 +76,7 @@ def test_norm(self):
for axes in neg_axes:
out_np = np.linalg.norm(
x_np,
ord=np.inf if ord == float("inf") else -np.inf,
ord=ord,
axis=tuple(axes),
)
out_mx = mx.linalg.norm(x_mx, ord=ord, axis=axes)
Expand Down
15 changes: 15 additions & 0 deletions tests/linalg_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ TEST_CASE("[mlx.core.linalg.norm] double ord") {
Device::cpu)
.item<float>(),
doctest::Approx(3.0));
CHECK_EQ(
norm(x, -1, std::vector<int>{-1, -2}, false, Device::cpu).item<float>(),
doctest::Approx(3.0));
CHECK_EQ(
norm(x, 1, std::vector<int>{-1, -2}, false, Device::cpu).item<float>(),
doctest::Approx(21.0));

x = reshape(arange(18, float32), {2, 3, 3});
CHECK_THROWS(norm(x, 2.0, std::vector{0, 1, 2}));
CHECK(allclose(
Expand Down Expand Up @@ -283,6 +290,14 @@ TEST_CASE("[mlx.core.linalg.norm] double ord") {
Device::cpu),
array({3.0, 30.0}))
.item<bool>());
CHECK(allclose(
norm(x, 1, std::vector<int>{-1, -2}, false, Device::cpu),
array({21.0, 48.0}))
.item<bool>());
CHECK(allclose(
norm(x, 1, std::vector<int>{-1, -2}, false, Device::cpu),
array({3.0, 30.0}))
.item<bool>());
}

TEST_CASE("[mlx.core.linalg.norm] string ord") {
Expand Down