2024-01-27 05:45:30 +08:00
|
|
|
// Copyright © 2023-2024 Apple Inc.
|
2024-03-19 11:12:25 +08:00
|
|
|
#include <nanobind/nanobind.h>
|
|
|
|
#include <nanobind/stl/optional.h>
|
|
|
|
#include <nanobind/stl/pair.h>
|
|
|
|
#include <nanobind/stl/string.h>
|
|
|
|
#include <nanobind/stl/variant.h>
|
|
|
|
#include <nanobind/stl/vector.h>
|
|
|
|
|
2023-11-30 02:52:08 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
|
|
|
#include <numeric>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "mlx/array.h"
|
2024-02-05 22:51:22 +08:00
|
|
|
#include "mlx/compile.h"
|
2023-11-30 02:52:08 +08:00
|
|
|
#include "mlx/graph_utils.h"
|
|
|
|
#include "mlx/transforms.h"
|
|
|
|
#include "mlx/transforms_impl.h"
|
2024-02-27 11:28:53 +08:00
|
|
|
#include "python/src/trees.h"
|
2023-11-30 02:52:08 +08:00
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
namespace nb = nanobind;
|
|
|
|
using namespace nb::literals;
|
2023-11-30 02:52:08 +08:00
|
|
|
using namespace mlx::core;
|
|
|
|
|
|
|
|
using IntOrVec = std::variant<int, std::vector<int>>;
|
|
|
|
using StrOrVec = std::variant<std::string, std::vector<std::string>>;
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
inline std::string type_name_str(const nb::handle& o) {
|
|
|
|
return nb::cast<std::string>(nb::type_name(o.type()));
|
|
|
|
}
|
|
|
|
|
2023-11-30 02:52:08 +08:00
|
|
|
template <typename T>
|
|
|
|
std::vector<T> to_vector(const std::variant<T, std::vector<T>>& v) {
|
|
|
|
std::vector<T> vals;
|
|
|
|
if (auto pv = std::get_if<T>(&v); pv) {
|
|
|
|
vals.push_back(*pv);
|
|
|
|
} else {
|
|
|
|
vals = std::get<std::vector<T>>(v);
|
|
|
|
}
|
|
|
|
return vals;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto validate_argnums_argnames(
|
|
|
|
const std::optional<IntOrVec>& argnums,
|
|
|
|
const StrOrVec& argnames) {
|
|
|
|
auto vec_names = to_vector(argnames);
|
|
|
|
|
|
|
|
if (!argnums.has_value()) {
|
|
|
|
// argnums was not provided and argnames was empty
|
|
|
|
if (vec_names.empty()) {
|
|
|
|
return std::make_pair(std::vector<int>{0}, vec_names);
|
|
|
|
} else {
|
|
|
|
return std::make_pair(std::vector<int>{}, vec_names);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(to_vector(*argnums), vec_names);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto py_value_and_grad(
|
2024-03-19 11:12:25 +08:00
|
|
|
const nb::callable& fun,
|
2023-11-30 02:52:08 +08:00
|
|
|
std::vector<int> argnums,
|
|
|
|
std::vector<std::string> argnames,
|
|
|
|
const std::string& error_msg_tag,
|
|
|
|
bool scalar_func_only) {
|
|
|
|
// Sanitize argnums
|
|
|
|
if (argnums.size() == 0 && argnames.size() == 0) {
|
|
|
|
throw std::invalid_argument(
|
|
|
|
error_msg_tag + " Gradient wrt no argument requested");
|
|
|
|
}
|
|
|
|
if (argnums.size() > 0) {
|
|
|
|
std::sort(argnums.begin(), argnums.end());
|
|
|
|
if (argnums[0] < 0) {
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << error_msg_tag
|
|
|
|
<< " Can't compute the gradient of negative argument index "
|
|
|
|
<< argnums[0];
|
|
|
|
throw std::invalid_argument(msg.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [fun, argnums, argnames, error_msg_tag, scalar_func_only](
|
2024-03-19 11:12:25 +08:00
|
|
|
const nb::args& args, const nb::kwargs& kwargs) {
|
2023-11-30 02:52:08 +08:00
|
|
|
// Sanitize the input
|
|
|
|
if (argnums.size() > 0 && argnums.back() >= args.size()) {
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << error_msg_tag << " Can't compute the gradient of argument index "
|
|
|
|
<< argnums.back() << " because the function is called with only "
|
|
|
|
<< args.size() << " arguments.";
|
|
|
|
throw std::invalid_argument(msg.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& key : argnames) {
|
|
|
|
if (!kwargs.contains(key)) {
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << error_msg_tag
|
|
|
|
<< " Can't compute the gradient of keyword argument '" << key
|
|
|
|
<< "' because the function is called with the "
|
|
|
|
<< "following keyword arguments {";
|
|
|
|
for (auto item : kwargs) {
|
2024-03-19 11:12:25 +08:00
|
|
|
msg << nb::cast<std::string>(item.first) << ",";
|
2023-11-30 02:52:08 +08:00
|
|
|
}
|
|
|
|
msg << "}";
|
|
|
|
throw std::invalid_argument(msg.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect the arrays
|
|
|
|
std::vector<array> arrays;
|
|
|
|
std::vector<int> counts(1, 0);
|
|
|
|
for (auto i : argnums) {
|
|
|
|
auto argsi = tree_flatten(args[i]);
|
|
|
|
arrays.insert(arrays.end(), argsi.begin(), argsi.end());
|
|
|
|
counts.push_back(argsi.size());
|
|
|
|
}
|
|
|
|
for (auto& key : argnames) {
|
|
|
|
auto argsk = tree_flatten(kwargs[key.c_str()]);
|
|
|
|
arrays.insert(arrays.end(), argsk.begin(), argsk.end());
|
|
|
|
counts.push_back(argsk.size());
|
|
|
|
}
|
|
|
|
std::partial_sum(counts.cbegin(), counts.cend(), counts.begin());
|
|
|
|
std::vector<int> gradient_indices(arrays.size());
|
|
|
|
std::iota(gradient_indices.begin(), gradient_indices.end(), 0);
|
|
|
|
|
|
|
|
// value_out will hold the output of the python function in order to be
|
|
|
|
// able to reconstruct the python tree of extra return values
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object py_value_out;
|
2023-11-30 02:52:08 +08:00
|
|
|
auto value_and_grads = value_and_grad(
|
|
|
|
[&fun,
|
|
|
|
&args,
|
|
|
|
&kwargs,
|
|
|
|
&argnums,
|
|
|
|
&argnames,
|
|
|
|
&counts,
|
|
|
|
&py_value_out,
|
|
|
|
&error_msg_tag,
|
|
|
|
scalar_func_only](const std::vector<array>& a) {
|
|
|
|
// Copy the arguments
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::list args_cpy;
|
|
|
|
nb::kwargs kwargs_cpy = nb::kwargs();
|
2023-11-30 02:52:08 +08:00
|
|
|
int j = 0;
|
|
|
|
for (int i = 0; i < args.size(); ++i) {
|
|
|
|
if (j < argnums.size() && i == argnums[j]) {
|
2024-03-19 11:12:25 +08:00
|
|
|
args_cpy.append(tree_unflatten(args[i], a, counts[j]));
|
2023-11-30 02:52:08 +08:00
|
|
|
j++;
|
|
|
|
} else {
|
2024-03-19 11:12:25 +08:00
|
|
|
args_cpy.append(args[i]);
|
2023-11-30 02:52:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& key : argnames) {
|
|
|
|
kwargs_cpy[key.c_str()] =
|
|
|
|
tree_unflatten(kwargs[key.c_str()], a, counts[j]);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
for (auto item : kwargs) {
|
|
|
|
if (kwargs_cpy.contains(item.first)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
kwargs_cpy[item.first] = item.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the python function
|
|
|
|
py_value_out = fun(*args_cpy, **kwargs_cpy);
|
|
|
|
|
|
|
|
// Validate the return value of the python function
|
2024-03-19 11:12:25 +08:00
|
|
|
if (!nb::isinstance<array>(py_value_out)) {
|
2023-11-30 02:52:08 +08:00
|
|
|
if (scalar_func_only) {
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << error_msg_tag << " The return value of the function "
|
|
|
|
<< "whose gradient we want to compute should be a "
|
2024-03-19 11:12:25 +08:00
|
|
|
<< "scalar array; but " << type_name_str(py_value_out)
|
2023-11-30 02:52:08 +08:00
|
|
|
<< " was returned.";
|
|
|
|
throw std::invalid_argument(msg.str());
|
|
|
|
}
|
2024-03-19 11:12:25 +08:00
|
|
|
if (!nb::isinstance<nb::tuple>(py_value_out)) {
|
2023-11-30 02:52:08 +08:00
|
|
|
std::ostringstream msg;
|
|
|
|
msg << error_msg_tag << " The return value of the function "
|
|
|
|
<< "whose gradient we want to compute should be either a "
|
|
|
|
<< "scalar array or a tuple with the first value being a "
|
|
|
|
<< "scalar array (Union[array, Tuple[array, Any, ...]]); but "
|
2024-03-19 11:12:25 +08:00
|
|
|
<< type_name_str(py_value_out) << " was returned.";
|
2023-11-30 02:52:08 +08:00
|
|
|
throw std::invalid_argument(msg.str());
|
|
|
|
}
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::tuple ret = nb::cast<nb::tuple>(py_value_out);
|
2023-11-30 02:52:08 +08:00
|
|
|
if (ret.size() == 0) {
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << error_msg_tag << " The return value of the function "
|
|
|
|
<< "whose gradient we want to compute should be either a "
|
|
|
|
<< "scalar array or a non-empty tuple. The first value should be a "
|
|
|
|
<< "scalar array and the rest can be anything. Instead, "
|
|
|
|
<< "we got an empty tuple.";
|
|
|
|
throw std::invalid_argument(msg.str());
|
|
|
|
}
|
2024-03-19 11:12:25 +08:00
|
|
|
if (!nb::isinstance<array>(ret[0])) {
|
2023-11-30 02:52:08 +08:00
|
|
|
std::ostringstream msg;
|
|
|
|
msg << error_msg_tag << " The return value of the function "
|
|
|
|
<< "whose gradient we want to compute should be either a "
|
|
|
|
<< "scalar array or a tuple with the first value being a "
|
|
|
|
<< "scalar array (Union[array, Tuple[array, Any, ...]]); but it "
|
|
|
|
<< "was a tuple with the first value being of type "
|
2024-03-19 11:12:25 +08:00
|
|
|
<< type_name_str(ret[0]) << " .";
|
2023-11-30 02:52:08 +08:00
|
|
|
throw std::invalid_argument(msg.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree_flatten(py_value_out, false);
|
|
|
|
},
|
|
|
|
gradient_indices)(arrays);
|
|
|
|
|
|
|
|
auto value = value_and_grads.first;
|
|
|
|
auto gradients = value_and_grads.second;
|
|
|
|
|
|
|
|
// Put the gradients back in their container.
|
|
|
|
// We have the following cases:
|
|
|
|
//
|
|
|
|
// 1. Single python positional argument has a gradient (eg argnums=[0])
|
|
|
|
// 2. Many python positional arguments have gradients (eg argnums=[0, 1])
|
|
|
|
// 3. A python keyword argument has gradients
|
|
|
|
//
|
|
|
|
// In case 1 we return the original python variable but with the gradients.
|
|
|
|
// In case 2 we return a tuple of the above.
|
|
|
|
// In case 3 we return a tuple containing a tuple and dict (sth like
|
|
|
|
// (tuple(), dict(x=mx.array(5))) ).
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object positional_grads;
|
|
|
|
nb::object keyword_grads;
|
|
|
|
nb::object py_grads;
|
2023-11-30 02:52:08 +08:00
|
|
|
|
|
|
|
// Collect the gradients for the positional arguments
|
|
|
|
if (argnums.size() == 1) {
|
|
|
|
positional_grads = tree_unflatten(args[argnums[0]], gradients, counts[0]);
|
|
|
|
} else if (argnums.size() > 1) {
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::list grads_;
|
2023-11-30 02:52:08 +08:00
|
|
|
for (int i = 0; i < argnums.size(); i++) {
|
2024-03-19 11:12:25 +08:00
|
|
|
grads_.append(tree_unflatten(args[argnums[i]], gradients, counts[i]));
|
2023-11-30 02:52:08 +08:00
|
|
|
}
|
2024-03-19 11:12:25 +08:00
|
|
|
positional_grads = nb::tuple(grads_);
|
2023-11-30 02:52:08 +08:00
|
|
|
} else {
|
2024-03-19 11:12:25 +08:00
|
|
|
positional_grads = nb::none();
|
2023-11-30 02:52:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// No keyword argument gradients so return the tuple of gradients
|
|
|
|
if (argnames.size() == 0) {
|
|
|
|
py_grads = positional_grads;
|
|
|
|
} else {
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::dict grads_;
|
2023-11-30 02:52:08 +08:00
|
|
|
for (int i = 0; i < argnames.size(); i++) {
|
|
|
|
auto& k = argnames[i];
|
|
|
|
grads_[k.c_str()] = tree_unflatten(
|
|
|
|
kwargs[k.c_str()], gradients, counts[i + argnums.size()]);
|
|
|
|
}
|
2024-03-19 11:12:25 +08:00
|
|
|
keyword_grads = grads_;
|
2023-11-30 02:52:08 +08:00
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
py_grads = nb::make_tuple(positional_grads, keyword_grads);
|
2023-11-30 02:52:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put the values back in the container
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object return_value = tree_unflatten(py_value_out, value);
|
2023-11-30 02:52:08 +08:00
|
|
|
return std::make_pair(return_value, py_grads);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto py_vmap(
|
2024-03-19 11:12:25 +08:00
|
|
|
const nb::callable& fun,
|
|
|
|
const nb::object& in_axes,
|
|
|
|
const nb::object& out_axes) {
|
|
|
|
return [fun, in_axes, out_axes](const nb::args& args) {
|
|
|
|
auto axes_to_flat_tree = [](const nb::object& tree,
|
|
|
|
const nb::object& axes) {
|
2023-11-30 02:52:08 +08:00
|
|
|
auto tree_axes = tree_map(
|
|
|
|
{tree, axes},
|
2024-03-19 11:12:25 +08:00
|
|
|
[](const std::vector<nb::object>& inputs) { return inputs[1]; });
|
2023-11-30 02:52:08 +08:00
|
|
|
std::vector<int> flat_axes;
|
2024-03-19 11:12:25 +08:00
|
|
|
tree_visit(tree_axes, [&flat_axes](nb::handle obj) {
|
2023-11-30 02:52:08 +08:00
|
|
|
if (obj.is_none()) {
|
|
|
|
flat_axes.push_back(-1);
|
2024-03-19 11:12:25 +08:00
|
|
|
} else if (nb::isinstance<nb::int_>(obj)) {
|
|
|
|
flat_axes.push_back(nb::cast<int>(nb::cast<nb::int_>(obj)));
|
2023-11-30 02:52:08 +08:00
|
|
|
} else {
|
|
|
|
throw std::invalid_argument("[vmap] axis must be int or None.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return flat_axes;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Inputs must be array or tree of arrays
|
|
|
|
auto inputs = tree_flatten(args, true);
|
|
|
|
auto flat_in_axes = axes_to_flat_tree(args, in_axes);
|
|
|
|
|
|
|
|
// py_value_out will hold the output of the python function in order to be
|
|
|
|
// able to reconstruct the python tree of extra return values
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object py_outputs;
|
2023-11-30 02:52:08 +08:00
|
|
|
|
|
|
|
auto vmap_fn =
|
|
|
|
[&fun, &args, &inputs, &py_outputs](const std::vector<array>& a) {
|
|
|
|
// Call the python function
|
|
|
|
py_outputs = fun(*tree_unflatten(args, a));
|
|
|
|
|
|
|
|
// Flatten the outputs
|
|
|
|
return tree_flatten(py_outputs, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto [trace_inputs, trace_outputs] =
|
|
|
|
detail::vmap_trace(vmap_fn, inputs, flat_in_axes);
|
|
|
|
|
|
|
|
auto flat_out_axes = axes_to_flat_tree(py_outputs, out_axes);
|
|
|
|
|
|
|
|
// Perform the vmap
|
|
|
|
auto outputs = detail::vmap_replace(
|
|
|
|
inputs, trace_inputs, trace_outputs, flat_in_axes, flat_out_axes);
|
|
|
|
|
|
|
|
// Put the outputs back in the container
|
|
|
|
return tree_unflatten(py_outputs, outputs);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-28 21:37:59 +08:00
|
|
|
std::unordered_map<std::uintptr_t, nb::object>& tree_cache() {
|
2024-01-27 05:45:30 +08:00
|
|
|
// This map is used to Cache the tree structure of the outputs
|
2024-03-28 21:37:59 +08:00
|
|
|
static std::unordered_map<std::uintptr_t, nb::object> tree_cache_;
|
2024-01-27 05:45:30 +08:00
|
|
|
return tree_cache_;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PyCompiledFun {
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::callable fun;
|
2024-03-28 21:37:59 +08:00
|
|
|
std::uintptr_t fun_id;
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object captured_inputs;
|
|
|
|
nb::object captured_outputs;
|
2024-02-20 13:43:54 +08:00
|
|
|
bool shapeless;
|
2024-03-19 11:12:25 +08:00
|
|
|
mutable size_t num_outputs{0};
|
2024-01-27 05:45:30 +08:00
|
|
|
|
2024-02-20 13:43:54 +08:00
|
|
|
PyCompiledFun(
|
2024-03-19 11:12:25 +08:00
|
|
|
const nb::callable& fun,
|
|
|
|
nb::object inputs,
|
|
|
|
nb::object outputs,
|
2024-02-20 13:43:54 +08:00
|
|
|
bool shapeless)
|
2024-02-08 09:29:22 +08:00
|
|
|
: fun(fun),
|
2024-03-28 21:37:59 +08:00
|
|
|
fun_id(reinterpret_cast<std::uintptr_t>(fun.ptr())),
|
2024-02-08 09:29:22 +08:00
|
|
|
captured_inputs(inputs),
|
2024-02-20 13:43:54 +08:00
|
|
|
captured_outputs(outputs),
|
|
|
|
shapeless(shapeless) {}
|
2024-01-27 05:45:30 +08:00
|
|
|
|
|
|
|
PyCompiledFun(const PyCompiledFun&) = delete;
|
|
|
|
PyCompiledFun& operator=(const PyCompiledFun&) = delete;
|
|
|
|
PyCompiledFun& operator=(PyCompiledFun&& other) = delete;
|
|
|
|
PyCompiledFun(PyCompiledFun&& other)
|
2024-03-28 21:37:59 +08:00
|
|
|
: fun(std::move(other.fun)),
|
|
|
|
fun_id(reinterpret_cast<std::uintptr_t>(fun.ptr())) {
|
2024-01-27 05:45:30 +08:00
|
|
|
other.fun_id = 0;
|
2024-02-08 09:29:22 +08:00
|
|
|
captured_inputs = std::move(other.captured_inputs);
|
|
|
|
captured_outputs = std::move(other.captured_outputs);
|
2024-02-20 13:43:54 +08:00
|
|
|
shapeless = other.shapeless;
|
2024-02-08 09:29:22 +08:00
|
|
|
num_outputs = other.num_outputs;
|
2024-01-27 05:45:30 +08:00
|
|
|
};
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object call_impl(const nb::args& args, const nb::kwargs& kwargs) {
|
2024-02-27 11:28:53 +08:00
|
|
|
// Flat array inputs
|
|
|
|
std::vector<array> inputs;
|
|
|
|
|
|
|
|
// Compilation constants which includes the tree structure of the arguments
|
|
|
|
std::vector<uint64_t> constants;
|
2024-02-20 13:43:54 +08:00
|
|
|
|
2024-02-27 11:28:53 +08:00
|
|
|
// Reserve some large primes to signify the presence of an array, a list or
|
|
|
|
// a dict in order to encode the structure of the pytree. We choose primes
|
2024-03-01 14:23:46 +08:00
|
|
|
// to reduce slightly the chances of these numbers occurring by a
|
2024-02-27 11:28:53 +08:00
|
|
|
// multiplication as values in the constants list.
|
|
|
|
constexpr uint64_t array_identifier = 18446744073709551557UL;
|
|
|
|
constexpr uint64_t list_identifier = 18446744073709551533UL;
|
|
|
|
constexpr uint64_t dict_identifier = 18446744073709551521UL;
|
|
|
|
|
|
|
|
// Flatten the tree with hashed constants and structure
|
2024-03-19 11:12:25 +08:00
|
|
|
std::function<void(nb::handle)> recurse;
|
|
|
|
recurse = [&](nb::handle obj) {
|
|
|
|
if (nb::isinstance<nb::list>(obj)) {
|
|
|
|
auto l = nb::cast<nb::list>(obj);
|
2024-02-27 11:28:53 +08:00
|
|
|
constants.push_back(list_identifier);
|
|
|
|
for (int i = 0; i < l.size(); ++i) {
|
|
|
|
recurse(l[i]);
|
|
|
|
}
|
2024-03-19 11:12:25 +08:00
|
|
|
} else if (nb::isinstance<nb::tuple>(obj)) {
|
|
|
|
auto l = nb::cast<nb::tuple>(obj);
|
2024-02-27 11:28:53 +08:00
|
|
|
constants.push_back(list_identifier);
|
|
|
|
for (auto item : obj) {
|
|
|
|
recurse(item);
|
|
|
|
}
|
2024-03-19 11:12:25 +08:00
|
|
|
} else if (nb::isinstance<nb::dict>(obj)) {
|
|
|
|
auto d = nb::cast<nb::dict>(obj);
|
2024-02-27 11:28:53 +08:00
|
|
|
constants.push_back(dict_identifier);
|
|
|
|
for (auto item : d) {
|
2024-03-19 11:12:25 +08:00
|
|
|
auto r = item.first.attr("__hash__");
|
2024-02-27 11:28:53 +08:00
|
|
|
constants.push_back(*reinterpret_cast<uint64_t*>(&r));
|
|
|
|
recurse(item.second);
|
|
|
|
}
|
2024-03-19 11:12:25 +08:00
|
|
|
} else if (nb::isinstance<array>(obj)) {
|
|
|
|
inputs.push_back(nb::cast<array>(obj));
|
2024-02-27 11:28:53 +08:00
|
|
|
constants.push_back(array_identifier);
|
2024-03-19 11:12:25 +08:00
|
|
|
} else if (nb::isinstance<nb::str>(obj)) {
|
|
|
|
auto r = obj.attr("__hash__");
|
2024-02-27 11:28:53 +08:00
|
|
|
constants.push_back(*reinterpret_cast<uint64_t*>(&r));
|
2024-03-19 11:12:25 +08:00
|
|
|
} else if (nb::isinstance<nb::int_>(obj)) {
|
|
|
|
auto r = nb::cast<int64_t>(obj);
|
2024-02-27 11:28:53 +08:00
|
|
|
constants.push_back(*reinterpret_cast<uint64_t*>(&r));
|
2024-03-19 11:12:25 +08:00
|
|
|
} else if (nb::isinstance<nb::float_>(obj)) {
|
|
|
|
auto r = nb::cast<double>(obj);
|
2024-02-27 11:28:53 +08:00
|
|
|
constants.push_back(*reinterpret_cast<uint64_t*>(&r));
|
|
|
|
} else {
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << "[compile] Function arguments must be trees of arrays "
|
|
|
|
<< "or constants (floats, ints, or strings), but received "
|
2024-03-19 11:12:25 +08:00
|
|
|
<< "type " << type_name_str(obj) << ".";
|
2024-02-27 11:28:53 +08:00
|
|
|
throw std::invalid_argument(msg.str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
recurse(args);
|
|
|
|
int num_args = inputs.size();
|
|
|
|
recurse(kwargs);
|
|
|
|
auto compile_fun = [this, &args, &kwargs, num_args](
|
2024-02-20 13:43:54 +08:00
|
|
|
const std::vector<array>& a) {
|
2024-02-08 09:29:22 +08:00
|
|
|
// Put tracers into captured inputs
|
|
|
|
std::vector<array> flat_in_captures;
|
|
|
|
std::vector<array> trace_captures;
|
2024-03-19 11:12:25 +08:00
|
|
|
if (!captured_inputs.is_none()) {
|
2024-02-08 09:29:22 +08:00
|
|
|
flat_in_captures = tree_flatten(captured_inputs, false);
|
|
|
|
trace_captures.insert(
|
|
|
|
trace_captures.end(), a.end() - flat_in_captures.size(), a.end());
|
|
|
|
tree_fill(captured_inputs, trace_captures);
|
|
|
|
}
|
|
|
|
|
2024-02-20 13:43:54 +08:00
|
|
|
auto tree_outputs =
|
|
|
|
fun(*tree_unflatten(args, a), **tree_unflatten(kwargs, a, num_args));
|
|
|
|
auto [outputs, py_outputs] =
|
|
|
|
tree_flatten_with_structure(std::move(tree_outputs), false);
|
2024-01-27 05:45:30 +08:00
|
|
|
|
2024-02-08 09:29:22 +08:00
|
|
|
tree_cache().insert({fun_id, py_outputs});
|
|
|
|
|
|
|
|
num_outputs = outputs.size();
|
2024-03-19 11:12:25 +08:00
|
|
|
if (!captured_outputs.is_none()) {
|
2024-02-08 09:29:22 +08:00
|
|
|
auto flat_out_captures = tree_flatten(captured_outputs, false);
|
|
|
|
outputs.insert(
|
|
|
|
outputs.end(),
|
|
|
|
std::make_move_iterator(flat_out_captures.begin()),
|
|
|
|
std::make_move_iterator(flat_out_captures.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace tracers with originals in captured inputs
|
2024-03-19 11:12:25 +08:00
|
|
|
if (!captured_inputs.is_none()) {
|
2024-02-08 09:29:22 +08:00
|
|
|
tree_replace(captured_inputs, trace_captures, flat_in_captures);
|
|
|
|
}
|
2024-01-27 05:45:30 +08:00
|
|
|
return outputs;
|
|
|
|
};
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
if (!captured_inputs.is_none()) {
|
2024-02-08 09:29:22 +08:00
|
|
|
auto flat_in_captures = tree_flatten(captured_inputs, false);
|
|
|
|
inputs.insert(
|
|
|
|
inputs.end(),
|
|
|
|
std::make_move_iterator(flat_in_captures.begin()),
|
|
|
|
std::make_move_iterator(flat_in_captures.end()));
|
|
|
|
}
|
2024-01-27 05:45:30 +08:00
|
|
|
|
|
|
|
// Compile and call
|
2024-02-20 13:43:54 +08:00
|
|
|
auto outputs =
|
|
|
|
detail::compile(compile_fun, fun_id, shapeless, constants)(inputs);
|
2024-03-19 11:12:25 +08:00
|
|
|
if (!captured_outputs.is_none()) {
|
2024-02-08 09:29:22 +08:00
|
|
|
std::vector<array> captures(
|
|
|
|
std::make_move_iterator(outputs.begin() + num_outputs),
|
|
|
|
std::make_move_iterator(outputs.end()));
|
|
|
|
tree_fill(captured_outputs, captures);
|
|
|
|
}
|
2024-01-27 05:45:30 +08:00
|
|
|
|
|
|
|
// Put the outputs back in the container
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object py_outputs = tree_cache().at(fun_id);
|
2024-01-31 08:04:45 +08:00
|
|
|
return tree_unflatten_from_structure(py_outputs, outputs);
|
2024-03-19 11:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nb::object operator()(const nb::args& args, const nb::kwargs& kwargs) const {
|
|
|
|
return const_cast<PyCompiledFun*>(this)->call_impl(args, kwargs);
|
2024-01-27 05:45:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
~PyCompiledFun() {
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::gil_scoped_acquire gil;
|
2024-01-31 08:04:45 +08:00
|
|
|
|
2024-01-27 05:45:30 +08:00
|
|
|
tree_cache().erase(fun_id);
|
|
|
|
detail::compile_erase(fun_id);
|
2024-01-31 08:04:45 +08:00
|
|
|
fun.release().dec_ref();
|
2024-02-08 09:29:22 +08:00
|
|
|
captured_inputs.release().dec_ref();
|
|
|
|
captured_outputs.release().dec_ref();
|
2024-01-27 05:45:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-31 08:04:45 +08:00
|
|
|
class PyCheckpointedFun {
|
|
|
|
public:
|
2024-03-19 11:12:25 +08:00
|
|
|
PyCheckpointedFun(nb::callable fun) : fun_(std::move(fun)) {}
|
2024-01-31 08:04:45 +08:00
|
|
|
|
|
|
|
~PyCheckpointedFun() {
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::gil_scoped_acquire gil;
|
2024-01-31 08:04:45 +08:00
|
|
|
|
|
|
|
fun_.release().dec_ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct InnerFunction {
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object fun_;
|
|
|
|
nb::object args_structure_;
|
|
|
|
std::weak_ptr<nb::object> output_structure_;
|
2024-01-31 08:04:45 +08:00
|
|
|
|
|
|
|
InnerFunction(
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object fun,
|
|
|
|
nb::object args_structure,
|
|
|
|
std::weak_ptr<nb::object> output_structure)
|
2024-01-31 08:04:45 +08:00
|
|
|
: fun_(std::move(fun)),
|
|
|
|
args_structure_(std::move(args_structure)),
|
|
|
|
output_structure_(output_structure) {}
|
|
|
|
~InnerFunction() {
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::gil_scoped_acquire gil;
|
2024-01-31 08:04:45 +08:00
|
|
|
|
|
|
|
fun_.release().dec_ref();
|
|
|
|
args_structure_.release().dec_ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<array> operator()(const std::vector<array>& inputs) {
|
2024-03-19 11:12:25 +08:00
|
|
|
auto args = nb::cast<nb::tuple>(
|
2024-01-31 08:04:45 +08:00
|
|
|
tree_unflatten_from_structure(args_structure_, inputs));
|
|
|
|
auto [outputs, output_structure] =
|
|
|
|
tree_flatten_with_structure(fun_(*args[0], **args[1]), false);
|
|
|
|
if (auto s = output_structure_.lock()) {
|
|
|
|
*s = output_structure;
|
|
|
|
}
|
|
|
|
return outputs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object call_impl(const nb::args& args, const nb::kwargs& kwargs) {
|
|
|
|
auto output_structure = std::make_shared<nb::object>();
|
|
|
|
auto full_args = nb::make_tuple(args, kwargs);
|
2024-01-31 08:04:45 +08:00
|
|
|
auto [inputs, args_structure] =
|
|
|
|
tree_flatten_with_structure(full_args, false);
|
|
|
|
|
|
|
|
auto outputs = checkpoint(
|
|
|
|
InnerFunction(fun_, args_structure, output_structure))(inputs);
|
|
|
|
|
|
|
|
return tree_unflatten_from_structure(*output_structure, outputs);
|
|
|
|
}
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::object operator()(const nb::args& args, const nb::kwargs& kwargs) const {
|
|
|
|
return const_cast<PyCheckpointedFun*>(this)->call_impl(args, kwargs);
|
|
|
|
}
|
|
|
|
|
2024-01-31 08:04:45 +08:00
|
|
|
private:
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::callable fun_;
|
2024-01-31 08:04:45 +08:00
|
|
|
};
|
|
|
|
|
2024-03-19 11:12:25 +08:00
|
|
|
void init_transforms(nb::module_& m) {
|
2023-11-30 02:52:08 +08:00
|
|
|
m.def(
|
|
|
|
"eval",
|
2024-03-19 11:12:25 +08:00
|
|
|
[](const nb::args& args) {
|
2024-02-08 09:29:22 +08:00
|
|
|
std::vector<array> arrays = tree_flatten(args, false);
|
2024-01-27 14:03:52 +08:00
|
|
|
{
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::gil_scoped_release nogil;
|
2024-01-27 14:03:52 +08:00
|
|
|
eval(arrays);
|
|
|
|
}
|
2023-11-30 02:52:08 +08:00
|
|
|
},
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::arg(),
|
|
|
|
nb::sig("def eval(*args) -> None"),
|
2023-11-30 02:52:08 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Evaluate an :class:`array` or tree of :class:`array`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
*args (arrays or trees of arrays): Each argument can be a single array
|
|
|
|
or a tree of arrays. If a tree is given the nodes can be a Python
|
2024-02-08 09:29:22 +08:00
|
|
|
:class:`list`, :class:`tuple` or :class:`dict`. Leaves which are not
|
|
|
|
arrays are ignored.
|
2023-11-30 02:52:08 +08:00
|
|
|
)pbdoc");
|
|
|
|
m.def(
|
|
|
|
"jvp",
|
2024-03-19 11:12:25 +08:00
|
|
|
[](const nb::callable& fun,
|
2023-11-30 02:52:08 +08:00
|
|
|
const std::vector<array>& primals,
|
|
|
|
const std::vector<array>& tangents) {
|
|
|
|
auto vfun = [&fun](const std::vector<array>& primals) {
|
2024-03-19 11:12:25 +08:00
|
|
|
auto out = fun(*nb::cast(primals));
|
|
|
|
if (nb::isinstance<array>(out)) {
|
|
|
|
return std::vector<array>{nb::cast<array>(out)};
|
2023-11-30 02:52:08 +08:00
|
|
|
} else {
|
2024-03-19 11:12:25 +08:00
|
|
|
return nb::cast<std::vector<array>>(out);
|
2023-11-30 02:52:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return jvp(vfun, primals, tangents);
|
|
|
|
},
|
|
|
|
"fun"_a,
|
|
|
|
"primals"_a,
|
|
|
|
"tangents"_a,
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::sig(
|
|
|
|
"def jvp(fun: callable, primals: List[array], tangents: List[array]) -> Tuple[List[array], List[array]]"),
|
2023-11-30 02:52:08 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Compute the Jacobian-vector product.
|
|
|
|
|
|
|
|
This computes the product of the Jacobian of a function ``fun`` evaluated
|
|
|
|
at ``primals`` with the ``tangents``.
|
|
|
|
|
|
|
|
Args:
|
2024-03-19 11:12:25 +08:00
|
|
|
fun (callable): A function which takes a variable number of :class:`array`
|
2023-11-30 02:52:08 +08:00
|
|
|
and returns a single :class:`array` or list of :class:`array`.
|
|
|
|
primals (list(array)): A list of :class:`array` at which to
|
|
|
|
evaluate the Jacobian.
|
|
|
|
tangents (list(array)): A list of :class:`array` which are the
|
|
|
|
"vector" in the Jacobian-vector product. The ``tangents`` should be the
|
|
|
|
same in number, shape, and type as the inputs of ``fun`` (i.e. the ``primals``).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list(array): A list of the Jacobian-vector products which
|
|
|
|
is the same in number, shape, and type of the inputs to ``fun``.
|
|
|
|
)pbdoc");
|
|
|
|
m.def(
|
|
|
|
"vjp",
|
2024-03-19 11:12:25 +08:00
|
|
|
[](const nb::callable& fun,
|
2023-11-30 02:52:08 +08:00
|
|
|
const std::vector<array>& primals,
|
|
|
|
const std::vector<array>& cotangents) {
|
|
|
|
auto vfun = [&fun](const std::vector<array>& primals) {
|
2024-03-19 11:12:25 +08:00
|
|
|
auto out = fun(*nb::cast(primals));
|
|
|
|
if (nb::isinstance<array>(out)) {
|
|
|
|
return std::vector<array>{nb::cast<array>(out)};
|
2023-11-30 02:52:08 +08:00
|
|
|
} else {
|
2024-03-19 11:12:25 +08:00
|
|
|
return nb::cast<std::vector<array>>(out);
|
2023-11-30 02:52:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return vjp(vfun, primals, cotangents);
|
|
|
|
},
|
|
|
|
"fun"_a,
|
|
|
|
"primals"_a,
|
|
|
|
"cotangents"_a,
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::sig(
|
|
|
|
"def vjp(fun: callable, primals: List[array], cotangents: List[array]) -> Tuple[List[array], List[array]]"),
|
2023-11-30 02:52:08 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Compute the vector-Jacobian product.
|
|
|
|
|
|
|
|
Computes the product of the ``cotangents`` with the Jacobian of a
|
|
|
|
function ``fun`` evaluated at ``primals``.
|
|
|
|
|
|
|
|
Args:
|
2024-03-19 11:12:25 +08:00
|
|
|
fun (callable): A function which takes a variable number of :class:`array`
|
2023-11-30 02:52:08 +08:00
|
|
|
and returns a single :class:`array` or list of :class:`array`.
|
|
|
|
primals (list(array)): A list of :class:`array` at which to
|
|
|
|
evaluate the Jacobian.
|
|
|
|
cotangents (list(array)): A list of :class:`array` which are the
|
|
|
|
"vector" in the vector-Jacobian product. The ``cotangents`` should be the
|
|
|
|
same in number, shape, and type as the outputs of ``fun``.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list(array): A list of the vector-Jacobian products which
|
|
|
|
is the same in number, shape, and type of the outputs of ``fun``.
|
|
|
|
)pbdoc");
|
|
|
|
m.def(
|
|
|
|
"value_and_grad",
|
2024-03-19 11:12:25 +08:00
|
|
|
[](const nb::callable& fun,
|
2023-11-30 02:52:08 +08:00
|
|
|
const std::optional<IntOrVec>& argnums,
|
|
|
|
const StrOrVec& argnames) {
|
|
|
|
auto [argnums_vec, argnames_vec] =
|
|
|
|
validate_argnums_argnames(argnums, argnames);
|
2024-03-19 11:12:25 +08:00
|
|
|
return nb::cpp_function(py_value_and_grad(
|
2023-11-30 02:52:08 +08:00
|
|
|
fun, argnums_vec, argnames_vec, "[value_and_grad]", false));
|
|
|
|
},
|
|
|
|
"fun"_a,
|
2024-03-19 11:12:25 +08:00
|
|
|
"argnums"_a = nb::none(),
|
2023-11-30 02:52:08 +08:00
|
|
|
"argnames"_a = std::vector<std::string>{},
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::sig(
|
|
|
|
"def value_and_grad(fun: callable, argnums: Optional[Union[int, List[int]]] = None, argnames: Union[str, List[str]] = []) -> callable"),
|
2023-11-30 02:52:08 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Returns a function which computes the value and gradient of ``fun``.
|
|
|
|
|
|
|
|
The function passed to :func:`value_and_grad` should return either
|
|
|
|
a scalar loss or a tuple in which the first element is a scalar
|
|
|
|
loss and the remaining elements can be anything.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import mlx.core as mx
|
|
|
|
|
|
|
|
def mse(params, inputs, targets):
|
|
|
|
outputs = forward(params, inputs)
|
|
|
|
lvalue = (outputs - targets).square().mean()
|
|
|
|
return lvalue
|
|
|
|
|
|
|
|
# Returns lvalue, dlvalue/dparams
|
2023-12-24 03:06:38 +08:00
|
|
|
lvalue, grads = mx.value_and_grad(mse)(params, inputs, targets)
|
2023-11-30 02:52:08 +08:00
|
|
|
|
|
|
|
def lasso(params, inputs, targets, a=1.0, b=1.0):
|
|
|
|
outputs = forward(params, inputs)
|
|
|
|
mse = (outputs - targets).square().mean()
|
|
|
|
l1 = mx.abs(outputs - targets).mean()
|
|
|
|
|
|
|
|
loss = a*mse + b*l1
|
|
|
|
|
|
|
|
return loss, mse, l1
|
|
|
|
|
2023-12-24 03:06:38 +08:00
|
|
|
(loss, mse, l1), grads = mx.value_and_grad(lasso)(params, inputs, targets)
|
2023-11-30 02:52:08 +08:00
|
|
|
|
|
|
|
Args:
|
2024-03-19 11:12:25 +08:00
|
|
|
fun (callable): A function which takes a variable number of
|
2023-11-30 02:52:08 +08:00
|
|
|
:class:`array` or trees of :class:`array` and returns
|
|
|
|
a scalar output :class:`array` or a tuple the first element
|
|
|
|
of which should be a scalar :class:`array`.
|
|
|
|
argnums (int or list(int), optional): Specify the index (or indices)
|
|
|
|
of the positional arguments of ``fun`` to compute the gradient
|
|
|
|
with respect to. If neither ``argnums`` nor ``argnames`` are
|
|
|
|
provided ``argnums`` defaults to ``0`` indicating ``fun``'s first
|
|
|
|
argument.
|
|
|
|
argnames (str or list(str), optional): Specify keyword arguments of
|
|
|
|
``fun`` to compute gradients with respect to. It defaults to [] so
|
|
|
|
no gradients for keyword arguments by default.
|
|
|
|
|
|
|
|
Returns:
|
2024-03-19 11:12:25 +08:00
|
|
|
callable: A function which returns a tuple where the first element
|
2023-11-30 02:52:08 +08:00
|
|
|
is the output of `fun` and the second element is the gradients w.r.t.
|
|
|
|
the loss.
|
|
|
|
)pbdoc");
|
|
|
|
m.def(
|
|
|
|
"grad",
|
2024-03-19 11:12:25 +08:00
|
|
|
[](const nb::callable& fun,
|
2023-11-30 02:52:08 +08:00
|
|
|
const std::optional<IntOrVec>& argnums,
|
|
|
|
const StrOrVec& argnames) {
|
|
|
|
auto [argnums_vec, argnames_vec] =
|
|
|
|
validate_argnums_argnames(argnums, argnames);
|
|
|
|
auto fn =
|
|
|
|
py_value_and_grad(fun, argnums_vec, argnames_vec, "[grad]", true);
|
2024-03-19 11:12:25 +08:00
|
|
|
return nb::cpp_function(
|
|
|
|
[fn](const nb::args& args, const nb::kwargs& kwargs) {
|
2023-11-30 02:52:08 +08:00
|
|
|
return fn(args, kwargs).second;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
"fun"_a,
|
2024-03-19 11:12:25 +08:00
|
|
|
"argnums"_a = nb::none(),
|
2023-11-30 02:52:08 +08:00
|
|
|
"argnames"_a = std::vector<std::string>{},
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::sig(
|
|
|
|
"def grad(fun: callable, argnums: Optional[Union[int, List[int]]] = None, argnames: Union[str, List[str]] = []) -> callable"),
|
2023-11-30 02:52:08 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Returns a function which computes the gradient of ``fun``.
|
|
|
|
|
|
|
|
Args:
|
2024-03-19 11:12:25 +08:00
|
|
|
fun (callable): A function which takes a variable number of
|
2023-11-30 02:52:08 +08:00
|
|
|
:class:`array` or trees of :class:`array` and returns
|
|
|
|
a scalar output :class:`array`.
|
|
|
|
argnums (int or list(int), optional): Specify the index (or indices)
|
|
|
|
of the positional arguments of ``fun`` to compute the gradient
|
|
|
|
with respect to. If neither ``argnums`` nor ``argnames`` are
|
|
|
|
provided ``argnums`` defaults to ``0`` indicating ``fun``'s first
|
|
|
|
argument.
|
|
|
|
argnames (str or list(str), optional): Specify keyword arguments of
|
|
|
|
``fun`` to compute gradients with respect to. It defaults to [] so
|
|
|
|
no gradients for keyword arguments by default.
|
|
|
|
|
|
|
|
Returns:
|
2024-03-19 11:12:25 +08:00
|
|
|
callable: A function which has the same input arguments as ``fun`` and
|
2023-11-30 02:52:08 +08:00
|
|
|
returns the gradient(s).
|
|
|
|
)pbdoc");
|
|
|
|
m.def(
|
|
|
|
"vmap",
|
2024-03-19 11:12:25 +08:00
|
|
|
[](const nb::callable& fun,
|
|
|
|
const nb::object& in_axes,
|
|
|
|
const nb::object& out_axes) {
|
|
|
|
return nb::cpp_function(py_vmap(fun, in_axes, out_axes));
|
2023-11-30 02:52:08 +08:00
|
|
|
},
|
|
|
|
"fun"_a,
|
|
|
|
"in_axes"_a = 0,
|
|
|
|
"out_axes"_a = 0,
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::sig(
|
|
|
|
"def vmap(fun: callable, in_axes: object = 0, out_axes: object = 0) -> callable"),
|
2023-11-30 02:52:08 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Returns a vectorized version of ``fun``.
|
|
|
|
|
|
|
|
Args:
|
2024-03-19 11:12:25 +08:00
|
|
|
fun (callable): A function which takes a variable number of
|
2023-11-30 02:52:08 +08:00
|
|
|
:class:`array` or a tree of :class:`array` and returns
|
|
|
|
a variable number of :class:`array` or a tree of :class:`array`.
|
|
|
|
in_axes (int, optional): An integer or a valid prefix tree of the
|
|
|
|
inputs to ``fun`` where each node specifies the vmapped axis. If
|
|
|
|
the value is ``None`` then the corresponding input(s) are not vmapped.
|
|
|
|
Defaults to ``0``.
|
|
|
|
out_axes (int, optional): An integer or a valid prefix tree of the
|
|
|
|
outputs of ``fun`` where each node specifies the vmapped axis. If
|
|
|
|
the value is ``None`` then the corresponding outputs(s) are not vmapped.
|
|
|
|
Defaults to ``0``.
|
|
|
|
|
|
|
|
Returns:
|
2024-03-19 11:12:25 +08:00
|
|
|
callable: The vectorized function.
|
2023-11-30 02:52:08 +08:00
|
|
|
)pbdoc");
|
|
|
|
m.def(
|
|
|
|
"export_to_dot",
|
2024-03-19 11:12:25 +08:00
|
|
|
[](nb::object file, const nb::args& args) {
|
2023-11-30 02:52:08 +08:00
|
|
|
std::vector<array> arrays = tree_flatten(args);
|
2024-03-19 11:12:25 +08:00
|
|
|
if (nb::isinstance<nb::str>(file)) {
|
|
|
|
std::ofstream out(nb::cast<std::string>(file));
|
2023-11-30 02:52:08 +08:00
|
|
|
export_to_dot(out, arrays);
|
2024-03-19 11:12:25 +08:00
|
|
|
} else if (nb::hasattr(file, "write")) {
|
2023-11-30 02:52:08 +08:00
|
|
|
std::ostringstream out;
|
|
|
|
export_to_dot(out, arrays);
|
|
|
|
auto write = file.attr("write");
|
|
|
|
write(out.str());
|
|
|
|
} else {
|
|
|
|
throw std::invalid_argument(
|
|
|
|
"export_to_dot accepts file-like objects or strings to be used as filenames");
|
|
|
|
}
|
|
|
|
},
|
2024-03-19 11:12:25 +08:00
|
|
|
"file"_a,
|
|
|
|
"args"_a);
|
2024-01-27 05:45:30 +08:00
|
|
|
m.def(
|
|
|
|
"compile",
|
2024-03-19 11:12:25 +08:00
|
|
|
[](const nb::callable& fun,
|
|
|
|
const nb::object& inputs,
|
|
|
|
const nb::object& outputs,
|
2024-02-20 13:43:54 +08:00
|
|
|
bool shapeless) {
|
2024-03-19 11:12:25 +08:00
|
|
|
// Try to get the name
|
|
|
|
auto n = fun.attr("__name__");
|
|
|
|
auto name = n.is_none() ? "compiled" : nb::cast<std::string>(n);
|
2024-02-28 05:18:59 +08:00
|
|
|
|
|
|
|
// Try to get the signature
|
2024-03-19 11:12:25 +08:00
|
|
|
std::ostringstream sig;
|
|
|
|
sig << "def " << name;
|
|
|
|
auto inspect = nb::module_::import_("inspect");
|
|
|
|
if (nb::cast<bool>(inspect.attr("isroutine")(fun))) {
|
|
|
|
sig << nb::cast<std::string>(
|
|
|
|
inspect.attr("signature")(fun).attr("__str__")());
|
|
|
|
} else {
|
|
|
|
sig << "(*args, **kwargs)";
|
2024-02-28 05:18:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get the doc string
|
2024-03-19 11:12:25 +08:00
|
|
|
auto d = inspect.attr("getdoc")(fun);
|
|
|
|
std::string doc =
|
|
|
|
d.is_none() ? "MLX compiled function." : nb::cast<std::string>(d);
|
|
|
|
|
|
|
|
auto sig_str = sig.str();
|
|
|
|
return nb::cpp_function(
|
2024-02-28 05:18:59 +08:00
|
|
|
PyCompiledFun{fun, inputs, outputs, shapeless},
|
2024-03-19 11:12:25 +08:00
|
|
|
nb::name(name.c_str()),
|
|
|
|
nb::sig(sig_str.c_str()),
|
|
|
|
doc.c_str());
|
2024-01-27 05:45:30 +08:00
|
|
|
},
|
|
|
|
"fun"_a,
|
2024-03-19 11:12:25 +08:00
|
|
|
"inputs"_a = nb::none(),
|
|
|
|
"outputs"_a = nb::none(),
|
2024-02-20 13:43:54 +08:00
|
|
|
"shapeless"_a = false,
|
2024-01-27 05:45:30 +08:00
|
|
|
R"pbdoc(
|
|
|
|
Returns a compiled function which produces the same output as ``fun``.
|
|
|
|
|
|
|
|
Args:
|
2024-03-19 11:12:25 +08:00
|
|
|
fun (callable): A function which takes a variable number of
|
2024-01-27 05:45:30 +08:00
|
|
|
:class:`array` or trees of :class:`array` and returns
|
|
|
|
a variable number of :class:`array` or trees of :class:`array`.
|
2024-02-08 09:29:22 +08:00
|
|
|
inputs (list or dict, optional): These inputs will be captured during
|
|
|
|
the function compilation along with the inputs to ``fun``. The ``inputs``
|
|
|
|
can be a :obj:`list` or a :obj:`dict` containing arbitrarily nested
|
|
|
|
lists, dictionaries, or arrays. Leaf nodes that are not
|
|
|
|
:obj:`array` are ignored. Default: ``None``
|
|
|
|
outputs (list or dict, optional): These outputs will be captured and
|
|
|
|
updated in a compiled function. The ``outputs`` can be a
|
|
|
|
:obj:`list` or a :obj:`dict` containing arbitrarily nested lists,
|
|
|
|
dictionaries, or arrays. Leaf nodes that are not :obj:`array` are ignored.
|
|
|
|
Default: ``None``
|
2024-02-20 13:43:54 +08:00
|
|
|
shapeless (bool, optional): A function compiled with the ``shapeless``
|
|
|
|
option enabled will not be recompiled when the input shape changes. Not all
|
|
|
|
functions can be compiled with ``shapeless`` enabled. Attempting to compile
|
|
|
|
such functions with shapeless enabled will throw. Note, changing the number
|
|
|
|
of dimensions or type of any input will result in a recompilation even with
|
|
|
|
``shapeless`` set to ``True``. Default: ``False``
|
2024-01-27 05:45:30 +08:00
|
|
|
|
|
|
|
Returns:
|
2024-03-19 11:12:25 +08:00
|
|
|
callable: A compiled function which has the same input arguments
|
2024-01-27 05:45:30 +08:00
|
|
|
as ``fun`` and returns the the same output(s).
|
|
|
|
)pbdoc");
|
|
|
|
m.def(
|
|
|
|
"disable_compile",
|
|
|
|
&disable_compile,
|
|
|
|
R"pbdoc(
|
|
|
|
Globally disable compilation. Setting the environment variable
|
|
|
|
``MLX_DISABLE_COMPILE`` can also be used to disable compilation.
|
|
|
|
)pbdoc");
|
|
|
|
m.def(
|
|
|
|
"enable_compile",
|
|
|
|
&enable_compile,
|
|
|
|
R"pbdoc(
|
|
|
|
Globally enable compilation. This will override the environment
|
|
|
|
variable ``MLX_DISABLE_COMPILE`` if set.
|
|
|
|
)pbdoc");
|
2024-01-31 08:04:45 +08:00
|
|
|
m.def(
|
|
|
|
"checkpoint",
|
2024-03-19 11:12:25 +08:00
|
|
|
[](nb::callable fun) { return nb::cpp_function(PyCheckpointedFun{fun}); },
|
2024-01-31 08:04:45 +08:00
|
|
|
"fun"_a);
|
2024-01-27 05:45:30 +08:00
|
|
|
|
|
|
|
// Register static Python object cleanup before the interpreter exits
|
2024-03-19 11:12:25 +08:00
|
|
|
auto atexit = nb::module_::import_("atexit");
|
|
|
|
atexit.attr("register")(nb::cpp_function([]() { tree_cache().clear(); }));
|
2023-11-30 02:52:08 +08:00
|
|
|
}
|