2024-03-19 11:12:25 +08:00
|
|
|
// Copyright © 2023-2024 Apple Inc.
|
2023-11-30 02:30:41 +08:00
|
|
|
#pragma once
|
|
|
|
#include <numeric>
|
2024-03-19 11:12:25 +08:00
|
|
|
#include <optional>
|
2023-11-30 02:30:41 +08:00
|
|
|
#include <variant>
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
#include <nanobind/nanobind.h>
|
|
|
|
#include <nanobind/stl/complex.h>
|
|
|
|
#include <nanobind/stl/variant.h>
|
2023-11-30 02:30:41 +08:00
|
|
|
|
|
|
|
#include "mlx/array.h"
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
namespace nb = nanobind;
|
2023-11-30 02:30:41 +08:00
|
|
|
|
|
|
|
using namespace mlx::core;
|
|
|
|
|
|
|
|
using IntOrVec = std::variant<std::monostate, int, std::vector<int>>;
|
2023-12-12 05:42:55 +08:00
|
|
|
using ScalarOrArray = std::
|
2024-03-19 11:12:25 +08:00
|
|
|
variant<nb::bool_, nb::int_, nb::float_, std::complex<float>, nb::object>;
|
2023-11-30 02:30:41 +08:00
|
|
|
|
|
|
|
inline std::vector<int> get_reduce_axes(const IntOrVec& v, int dims) {
|
|
|
|
std::vector<int> axes;
|
|
|
|
if (std::holds_alternative<std::monostate>(v)) {
|
|
|
|
axes.resize(dims);
|
|
|
|
std::iota(axes.begin(), axes.end(), 0);
|
|
|
|
} else if (auto pv = std::get_if<int>(&v); pv) {
|
|
|
|
axes.push_back(*pv);
|
|
|
|
} else {
|
|
|
|
axes = std::get<std::vector<int>>(v);
|
|
|
|
}
|
|
|
|
return axes;
|
|
|
|
}
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
inline array to_array_with_accessor(nb::object obj) {
|
|
|
|
if (nb::hasattr(obj, "__mlx_array__")) {
|
|
|
|
return nb::cast<array>(obj.attr("__mlx_array__")());
|
|
|
|
} else if (nb::isinstance<array>(obj)) {
|
|
|
|
return nb::cast<array>(obj);
|
2023-12-12 05:42:55 +08:00
|
|
|
} else {
|
2024-03-19 11:12:25 +08:00
|
|
|
std::ostringstream msg;
|
|
|
|
msg << "Invalid type " << nb::type_name(obj.type()).c_str()
|
|
|
|
<< " received in array initialization.";
|
|
|
|
throw std::invalid_argument(msg.str());
|
2023-12-12 05:42:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 21:52:30 +08:00
|
|
|
inline bool is_comparable_with_array(const ScalarOrArray& v) {
|
|
|
|
// Checks if the value can be compared to an array (or is already an
|
|
|
|
// mlx array)
|
|
|
|
if (auto pv = std::get_if<nb::object>(&v); pv) {
|
|
|
|
return nb::isinstance<array>(*pv) || nb::hasattr(*pv, "__mlx_array__");
|
|
|
|
} else {
|
|
|
|
// If it's not an object, it's a scalar (nb::int_, nb::float_, etc.)
|
|
|
|
// and can be compared to an array
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 02:30:41 +08:00
|
|
|
inline array to_array(
|
|
|
|
const ScalarOrArray& v,
|
|
|
|
std::optional<Dtype> dtype = std::nullopt) {
|
2024-03-19 11:12:25 +08:00
|
|
|
if (auto pv = std::get_if<nb::bool_>(&v); pv) {
|
|
|
|
return array(nb::cast<bool>(*pv), dtype.value_or(bool_));
|
|
|
|
} else if (auto pv = std::get_if<nb::int_>(&v); pv) {
|
2023-11-30 02:30:41 +08:00
|
|
|
auto out_t = dtype.value_or(int32);
|
|
|
|
// bool_ is an exception and is always promoted
|
2024-03-19 11:12:25 +08:00
|
|
|
return array(nb::cast<int>(*pv), (out_t == bool_) ? int32 : out_t);
|
|
|
|
} else if (auto pv = std::get_if<nb::float_>(&v); pv) {
|
2023-11-30 02:30:41 +08:00
|
|
|
auto out_t = dtype.value_or(float32);
|
|
|
|
return array(
|
2024-03-26 03:32:59 +08:00
|
|
|
nb::cast<float>(*pv), issubdtype(out_t, floating) ? out_t : float32);
|
2023-11-30 02:30:41 +08:00
|
|
|
} else if (auto pv = std::get_if<std::complex<float>>(&v); pv) {
|
|
|
|
return array(static_cast<complex64_t>(*pv), complex64);
|
|
|
|
} else {
|
2024-03-19 11:12:25 +08:00
|
|
|
return to_array_with_accessor(std::get<nb::object>(v));
|
2023-11-30 02:30:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::pair<array, array> to_arrays(
|
|
|
|
const ScalarOrArray& a,
|
|
|
|
const ScalarOrArray& b) {
|
|
|
|
// Four cases:
|
|
|
|
// - If both a and b are arrays leave their types alone
|
|
|
|
// - If a is an array but b is not, treat b as a weak python type
|
|
|
|
// - If b is an array but a is not, treat a as a weak python type
|
|
|
|
// - If neither is an array convert to arrays but leave their types alone
|
2024-03-19 11:12:25 +08:00
|
|
|
if (auto pa = std::get_if<nb::object>(&a); pa) {
|
2023-12-12 05:42:55 +08:00
|
|
|
auto arr_a = to_array_with_accessor(*pa);
|
2024-03-19 11:12:25 +08:00
|
|
|
if (auto pb = std::get_if<nb::object>(&b); pb) {
|
2023-12-12 05:42:55 +08:00
|
|
|
auto arr_b = to_array_with_accessor(*pb);
|
|
|
|
return {arr_a, arr_b};
|
2023-11-30 02:30:41 +08:00
|
|
|
}
|
2023-12-12 05:42:55 +08:00
|
|
|
return {arr_a, to_array(b, arr_a.dtype())};
|
2024-03-19 11:12:25 +08:00
|
|
|
} else if (auto pb = std::get_if<nb::object>(&b); pb) {
|
2023-12-12 05:42:55 +08:00
|
|
|
auto arr_b = to_array_with_accessor(*pb);
|
|
|
|
return {to_array(a, arr_b.dtype()), arr_b};
|
2023-11-30 02:30:41 +08:00
|
|
|
} else {
|
|
|
|
return {to_array(a), to_array(b)};
|
|
|
|
}
|
|
|
|
}
|