2024-03-19 11:12:25 +08:00
|
|
|
// Copyright © 2023-2024 Apple Inc.
|
2025-03-21 22:25:12 +08:00
|
|
|
#include <iostream>
|
2023-11-30 02:42:59 +08:00
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
#include <nanobind/nanobind.h>
|
2024-04-22 23:25:46 +08:00
|
|
|
#include <nanobind/stl/optional.h>
|
2024-04-08 12:47:43 +08:00
|
|
|
#include <nanobind/stl/string.h>
|
2024-05-01 06:47:27 +08:00
|
|
|
#include <nanobind/stl/unordered_map.h>
|
|
|
|
#include <nanobind/stl/variant.h>
|
2024-10-26 00:35:33 +08:00
|
|
|
#include <nanobind/stl/vector.h>
|
2025-03-21 22:25:12 +08:00
|
|
|
#include "mlx/backend/metal/metal.h"
|
|
|
|
#include "mlx/memory.h"
|
2023-11-30 02:42:59 +08:00
|
|
|
|
2024-12-12 07:45:39 +08:00
|
|
|
namespace mx = mlx::core;
|
2024-03-19 11:12:25 +08:00
|
|
|
namespace nb = nanobind;
|
|
|
|
using namespace nb::literals;
|
2023-11-30 02:42:59 +08:00
|
|
|
|
2025-03-21 22:25:12 +08:00
|
|
|
bool DEPRECATE(const std::string& old_fn, const std::string new_fn) {
|
|
|
|
std::cerr << old_fn << " is deprecated and will be removed in a future "
|
|
|
|
<< "version. Use " << new_fn << " instead." << std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DEPRECATE(oldfn, newfn) static bool dep = DEPRECATE(oldfn, newfn)
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
void init_metal(nb::module_& m) {
|
|
|
|
nb::module_ metal = m.def_submodule("metal", "mlx.metal");
|
2024-01-19 00:33:34 +08:00
|
|
|
metal.def(
|
2024-03-02 11:51:58 +08:00
|
|
|
"is_available",
|
2024-12-12 07:45:39 +08:00
|
|
|
&mx::metal::is_available,
|
2024-03-02 11:51:58 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Check if the Metal back-end is available.
|
|
|
|
)pbdoc");
|
2025-03-21 22:25:12 +08:00
|
|
|
metal.def("get_active_memory", []() {
|
|
|
|
DEPRECATE("mx.metal.get_active_memory", "mx.get_active_memory");
|
|
|
|
return mx::get_active_memory();
|
|
|
|
});
|
|
|
|
metal.def("get_peak_memory", []() {
|
|
|
|
DEPRECATE("mx.metal.get_peak_memory", "mx.get_peak_memory");
|
2025-03-22 03:28:36 +08:00
|
|
|
return mx::get_peak_memory();
|
2025-03-21 22:25:12 +08:00
|
|
|
});
|
|
|
|
metal.def("reset_peak_memory", []() {
|
|
|
|
DEPRECATE("mx.metal.reset_peak_memory", "mx.reset_peak_memory");
|
|
|
|
mx::reset_peak_memory();
|
|
|
|
});
|
|
|
|
metal.def("get_cache_memory", []() {
|
|
|
|
DEPRECATE("mx.metal.get_cache_memory", "mx.get_cache_memory");
|
|
|
|
return mx::get_cache_memory();
|
|
|
|
});
|
2024-03-02 11:51:58 +08:00
|
|
|
metal.def(
|
|
|
|
"set_memory_limit",
|
2025-03-21 22:25:12 +08:00
|
|
|
[](size_t limit) {
|
|
|
|
DEPRECATE("mx.metal.set_memory_limt", "mx.set_memory_limit");
|
|
|
|
return mx::set_memory_limit(limit);
|
|
|
|
},
|
|
|
|
"limit"_a);
|
2024-03-02 11:51:58 +08:00
|
|
|
metal.def(
|
|
|
|
"set_cache_limit",
|
2025-03-21 22:25:12 +08:00
|
|
|
[](size_t limit) {
|
|
|
|
DEPRECATE("mx.metal.set_cache_limt", "mx.set_cache_limit");
|
|
|
|
return mx::set_cache_limit(limit);
|
|
|
|
},
|
|
|
|
"limit"_a);
|
2024-10-26 00:35:33 +08:00
|
|
|
metal.def(
|
|
|
|
"set_wired_limit",
|
2025-03-21 22:25:12 +08:00
|
|
|
[](size_t limit) {
|
|
|
|
DEPRECATE("mx.metal.set_wired_limt", "mx.set_wired_limit");
|
|
|
|
return mx::set_wired_limit(limit);
|
|
|
|
},
|
|
|
|
"limit"_a);
|
|
|
|
metal.def("clear_cache", []() {
|
|
|
|
DEPRECATE("mx.metal.clear_cache", "mx.clear_cache");
|
|
|
|
mx::clear_cache();
|
|
|
|
});
|
2024-04-08 12:47:43 +08:00
|
|
|
metal.def(
|
|
|
|
"start_capture",
|
2024-12-12 07:45:39 +08:00
|
|
|
&mx::metal::start_capture,
|
2024-04-08 12:47:43 +08:00
|
|
|
"path"_a,
|
|
|
|
R"pbdoc(
|
|
|
|
Start a Metal capture.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
path (str): The path to save the capture which should have
|
|
|
|
the extension ``.gputrace``.
|
|
|
|
)pbdoc");
|
|
|
|
metal.def(
|
|
|
|
"stop_capture",
|
2024-12-12 07:45:39 +08:00
|
|
|
&mx::metal::stop_capture,
|
2024-04-08 12:47:43 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Stop a Metal capture.
|
|
|
|
)pbdoc");
|
2024-05-01 06:47:27 +08:00
|
|
|
metal.def(
|
|
|
|
"device_info",
|
2024-12-12 07:45:39 +08:00
|
|
|
&mx::metal::device_info,
|
2024-05-01 06:47:27 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Get information about the GPU device and system settings.
|
|
|
|
|
|
|
|
Currently returns:
|
|
|
|
|
|
|
|
* ``architecture``
|
|
|
|
* ``max_buffer_size``
|
|
|
|
* ``max_recommended_working_set_size``
|
2024-05-03 21:50:15 +08:00
|
|
|
* ``memory_size``
|
2024-12-19 10:45:58 +08:00
|
|
|
* ``resource_limit``
|
2024-05-01 06:47:27 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: A dictionary with string keys and string or integer values.
|
|
|
|
)pbdoc");
|
2023-11-30 02:42:59 +08:00
|
|
|
}
|