2024-03-18 20:12:25 -07:00
|
|
|
// Copyright © 2024 Apple Inc.
|
2024-05-22 15:52:05 -07:00
|
|
|
#pragma once
|
2024-03-18 20:12:25 -07:00
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include <nanobind/nanobind.h>
|
|
|
|
#include <nanobind/ndarray.h>
|
|
|
|
|
|
|
|
#include "mlx/array.h"
|
2024-05-22 15:52:05 -07:00
|
|
|
#include "mlx/ops.h"
|
2024-03-18 20:12:25 -07:00
|
|
|
|
2024-12-12 08:45:39 +09:00
|
|
|
namespace mx = mlx::core;
|
2024-03-18 20:12:25 -07:00
|
|
|
namespace nb = nanobind;
|
|
|
|
|
2024-12-17 10:57:54 -08:00
|
|
|
struct ArrayLike {
|
|
|
|
ArrayLike(nb::object obj) : obj(obj) {};
|
|
|
|
nb::object obj;
|
|
|
|
};
|
|
|
|
|
2024-05-22 15:52:05 -07:00
|
|
|
using ArrayInitType = std::variant<
|
|
|
|
nb::bool_,
|
|
|
|
nb::int_,
|
|
|
|
nb::float_,
|
|
|
|
// Must be above ndarray
|
2024-12-12 08:45:39 +09:00
|
|
|
mx::array,
|
2024-05-22 15:52:05 -07:00
|
|
|
// Must be above complex
|
|
|
|
nb::ndarray<nb::ro, nb::c_contig, nb::device::cpu>,
|
|
|
|
std::complex<float>,
|
|
|
|
nb::list,
|
|
|
|
nb::tuple,
|
2024-12-17 10:57:54 -08:00
|
|
|
ArrayLike>;
|
2024-05-22 15:52:05 -07:00
|
|
|
|
2024-12-12 08:45:39 +09:00
|
|
|
mx::array nd_array_to_mlx(
|
2024-03-18 20:12:25 -07:00
|
|
|
nb::ndarray<nb::ro, nb::c_contig, nb::device::cpu> nd_array,
|
2024-12-12 08:45:39 +09:00
|
|
|
std::optional<mx::Dtype> dtype);
|
2024-05-16 16:11:37 -07:00
|
|
|
|
2024-12-12 08:45:39 +09:00
|
|
|
nb::ndarray<nb::numpy> mlx_to_np_array(const mx::array& a);
|
|
|
|
nb::ndarray<> mlx_to_dlpack(const mx::array& a);
|
2024-05-22 15:52:05 -07:00
|
|
|
|
2024-12-12 08:45:39 +09:00
|
|
|
nb::object to_scalar(mx::array& a);
|
2024-05-22 15:52:05 -07:00
|
|
|
|
2024-12-12 08:45:39 +09:00
|
|
|
nb::object tolist(mx::array& a);
|
2024-05-22 15:52:05 -07:00
|
|
|
|
2024-12-12 08:45:39 +09:00
|
|
|
mx::array create_array(ArrayInitType v, std::optional<mx::Dtype> t);
|
|
|
|
mx::array array_from_list(nb::list pl, std::optional<mx::Dtype> dtype);
|
|
|
|
mx::array array_from_list(nb::tuple pl, std::optional<mx::Dtype> dtype);
|