Adds isinf (#445)

* adds isinf

Signed-off-by: matthewfernst <matthew.f.ernst@gmail.com>

* use stream + nits

* typo

---------

Signed-off-by: matthewfernst <matthew.f.ernst@gmail.com>
Co-authored-by: Awni Hannun <awni@apple.com>
This commit is contained in:
Matthew Ernst 2024-01-15 19:50:44 -08:00 committed by GitHub
parent 6022d4129e
commit 92a2fdd577
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 7 deletions

View File

@ -52,6 +52,7 @@ Operations
identity identity
inner inner
isnan isnan
isinf
less less
less_equal less_equal
linspace linspace

View File

@ -1084,6 +1084,10 @@ array isnan(const array& a, StreamOrDevice s /* = {} */) {
return not_equal(a, a, s); return not_equal(a, a, s);
} }
array isinf(const array& a, StreamOrDevice s /* = {} */) {
return equal(a, array(std::numeric_limits<float>::infinity(), a.dtype()), s);
}
array where( array where(
const array& condition, const array& condition,
const array& x, const array& x,

View File

@ -378,6 +378,8 @@ array_equal(const array& a, const array& b, StreamOrDevice s = {}) {
array isnan(const array& a, StreamOrDevice s = {}); array isnan(const array& a, StreamOrDevice s = {});
array isinf(const array& a, StreamOrDevice s = {});
/** Select from x or y depending on condition. */ /** Select from x or y depending on condition. */
array where( array where(
const array& condition, const array& condition,

View File

@ -1134,12 +1134,6 @@ void init_array(py::module_& m) {
py::kw_only(), py::kw_only(),
"stream"_a = none, "stream"_a = none,
"See :func:`any`.") "See :func:`any`.")
.def(
"isnan",
&mlx::core::isnan,
py::kw_only(),
"stream"_a = none,
"See :func:`isnan`.")
.def( .def(
"moveaxis", "moveaxis",
&moveaxis, &moveaxis,

View File

@ -1836,7 +1836,25 @@ void init_ops(py::module_& m) {
a (array): Input array. a (array): Input array.
Returns: Returns:
array: The array with boolean values indicating which elements are NaN. array: The boolean array indicating which elements are NaN.
)pbdoc");
m.def(
"isinf",
&mlx::core::isinf,
"a"_a,
py::pos_only(),
py::kw_only(),
"stream"_a = none,
R"pbdoc(
isinf(a: array, stream: Union[None, Stream, Device] = None) -> array
Return a boolean array indicating which elements are +/- inifnity.
Args:
a (array): Input array.
Returns:
array: The boolean array indicating which elements are +/- infinity.
)pbdoc"); )pbdoc");
m.def( m.def(
"moveaxis", "moveaxis",

View File

@ -336,6 +336,21 @@ class TestOps(mlx_tests.MLXTestCase):
self.assertEqual(mx.isnan(0 * mx.array(float("inf"))).tolist(), True) self.assertEqual(mx.isnan(0 * mx.array(float("inf"))).tolist(), True)
def test_isinf(self):
x = mx.array([0.0, float("inf")])
self.assertEqual(mx.isinf(x).tolist(), [False, True])
x = mx.array([0.0, float("inf")]).astype(mx.float16)
self.assertEqual(mx.isinf(x).tolist(), [False, True])
x = mx.array([0.0, float("inf")]).astype(mx.bfloat16)
self.assertEqual(mx.isinf(x).tolist(), [False, True])
x = mx.array([0.0, float("inf")]).astype(mx.complex64)
self.assertEqual(mx.isinf(x).tolist(), [False, True])
self.assertEqual(mx.isinf(0 * mx.array(float("inf"))).tolist(), False)
def test_tri(self): def test_tri(self):
for shape in [[4], [4, 4], [2, 10]]: for shape in [[4], [4, 4], [2, 10]]:
for diag in [-1, 0, 1, -2]: for diag in [-1, 0, 1, -2]:

View File

@ -505,6 +505,32 @@ TEST_CASE("test is nan") {
CHECK(isnan(d).item<bool>()); CHECK(isnan(d).item<bool>());
} }
TEST_CASE("test is inf") {
array x(1.0f);
CHECK_FALSE(isinf(x).item<bool>());
array y(std::numeric_limits<double>::infinity());
CHECK(isinf(y).item<bool>());
array z = identity(7);
CHECK_FALSE(any(isinf(z)).item<bool>());
array w = array({1.0f, std::numeric_limits<double>::infinity(), 2.0f});
CHECK(array_equal({false, true, false}, isinf(w)).item<bool>());
array a(1.0f, bfloat16);
CHECK_FALSE(isinf(a).item<bool>());
array b(1.0f, float16);
CHECK_FALSE(isinf(b).item<bool>());
array c(std::numeric_limits<double>::infinity(), bfloat16);
CHECK(isinf(c).item<bool>());
array d(std::numeric_limits<double>::infinity(), float16);
CHECK(isinf(d).item<bool>());
}
TEST_CASE("test all close") { TEST_CASE("test all close") {
array x(1.0f); array x(1.0f);
array y(1.0f); array y(1.0f);