mirror of
https://github.com/ml-explore/mlx.git
synced 2025-09-19 19:38:16 +08:00
linalg.norm (#187)
* implemented vector_norm in cpp added linalg to mlx * implemented vector_norm python binding * renamed vector_norm to norm, implemented norm without provided ord * completed the implementation of the norm * added tests * removed unused import in linalg.cpp * updated python bindings * added some tests for python bindings * handling inf, -inf as numpy does, more extensive tests of compatibility with numpy * added better docs and examples * refactored mlx.linalg.norm bindings * reused existing util for implementation of linalg.norm * more tests * fixed a bug with no ord and axis provided * removed unused imports * some style and API consistency updates to linalg norm * remove unused includes * fix python tests * fixed a bug with frobenius norm of a complex-valued matrix * complex for vector too --------- Co-authored-by: Awni Hannun <awni@apple.com>
This commit is contained in:

committed by
GitHub

parent
447bc089b9
commit
6b0d30bb85
@@ -14,6 +14,7 @@ target_sources(
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/scheduler.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/transforms.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/utils.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/linalg.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/backend/metal/metal.h
|
||||
)
|
||||
|
||||
|
175
mlx/linalg.cpp
Normal file
175
mlx/linalg.cpp
Normal file
@@ -0,0 +1,175 @@
|
||||
// Copyright © 2023 Apple Inc.
|
||||
|
||||
#include <numeric>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
#include "mlx/dtype.h"
|
||||
#include "mlx/linalg.h"
|
||||
|
||||
namespace mlx::core::linalg {
|
||||
|
||||
Dtype at_least_float(const Dtype& d) {
|
||||
return is_floating_point(d) ? d : promote_types(d, float32);
|
||||
}
|
||||
|
||||
inline array l2_norm(
|
||||
const array& a,
|
||||
const std::vector<int>& axis,
|
||||
bool keepdims,
|
||||
StreamOrDevice s) {
|
||||
if (is_complex(a.dtype())) {
|
||||
return sqrt(sum(abs(a, s) * abs(a, s), axis, keepdims, s), s);
|
||||
} else {
|
||||
return sqrt(sum(square(a, s), axis, keepdims, s), s);
|
||||
}
|
||||
}
|
||||
|
||||
inline array vector_norm(
|
||||
const array& a,
|
||||
const double ord,
|
||||
const std::vector<int>& axis,
|
||||
bool keepdims,
|
||||
StreamOrDevice s) {
|
||||
auto dtype = at_least_float(a.dtype());
|
||||
if (ord == 0.0) {
|
||||
return astype(sum(not_equal(a, array(0), s), axis, keepdims, s), dtype, s);
|
||||
} else if (ord == 1.0) {
|
||||
return astype(sum(abs(a, s), axis, keepdims, s), dtype, s);
|
||||
} else if (ord == 2.0) {
|
||||
return l2_norm(a, axis, keepdims, s);
|
||||
} else if (ord == std::numeric_limits<double>::infinity()) {
|
||||
return astype(max(abs(a, s), axis, keepdims, s), dtype, s);
|
||||
} else if (ord == -std::numeric_limits<double>::infinity()) {
|
||||
return astype(min(abs(a, s), axis, keepdims, s), dtype, s);
|
||||
} else {
|
||||
return power(
|
||||
sum(power(abs(a, s), array(ord, dtype), s), axis, keepdims, s),
|
||||
array(1.0 / ord, dtype),
|
||||
s);
|
||||
}
|
||||
}
|
||||
|
||||
inline array matrix_norm(
|
||||
const array& a,
|
||||
const double ord,
|
||||
const std::vector<int>& axis,
|
||||
bool keepdims,
|
||||
StreamOrDevice s) {
|
||||
auto dtype = at_least_float(a.dtype());
|
||||
auto row_axis = axis[0];
|
||||
auto col_axis = axis[1];
|
||||
if (ord == -1.0) {
|
||||
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) {
|
||||
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),
|
||||
dtype,
|
||||
s);
|
||||
} else if (ord == std::numeric_limits<double>::infinity()) {
|
||||
row_axis -= (!keepdims && row_axis > col_axis && row_axis > 0);
|
||||
return astype(
|
||||
max(sum(abs(a, s), col_axis, keepdims, s), row_axis, keepdims, s),
|
||||
dtype,
|
||||
s);
|
||||
} else if (ord == -std::numeric_limits<double>::infinity()) {
|
||||
row_axis -= (!keepdims && row_axis > col_axis && row_axis > 0);
|
||||
return astype(
|
||||
min(sum(abs(a, s), col_axis, keepdims, s), row_axis, keepdims, s),
|
||||
dtype,
|
||||
s);
|
||||
} else if (ord == 2.0 || ord == -2.0) {
|
||||
throw std::runtime_error(
|
||||
"[linalg::norm] Singular value norms are not implemented.");
|
||||
} else {
|
||||
std::ostringstream msg;
|
||||
msg << "[linalg::norm] Invalid ord " << ord << " for matrix norm.";
|
||||
throw std::invalid_argument(msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
inline array matrix_norm(
|
||||
const array& a,
|
||||
const std::string& ord,
|
||||
const std::vector<int>& axis,
|
||||
bool keepdims,
|
||||
StreamOrDevice s) {
|
||||
if (ord == "f" || ord == "fro") {
|
||||
return l2_norm(a, axis, keepdims, s);
|
||||
} else if (ord == "nuc") {
|
||||
throw std::runtime_error(
|
||||
"[linalg::norm] Nuclear norm not yet implemented.");
|
||||
} else {
|
||||
std::ostringstream msg;
|
||||
msg << "[linalg::norm] Invalid ord value '" << ord << "' for matrix norm.";
|
||||
throw std::invalid_argument(msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
array norm(
|
||||
const array& a,
|
||||
const std::optional<std::vector<int>>& axis /* = std::nullopt */,
|
||||
bool keepdims /* = false */,
|
||||
StreamOrDevice s /* = {} */) {
|
||||
if (!axis) {
|
||||
return norm(flatten(a, s), std::vector<int>{0}, keepdims, s);
|
||||
}
|
||||
|
||||
if (axis.value().size() > 2) {
|
||||
throw std::invalid_argument(
|
||||
"[linalg::norm] Received too many axes for norm.");
|
||||
}
|
||||
return l2_norm(a, axis.value(), keepdims, s);
|
||||
}
|
||||
|
||||
array norm(
|
||||
const array& a,
|
||||
const double ord,
|
||||
const std::optional<std::vector<int>>& axis /* = std::nullopt */,
|
||||
bool keepdims /* = false */,
|
||||
StreamOrDevice s /* = {} */) {
|
||||
std::vector<int> ax;
|
||||
if (!axis) {
|
||||
ax.resize(a.ndim());
|
||||
std::iota(ax.begin(), ax.end(), 0);
|
||||
} else {
|
||||
ax = axis.value();
|
||||
}
|
||||
if (ax.size() == 1) {
|
||||
return vector_norm(a, ord, ax, keepdims, s);
|
||||
} else if (ax.size() == 2) {
|
||||
return matrix_norm(a, ord, ax, keepdims, s);
|
||||
} else {
|
||||
throw std::invalid_argument(
|
||||
"[linalg::norm] Received too many axes for norm.");
|
||||
}
|
||||
}
|
||||
|
||||
array norm(
|
||||
const array& a,
|
||||
const std::string& ord,
|
||||
const std::optional<std::vector<int>>& axis /* = std::nullopt */,
|
||||
bool keepdims /* = false */,
|
||||
StreamOrDevice s /* = {} */) {
|
||||
std::vector<int> ax;
|
||||
if (!axis) {
|
||||
ax.resize(a.ndim());
|
||||
std::iota(ax.begin(), ax.end(), 0);
|
||||
} else {
|
||||
ax = axis.value();
|
||||
}
|
||||
if (ax.size() != 2) {
|
||||
std::ostringstream msg;
|
||||
msg << "[linalg::norm] Norm '" << ord << "' only supported for matrices,"
|
||||
<< " but received " << ax.size() << " axis/axes.";
|
||||
throw std::invalid_argument(msg.str());
|
||||
}
|
||||
return matrix_norm(a, ord, ax, keepdims, s);
|
||||
}
|
||||
|
||||
} // namespace mlx::core::linalg
|
63
mlx/linalg.h
Normal file
63
mlx/linalg.h
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright © 2023 Apple Inc.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "mlx/array.h"
|
||||
#include "mlx/device.h"
|
||||
#include "mlx/ops.h"
|
||||
#include "mlx/stream.h"
|
||||
|
||||
namespace mlx::core::linalg {
|
||||
|
||||
/**
|
||||
* Compute vector or matrix norms.
|
||||
*
|
||||
* - If axis and ord are both unspecified, computes the 2-norm of flatten(x).
|
||||
* - If axis is not provided but ord is, then x must be either 1D or 2D.
|
||||
* - If axis is provided, but ord is not, then the 2-norm (or Frobenius norm
|
||||
* for matrices) is computed along the given axes. At most 2 axes can be
|
||||
* specified.
|
||||
* - If both axis and ord are provided, then the corresponding matrix or vector
|
||||
* norm is computed. At most 2 axes can be specified.
|
||||
*/
|
||||
array norm(
|
||||
const array& a,
|
||||
const double ord,
|
||||
const std::optional<std::vector<int>>& axis = std::nullopt,
|
||||
bool keepdims = false,
|
||||
StreamOrDevice s = {});
|
||||
inline array norm(
|
||||
const array& a,
|
||||
const double ord,
|
||||
int axis,
|
||||
bool keepdims = false,
|
||||
StreamOrDevice s = {}) {
|
||||
return norm(a, ord, std::vector<int>{axis}, keepdims, s);
|
||||
}
|
||||
array norm(
|
||||
const array& a,
|
||||
const std::string& ord,
|
||||
const std::optional<std::vector<int>>& axis = std::nullopt,
|
||||
bool keepdims = false,
|
||||
StreamOrDevice s = {});
|
||||
inline array norm(
|
||||
const array& a,
|
||||
const std::string& ord,
|
||||
int axis,
|
||||
bool keepdims = false,
|
||||
StreamOrDevice s = {}) {
|
||||
return norm(a, ord, std::vector<int>{axis}, keepdims, s);
|
||||
}
|
||||
array norm(
|
||||
const array& a,
|
||||
const std::optional<std::vector<int>>& axis = std::nullopt,
|
||||
bool keepdims = false,
|
||||
StreamOrDevice s = {});
|
||||
inline array
|
||||
norm(const array& a, int axis, bool keepdims = false, StreamOrDevice s = {}) {
|
||||
return norm(a, std::vector<int>{axis}, keepdims, s);
|
||||
}
|
||||
|
||||
} // namespace mlx::core::linalg
|
Reference in New Issue
Block a user