From 9f5b9a68208681ed07067b246e29cacc7bd9e39c Mon Sep 17 00:00:00 2001 From: Anastasiia Filippova Date: Wed, 11 Nov 2026 23:59:20 +0100 Subject: [PATCH 1/8] draft recomputation of activations --- mlx/array.cpp | 1 + mlx/array.h | 12 ++ mlx/compile.cpp | 353 ++++++++++++++++++++++++++++++++++++++++-- mlx/transforms.cpp | 2 + mlx/transforms_impl.h | 16 ++ 5 files changed, 371 insertions(+), 13 deletions(-) diff --git a/mlx/array.cpp b/mlx/array.cpp index cf49a0fe5f..ade99dad77 100644 --- a/mlx/array.cpp +++ b/mlx/array.cpp @@ -249,6 +249,7 @@ void array::ArrayDesc::init() { for (const auto& in : inputs) { is_tracer |= in.is_tracer(); } + is_backward = detail::InBackward::in_backward(); } array::ArrayDesc::ArrayDesc(Shape shape, Dtype dtype) diff --git a/mlx/array.h b/mlx/array.h index 59ab941945..37c13abd54 100644 --- a/mlx/array.h +++ b/mlx/array.h @@ -426,6 +426,15 @@ class MLX_API array { // Check if the array is a tracer array bool is_tracer() const; + // Mark the array as part of a backward graph (true) or not. + void set_backward(bool is_backward) { + array_desc_->is_backward = is_backward; + } + // Check if the array was made while autodiff built a backward graph + bool is_backward() const { + return array_desc_->is_backward; + } + void set_data(allocator::Data data); void set_data( @@ -473,6 +482,9 @@ class MLX_API array { // and should not be detached from the graph bool is_tracer{false}; + // Indicates an array was made while autodiff built a backward graph + bool is_backward{false}; + // This is a shared pointer so that *different* arrays // can share the underlying data buffer. std::shared_ptr data; diff --git a/mlx/compile.cpp b/mlx/compile.cpp index e6bb2953b5..97ef003fdc 100644 --- a/mlx/compile.cpp +++ b/mlx/compile.cpp @@ -2,8 +2,11 @@ #include #include +#include +#include #include #include +#include #include #include #include @@ -22,6 +25,9 @@ namespace mlx::core { +using ArrayFunction = + std::function(const std::vector&)>; + constexpr int max_compile_depth = 11; constexpr int max_compile_arrays = 24; @@ -528,12 +534,14 @@ std::pair, ParentsMap> compile_dfs( auto as = array::make_arrays( std::move(shapes), types, arr.primitive_ptr(), std::move(inputs)); for (int i = 0; i < out.size(); ++i) { + as[i].set_backward(out[i].is_backward()); old_to_new.insert({out[i].id(), as[i]}); } new_tape.push_back(as[arr.sibling_position()]); } else { auto a = array( arr.shape(), arr.dtype(), arr.primitive_ptr(), std::move(inputs)); + a.set_backward(arr.is_backward()); old_to_new.insert({arr.id(), a}); new_tape.push_back(a); } @@ -796,6 +804,323 @@ void compile_simplify( } } +// Max-flow solver (Dinic) for compile_recompute. +class MaxFlow { + public: + static constexpr int64_t inf = std::numeric_limits::max() / 4; + + explicit MaxFlow(int n) : adj_(n), level_(n), next_(n) {} + + void add_edge(int u, int v, int64_t cap) { + adj_[u].push_back(edges_.size()); + edges_.push_back({v, cap}); + adj_[v].push_back(edges_.size()); + edges_.push_back({u, 0}); + } + + // Returns the max flow, or inf if there is no finite cut. + int64_t solve(int s, int t) { + int64_t flow = 0; + while (make_levels(s, t)) { + std::fill(next_.begin(), next_.end(), 0); + while (int64_t f = augment(s, t)) { + flow += f; + if (flow >= inf) { + return inf; + } + } + } + return flow; + } + + // Vertices that s still reaches in the residual graph. + std::vector source_side(int s) const { + std::vector seen(adj_.size(), false); + std::vector stack = {s}; + seen[s] = true; + while (!stack.empty()) { + int u = stack.back(); + stack.pop_back(); + for (int e : adj_[u]) { + int v = edges_[e].to; + if (edges_[e].cap > 0 && !seen[v]) { + seen[v] = true; + stack.push_back(v); + } + } + } + return seen; + } + + private: + struct Edge { + int to; + int64_t cap; + }; + + bool make_levels(int s, int t) { + std::fill(level_.begin(), level_.end(), -1); + std::queue queue; + level_[s] = 0; + queue.push(s); + while (!queue.empty()) { + int u = queue.front(); + queue.pop(); + for (int e : adj_[u]) { + int v = edges_[e].to; + if (edges_[e].cap > 0 && level_[v] < 0) { + level_[v] = level_[u] + 1; + queue.push(v); + } + } + } + return level_[t] >= 0; + } + + // Push flow along one path of the level graph. A stack is used instead of + // recursion because compiled graphs can be deep. + int64_t augment(int s, int t) { + std::vector path; + int u = s; + while (u != t) { + bool moved = false; + for (; next_[u] < adj_[u].size(); ++next_[u]) { + int e = adj_[u][next_[u]]; + int v = edges_[e].to; + if (edges_[e].cap > 0 && level_[v] == level_[u] + 1) { + path.push_back(e); + u = v; + moved = true; + break; + } + } + if (!moved) { + if (path.empty()) { + return 0; + } + // Dead end: remove u from the level graph and step back. + level_[u] = -1; + u = edges_[path.back() ^ 1].to; + path.pop_back(); + } + } + int64_t f = inf; + for (int e : path) { + f = std::min(f, edges_[e].cap); + } + for (int e : path) { + edges_[e].cap -= f; + edges_[e ^ 1].cap += f; + } + return f; + } + + std::vector edges_; + std::vector> adj_; + std::vector level_; + std::vector next_; +}; + +void compile_recompute(std::vector& tape, ParentsMap& parents_map) { + int n = tape.size(); + std::unordered_map vid; + for (int i = 0; i < n; ++i) { + vid.insert({tape[i].id(), i}); + for (auto& s : tape[i].siblings()) { + vid.insert({s.id(), i}); + } + } + + // Backward nodes are made by autodiff, or computed from such nodes, like an + // optimizer update. + std::vector backward(n, false); + bool has_backward = false; + for (int i = 0; i < n; ++i) { + auto& a = tape[i]; + if (!a.has_primitive()) { + continue; + } + bool b = a.is_backward(); + for (auto& in : a.inputs()) { + b = b || backward[vid.at(in.id())]; + } + backward[i] = b; + has_backward |= b; + } + if (!has_backward) { + return; + } + + auto readers = [&](int i) { + std::vector rs; + for (auto& o : tape[i].outputs()) { + if (auto it = parents_map.find(o.id()); it != parents_map.end()) { + for (auto& [p, _] : it->second) { + if (auto r = vid.find(p.id()); r != vid.end()) { + rs.push_back(r->second); + } + } + } + } + return rs; + }; + auto fusable = [&](int i) { + return tape[i].has_primitive() && is_fusable(tape[i].primitive()); + }; + auto is_view = [&](int i) { + if (!tape[i].has_primitive()) { + return false; + } + auto& p = tape[i].primitive(); + return typeid(p) == typeid(Broadcast) || typeid(p) == typeid(Reshape) || + typeid(p) == typeid(Transpose) || typeid(p) == typeid(ExpandDims) || + typeid(p) == typeid(Squeeze); + }; + + MaxFlow graph(2 * n + 2); + int source = 2 * n; + int sink = 2 * n + 1; + for (int i = 0; i < n; ++i) { + auto& a = tape[i]; + int in = 2 * i; + int out = 2 * i + 1; + if (backward[i]) { + graph.add_edge(in, sink, MaxFlow::inf); + continue; + } + auto rs = readers(i); + for (int r : rs) { + graph.add_edge(out, 2 * r, MaxFlow::inf); + } + if (!a.has_primitive()) { + // Inputs and constants stay alive anyway, so keeping them is free. + graph.add_edge(source, in, MaxFlow::inf); + graph.add_edge(in, out, 0); + continue; + } + if (is_view(i)) { + // Views are free to recompute, so they are never kept. + graph.add_edge(in, out, MaxFlow::inf); + continue; + } + + // Keeping a value costs its size. The cost doubles when a fused kernel + // would otherwise keep the value in registers. + bool in_registers = + fusable(i) && !rs.empty() && std::all_of(rs.begin(), rs.end(), fusable); + int64_t bytes = 0; + for (auto& o : a.outputs()) { + bytes += o.nbytes(); + } + graph.add_edge(in, out, in_registers ? 2 * bytes : bytes); + + // Only cheap elementwise ops are recomputed, and only when every backward + // reader can fuse with them. Other ops must run in the forward. + bool recompute = fusable(i) && a.siblings().empty() && + std::all_of(rs.begin(), rs.end(), [&](int r) { + return !backward[r] || fusable(r); + }); + if (!recompute) { + graph.add_edge(source, in, MaxFlow::inf); + } + } + + auto kept_bytes = graph.solve(source, sink); + if (kept_bytes >= MaxFlow::inf) { + return; + } + auto source_side = graph.source_side(source); + + // The value the backward reads for `x`: x itself if it is kept, otherwise a + // copy computed from kept values. + std::unordered_map copies; + std::vector new_nodes; + std::function backward_value; + backward_value = [&](const array& x) -> array { + int j = vid.at(x.id()); + if (backward[j] || source_side[2 * j]) { + return x; + } + if (auto it = copies.find(j); it != copies.end()) { + return it->second; + } + auto& a = tape[j]; + std::vector inputs; + for (auto& in : a.inputs()) { + inputs.push_back(backward_value(in)); + } + array c(a.shape(), a.dtype(), a.primitive_ptr(), std::move(inputs)); + c.set_backward(true); + for (int k = 0; k < c.inputs().size(); ++k) { + parents_map[c.inputs()[k].id()].push_back({c, k}); + } + copies.insert({j, c}); + new_nodes.push_back(c); + return c; + }; + + // Point the backward readers at the copies. + for (int i = 0; i < n; ++i) { + if (!backward[i]) { + continue; + } + for (int k = 0; k < tape[i].inputs().size(); ++k) { + auto x = tape[i].inputs()[k]; + auto c = backward_value(x); + if (c.id() == x.id()) { + continue; + } + auto& from = parents_map[x.id()]; + auto& to = parents_map[c.id()]; + for (auto it = from.begin(); it != from.end();) { + if (it->second == k && vid.at(it->first.id()) == i) { + it->first.inputs()[k] = c; + to.push_back(std::move(*it)); + it = from.erase(it); + } else { + ++it; + } + } + } + } + + if (config::get("MLX_COMPILE_RECOMPUTE", 0) > 1) { + std::cerr << "[compile_recompute] kept " << kept_bytes + << " bytes, recomputed " << new_nodes.size() << " ops:"; + for (auto& c : new_nodes) { + std::cerr << " " << c.primitive().name(); + } + std::cerr << std::endl; + } + if (new_nodes.empty()) { + return; + } + + // Put each copy in the tape before the first node that reads it. + std::unordered_map copy_of_id; + for (auto& c : new_nodes) { + copy_of_id.insert({c.id(), c}); + } + std::vector new_tape; + new_tape.reserve(tape.size() + new_nodes.size()); + std::unordered_set placed; + std::function place_inputs; + place_inputs = [&](const array& a) { + for (auto& in : a.inputs()) { + auto it = copy_of_id.find(in.id()); + if (it != copy_of_id.end() && placed.insert(in.id()).second) { + place_inputs(it->second); + new_tape.push_back(it->second); + } + } + }; + for (auto& a : tape) { + place_inputs(a); + new_tape.push_back(a); + } + tape = std::move(new_tape); +} + // Extract sub-graphs of the graph that can be compiled // and replace them with a Compiled Primitive. void compile_fuse( @@ -1163,6 +1488,12 @@ ArrayFnWithExtra compile( entry.tape, parents_map, entry.outputs, /* passes */ 3); } + // Experimental: recompute cheap forward values in the backward + // instead of keeping them. Enable with MLX_COMPILE_RECOMPUTE=1. + if (config::get("MLX_COMPILE_RECOMPUTE", 0)) { + compile_recompute(entry.tape, parents_map); + } + // Kernel fusion to generate Compiled primitives. The tape and // new outputs must be updated accordingly if (mode != CompileMode::no_fuse) { @@ -1184,8 +1515,8 @@ ArrayFnWithExtra compile( }; } -std::function(const std::vector&)> compile( - std::function(const std::vector&)> fun, +ArrayFunction compile( + ArrayFunction fun, std::uintptr_t fun_id, bool shapeless /* = false */, std::vector constants /* = {} */) { @@ -1229,9 +1560,7 @@ void compile_clear_cache(const CompileCacheWeakPtr& cache) { } // namespace detail -std::function(const std::vector&)> compile( - std::function(const std::vector&)> fun, - bool shapeless /* false */) { +ArrayFunction compile(ArrayFunction fun, bool shapeless /* false */) { if (detail::skip_compile()) { return fun; } @@ -1241,13 +1570,11 @@ std::function(const std::vector&)> compile( // lifetime return detail::compile(std::move(fun), fun_id, shapeless); } else { - auto pfun = std::shared_ptr< - std::function(const std::vector&)>>( - new std::function(const std::vector&)>{fun}, - [cache = detail::compile_cache()](auto* p) { - detail::compile_erase(cache, reinterpret_cast(p)); - delete p; - }); + auto cleanup = [cache = detail::compile_cache()](auto* p) { + detail::compile_erase(cache, reinterpret_cast(p)); + delete p; + }; + auto pfun = std::shared_ptr(new ArrayFunction{fun}, cleanup); fun_id = reinterpret_cast(pfun.get()); return detail::compile( [pfun = std::move(pfun)](const auto& inputs) { @@ -1258,7 +1585,7 @@ std::function(const std::vector&)> compile( } } -std::function(const std::vector&)> compile( +ArrayFunction compile( std::vector (*fun)(const std::vector&), bool shapeless /* = false */) { if (detail::skip_compile()) { diff --git a/mlx/transforms.cpp b/mlx/transforms.cpp index 24c91f3483..90254b0046 100644 --- a/mlx/transforms.cpp +++ b/mlx/transforms.cpp @@ -121,6 +121,7 @@ int& detail::InExportTracing::counter() { return counter_; } thread_local int detail::RetainGraph::tracing_counter{0}; +thread_local int detail::InBackward::counter{0}; array eval_impl(std::vector outputs, bool async) { std::deque tape; @@ -544,6 +545,7 @@ std::pair, std::vector> vjp( // Run the tape backwards, computing vector-jacobian // products for each primitive + detail::InBackward in_backward; std::unordered_map cotan_map; for (auto [out_idx, cotan_idx] : output_cotan_pairs) { auto& o = outputs[out_idx]; diff --git a/mlx/transforms_impl.h b/mlx/transforms_impl.h index 4fc78a1ac2..5d17540381 100644 --- a/mlx/transforms_impl.h +++ b/mlx/transforms_impl.h @@ -80,6 +80,22 @@ struct RetainGraph { static thread_local int tracing_counter; }; +struct InBackward { + InBackward() { + counter++; + } + ~InBackward() { + counter--; + } + + static bool in_backward() { + return counter > 0; + } + + private: + static thread_local int counter; +}; + /** Return true if we are currently performing a function transformation in * order to keep the graph when evaluating tracer arrays. */ inline bool in_tracing() { From f2bb22fd3f1c134ea0aaa029cf28a290ed569603 Mon Sep 17 00:00:00 2001 From: Anastasiia Filippova Date: Fri, 13 Nov 2026 23:25:43 +0100 Subject: [PATCH 2/8] draft of new compiler --- mlx/compile.cpp | 780 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 589 insertions(+), 191 deletions(-) diff --git a/mlx/compile.cpp b/mlx/compile.cpp index 97ef003fdc..4b2deeba46 100644 --- a/mlx/compile.cpp +++ b/mlx/compile.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -30,6 +29,7 @@ using ArrayFunction = constexpr int max_compile_depth = 11; constexpr int max_compile_arrays = 24; +constexpr int max_fuse_distance = 0; bool is_unary(const Primitive& p) { return ( @@ -977,6 +977,31 @@ void compile_recompute(std::vector& tape, ParentsMap& parents_map) { typeid(p) == typeid(Squeeze); }; + // True if the backward reads node i, directly or through views + std::function backward_reads; + backward_reads = [&](int i) -> bool { + for (int r : readers(i)) { + if (backward[r] || (is_view(r) && backward_reads(r))) { + return true; + } + } + return false; + }; + + // True if the backward reads node i only with fusable ops and not through a + // view. After a transpose or a reshape the copy does not fuse, for example a + // weight cast read by a matmul. After a broadcast the copy runs once for each + // element, for example a scalar learning rate in each optimizer update. + auto fuses_into_backward = [&](int i) { + for (int r : readers(i)) { + if (is_view(r) ? (backward[r] || backward_reads(r)) + : (backward[r] && !fusable(r))) { + return false; + } + } + return true; + }; + MaxFlow graph(2 * n + 2); int source = 2 * n; int sink = 2 * n + 1; @@ -1014,12 +1039,10 @@ void compile_recompute(std::vector& tape, ParentsMap& parents_map) { } graph.add_edge(in, out, in_registers ? 2 * bytes : bytes); - // Only cheap elementwise ops are recomputed, and only when every backward - // reader can fuse with them. Other ops must run in the forward. - bool recompute = fusable(i) && a.siblings().empty() && - std::all_of(rs.begin(), rs.end(), [&](int r) { - return !backward[r] || fusable(r); - }); + // Only cheap elementwise ops are recomputed, and only when the backward + // can fuse them. Other ops must run in the forward. + bool recompute = + fusable(i) && a.siblings().empty() && fuses_into_backward(i); if (!recompute) { graph.add_edge(source, in, MaxFlow::inf); } @@ -1064,6 +1087,8 @@ void compile_recompute(std::vector& tape, ParentsMap& parents_map) { if (!backward[i]) { continue; } + // Give each backward node its own copies, so each kernel can fuse them. + copies.clear(); for (int k = 0; k < tape[i].inputs().size(); ++k) { auto x = tape[i].inputs()[k]; auto c = backward_value(x); @@ -1084,14 +1109,6 @@ void compile_recompute(std::vector& tape, ParentsMap& parents_map) { } } - if (config::get("MLX_COMPILE_RECOMPUTE", 0) > 1) { - std::cerr << "[compile_recompute] kept " << kept_bytes - << " bytes, recomputed " << new_nodes.size() << " ops:"; - for (auto& c : new_nodes) { - std::cerr << " " << c.primitive().name(); - } - std::cerr << std::endl; - } if (new_nodes.empty()) { return; } @@ -1123,139 +1140,563 @@ void compile_recompute(std::vector& tape, ParentsMap& parents_map) { // Extract sub-graphs of the graph that can be compiled // and replace them with a Compiled Primitive. -void compile_fuse( - std::vector& tape, - ParentsMap& parents_map, +// Replace the arrays in `fused_tape` with one Compiled primitive. The arrays in +// `old_outputs` become the outputs of the primitive. +std::vector make_compiled( + std::vector fused_tape, const std::vector& inputs, - std::vector& outputs) { - // Track outputs to replace with new compiled outputs - std::unordered_map output_map; - for (auto& o : outputs) { - output_map.insert({o.id(), o}); + std::vector old_outputs, + const std::unordered_set& input_ids, + std::unordered_map& output_map, + ParentsMap& parents_map) { + std::unordered_set fused_ids; + for (auto& f : fused_tape) { + fused_ids.insert(f.id()); + } + std::unordered_set output_ids; + for (auto& o : old_outputs) { + output_ids.insert(o.id()); } - // Set of inputs to distinguish constants - std::unordered_set input_ids; + for (auto& f : fused_tape) { + if (output_ids.find(f.id()) != output_ids.end()) { + // Parents are now siblings, update the parent map + auto& pairs = parents_map[f.id()]; + pairs.erase( + std::remove_if( + pairs.begin(), + pairs.end(), + [&](auto& p) { + return fused_ids.find(p.first.id()) != fused_ids.end(); + }), + pairs.end()); + } else { + // Remove inner fused arrays parents from the parents map + // to keep the parents map in a valid state + parents_map.erase(f.id()); + } + } + + std::vector shapes; + std::vector types; + for (auto& o : old_outputs) { + if (o.shape() != old_outputs.back().shape()) { + throw std::runtime_error( + "[compile] Compilation failed. Tried to fuse operations with different output shapes"); + } + shapes.push_back(o.shape()); + types.push_back(o.dtype()); + } + std::unordered_set constant_ids; for (auto& in : inputs) { - input_ids.insert(in.id()); + // Scalar constant + if (in.size() == 1 && !in.has_primitive() && + input_ids.find(in.id()) == input_ids.end()) { + constant_ids.insert(in.id()); + } + } + auto compiled_outputs = array::make_arrays( + std::move(shapes), + types, + std::make_shared( + old_outputs.back().primitive().stream(), + inputs, + old_outputs, + std::move(fused_tape), + std::move(constant_ids)), + inputs); + + // Replace inputs old parents with compiled_outputs + for (int i = 0; i < inputs.size(); ++i) { + auto& pairs = parents_map[inputs[i].id()]; + pairs.erase( + std::remove_if( + pairs.begin(), + pairs.end(), + [&](auto& p) { + return fused_ids.find(p.first.id()) != fused_ids.end(); + }), + pairs.end()); + for (auto& o : compiled_outputs) { + pairs.push_back({o, i}); + } } - // Go through the tape in reverse order and check for fusable sub-graphs - std::vector new_tape; - std::unordered_set global_cache; - for (int i = tape.size() - 1; i >= 0; --i) { - auto& arr = tape[i]; + // - Update outputs parents to point to compiled outputs + // - Update any overall graph outputs to be compiled outputs + for (int o = 0; o < old_outputs.size(); ++o) { + merge_one(compiled_outputs[o], old_outputs[o], parents_map); + if (auto it = output_map.find(old_outputs[o].id()); + it != output_map.end()) { + it->second = compiled_outputs[o]; + } + } + return compiled_outputs; +} + +// Groups of arrays to fuse. Each group with more than one array becomes one +// Compiled primitive. +struct FuseGroups { + std::vector> members; + std::unordered_map group_of; + + // The group of `a`, or -1 if `a` is not fusable + int find(const array& a) const { + auto it = group_of.find(a.id()); + return (it == group_of.end()) ? -1 : it->second; + } + + void add(const array& a, int g) { + group_of[a.id()] = g; + members[g].push_back(a); + } + + void make_group(const array& a) { + members.emplace_back(); + add(a, members.size() - 1); + } + + // Move the arrays of group `src` to group `dst` + void merge(int dst, int src) { + for (auto& a : members[src]) { + add(a, dst); + } + members[src].clear(); + } + + // Undo merge(dst, src), where `n` is the size of `dst` before the merge + void unmerge(int dst, int src, int n) { + for (int i = n; i < members[dst].size(); ++i) { + add(members[dst][i], src); + } + members[dst].erase(members[dst].begin() + n, members[dst].end()); + } +}; + +// True if `a` is read outside of its group or is an output of the graph +bool is_group_output( + const array& a, + const FuseGroups& groups, + const ParentsMap& parents_map, + const std::unordered_map& output_map) { + if (output_map.find(a.id()) != output_map.end()) { + return true; + } + int g = groups.find(a); + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + if (groups.find(p) != g) { + return true; + } + } + } + return false; +} - // Already compiled - if (global_cache.find(arr.id()) != global_cache.end()) { +// True if group `g` can become one Compiled primitive +bool is_valid_group( + int g, + const FuseGroups& groups, + const ParentsMap& parents_map, + const std::unordered_map& output_map) { + // All outputs have the same shape. Do not write out a broadcast. + std::vector outputs; + for (auto& a : groups.members[g]) { + if (!is_group_output(a, groups, parents_map, output_map)) { continue; } + if (is_broadcast(a.primitive()) && + output_map.find(a.id()) == output_map.end()) { + return false; + } + if (!outputs.empty() && a.shape() != outputs[0].shape()) { + return false; + } + outputs.push_back(a); + } - // Two pass recursion: - // First pass: - // - Collect all the primitives which we can fuse with - // - Keeps a cache of fusable primitives which may be added out of - // DAG order. We have to determine if all of a fused primitive's - // outputs are also in the fused section, and this may not be the - // case the first time we visit it. - // Second pass: - // - Collect inputs to the new compiled primitive - // - Add fusable primitives to a tape in the correct order + // Count the inputs and find the longest chain of arrays in the group + std::unordered_set inputs; + std::unordered_map depths; + std::function recurse; + recurse = [&](const array& a) -> int { + if (groups.find(a) != g) { + inputs.insert(a.id()); + return 0; + } + if (auto it = depths.find(a.id()); it != depths.end()) { + return it->second; + } + int depth = 0; + for (auto& in : a.inputs()) { + depth = std::max(depth, recurse(in)); + } + depths.insert({a.id(), depth + 1}); + return depth + 1; + }; + int depth = 0; + for (auto& a : groups.members[g]) { + depth = std::max(depth, recurse(a)); + } + return depth <= max_compile_depth && + inputs.size() + outputs.size() <= max_compile_arrays; +} - std::function recurse; - std::unordered_set cache; - std::unordered_set input_set; - recurse = [&](const array& a, - int depth, - const Stream& s, - const Shape& shape) { - if (cache.find(a.id()) != cache.end()) { - return; +// Give group `g` its own copy of the broadcast `b` with split_one. Return +// true if the copy is fused into the group. +bool split_broadcast( + array b, + int g, + FuseGroups& groups, + ParentsMap& parents_map, + const std::unordered_map& output_map) { + std::unordered_set divider; + for (auto& a : groups.members[g]) { + divider.insert(a.id()); + } + array c = split_one(b, parents_map, divider); + for (int i = 0; i < c.inputs().size(); ++i) { + parents_map[c.inputs()[i].id()].push_back({c, i}); + } + groups.add(c, g); + if (is_valid_group(g, groups, parents_map, output_map)) { + return true; + } + + // Undo the copy + groups.members[g].pop_back(); + groups.group_of.erase(c.id()); + for (auto& in : c.inputs()) { + auto& pairs = parents_map[in.id()]; + pairs.erase( + std::remove_if( + pairs.begin(), + pairs.end(), + [&](auto& p) { return p.first.id() == c.id(); }), + pairs.end()); + } + merge_one(b, c, parents_map); + return false; +} + +// Fuse a group into the group that reads it, if all the readers of the group +// are in that group. Return true if a group changed. +bool compile_fuse_vertical( + FuseGroups& groups, + const std::vector& tape, + ParentsMap& parents_map, + const std::unordered_map& output_map) { + bool changed = false; + + // Go through the tape in reverse order so readers are fused first + for (int i = tape.size() - 1; i >= 0; --i) { + auto& a = tape[i]; + int src = groups.find(a); + if (src < 0) { + continue; + } + + // Find the groups that read the group of `a` + std::vector readers; + bool all_fusable = true; + for (auto& m : groups.members[src]) { + auto it = parents_map.find(m.id()); + if (it == parents_map.end()) { + continue; } + for (auto& [p, idx] : it->second) { + int dst = groups.find(p); + if (dst < 0) { + all_fusable = false; + } else if ( + dst != src && + std::find(readers.begin(), readers.end(), dst) == readers.end()) { + readers.push_back(dst); + } + } + } - // Stop fusing if: - // - Depth limit exceeded - // - Constant input - // - Stream mismatch - // - Non fusable primitive - // - Is global output but has a different shape - if (depth >= max_compile_depth || !a.has_primitive() || - a.primitive().stream() != s || !is_fusable(a.primitive()) || - (output_map.find(a.id()) != output_map.end() && a.shape() != shape)) { - // Possible input - input_set.insert(a.id()); - return; + // Split a broadcast with readers in more than one place, so that each + // group can fuse its own copy + if (is_broadcast(a.primitive()) && groups.members[src].size() == 1 && + (readers.size() > 1 || (readers.size() == 1 && !all_fusable))) { + for (int dst : readers) { + changed |= split_broadcast(a, dst, groups, parents_map, output_map); } + continue; + } - bool all_parents_in = true; - if (depth > 0) { - // Guaranteed to have a parent since nested in the - // recursion. - auto& parents = parents_map.at(a.id()); - for (auto& [p, idx] : parents) { - auto in_cache = cache.find(p.id()) != cache.end(); - if (!in_cache) { - all_parents_in = false; - break; + if (!all_fusable || readers.size() != 1) { + continue; + } + int dst = readers[0]; + if (a.primitive().stream() != groups.members[dst][0].primitive().stream()) { + continue; + } + int n = groups.members[dst].size(); + groups.merge(dst, src); + if (is_valid_group(dst, groups, parents_map, output_map)) { + changed = true; + } else { + groups.unmerge(dst, src, n); + } + } + return changed; +} + +// True if a path through arrays outside of groups `g1` and `g2` connects them. +// The two groups cannot be one kernel then. +bool is_connected( + int g1, + int g2, + const FuseGroups& groups, + const std::unordered_map& position, + const ParentsMap& parents_map) { + auto in_groups = [&](const array& a) { + int g = groups.find(a); + return g == g1 || g == g2; + }; + + // Readers come after their inputs in the tape, so stop the search after the + // last array of the two groups + int last = 0; + for (int g : {g1, g2}) { + for (auto& a : groups.members[g]) { + if (auto it = position.find(a.id()); it != position.end()) { + last = std::max(last, it->second); + } + } + } + + // Start from the readers outside of the two groups + std::vector stack; + for (int g : {g1, g2}) { + for (auto& a : groups.members[g]) { + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + if (!in_groups(p)) { + stack.push_back(p); } } } + } + } - // Arrays with a mix of parents outside the compilable section - // are not fusable except for broadcast which we can split to avoid - // stopping fusion - if (!all_parents_in) { - if (a.has_primitive() && is_broadcast(a.primitive()) && - input_set.size() < max_compile_arrays) { - array b = split_one(a, parents_map, cache); - recurse(b, depth, s, shape); - } else { - // Possible input - input_set.insert(a.id()); - } - return; + std::unordered_set visited; + while (!stack.empty()) { + auto a = std::move(stack.back()); + stack.pop_back(); + if (in_groups(a)) { + return true; + } + if (!visited.insert(a.id()).second) { + continue; + } + if (auto it = position.find(a.id()); + it != position.end() && it->second > last) { + continue; + } + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + stack.push_back(p); } + } + } + return false; +} - if (output_map.find(a.id()) != output_map.end()) { - input_set.insert(a.id()); - } else { - // Not an input anymore since fusing it - input_set.erase(a.id()); +// Fuse two groups which read the same arrays, if no path through other arrays +// connects them and they run at about the same time in the eval order. Return +// true if a group changed. +bool compile_fuse_horizontal( + FuseGroups& groups, + const std::vector& tape, + const std::unordered_map& position, + ParentsMap& parents_map, + const std::unordered_map& output_map) { + // Score each pair of groups with the bytes of the arrays they both read + std::map, size_t> scores; + for (auto& x : tape) { + // Scalars become constants in the kernel, so they are not read + if (x.size() == 1) { + continue; + } + auto it = parents_map.find(x.id()); + if (it == parents_map.end()) { + continue; + } + std::vector readers; + for (auto& [p, idx] : it->second) { + int g = groups.find(p); + if (g >= 0 && g != groups.find(x) && + std::find(readers.begin(), readers.end(), g) == readers.end()) { + readers.push_back(g); } - if (input_set.size() >= max_compile_arrays) { - return; + } + for (int i = 0; i < readers.size(); ++i) { + for (int j = i + 1; j < readers.size(); ++j) { + auto key = std::minmax(readers[i], readers[j]); + scores[{key.first, key.second}] += x.nbytes(); } - cache.insert({a.id()}); + } + } - for (auto& in : a.inputs()) { - recurse(in, depth + 1, s, shape); + // Eval runs an array as late as possible, just before its readers. So the + // level of an array, its longest path to an output, sets when it runs. + std::unordered_map levels; + std::function level; + level = [&](const array& a) -> int { + if (auto it = levels.find(a.id()); it != levels.end()) { + return it->second; + } + int l = 0; + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + l = std::max(l, level(p) + 1); } - }; + } + levels.insert({a.id(), l}); + return l; + }; - // This will be the result of the fused operation so it needs - // a) to not be already computed ie have a primitive - // b) that primitive to not be a broadcast since it will unnecessarily - // cast to a contiguous array potentially blowing up memory - if (arr.has_primitive() && !is_broadcast(arr.primitive())) { - Stream s = arr.primitive().stream(); - recurse(arr, 0, s, arr.shape()); + // A group runs at the level of its deepest output + auto group_level = [&](int g) { + int l = 0; + for (auto& a : groups.members[g]) { + if (is_group_output(a, groups, parents_map, output_map)) { + l = std::max(l, level(a)); + } + } + return l; + }; + auto distance = [&](int g1, int g2) { + return std::abs(group_level(g1) - group_level(g2)); + }; + auto is_used = [&](int g) { + for (auto& a : groups.members[g]) { + if (is_group_output(a, groups, parents_map, output_map)) { + return true; + } } + return false; + }; - // Not worth fusing a single primitive - if (cache.size() <= 1) { - new_tape.push_back(arr); + // Try the pairs with the most shared bytes first, then the closest pairs. + // Keep one array of each group, since groups change when they merge. + struct Candidate { + size_t score; + int distance; + array a; + array b; + }; + std::vector candidates; + for (auto& [key, score] : scores) { + auto [g1, g2] = key; + candidates.push_back( + {score, + distance(g1, g2), + groups.members[g1][0], + groups.members[g2][0]}); + } + std::sort(candidates.begin(), candidates.end(), [](auto& x, auto& y) { + return (x.score != y.score) ? x.score > y.score : x.distance < y.distance; + }); + + // Merging groups at different levels moves a kernel in the eval order, and + // this can keep large arrays longer in memory + bool changed = false; + for (auto& c : candidates) { + int g1 = groups.find(c.a); + int g2 = groups.find(c.b); + if (g1 == g2 || c.a.primitive().stream() != c.b.primitive().stream() || + distance(g1, g2) > max_fuse_distance || !is_used(g1) || !is_used(g2) || + is_connected(g1, g2, groups, position, parents_map)) { continue; } + int size = groups.members[g1].size(); + groups.merge(g1, g2); + if (is_valid_group(g1, groups, parents_map, output_map)) { + changed = true; + } else { + groups.unmerge(g1, g2, size); + } + } + return changed; +} - // Recurse a second time to build the tape in the right - // order and collect the inputs - input_set.clear(); +// Fuse arrays in groups with the fusion rules, then replace each group with +// one Compiled primitive. +void compile_fuse( + std::vector& tape, + ParentsMap& parents_map, + const std::vector& inputs, + std::vector& outputs) { + // Track outputs to replace with new compiled outputs + std::unordered_map output_map; + for (auto& o : outputs) { + output_map.insert({o.id(), o}); + } + + // Set of inputs to distinguish constants + std::unordered_set input_ids; + for (auto& in : inputs) { + input_ids.insert(in.id()); + } + + // Start with one group for each fusable array + FuseGroups groups; + std::unordered_map position; + for (int i = 0; i < tape.size(); ++i) { + auto& a = tape[i]; + position.insert({a.id(), i}); + if (a.has_primitive() && is_fusable(a.primitive()) && + input_ids.find(a.id()) == input_ids.end()) { + groups.make_group(a); + } + } + + // Merge groups until no rule changes them. A merge can make a new merge + // possible, so repeat the rules. + while (compile_fuse_vertical(groups, tape, parents_map, output_map) || + compile_fuse_horizontal( + groups, tape, position, parents_map, output_map)) { + } + + // Replace each group of more than one array with a Compiled primitive + for (int g = 0; g < groups.members.size(); ++g) { + auto& group = groups.members[g]; + if (group.size() <= 1) { + continue; + } + + // The roots are the arrays which are not read in the group + std::vector roots; + for (auto& a : group) { + bool is_root = true; + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + if (groups.find(p) == g) { + is_root = false; + break; + } + } + } + if (is_root) { + roots.push_back(a); + } + } + std::sort(roots.begin(), roots.end(), [&](auto& a, auto& b) { + return position.at(a.id()) < position.at(b.id()); + }); + + // Build the tape of the group in the right order and collect the inputs std::vector inputs; std::vector fused_tape; + std::unordered_set input_set; std::unordered_set tape_set; std::function recurse_tape; recurse_tape = [&](const array& a) { - if (cache.find(a.id()) == cache.end()) { + if (groups.find(a) != g) { if (input_set.find(a.id()) == input_set.end()) { input_set.insert(a.id()); inputs.push_back(a); @@ -1271,98 +1712,55 @@ void compile_fuse( } fused_tape.push_back(a); }; - recurse_tape(arr); + for (auto& r : roots) { + recurse_tape(r); + } std::vector old_outputs; - // Add to global cache and add any global outputs to outputs - // of new primitive - for (int j = 0; j < fused_tape.size() - 1; ++j) { - auto& f = fused_tape[j]; - if (output_map.find(f.id()) != output_map.end()) { + for (auto& f : fused_tape) { + if (is_group_output(f, groups, parents_map, output_map)) { old_outputs.push_back(f); - // Parents are now siblings, update the parent map - auto& pairs = parents_map[f.id()]; - pairs.erase( - std::remove_if( - pairs.begin(), - pairs.end(), - [&](auto& p) { - return cache.find(p.first.id()) != cache.end(); - }), - pairs.end()); - } else { - // Remove inner fused arrays parents from the parents map - // to keep the parents map in a valid state - parents_map.erase(f.id()); } - global_cache.insert({f.id()}); } - old_outputs.push_back(arr); - std::vector shapes; - std::vector types; - for (auto& o : old_outputs) { - if (o.shape() != old_outputs.back().shape()) { - throw std::runtime_error( - "[compile] Compilation failed. Tried to fuse operations with different output shapes"); - } - shapes.push_back(o.shape()); - types.push_back(o.dtype()); - } - std::unordered_set constant_ids; - for (auto& in : inputs) { - // Scalar constant - if (in.size() == 1 && !in.has_primitive() && - input_ids.find(in.id()) == input_ids.end()) { - constant_ids.insert(in.id()); - } - } - auto compiled_outputs = array::make_arrays( - std::move(shapes), - types, - std::make_shared( - old_outputs.back().primitive().stream(), - inputs, - old_outputs, - std::move(fused_tape), - std::move(constant_ids)), - inputs); - - // One output per primitive - new_tape.push_back(compiled_outputs.back()); - - // Replace inputs old parents with compiled_outputs - for (int i = 0; i < inputs.size(); ++i) { - auto& pairs = parents_map[inputs[i].id()]; - pairs.erase( - std::remove_if( - pairs.begin(), - pairs.end(), - [&](auto& p) { return cache.find(p.first.id()) != cache.end(); }), - pairs.end()); - for (auto& o : compiled_outputs) { - pairs.push_back({o, i}); - } + // Nothing reads the group, so it is not used + if (old_outputs.empty()) { + continue; } + make_compiled( + std::move(fused_tape), + inputs, + std::move(old_outputs), + input_ids, + output_map, + parents_map); + } - // - Update outputs parents to point to compiled outputs - // - Update any overall graph outputs to be compiled outputs - for (int o = 0; o < old_outputs.size(); ++o) { - merge_one(compiled_outputs[o], old_outputs[o], parents_map); - if (auto it = output_map.find(old_outputs[o].id()); - it != output_map.end()) { - it->second = compiled_outputs[o]; + // Build the new tape in execution order + std::vector new_tape; + std::unordered_set cache; + std::function recurse; + recurse = [&](const array& a) { + if (cache.find(a.id()) != cache.end()) { + return; + } + // Don't recurse on inputs + if (input_ids.find(a.id()) == input_ids.end()) { + for (auto& in : a.inputs()) { + recurse(in); } } - } - - std::reverse(new_tape.begin(), new_tape.end()); - tape = std::move(new_tape); - - // Replace output with potentially compiled output + cache.insert(a.id()); + for (auto& s : a.siblings()) { + cache.insert(s.id()); + } + new_tape.push_back(a); + }; for (auto& o : outputs) { o = output_map.at(o.id()); + recurse(o); } + tape = std::move(new_tape); } std::vector compile_replace( From 5aaed7b097ada270d9e1c743c98689ae8af9a8e4 Mon Sep 17 00:00:00 2001 From: Anastasiia Filippova Date: Tue, 17 Nov 2026 15:39:41 +0100 Subject: [PATCH 3/8] split compiler --- mlx/CMakeLists.txt | 2 +- mlx/compile.cpp | 2007 ------------------------------------ mlx/compile/CMakeLists.txt | 7 + mlx/compile/compile.cpp | 473 +++++++++ mlx/compile/fuse.cpp | 726 +++++++++++++ mlx/compile/recompute.cpp | 349 +++++++ mlx/compile/simplify.cpp | 280 +++++ mlx/compile/trace.cpp | 233 +++++ mlx/compile_impl.h | 28 + 9 files changed, 2097 insertions(+), 2008 deletions(-) delete mode 100644 mlx/compile.cpp create mode 100644 mlx/compile/CMakeLists.txt create mode 100644 mlx/compile/compile.cpp create mode 100644 mlx/compile/fuse.cpp create mode 100644 mlx/compile/recompute.cpp create mode 100644 mlx/compile/simplify.cpp create mode 100644 mlx/compile/trace.cpp diff --git a/mlx/CMakeLists.txt b/mlx/CMakeLists.txt index f1b1a7b67e..fb5bb9ccd4 100644 --- a/mlx/CMakeLists.txt +++ b/mlx/CMakeLists.txt @@ -2,7 +2,6 @@ target_sources( mlx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/allocator.cpp ${CMAKE_CURRENT_SOURCE_DIR}/array.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/compile.cpp ${CMAKE_CURRENT_SOURCE_DIR}/config.cpp ${CMAKE_CURRENT_SOURCE_DIR}/device.cpp ${CMAKE_CURRENT_SOURCE_DIR}/dtype.cpp @@ -95,6 +94,7 @@ else() endif() add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/distributed) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/compile) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/io) if(MLX_BUILD_METAL) diff --git a/mlx/compile.cpp b/mlx/compile.cpp deleted file mode 100644 index 4b2deeba46..0000000000 --- a/mlx/compile.cpp +++ /dev/null @@ -1,2007 +0,0 @@ -// Copyright © 2023-2024 Apple Inc. - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "mlx/allocator.h" -#include "mlx/backend/common/compiled.h" -#include "mlx/compile.h" -#include "mlx/compile_impl.h" -#include "mlx/fast_primitives.h" -#include "mlx/graph_utils.h" -#include "mlx/primitives.h" -#include "mlx/transforms.h" -#include "mlx/transforms_impl.h" -#include "mlx/utils.h" - -namespace mlx::core { - -using ArrayFunction = - std::function(const std::vector&)>; - -constexpr int max_compile_depth = 11; -constexpr int max_compile_arrays = 24; -constexpr int max_fuse_distance = 0; - -bool is_unary(const Primitive& p) { - return ( - typeid(p) == typeid(Abs) || typeid(p) == typeid(ArcCos) || - typeid(p) == typeid(ArcCosh) || typeid(p) == typeid(ArcSin) || - typeid(p) == typeid(ArcSinh) || typeid(p) == typeid(ArcTan) || - typeid(p) == typeid(ArcTanh) || typeid(p) == typeid(AsType) || - typeid(p) == typeid(Ceil) || typeid(p) == typeid(Cos) || - typeid(p) == typeid(Conjugate) || typeid(p) == typeid(Cosh) || - typeid(p) == typeid(Remainder) || typeid(p) == typeid(Erf) || - typeid(p) == typeid(ErfInv) || typeid(p) == typeid(Exp) || - typeid(p) == typeid(Floor) || typeid(p) == typeid(Log) || - typeid(p) == typeid(Log1p) || typeid(p) == typeid(LogicalNot) || - typeid(p) == typeid(Negative) || typeid(p) == typeid(Round) || - typeid(p) == typeid(Sigmoid) || typeid(p) == typeid(Sign) || - typeid(p) == typeid(Sin) || typeid(p) == typeid(Sinh) || - typeid(p) == typeid(Square) || typeid(p) == typeid(Sqrt) || - typeid(p) == typeid(Tan) || typeid(p) == typeid(Tanh) || - typeid(p) == typeid(Expm1) || typeid(p) == typeid(Real) || - typeid(p) == typeid(Imag) || typeid(p) == typeid(BitwiseInvert)); -} - -bool is_binary(const Primitive& p) { - return typeid(p) == typeid(Add) || typeid(p) == typeid(ArcTan2) || - typeid(p) == typeid(BitwiseBinary) || typeid(p) == typeid(Divide) || - typeid(p) == typeid(Equal) || typeid(p) == typeid(FloorDivide) || - typeid(p) == typeid(Greater) || typeid(p) == typeid(GreaterEqual) || - typeid(p) == typeid(Less) || typeid(p) == typeid(LessEqual) || - typeid(p) == typeid(LogicalNot) || typeid(p) == typeid(LogicalAnd) || - typeid(p) == typeid(LogicalOr) || typeid(p) == typeid(LogAddExp) || - typeid(p) == typeid(Maximum) || typeid(p) == typeid(Minimum) || - typeid(p) == typeid(Multiply) || typeid(p) == typeid(NotEqual) || - typeid(p) == typeid(Power) || typeid(p) == typeid(Subtract); -} - -bool is_ternary(const Primitive& p) { - return typeid(p) == typeid(Select); -} - -bool is_broadcast(const Primitive& p) { - return typeid(p) == typeid(Broadcast); -} - -bool is_noop(const Primitive& p) { - return typeid(p) == typeid(Copy) || typeid(p) == typeid(StopGradient); -} - -bool is_reduction(const Primitive& p) { - return typeid(p) == typeid(Reduce) || typeid(p) == typeid(ArgReduce); -} - -bool is_fusable(const Primitive& p) { - return is_unary(p) || is_binary(p) || is_ternary(p) || is_broadcast(p); -} - -Compiled::Compiled( - Stream stream, - std::vector inputs, - std::vector outputs, - std::vector tape, - std::unordered_set constant_ids) - : Primitive(stream), - inputs_(std::move(inputs)), - outputs_(std::move(outputs)), - tape_(std::move(tape)), - constant_ids_(std::move(constant_ids)), - is_constant_([this](size_t i) { - return constant_ids_.find(inputs_[i].id()) != constant_ids_.end(); - }) { - // Build the kernel name. - NodeNamer namer; - std::ostringstream os; - std::ostringstream constant_hasher; - - std::unordered_set output_ids; - for (auto& o : outputs_) { - output_ids.insert(o.id()); - } - - // Fill the input names. This is not really necessary, I just like having A, - // B, C, ... as the inputs. - for (const auto& x : inputs_) { - namer.get_name(x); - } - - // The primitives describing the tape. For unary and binary primitives this - // must be enough to describe the full computation. - for (const auto& a : tape_) { - // name and type of output - os << namer.get_name(a) << kindof(a.dtype()) << a.itemsize(); - // whether or not it's an output - if (output_ids.find(a.id()) != output_ids.end()) { - os << "O"; - } else { - os << "I"; - } - // computation performed - os << a.primitive().name(); - // name of inputs to the function - for (auto& inp : a.inputs()) { - os << namer.get_name(inp); - } - } - os << "_"; - - for (const auto& x : inputs_) { - if (constant_ids_.find(x.id()) != constant_ids_.end()) { - os << "C"; - print_constant(constant_hasher, x); - } else { - os << (is_scalar(x) ? "S" : "V"); - } - } - os << "_"; - // Iterate the moved-into members; the parameters are moved-from above. - for (const auto& x : inputs_) { - if (constant_ids_.find(x.id()) != constant_ids_.end()) { - continue; - } - os << kindof(x.dtype()) << x.itemsize(); - } - os << "_" << std::hash{}(constant_hasher.str()); - - kernel_lib_ = os.str(); -} - -std::vector Compiled::vjp( - const std::vector&, - const std::vector&, - const std::vector&, - const std::vector&) { - throw std::runtime_error("[Compiled] Cannot vjp primitive."); -} - -std::vector Compiled::jvp( - const std::vector&, - const std::vector&, - const std::vector&) { - throw std::runtime_error("[Compiled] Cannot jvp primitive."); -} - -std::pair, std::vector> Compiled::vmap( - const std::vector&, - const std::vector&) { - throw std::runtime_error("[Compiled] Cannot vmap primitive."); -} - -bool Compiled::is_equivalent(const Primitive& other) const { - const Compiled& a_other = static_cast(other); - return std::equal( - tape_.begin(), - tape_.end(), - a_other.tape_.begin(), - a_other.tape_.end(), - [](const array& a1, const array& a2) { - auto& p1 = a1.primitive(); - auto& p2 = a2.primitive(); - return typeid(p1) == typeid(p2) && p1.is_equivalent(p2); - }); -} - -const char* Compiled::name() const { - if (name_.empty()) { - std::ostringstream os; - os << "Compiled"; - for (auto& a : tape_) { - os << a.primitive().name(); - } - name_ = os.str(); - } - return name_.c_str(); -} - -std::vector Compiled::output_shapes(const std::vector& inputs) { - size_t nd = 0; - for (auto& in : inputs) { - nd = std::max(nd, in.ndim()); - } - Shape out_shape(nd, 0); - for (auto& in : inputs) { - auto dd = nd - in.ndim(); - for (auto i = dd; i < nd; ++i) { - out_shape[i] = std::max(out_shape[i], in.shape()[i - dd]); - } - } - // All outputs have the same shape - return std::vector(outputs_.size(), out_shape); -} - -namespace detail { - -std::atomic& compile_mode() { - auto get_val = []() { - if (std::getenv("MLX_DISABLE_COMPILE")) { - return CompileMode::disabled; - } else { - return CompileMode::enabled; - } - }; - static std::atomic compile_mode_ = get_val(); - return compile_mode_; -} - -// Helper like below but only merges the two provided arrays. If the src has -// siblings then these won't be merged to the dst. -void merge_one(array& dst, array& src, ParentsMap& parents_map) { - auto src_parents = parents_map.find(src.id()); - if (src_parents == parents_map.end()) { - return; - } - auto& pairs = parents_map[dst.id()]; - for (auto& parent : src_parents->second) { - parent.first.inputs()[parent.second] = dst; - pairs.push_back(parent); - } - - // If src is a parent of dst, remove it from dst's parents - for (auto it = pairs.begin(); it != pairs.end();) { - if (it->first.id() == src.id()) { - it = pairs.erase(it); - } else { - it++; - } - } - // Remove the source from the map to avoid fusing with it again - parents_map.erase(src_parents); -} - -// Helper that merges two arrays in the graph by setting the parents of the -// source to point to the destination. The arrays are assumed to be coming from -// equivalent primitives so their siblings are merged as well. -void merge(array& dst, array& src, ParentsMap& parents_map) { - // Canonicalize the order of the primitives outputs - auto sources = src.outputs(); - auto dests = dst.outputs(); - // For each src parent, point it to the corresponding dst - for (int i = 0; i < sources.size(); ++i) { - merge_one(dests[i], sources[i], parents_map); - } -} - -// Any parent in the divider will continue to refer to `x` but any parent not -// in the divider will refer to a copy of the operation. -array split_one( - const array& x, - ParentsMap& parents_map, - const std::unordered_set& divider) { - array y(x.shape(), x.dtype(), x.primitive_ptr(), x.inputs()); - - auto& x_parents = parents_map[x.id()]; - auto& y_parents = parents_map[y.id()]; - - for (auto it = x_parents.begin(); it != x_parents.end();) { - if (divider.find(it->first.id()) != divider.end()) { - it->first.inputs()[it->second] = y; - y_parents.emplace_back(std::move(*it)); - it = x_parents.erase(it); - } else { - it++; - } - } - - return y; -} - -template -std::uintptr_t get_function_address(const std::function& fun) { - using FunType = T (*)(U...); - const FunType* fun_ptr = fun.template target(); - if (fun_ptr == nullptr) { - return 0; - } - return reinterpret_cast(*fun_ptr); -} - -class CompileCache { - public: - struct CacheEntry { - CacheEntry(std::optional stream, bool shapeless) - : stream(stream), shapeless(shapeless) {} - std::optional stream; - bool shapeless; - std::vector inputs; - std::vector outputs; - std::vector tape; - bool empty{true}; - std::vector constants; - std::shared_ptr extra; - }; - - CompileCache() { - // Make sure the allocator is fully initialized before the compiler cache. - allocator::allocator(); - } - - // Returns a reference to a CacheEntry which can be updated by the caller to - // avoid copying large tapes / inputs / outputs, with the shared_ptr of - // entries to avoid getting erased during compilation. - std::tuple>> find( - std::uintptr_t fun_id, - const std::vector& inputs, - bool shapeless, - const std::vector& constants) { - // Find the cache entries for |fun_id| in a thread-safe way. - auto entries_ptr = [&]() { - // Lookup with shared lock. - { - std::shared_lock lock(mutex_); - auto it = cache_.find(fun_id); - if (it != cache_.end()) { - return it->second; - } - } - // Insertion with exclusive lock. - std::unique_lock lock(mutex_); - auto& ptr = cache_[fun_id]; - if (!ptr) { - ptr = std::make_shared>(); - } - return ptr; - }(); - auto& entries = *entries_ptr; - - // Compare if 2 arrays have same shape and dtype. - auto has_same_shape_and_dtype = [shapeless]( - const std::vector& in1, - const std::vector& in2) { - if (in1.size() != in2.size()) { - return false; - } - for (size_t i = 0; i < in1.size(); ++i) { - if (in1[i].ndim() != in2[i].ndim()) { - return false; - } - if (!shapeless && in1[i].shape() != in2[i].shape()) { - return false; - } - if (in1[i].dtype() != in2[i].dtype()) { - return false; - } - } - return true; - }; - // Loop over entries and check: - // - Default stream and device match the entry's default stream - // - Inputs match i.e. shapes and types must be equal. - auto stream = peek_default_stream(default_device()); - for (CacheEntry& entry : entries) { - // Check that the default stream and device match - if (entry.stream != stream) { - continue; - } - if (entry.shapeless != shapeless) { - continue; - } - - // Check the inputs match and return if so - if (has_same_shape_and_dtype(inputs, entry.inputs) && - constants == entry.constants) { - return {entry, std::move(entries_ptr)}; - } - } - // Otherwise append a new cache entry - entries.push_back(CacheEntry{stream, shapeless}); - return {entries.back(), std::move(entries_ptr)}; - } - - void erase(std::uintptr_t fun_id) { - std::unique_lock lock(mutex_); - cache_.erase(fun_id); - } - - void clear() { - std::unique_lock lock(mutex_); - cache_.clear(); - } - - private: - // The cache may get its key erased from a separate thread, but its value is - // only added and modified in the thread of creation. - // Put value in a shared_ptr to avoid race condition when erasing happened - // during compilation for the same function. - std::unordered_map>> - cache_; - std::shared_mutex mutex_; -}; - -std::shared_ptr& compile_cache_unsafe() { - static thread_local auto cache = std::make_shared(); - return cache; -} - -std::tuple, std::vector, std::shared_ptr> -compile_trace( - const ArrayFnWithExtra& fun, - const std::vector& inputs, - bool shapeless) { - // Set the global tracing flag. - detail::InTracing in_tracing{shapeless}; - - // Run the function on placeholder inputs - // to get compute graph - std::vector tracer_inputs; - for (int i = 0; i < inputs.size(); ++i) { - array in(inputs[i].shape(), inputs[i].dtype(), nullptr, {}); - in.set_tracer(true); - tracer_inputs.push_back(std::move(in)); - } - - auto output = fun(tracer_inputs); - return {tracer_inputs, output.first, output.second}; -} - -// Traverses the graph to build a tape and a map of array ids to their parents -std::pair, ParentsMap> compile_dfs( - const std::vector& inputs, - std::vector& outputs, - const std::vector& original_inputs) { - std::vector tape; - std::unordered_map>> - parents_map; - { - std::function recurse; - std::unordered_set input_set; - std::unordered_set original_input_set; - for (int i = 0; i < inputs.size(); ++i) { - input_set.insert(inputs[i].id()); - original_input_set.insert(original_inputs[i].id()); - } - - // DFS the graph to build the tape, and log parents and scalars - std::unordered_set cache; - recurse = [&](const array& a) { - auto id = a.id(); - if (original_input_set.find(id) != original_input_set.end()) { - throw std::invalid_argument( - "[compile] Attempting to compile a function with uncaptured inputs is not allowed."); - } - if (cache.find(id) != cache.end()) { - return; - } - for (int i = 0; i < a.inputs().size(); i++) { - auto& in = a.inputs()[i]; - parents_map[in.id()].push_back({a, i}); - for (auto& s : a.siblings()) { - parents_map[in.id()].push_back({s, i}); - } - // Don't recurse on inputs (but add them to the tape for the purpose - // of future optimizations) - if (input_set.find(a.id()) == input_set.end()) { - recurse(in); - } - } - cache.insert(id); - for (auto& s : a.siblings()) { - cache.insert(s.id()); - } - tape.push_back(a); - }; - for (auto& a : outputs) { - recurse(a); - } - } - - // Deep copy the tape and parents map while preserving inputs and outputs - std::vector new_tape; - std::unordered_set io_set; - std::unordered_map old_to_new; - for (auto& o : outputs) { - old_to_new.insert({o.id(), o}); - io_set.insert(o.id()); - for (auto& s : o.siblings()) { - old_to_new.insert({s.id(), s}); - io_set.insert(s.id()); - } - } - for (auto& i : inputs) { - io_set.insert(i.id()); - old_to_new.insert({i.id(), i}); - } - - new_tape.reserve(tape.size()); - for (auto& arr : tape) { - if (!arr.has_primitive() || (io_set.find(arr.id()) != io_set.end())) { - old_to_new.insert({arr.id(), arr}); - new_tape.push_back(arr); - continue; - } - std::vector inputs; - inputs.reserve(arr.inputs().size()); - for (auto& i : arr.inputs()) { - inputs.push_back(old_to_new.find(i.id())->second); - } - if (arr.siblings().size() > 0) { - std::vector types; - std::vector shapes; - auto out = arr.outputs(); - for (auto& o : out) { - types.push_back(o.dtype()); - shapes.push_back(o.shape()); - } - auto as = array::make_arrays( - std::move(shapes), types, arr.primitive_ptr(), std::move(inputs)); - for (int i = 0; i < out.size(); ++i) { - as[i].set_backward(out[i].is_backward()); - old_to_new.insert({out[i].id(), as[i]}); - } - new_tape.push_back(as[arr.sibling_position()]); - } else { - auto a = array( - arr.shape(), arr.dtype(), arr.primitive_ptr(), std::move(inputs)); - a.set_backward(arr.is_backward()); - old_to_new.insert({arr.id(), a}); - new_tape.push_back(a); - } - } - io_set.clear(); - for (auto& o : outputs) { - if (!(io_set.insert(o.id()).second)) { - continue; - } - for (auto& i : o.inputs()) { - i = old_to_new.find(i.id())->second; - } - for (auto& s : o.siblings()) { - io_set.insert(s.id()); - for (auto& i : s.inputs()) { - i = old_to_new.find(i.id())->second; - } - } - } - tape = std::move(new_tape); - - std::unordered_map>> - new_parents_map; - for (auto& [id, vec] : parents_map) { - for (auto& [a, _] : vec) { - a = old_to_new.find(a.id())->second; - } - new_parents_map[old_to_new.find(id)->second.id()] = std::move(vec); - } - parents_map = std::move(new_parents_map); - return {tape, parents_map}; -} - -static inline uint64_t splitmix64(uint64_t x) noexcept { - x += 0x9e3779b97f4a7c15ull; - x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ull; - x = (x ^ (x >> 27)) * 0x94d049bb133111ebull; - return x ^ (x >> 31); -} - -struct VecU64Hash { - size_t operator()(const std::vector& s) const noexcept { - uint64_t h = - 0x243f6a8885a308d3ull ^ (uint64_t)s.size() * 0x9e3779b97f4a7c15ull; - for (uint64_t x : s) { - h = splitmix64(x ^ splitmix64(h + 0x9e3779b97f4a7c15ull)); - } - return (size_t)h; - } -}; - -// Simplify the tape. Note, this function modifies in-place both the tape, -// the parents map to remove orphaned arrays, and potentially the outputs -void compile_simplify( - std::vector& tape, - ParentsMap& parents_map, - std::vector& outputs, - int passes) { - // Helpers to identify identical scalars - std::map, array> scalars; - auto is_scalar = [](const array& a) { - // Condition for when it's safe to read an array - return a.is_available() && a.ndim() == 0; - }; - auto get_scalar_rep = [](const array& a) { - uint64_t v = 0; - switch (a.dtype().size()) { - case 1: - v = *a.data(); - break; - case 2: - v = *a.data(); - break; - case 4: - v = *a.data(); - break; - case 8: - v = *a.data(); - break; - } - return std::make_pair(v, a.dtype().val()); - }; - - for (auto& a : tape) { - if (is_scalar(a)) { - scalars.insert({get_scalar_rep(a), a}); - } - } - - // Depth-1 array equivalence check. - auto array_equivalent = [](const array& a, const array& b) { - if (!a.has_primitive() || !b.has_primitive()) { - return false; - } - if (a.primitive_id() == b.primitive_id()) { - return false; - } - const auto& pa = a.primitive(); - const auto& pb = b.primitive(); - if (typeid(pa) != typeid(pb)) { - return false; - } - - if (a.inputs().size() != b.inputs().size()) { - return false; - } - - for (int i = 0; i < a.inputs().size(); i++) { - if (a.inputs()[i].id() != b.inputs()[i].id()) { - return false; - } - } - - return pa.is_equivalent(pb); - }; - - std::unordered_map output_map; - for (auto& o : outputs) { - output_map.insert({o.id(), o}); - } - - // Merge scalars, except outputs - std::vector new_tape; - for (auto& arr : tape) { - // Check if we can merge scalars - if (is_scalar(arr) && output_map.find(arr.id()) == output_map.end()) { - auto scalar = scalars.find(get_scalar_rep(arr)); - if (scalar->second.id() != arr.id()) { - merge(scalar->second, arr, parents_map); - // Don't keep orphaned scalars in the tape - continue; - } - } - new_tape.push_back(std::move(arr)); - } - tape = std::move(new_tape); - - // Remove no-ops - { - for (auto& arr : tape) { - if (!arr.has_primitive() || !is_noop(arr.primitive())) { - new_tape.push_back(std::move(arr)); - continue; - } - merge_one(arr.inputs()[0], arr, parents_map); - if (auto it = output_map.find(arr.id()); it != output_map.end()) { - it->second = arr.inputs()[0]; - } - } - tape = std::move(new_tape); - for (auto& o : outputs) { - o = output_map.at(o.id()); - } - } - - std::unordered_map tape_order; - for (uint32_t i = 0; i < tape.size(); ++i) { - tape_order.insert({tape[i].id(), i}); - } - - // Multi-pass merge only keeping non-orphaned arrays in the tape - for (int pass = 0; pass < passes; ++pass) { - for (auto& arr : tape) { - // Helper to check if we can merge the parents of the - // given array - auto maybe_merge_parents = [&](auto& a) { - auto parents = parents_map.find(a.id()); - if (parents != parents_map.end()) { - auto N = parents->second.size(); - std::vector mask(N, false); - - auto try_merge = [&](int dst_idx, int src_idx) { - if (tape_order[parents->second[src_idx].first.id()] < - tape_order[parents->second[dst_idx].first.id()]) { - std::swap(src_idx, dst_idx); - } - auto& src = parents->second[src_idx].first; - auto& dst = parents->second[dst_idx].first; - if (src.id() != dst.id() && array_equivalent(src, dst) && - output_map.find(src.id()) == output_map.end()) { - merge(dst, src, parents_map); - mask[src_idx] = true; - } - }; - - if (N > 100) { - std::unordered_map< - std::vector, - std::vector, - VecU64Hash> - dst_map; - // Find possibly mergeable groups - for (int i = 0; i < N; i++) { - // Make the hash key - std::vector key; - auto& curr = parents->second[i].first; - key.reserve(curr.inputs().size() + 2); - for (auto& in : curr.inputs()) { - key.push_back(in.id()); - } - auto& p = curr.primitive(); - key.push_back(curr.inputs().size()); - key.push_back(typeid(p).hash_code()); - auto it = dst_map.find(key); - if (it == dst_map.end()) { - bool _; - std::tie(it, _) = dst_map.insert({key, std::vector{}}); - } - it->second.push_back(i); - } - for (auto& [_, group] : dst_map) { - for (int i = 0; i < group.size(); ++i) { - if (mask[group[i]]) { - continue; - } - for (int j = i + 1; j < group.size(); ++j) { - if (mask[group[j]]) { - continue; - } - try_merge(group[i], group[j]); - } - } - } - } else { - for (int i = 0; i < N; ++i) { - if (mask[i]) { - continue; - } - for (int j = i + 1; j < N; ++j) { - if (mask[j]) { - continue; - } - try_merge(i, j); - } - } - } - - // Erase orphaned parents so we don't keep fusing with them - for (int i = N - 1; i >= 0; --i) { - if (mask[i]) { - parents->second.erase(parents->second.begin() + i); - } - } - return false; - } else { - return output_map.find(a.id()) == output_map.end(); - } - }; - bool discard = maybe_merge_parents(arr); - for (auto& s : arr.siblings()) { - discard &= maybe_merge_parents(s); - } - // If an array and its siblings have no parents, and none of them are - // outputs, it is safe to remove it from the tape - if (!discard) { - new_tape.push_back(std::move(arr)); - } - } - tape = std::move(new_tape); - } -} - -// Max-flow solver (Dinic) for compile_recompute. -class MaxFlow { - public: - static constexpr int64_t inf = std::numeric_limits::max() / 4; - - explicit MaxFlow(int n) : adj_(n), level_(n), next_(n) {} - - void add_edge(int u, int v, int64_t cap) { - adj_[u].push_back(edges_.size()); - edges_.push_back({v, cap}); - adj_[v].push_back(edges_.size()); - edges_.push_back({u, 0}); - } - - // Returns the max flow, or inf if there is no finite cut. - int64_t solve(int s, int t) { - int64_t flow = 0; - while (make_levels(s, t)) { - std::fill(next_.begin(), next_.end(), 0); - while (int64_t f = augment(s, t)) { - flow += f; - if (flow >= inf) { - return inf; - } - } - } - return flow; - } - - // Vertices that s still reaches in the residual graph. - std::vector source_side(int s) const { - std::vector seen(adj_.size(), false); - std::vector stack = {s}; - seen[s] = true; - while (!stack.empty()) { - int u = stack.back(); - stack.pop_back(); - for (int e : adj_[u]) { - int v = edges_[e].to; - if (edges_[e].cap > 0 && !seen[v]) { - seen[v] = true; - stack.push_back(v); - } - } - } - return seen; - } - - private: - struct Edge { - int to; - int64_t cap; - }; - - bool make_levels(int s, int t) { - std::fill(level_.begin(), level_.end(), -1); - std::queue queue; - level_[s] = 0; - queue.push(s); - while (!queue.empty()) { - int u = queue.front(); - queue.pop(); - for (int e : adj_[u]) { - int v = edges_[e].to; - if (edges_[e].cap > 0 && level_[v] < 0) { - level_[v] = level_[u] + 1; - queue.push(v); - } - } - } - return level_[t] >= 0; - } - - // Push flow along one path of the level graph. A stack is used instead of - // recursion because compiled graphs can be deep. - int64_t augment(int s, int t) { - std::vector path; - int u = s; - while (u != t) { - bool moved = false; - for (; next_[u] < adj_[u].size(); ++next_[u]) { - int e = adj_[u][next_[u]]; - int v = edges_[e].to; - if (edges_[e].cap > 0 && level_[v] == level_[u] + 1) { - path.push_back(e); - u = v; - moved = true; - break; - } - } - if (!moved) { - if (path.empty()) { - return 0; - } - // Dead end: remove u from the level graph and step back. - level_[u] = -1; - u = edges_[path.back() ^ 1].to; - path.pop_back(); - } - } - int64_t f = inf; - for (int e : path) { - f = std::min(f, edges_[e].cap); - } - for (int e : path) { - edges_[e].cap -= f; - edges_[e ^ 1].cap += f; - } - return f; - } - - std::vector edges_; - std::vector> adj_; - std::vector level_; - std::vector next_; -}; - -void compile_recompute(std::vector& tape, ParentsMap& parents_map) { - int n = tape.size(); - std::unordered_map vid; - for (int i = 0; i < n; ++i) { - vid.insert({tape[i].id(), i}); - for (auto& s : tape[i].siblings()) { - vid.insert({s.id(), i}); - } - } - - // Backward nodes are made by autodiff, or computed from such nodes, like an - // optimizer update. - std::vector backward(n, false); - bool has_backward = false; - for (int i = 0; i < n; ++i) { - auto& a = tape[i]; - if (!a.has_primitive()) { - continue; - } - bool b = a.is_backward(); - for (auto& in : a.inputs()) { - b = b || backward[vid.at(in.id())]; - } - backward[i] = b; - has_backward |= b; - } - if (!has_backward) { - return; - } - - auto readers = [&](int i) { - std::vector rs; - for (auto& o : tape[i].outputs()) { - if (auto it = parents_map.find(o.id()); it != parents_map.end()) { - for (auto& [p, _] : it->second) { - if (auto r = vid.find(p.id()); r != vid.end()) { - rs.push_back(r->second); - } - } - } - } - return rs; - }; - auto fusable = [&](int i) { - return tape[i].has_primitive() && is_fusable(tape[i].primitive()); - }; - auto is_view = [&](int i) { - if (!tape[i].has_primitive()) { - return false; - } - auto& p = tape[i].primitive(); - return typeid(p) == typeid(Broadcast) || typeid(p) == typeid(Reshape) || - typeid(p) == typeid(Transpose) || typeid(p) == typeid(ExpandDims) || - typeid(p) == typeid(Squeeze); - }; - - // True if the backward reads node i, directly or through views - std::function backward_reads; - backward_reads = [&](int i) -> bool { - for (int r : readers(i)) { - if (backward[r] || (is_view(r) && backward_reads(r))) { - return true; - } - } - return false; - }; - - // True if the backward reads node i only with fusable ops and not through a - // view. After a transpose or a reshape the copy does not fuse, for example a - // weight cast read by a matmul. After a broadcast the copy runs once for each - // element, for example a scalar learning rate in each optimizer update. - auto fuses_into_backward = [&](int i) { - for (int r : readers(i)) { - if (is_view(r) ? (backward[r] || backward_reads(r)) - : (backward[r] && !fusable(r))) { - return false; - } - } - return true; - }; - - MaxFlow graph(2 * n + 2); - int source = 2 * n; - int sink = 2 * n + 1; - for (int i = 0; i < n; ++i) { - auto& a = tape[i]; - int in = 2 * i; - int out = 2 * i + 1; - if (backward[i]) { - graph.add_edge(in, sink, MaxFlow::inf); - continue; - } - auto rs = readers(i); - for (int r : rs) { - graph.add_edge(out, 2 * r, MaxFlow::inf); - } - if (!a.has_primitive()) { - // Inputs and constants stay alive anyway, so keeping them is free. - graph.add_edge(source, in, MaxFlow::inf); - graph.add_edge(in, out, 0); - continue; - } - if (is_view(i)) { - // Views are free to recompute, so they are never kept. - graph.add_edge(in, out, MaxFlow::inf); - continue; - } - - // Keeping a value costs its size. The cost doubles when a fused kernel - // would otherwise keep the value in registers. - bool in_registers = - fusable(i) && !rs.empty() && std::all_of(rs.begin(), rs.end(), fusable); - int64_t bytes = 0; - for (auto& o : a.outputs()) { - bytes += o.nbytes(); - } - graph.add_edge(in, out, in_registers ? 2 * bytes : bytes); - - // Only cheap elementwise ops are recomputed, and only when the backward - // can fuse them. Other ops must run in the forward. - bool recompute = - fusable(i) && a.siblings().empty() && fuses_into_backward(i); - if (!recompute) { - graph.add_edge(source, in, MaxFlow::inf); - } - } - - auto kept_bytes = graph.solve(source, sink); - if (kept_bytes >= MaxFlow::inf) { - return; - } - auto source_side = graph.source_side(source); - - // The value the backward reads for `x`: x itself if it is kept, otherwise a - // copy computed from kept values. - std::unordered_map copies; - std::vector new_nodes; - std::function backward_value; - backward_value = [&](const array& x) -> array { - int j = vid.at(x.id()); - if (backward[j] || source_side[2 * j]) { - return x; - } - if (auto it = copies.find(j); it != copies.end()) { - return it->second; - } - auto& a = tape[j]; - std::vector inputs; - for (auto& in : a.inputs()) { - inputs.push_back(backward_value(in)); - } - array c(a.shape(), a.dtype(), a.primitive_ptr(), std::move(inputs)); - c.set_backward(true); - for (int k = 0; k < c.inputs().size(); ++k) { - parents_map[c.inputs()[k].id()].push_back({c, k}); - } - copies.insert({j, c}); - new_nodes.push_back(c); - return c; - }; - - // Point the backward readers at the copies. - for (int i = 0; i < n; ++i) { - if (!backward[i]) { - continue; - } - // Give each backward node its own copies, so each kernel can fuse them. - copies.clear(); - for (int k = 0; k < tape[i].inputs().size(); ++k) { - auto x = tape[i].inputs()[k]; - auto c = backward_value(x); - if (c.id() == x.id()) { - continue; - } - auto& from = parents_map[x.id()]; - auto& to = parents_map[c.id()]; - for (auto it = from.begin(); it != from.end();) { - if (it->second == k && vid.at(it->first.id()) == i) { - it->first.inputs()[k] = c; - to.push_back(std::move(*it)); - it = from.erase(it); - } else { - ++it; - } - } - } - } - - if (new_nodes.empty()) { - return; - } - - // Put each copy in the tape before the first node that reads it. - std::unordered_map copy_of_id; - for (auto& c : new_nodes) { - copy_of_id.insert({c.id(), c}); - } - std::vector new_tape; - new_tape.reserve(tape.size() + new_nodes.size()); - std::unordered_set placed; - std::function place_inputs; - place_inputs = [&](const array& a) { - for (auto& in : a.inputs()) { - auto it = copy_of_id.find(in.id()); - if (it != copy_of_id.end() && placed.insert(in.id()).second) { - place_inputs(it->second); - new_tape.push_back(it->second); - } - } - }; - for (auto& a : tape) { - place_inputs(a); - new_tape.push_back(a); - } - tape = std::move(new_tape); -} - -// Extract sub-graphs of the graph that can be compiled -// and replace them with a Compiled Primitive. -// Replace the arrays in `fused_tape` with one Compiled primitive. The arrays in -// `old_outputs` become the outputs of the primitive. -std::vector make_compiled( - std::vector fused_tape, - const std::vector& inputs, - std::vector old_outputs, - const std::unordered_set& input_ids, - std::unordered_map& output_map, - ParentsMap& parents_map) { - std::unordered_set fused_ids; - for (auto& f : fused_tape) { - fused_ids.insert(f.id()); - } - std::unordered_set output_ids; - for (auto& o : old_outputs) { - output_ids.insert(o.id()); - } - - for (auto& f : fused_tape) { - if (output_ids.find(f.id()) != output_ids.end()) { - // Parents are now siblings, update the parent map - auto& pairs = parents_map[f.id()]; - pairs.erase( - std::remove_if( - pairs.begin(), - pairs.end(), - [&](auto& p) { - return fused_ids.find(p.first.id()) != fused_ids.end(); - }), - pairs.end()); - } else { - // Remove inner fused arrays parents from the parents map - // to keep the parents map in a valid state - parents_map.erase(f.id()); - } - } - - std::vector shapes; - std::vector types; - for (auto& o : old_outputs) { - if (o.shape() != old_outputs.back().shape()) { - throw std::runtime_error( - "[compile] Compilation failed. Tried to fuse operations with different output shapes"); - } - shapes.push_back(o.shape()); - types.push_back(o.dtype()); - } - std::unordered_set constant_ids; - for (auto& in : inputs) { - // Scalar constant - if (in.size() == 1 && !in.has_primitive() && - input_ids.find(in.id()) == input_ids.end()) { - constant_ids.insert(in.id()); - } - } - auto compiled_outputs = array::make_arrays( - std::move(shapes), - types, - std::make_shared( - old_outputs.back().primitive().stream(), - inputs, - old_outputs, - std::move(fused_tape), - std::move(constant_ids)), - inputs); - - // Replace inputs old parents with compiled_outputs - for (int i = 0; i < inputs.size(); ++i) { - auto& pairs = parents_map[inputs[i].id()]; - pairs.erase( - std::remove_if( - pairs.begin(), - pairs.end(), - [&](auto& p) { - return fused_ids.find(p.first.id()) != fused_ids.end(); - }), - pairs.end()); - for (auto& o : compiled_outputs) { - pairs.push_back({o, i}); - } - } - - // - Update outputs parents to point to compiled outputs - // - Update any overall graph outputs to be compiled outputs - for (int o = 0; o < old_outputs.size(); ++o) { - merge_one(compiled_outputs[o], old_outputs[o], parents_map); - if (auto it = output_map.find(old_outputs[o].id()); - it != output_map.end()) { - it->second = compiled_outputs[o]; - } - } - return compiled_outputs; -} - -// Groups of arrays to fuse. Each group with more than one array becomes one -// Compiled primitive. -struct FuseGroups { - std::vector> members; - std::unordered_map group_of; - - // The group of `a`, or -1 if `a` is not fusable - int find(const array& a) const { - auto it = group_of.find(a.id()); - return (it == group_of.end()) ? -1 : it->second; - } - - void add(const array& a, int g) { - group_of[a.id()] = g; - members[g].push_back(a); - } - - void make_group(const array& a) { - members.emplace_back(); - add(a, members.size() - 1); - } - - // Move the arrays of group `src` to group `dst` - void merge(int dst, int src) { - for (auto& a : members[src]) { - add(a, dst); - } - members[src].clear(); - } - - // Undo merge(dst, src), where `n` is the size of `dst` before the merge - void unmerge(int dst, int src, int n) { - for (int i = n; i < members[dst].size(); ++i) { - add(members[dst][i], src); - } - members[dst].erase(members[dst].begin() + n, members[dst].end()); - } -}; - -// True if `a` is read outside of its group or is an output of the graph -bool is_group_output( - const array& a, - const FuseGroups& groups, - const ParentsMap& parents_map, - const std::unordered_map& output_map) { - if (output_map.find(a.id()) != output_map.end()) { - return true; - } - int g = groups.find(a); - if (auto it = parents_map.find(a.id()); it != parents_map.end()) { - for (auto& [p, idx] : it->second) { - if (groups.find(p) != g) { - return true; - } - } - } - return false; -} - -// True if group `g` can become one Compiled primitive -bool is_valid_group( - int g, - const FuseGroups& groups, - const ParentsMap& parents_map, - const std::unordered_map& output_map) { - // All outputs have the same shape. Do not write out a broadcast. - std::vector outputs; - for (auto& a : groups.members[g]) { - if (!is_group_output(a, groups, parents_map, output_map)) { - continue; - } - if (is_broadcast(a.primitive()) && - output_map.find(a.id()) == output_map.end()) { - return false; - } - if (!outputs.empty() && a.shape() != outputs[0].shape()) { - return false; - } - outputs.push_back(a); - } - - // Count the inputs and find the longest chain of arrays in the group - std::unordered_set inputs; - std::unordered_map depths; - std::function recurse; - recurse = [&](const array& a) -> int { - if (groups.find(a) != g) { - inputs.insert(a.id()); - return 0; - } - if (auto it = depths.find(a.id()); it != depths.end()) { - return it->second; - } - int depth = 0; - for (auto& in : a.inputs()) { - depth = std::max(depth, recurse(in)); - } - depths.insert({a.id(), depth + 1}); - return depth + 1; - }; - int depth = 0; - for (auto& a : groups.members[g]) { - depth = std::max(depth, recurse(a)); - } - return depth <= max_compile_depth && - inputs.size() + outputs.size() <= max_compile_arrays; -} - -// Give group `g` its own copy of the broadcast `b` with split_one. Return -// true if the copy is fused into the group. -bool split_broadcast( - array b, - int g, - FuseGroups& groups, - ParentsMap& parents_map, - const std::unordered_map& output_map) { - std::unordered_set divider; - for (auto& a : groups.members[g]) { - divider.insert(a.id()); - } - array c = split_one(b, parents_map, divider); - for (int i = 0; i < c.inputs().size(); ++i) { - parents_map[c.inputs()[i].id()].push_back({c, i}); - } - groups.add(c, g); - if (is_valid_group(g, groups, parents_map, output_map)) { - return true; - } - - // Undo the copy - groups.members[g].pop_back(); - groups.group_of.erase(c.id()); - for (auto& in : c.inputs()) { - auto& pairs = parents_map[in.id()]; - pairs.erase( - std::remove_if( - pairs.begin(), - pairs.end(), - [&](auto& p) { return p.first.id() == c.id(); }), - pairs.end()); - } - merge_one(b, c, parents_map); - return false; -} - -// Fuse a group into the group that reads it, if all the readers of the group -// are in that group. Return true if a group changed. -bool compile_fuse_vertical( - FuseGroups& groups, - const std::vector& tape, - ParentsMap& parents_map, - const std::unordered_map& output_map) { - bool changed = false; - - // Go through the tape in reverse order so readers are fused first - for (int i = tape.size() - 1; i >= 0; --i) { - auto& a = tape[i]; - int src = groups.find(a); - if (src < 0) { - continue; - } - - // Find the groups that read the group of `a` - std::vector readers; - bool all_fusable = true; - for (auto& m : groups.members[src]) { - auto it = parents_map.find(m.id()); - if (it == parents_map.end()) { - continue; - } - for (auto& [p, idx] : it->second) { - int dst = groups.find(p); - if (dst < 0) { - all_fusable = false; - } else if ( - dst != src && - std::find(readers.begin(), readers.end(), dst) == readers.end()) { - readers.push_back(dst); - } - } - } - - // Split a broadcast with readers in more than one place, so that each - // group can fuse its own copy - if (is_broadcast(a.primitive()) && groups.members[src].size() == 1 && - (readers.size() > 1 || (readers.size() == 1 && !all_fusable))) { - for (int dst : readers) { - changed |= split_broadcast(a, dst, groups, parents_map, output_map); - } - continue; - } - - if (!all_fusable || readers.size() != 1) { - continue; - } - int dst = readers[0]; - if (a.primitive().stream() != groups.members[dst][0].primitive().stream()) { - continue; - } - int n = groups.members[dst].size(); - groups.merge(dst, src); - if (is_valid_group(dst, groups, parents_map, output_map)) { - changed = true; - } else { - groups.unmerge(dst, src, n); - } - } - return changed; -} - -// True if a path through arrays outside of groups `g1` and `g2` connects them. -// The two groups cannot be one kernel then. -bool is_connected( - int g1, - int g2, - const FuseGroups& groups, - const std::unordered_map& position, - const ParentsMap& parents_map) { - auto in_groups = [&](const array& a) { - int g = groups.find(a); - return g == g1 || g == g2; - }; - - // Readers come after their inputs in the tape, so stop the search after the - // last array of the two groups - int last = 0; - for (int g : {g1, g2}) { - for (auto& a : groups.members[g]) { - if (auto it = position.find(a.id()); it != position.end()) { - last = std::max(last, it->second); - } - } - } - - // Start from the readers outside of the two groups - std::vector stack; - for (int g : {g1, g2}) { - for (auto& a : groups.members[g]) { - if (auto it = parents_map.find(a.id()); it != parents_map.end()) { - for (auto& [p, idx] : it->second) { - if (!in_groups(p)) { - stack.push_back(p); - } - } - } - } - } - - std::unordered_set visited; - while (!stack.empty()) { - auto a = std::move(stack.back()); - stack.pop_back(); - if (in_groups(a)) { - return true; - } - if (!visited.insert(a.id()).second) { - continue; - } - if (auto it = position.find(a.id()); - it != position.end() && it->second > last) { - continue; - } - if (auto it = parents_map.find(a.id()); it != parents_map.end()) { - for (auto& [p, idx] : it->second) { - stack.push_back(p); - } - } - } - return false; -} - -// Fuse two groups which read the same arrays, if no path through other arrays -// connects them and they run at about the same time in the eval order. Return -// true if a group changed. -bool compile_fuse_horizontal( - FuseGroups& groups, - const std::vector& tape, - const std::unordered_map& position, - ParentsMap& parents_map, - const std::unordered_map& output_map) { - // Score each pair of groups with the bytes of the arrays they both read - std::map, size_t> scores; - for (auto& x : tape) { - // Scalars become constants in the kernel, so they are not read - if (x.size() == 1) { - continue; - } - auto it = parents_map.find(x.id()); - if (it == parents_map.end()) { - continue; - } - std::vector readers; - for (auto& [p, idx] : it->second) { - int g = groups.find(p); - if (g >= 0 && g != groups.find(x) && - std::find(readers.begin(), readers.end(), g) == readers.end()) { - readers.push_back(g); - } - } - for (int i = 0; i < readers.size(); ++i) { - for (int j = i + 1; j < readers.size(); ++j) { - auto key = std::minmax(readers[i], readers[j]); - scores[{key.first, key.second}] += x.nbytes(); - } - } - } - - // Eval runs an array as late as possible, just before its readers. So the - // level of an array, its longest path to an output, sets when it runs. - std::unordered_map levels; - std::function level; - level = [&](const array& a) -> int { - if (auto it = levels.find(a.id()); it != levels.end()) { - return it->second; - } - int l = 0; - if (auto it = parents_map.find(a.id()); it != parents_map.end()) { - for (auto& [p, idx] : it->second) { - l = std::max(l, level(p) + 1); - } - } - levels.insert({a.id(), l}); - return l; - }; - - // A group runs at the level of its deepest output - auto group_level = [&](int g) { - int l = 0; - for (auto& a : groups.members[g]) { - if (is_group_output(a, groups, parents_map, output_map)) { - l = std::max(l, level(a)); - } - } - return l; - }; - auto distance = [&](int g1, int g2) { - return std::abs(group_level(g1) - group_level(g2)); - }; - auto is_used = [&](int g) { - for (auto& a : groups.members[g]) { - if (is_group_output(a, groups, parents_map, output_map)) { - return true; - } - } - return false; - }; - - // Try the pairs with the most shared bytes first, then the closest pairs. - // Keep one array of each group, since groups change when they merge. - struct Candidate { - size_t score; - int distance; - array a; - array b; - }; - std::vector candidates; - for (auto& [key, score] : scores) { - auto [g1, g2] = key; - candidates.push_back( - {score, - distance(g1, g2), - groups.members[g1][0], - groups.members[g2][0]}); - } - std::sort(candidates.begin(), candidates.end(), [](auto& x, auto& y) { - return (x.score != y.score) ? x.score > y.score : x.distance < y.distance; - }); - - // Merging groups at different levels moves a kernel in the eval order, and - // this can keep large arrays longer in memory - bool changed = false; - for (auto& c : candidates) { - int g1 = groups.find(c.a); - int g2 = groups.find(c.b); - if (g1 == g2 || c.a.primitive().stream() != c.b.primitive().stream() || - distance(g1, g2) > max_fuse_distance || !is_used(g1) || !is_used(g2) || - is_connected(g1, g2, groups, position, parents_map)) { - continue; - } - int size = groups.members[g1].size(); - groups.merge(g1, g2); - if (is_valid_group(g1, groups, parents_map, output_map)) { - changed = true; - } else { - groups.unmerge(g1, g2, size); - } - } - return changed; -} - -// Fuse arrays in groups with the fusion rules, then replace each group with -// one Compiled primitive. -void compile_fuse( - std::vector& tape, - ParentsMap& parents_map, - const std::vector& inputs, - std::vector& outputs) { - // Track outputs to replace with new compiled outputs - std::unordered_map output_map; - for (auto& o : outputs) { - output_map.insert({o.id(), o}); - } - - // Set of inputs to distinguish constants - std::unordered_set input_ids; - for (auto& in : inputs) { - input_ids.insert(in.id()); - } - - // Start with one group for each fusable array - FuseGroups groups; - std::unordered_map position; - for (int i = 0; i < tape.size(); ++i) { - auto& a = tape[i]; - position.insert({a.id(), i}); - if (a.has_primitive() && is_fusable(a.primitive()) && - input_ids.find(a.id()) == input_ids.end()) { - groups.make_group(a); - } - } - - // Merge groups until no rule changes them. A merge can make a new merge - // possible, so repeat the rules. - while (compile_fuse_vertical(groups, tape, parents_map, output_map) || - compile_fuse_horizontal( - groups, tape, position, parents_map, output_map)) { - } - - // Replace each group of more than one array with a Compiled primitive - for (int g = 0; g < groups.members.size(); ++g) { - auto& group = groups.members[g]; - if (group.size() <= 1) { - continue; - } - - // The roots are the arrays which are not read in the group - std::vector roots; - for (auto& a : group) { - bool is_root = true; - if (auto it = parents_map.find(a.id()); it != parents_map.end()) { - for (auto& [p, idx] : it->second) { - if (groups.find(p) == g) { - is_root = false; - break; - } - } - } - if (is_root) { - roots.push_back(a); - } - } - std::sort(roots.begin(), roots.end(), [&](auto& a, auto& b) { - return position.at(a.id()) < position.at(b.id()); - }); - - // Build the tape of the group in the right order and collect the inputs - std::vector inputs; - std::vector fused_tape; - std::unordered_set input_set; - std::unordered_set tape_set; - std::function recurse_tape; - recurse_tape = [&](const array& a) { - if (groups.find(a) != g) { - if (input_set.find(a.id()) == input_set.end()) { - input_set.insert(a.id()); - inputs.push_back(a); - } - return; - } - if (tape_set.find(a.id()) != tape_set.end()) { - return; - } - tape_set.insert(a.id()); - for (auto& in : a.inputs()) { - recurse_tape(in); - } - fused_tape.push_back(a); - }; - for (auto& r : roots) { - recurse_tape(r); - } - - std::vector old_outputs; - for (auto& f : fused_tape) { - if (is_group_output(f, groups, parents_map, output_map)) { - old_outputs.push_back(f); - } - } - - // Nothing reads the group, so it is not used - if (old_outputs.empty()) { - continue; - } - make_compiled( - std::move(fused_tape), - inputs, - std::move(old_outputs), - input_ids, - output_map, - parents_map); - } - - // Build the new tape in execution order - std::vector new_tape; - std::unordered_set cache; - std::function recurse; - recurse = [&](const array& a) { - if (cache.find(a.id()) != cache.end()) { - return; - } - // Don't recurse on inputs - if (input_ids.find(a.id()) == input_ids.end()) { - for (auto& in : a.inputs()) { - recurse(in); - } - } - cache.insert(a.id()); - for (auto& s : a.siblings()) { - cache.insert(s.id()); - } - new_tape.push_back(a); - }; - for (auto& o : outputs) { - o = output_map.at(o.id()); - recurse(o); - } - tape = std::move(new_tape); -} - -std::vector compile_replace( - const std::vector& tape, - const std::vector& trace_inputs, - const std::vector& trace_outputs, - const std::vector& inputs, - bool shapeless) { - std::unordered_map trace_to_real; - for (int i = 0; i < inputs.size(); ++i) { - trace_to_real.insert({trace_inputs[i].id(), inputs[i]}); - } - - auto is_load = [](const Primitive& p) { return typeid(p) == typeid(Load); }; - - for (auto& a : tape) { - // Arrays in the tape without primitives are either: - // - inputs, which are already in the map - // - constants, which can be used directly - // - a load primitive which has no inputs and will become a constant - // after the first eval - if (!a.has_primitive() || is_load(a.primitive())) { - trace_to_real.insert({a.id(), a}); - } else { - // Find real inputs - std::vector real_inputs; - for (auto& in : a.inputs()) { - real_inputs.push_back(trace_to_real.at(in.id())); - } - if (a.siblings().empty()) { - auto shape = - shapeless ? a.primitive().output_shapes(real_inputs)[0] : a.shape(); - auto real_a = array( - std::move(shape), - a.dtype(), - a.primitive_ptr(), - std::move(real_inputs)); - trace_to_real.insert({a.id(), std::move(real_a)}); - } else { - // Ensure the order is correct for multi-output primitives - std::vector types; - auto trace_out = a.outputs(); - for (auto& o : trace_out) { - types.push_back(o.dtype()); - } - std::vector shapes; - if (shapeless) { - shapes = a.primitive().output_shapes(real_inputs); - } else { - for (auto& o : trace_out) { - shapes.push_back(o.shape()); - } - } - auto real_out = array::make_arrays( - std::move(shapes), types, a.primitive_ptr(), real_inputs); - for (int i = 0; i < trace_out.size(); ++i) { - trace_to_real.insert({trace_out[i].id(), std::move(real_out[i])}); - } - } - } - } - - std::vector outputs; - for (auto& o : trace_outputs) { - outputs.push_back(trace_to_real.at(o.id())); - } - return outputs; -} - -bool skip_compile() { - return compile_mode() == CompileMode::disabled || - !(compile_available_for_device(default_device())); -} - -ArrayFnWithExtra compile( - ArrayFnWithExtra fun, - std::uintptr_t fun_id, - bool shapeless /* = false */, - std::vector constants /* = {} */) { - if (skip_compile()) { - return fun; - } - if (!fun) { - throw std::invalid_argument( - "[compile] Cannot compile a function without a target."); - } - - return [fun = std::move(fun), - fun_id, - shapeless, - constants = std::move(constants)](const std::vector& inputs) { - // If the inputs are tracers, trace the original graph - if (std::any_of(inputs.begin(), inputs.end(), [](auto& in) { - return in.is_tracer(); - })) { - return fun(inputs); - } - - // Find a cache entry with the correct inputs - auto [entry, entries_ptr] = - compile_cache_unsafe()->find(fun_id, inputs, shapeless, constants); - static_assert(std::is_reference_v); - - // No matching cache entry existed, so compile - if (entry.empty) { - // Set the constants - entry.constants = std::move(constants); - // Trace to build the graph - std::tie(entry.inputs, entry.outputs, entry.extra) = - compile_trace(fun, inputs, shapeless); - - // DFS the graph and get a tape, and a map of array id to (parent, - // position in parent inputs) - std::unordered_map>> - parents_map; - std::tie(entry.tape, parents_map) = - compile_dfs(entry.inputs, entry.outputs, inputs); - - // Simplify the tape - auto mode = compile_mode().load(); - if (mode != CompileMode::no_simplify) { - compile_simplify( - entry.tape, parents_map, entry.outputs, /* passes */ 3); - } - - // Experimental: recompute cheap forward values in the backward - // instead of keeping them. Enable with MLX_COMPILE_RECOMPUTE=1. - if (config::get("MLX_COMPILE_RECOMPUTE", 0)) { - compile_recompute(entry.tape, parents_map); - } - - // Kernel fusion to generate Compiled primitives. The tape and - // new outputs must be updated accordingly - if (mode != CompileMode::no_fuse) { - compile_fuse(entry.tape, parents_map, entry.inputs, entry.outputs); - } - - // Mark the entry as filled only after every step above completed, so - // a throwing first trace leaves the entry empty and a later call - // re-traces cleanly instead of hitting a half-filled entry - entry.empty = false; - } - - // At this point we must have a tape, now replace the placeholders - // with real arrays that can be evaluated - return ArraysAndExtra{ - compile_replace( - entry.tape, entry.inputs, entry.outputs, inputs, shapeless), - entry.extra}; - }; -} - -ArrayFunction compile( - ArrayFunction fun, - std::uintptr_t fun_id, - bool shapeless /* = false */, - std::vector constants /* = {} */) { - if (skip_compile()) { - return fun; - } - if (!fun) { - throw std::invalid_argument( - "[compile] Cannot compile a function without a target."); - } - - ArrayFnWithExtra fun_with_extra = - [fun = std::move(fun)](const std::vector& inputs) { - return ArraysAndExtra{fun(inputs), nullptr}; - }; - - auto compiled_fun = compile( - std::move(fun_with_extra), fun_id, shapeless, std::move(constants)); - - return [compiled_fun = - std::move(compiled_fun)](const std::vector& inputs) { - return compiled_fun(inputs).first; - }; -} - -CompileCacheWeakPtr compile_cache() { - return compile_cache_unsafe(); -} - -void compile_erase(const CompileCacheWeakPtr& cache, std::uintptr_t fun_id) { - if (auto p = cache.lock()) { - p->erase(fun_id); - } -} - -void compile_clear_cache(const CompileCacheWeakPtr& cache) { - if (auto p = cache.lock()) { - p->clear(); - } -} - -} // namespace detail - -ArrayFunction compile(ArrayFunction fun, bool shapeless /* false */) { - if (detail::skip_compile()) { - return fun; - } - auto fun_id = detail::get_function_address(fun); - if (fun_id) { - // If the function has an addressable target then no need to manage it's - // lifetime - return detail::compile(std::move(fun), fun_id, shapeless); - } else { - auto cleanup = [cache = detail::compile_cache()](auto* p) { - detail::compile_erase(cache, reinterpret_cast(p)); - delete p; - }; - auto pfun = std::shared_ptr(new ArrayFunction{fun}, cleanup); - fun_id = reinterpret_cast(pfun.get()); - return detail::compile( - [pfun = std::move(pfun)](const auto& inputs) { - return (*pfun)(inputs); - }, - fun_id, - shapeless); - } -} - -ArrayFunction compile( - std::vector (*fun)(const std::vector&), - bool shapeless /* = false */) { - if (detail::skip_compile()) { - return fun; - } - return detail::compile(fun, reinterpret_cast(fun), shapeless); -} - -void disable_compile() { - detail::compile_mode() = CompileMode::disabled; -} - -void enable_compile() { - detail::compile_mode() = CompileMode::enabled; -} - -void set_compile_mode(CompileMode mode) { - detail::compile_mode() = mode; -} - -} // namespace mlx::core diff --git a/mlx/compile/CMakeLists.txt b/mlx/compile/CMakeLists.txt new file mode 100644 index 0000000000..9c5f4324fc --- /dev/null +++ b/mlx/compile/CMakeLists.txt @@ -0,0 +1,7 @@ +target_sources( + mlx + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/compile.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/trace.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/simplify.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/recompute.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/fuse.cpp) diff --git a/mlx/compile/compile.cpp b/mlx/compile/compile.cpp new file mode 100644 index 0000000000..9b83557ea3 --- /dev/null +++ b/mlx/compile/compile.cpp @@ -0,0 +1,473 @@ +// Copyright © 2023-2024 Apple Inc. + +#include +#include +#include +#include +#include +#include + +#include "mlx/backend/common/compiled.h" +#include "mlx/compile.h" +#include "mlx/compile_impl.h" +#include "mlx/graph_utils.h" +#include "mlx/primitives.h" +#include "mlx/transforms.h" +#include "mlx/transforms_impl.h" +#include "mlx/utils.h" + +namespace mlx::core { + +using ArrayFunction = + std::function(const std::vector&)>; + +Compiled::Compiled( + Stream stream, + std::vector inputs, + std::vector outputs, + std::vector tape, + std::unordered_set constant_ids) + : Primitive(stream), + inputs_(std::move(inputs)), + outputs_(std::move(outputs)), + tape_(std::move(tape)), + constant_ids_(std::move(constant_ids)), + is_constant_([this](size_t i) { + return constant_ids_.find(inputs_[i].id()) != constant_ids_.end(); + }) { + // Build the kernel name. + NodeNamer namer; + std::ostringstream os; + std::ostringstream constant_hasher; + + std::unordered_set output_ids; + for (auto& o : outputs_) { + output_ids.insert(o.id()); + } + + // Fill the input names. This is not really necessary, I just like having A, + // B, C, ... as the inputs. + for (const auto& x : inputs_) { + namer.get_name(x); + } + + // The primitives describing the tape. For unary and binary primitives this + // must be enough to describe the full computation. + for (const auto& a : tape_) { + // name and type of output + os << namer.get_name(a) << kindof(a.dtype()) << a.itemsize(); + // whether or not it's an output + if (output_ids.find(a.id()) != output_ids.end()) { + os << "O"; + } else { + os << "I"; + } + // computation performed + os << a.primitive().name(); + // name of inputs to the function + for (auto& inp : a.inputs()) { + os << namer.get_name(inp); + } + } + os << "_"; + + for (const auto& x : inputs_) { + if (constant_ids_.find(x.id()) != constant_ids_.end()) { + os << "C"; + print_constant(constant_hasher, x); + } else { + os << (is_scalar(x) ? "S" : "V"); + } + } + os << "_"; + // Iterate the moved-into members; the parameters are moved-from above. + for (const auto& x : inputs_) { + if (constant_ids_.find(x.id()) != constant_ids_.end()) { + continue; + } + os << kindof(x.dtype()) << x.itemsize(); + } + os << "_" << std::hash{}(constant_hasher.str()); + + kernel_lib_ = os.str(); +} + +std::vector Compiled::vjp( + const std::vector&, + const std::vector&, + const std::vector&, + const std::vector&) { + throw std::runtime_error("[Compiled] Cannot vjp primitive."); +} + +std::vector Compiled::jvp( + const std::vector&, + const std::vector&, + const std::vector&) { + throw std::runtime_error("[Compiled] Cannot jvp primitive."); +} + +std::pair, std::vector> Compiled::vmap( + const std::vector&, + const std::vector&) { + throw std::runtime_error("[Compiled] Cannot vmap primitive."); +} + +bool Compiled::is_equivalent(const Primitive& other) const { + const Compiled& a_other = static_cast(other); + return std::equal( + tape_.begin(), + tape_.end(), + a_other.tape_.begin(), + a_other.tape_.end(), + [](const array& a1, const array& a2) { + auto& p1 = a1.primitive(); + auto& p2 = a2.primitive(); + return typeid(p1) == typeid(p2) && p1.is_equivalent(p2); + }); +} + +const char* Compiled::name() const { + if (name_.empty()) { + std::ostringstream os; + os << "Compiled"; + for (auto& a : tape_) { + os << a.primitive().name(); + } + name_ = os.str(); + } + return name_.c_str(); +} + +std::vector Compiled::output_shapes(const std::vector& inputs) { + size_t nd = 0; + for (auto& in : inputs) { + nd = std::max(nd, in.ndim()); + } + Shape out_shape(nd, 0); + for (auto& in : inputs) { + auto dd = nd - in.ndim(); + for (auto i = dd; i < nd; ++i) { + out_shape[i] = std::max(out_shape[i], in.shape()[i - dd]); + } + } + // All outputs have the same shape + return std::vector(outputs_.size(), out_shape); +} + +namespace detail { + +std::atomic& compile_mode() { + auto get_val = []() { + if (std::getenv("MLX_DISABLE_COMPILE")) { + return CompileMode::disabled; + } else { + return CompileMode::enabled; + } + }; + static std::atomic compile_mode_ = get_val(); + return compile_mode_; +} + +template +std::uintptr_t get_function_address(const std::function& fun) { + using FunType = T (*)(U...); + const FunType* fun_ptr = fun.template target(); + if (fun_ptr == nullptr) { + return 0; + } + return reinterpret_cast(*fun_ptr); +} + +class CompileCache { + public: + struct CacheEntry { + CacheEntry(std::optional stream, bool shapeless) + : stream(stream), shapeless(shapeless) {} + std::optional stream; + bool shapeless; + std::vector inputs; + std::vector outputs; + std::vector tape; + bool empty{true}; + std::vector constants; + std::shared_ptr extra; + }; + + CompileCache() { + // Make sure the allocator is fully initialized before the compiler cache. + allocator::allocator(); + } + + // Returns a reference to a CacheEntry which can be updated by the caller to + // avoid copying large tapes / inputs / outputs, with the shared_ptr of + // entries to avoid getting erased during compilation. + std::tuple>> find( + std::uintptr_t fun_id, + const std::vector& inputs, + bool shapeless, + const std::vector& constants) { + // Find the cache entries for |fun_id| in a thread-safe way. + auto entries_ptr = [&]() { + // Lookup with shared lock. + { + std::shared_lock lock(mutex_); + auto it = cache_.find(fun_id); + if (it != cache_.end()) { + return it->second; + } + } + // Insertion with exclusive lock. + std::unique_lock lock(mutex_); + auto& ptr = cache_[fun_id]; + if (!ptr) { + ptr = std::make_shared>(); + } + return ptr; + }(); + auto& entries = *entries_ptr; + + // Compare if 2 arrays have same shape and dtype. + auto has_same_shape_and_dtype = [shapeless]( + const std::vector& in1, + const std::vector& in2) { + if (in1.size() != in2.size()) { + return false; + } + for (size_t i = 0; i < in1.size(); ++i) { + if (in1[i].ndim() != in2[i].ndim()) { + return false; + } + if (!shapeless && in1[i].shape() != in2[i].shape()) { + return false; + } + if (in1[i].dtype() != in2[i].dtype()) { + return false; + } + } + return true; + }; + // Loop over entries and check: + // - Default stream and device match the entry's default stream + // - Inputs match i.e. shapes and types must be equal. + auto stream = peek_default_stream(default_device()); + for (CacheEntry& entry : entries) { + // Check that the default stream and device match + if (entry.stream != stream) { + continue; + } + if (entry.shapeless != shapeless) { + continue; + } + + // Check the inputs match and return if so + if (has_same_shape_and_dtype(inputs, entry.inputs) && + constants == entry.constants) { + return {entry, std::move(entries_ptr)}; + } + } + // Otherwise append a new cache entry + entries.push_back(CacheEntry{stream, shapeless}); + return {entries.back(), std::move(entries_ptr)}; + } + + void erase(std::uintptr_t fun_id) { + std::unique_lock lock(mutex_); + cache_.erase(fun_id); + } + + void clear() { + std::unique_lock lock(mutex_); + cache_.clear(); + } + + private: + // The cache may get its key erased from a separate thread, but its value is + // only added and modified in the thread of creation. + // Put value in a shared_ptr to avoid race condition when erasing happened + // during compilation for the same function. + std::unordered_map>> + cache_; + std::shared_mutex mutex_; +}; + +std::shared_ptr& compile_cache_unsafe() { + static thread_local auto cache = std::make_shared(); + return cache; +} + +bool skip_compile() { + return compile_mode() == CompileMode::disabled || + !(compile_available_for_device(default_device())); +} + +ArrayFnWithExtra compile( + ArrayFnWithExtra fun, + std::uintptr_t fun_id, + bool shapeless /* = false */, + std::vector constants /* = {} */) { + if (skip_compile()) { + return fun; + } + if (!fun) { + throw std::invalid_argument( + "[compile] Cannot compile a function without a target."); + } + + return [fun = std::move(fun), + fun_id, + shapeless, + constants = std::move(constants)](const std::vector& inputs) { + // If the inputs are tracers, trace the original graph + if (std::any_of(inputs.begin(), inputs.end(), [](auto& in) { + return in.is_tracer(); + })) { + return fun(inputs); + } + + // Find a cache entry with the correct inputs + auto [entry, entries_ptr] = + compile_cache_unsafe()->find(fun_id, inputs, shapeless, constants); + static_assert(std::is_reference_v); + + // No matching cache entry existed, so compile + if (entry.empty) { + // Set the constants + entry.constants = std::move(constants); + // Trace to build the graph + std::tie(entry.inputs, entry.outputs, entry.extra) = + compile_trace(fun, inputs, shapeless); + + // DFS the graph and get a tape, and a map of array id to (parent, + // position in parent inputs) + std::unordered_map>> + parents_map; + std::tie(entry.tape, parents_map) = + compile_dfs(entry.inputs, entry.outputs, inputs); + + // Simplify the tape + auto mode = compile_mode().load(); + if (mode != CompileMode::no_simplify) { + compile_simplify( + entry.tape, parents_map, entry.outputs, /* passes */ 3); + } + + // Experimental: recompute cheap forward values in the backward + // instead of keeping them. Enable with MLX_COMPILE_RECOMPUTE=1. + if (config::get("MLX_COMPILE_RECOMPUTE", 0)) { + compile_recompute(entry.tape, parents_map); + } + + // Kernel fusion to generate Compiled primitives. The tape and + // new outputs must be updated accordingly + if (mode != CompileMode::no_fuse) { + compile_fuse(entry.tape, parents_map, entry.inputs, entry.outputs); + } + + // Mark the entry as filled only after every step above completed, so + // a throwing first trace leaves the entry empty and a later call + // re-traces cleanly instead of hitting a half-filled entry + entry.empty = false; + } + + // At this point we must have a tape, now replace the placeholders + // with real arrays that can be evaluated + return ArraysAndExtra{ + compile_replace( + entry.tape, entry.inputs, entry.outputs, inputs, shapeless), + entry.extra}; + }; +} + +ArrayFunction compile( + ArrayFunction fun, + std::uintptr_t fun_id, + bool shapeless /* = false */, + std::vector constants /* = {} */) { + if (skip_compile()) { + return fun; + } + if (!fun) { + throw std::invalid_argument( + "[compile] Cannot compile a function without a target."); + } + + ArrayFnWithExtra fun_with_extra = + [fun = std::move(fun)](const std::vector& inputs) { + return ArraysAndExtra{fun(inputs), nullptr}; + }; + + auto compiled_fun = compile( + std::move(fun_with_extra), fun_id, shapeless, std::move(constants)); + + return [compiled_fun = + std::move(compiled_fun)](const std::vector& inputs) { + return compiled_fun(inputs).first; + }; +} + +CompileCacheWeakPtr compile_cache() { + return compile_cache_unsafe(); +} + +void compile_erase(const CompileCacheWeakPtr& cache, std::uintptr_t fun_id) { + if (auto p = cache.lock()) { + p->erase(fun_id); + } +} + +void compile_clear_cache(const CompileCacheWeakPtr& cache) { + if (auto p = cache.lock()) { + p->clear(); + } +} + +} // namespace detail + +ArrayFunction compile(ArrayFunction fun, bool shapeless /* false */) { + if (detail::skip_compile()) { + return fun; + } + auto fun_id = detail::get_function_address(fun); + if (fun_id) { + // If the function has an addressable target then no need to manage it's + // lifetime + return detail::compile(std::move(fun), fun_id, shapeless); + } else { + auto cleanup = [cache = detail::compile_cache()](auto* p) { + detail::compile_erase(cache, reinterpret_cast(p)); + delete p; + }; + auto pfun = std::shared_ptr(new ArrayFunction{fun}, cleanup); + fun_id = reinterpret_cast(pfun.get()); + return detail::compile( + [pfun = std::move(pfun)](const auto& inputs) { + return (*pfun)(inputs); + }, + fun_id, + shapeless); + } +} + +ArrayFunction compile( + std::vector (*fun)(const std::vector&), + bool shapeless /* = false */) { + if (detail::skip_compile()) { + return fun; + } + return detail::compile(fun, reinterpret_cast(fun), shapeless); +} + +void disable_compile() { + detail::compile_mode() = CompileMode::disabled; +} + +void enable_compile() { + detail::compile_mode() = CompileMode::enabled; +} + +void set_compile_mode(CompileMode mode) { + detail::compile_mode() = mode; +} + +} // namespace mlx::core diff --git a/mlx/compile/fuse.cpp b/mlx/compile/fuse.cpp new file mode 100644 index 0000000000..38d32fb08c --- /dev/null +++ b/mlx/compile/fuse.cpp @@ -0,0 +1,726 @@ +// Copyright © 2023-2024 Apple Inc. + +#include +#include +#include +#include +#include +#include + +#include "mlx/compile_impl.h" +#include "mlx/primitives.h" + +namespace mlx::core { + +constexpr int max_compile_depth = 11; +constexpr int max_compile_arrays = 24; +constexpr int max_fuse_distance = 0; + +namespace detail { + +bool is_unary(const Primitive& p) { + return ( + typeid(p) == typeid(Abs) || typeid(p) == typeid(ArcCos) || + typeid(p) == typeid(ArcCosh) || typeid(p) == typeid(ArcSin) || + typeid(p) == typeid(ArcSinh) || typeid(p) == typeid(ArcTan) || + typeid(p) == typeid(ArcTanh) || typeid(p) == typeid(AsType) || + typeid(p) == typeid(Ceil) || typeid(p) == typeid(Cos) || + typeid(p) == typeid(Conjugate) || typeid(p) == typeid(Cosh) || + typeid(p) == typeid(Remainder) || typeid(p) == typeid(Erf) || + typeid(p) == typeid(ErfInv) || typeid(p) == typeid(Exp) || + typeid(p) == typeid(Floor) || typeid(p) == typeid(Log) || + typeid(p) == typeid(Log1p) || typeid(p) == typeid(LogicalNot) || + typeid(p) == typeid(Negative) || typeid(p) == typeid(Round) || + typeid(p) == typeid(Sigmoid) || typeid(p) == typeid(Sign) || + typeid(p) == typeid(Sin) || typeid(p) == typeid(Sinh) || + typeid(p) == typeid(Square) || typeid(p) == typeid(Sqrt) || + typeid(p) == typeid(Tan) || typeid(p) == typeid(Tanh) || + typeid(p) == typeid(Expm1) || typeid(p) == typeid(Real) || + typeid(p) == typeid(Imag) || typeid(p) == typeid(BitwiseInvert)); +} + +bool is_binary(const Primitive& p) { + return typeid(p) == typeid(Add) || typeid(p) == typeid(ArcTan2) || + typeid(p) == typeid(BitwiseBinary) || typeid(p) == typeid(Divide) || + typeid(p) == typeid(Equal) || typeid(p) == typeid(FloorDivide) || + typeid(p) == typeid(Greater) || typeid(p) == typeid(GreaterEqual) || + typeid(p) == typeid(Less) || typeid(p) == typeid(LessEqual) || + typeid(p) == typeid(LogicalNot) || typeid(p) == typeid(LogicalAnd) || + typeid(p) == typeid(LogicalOr) || typeid(p) == typeid(LogAddExp) || + typeid(p) == typeid(Maximum) || typeid(p) == typeid(Minimum) || + typeid(p) == typeid(Multiply) || typeid(p) == typeid(NotEqual) || + typeid(p) == typeid(Power) || typeid(p) == typeid(Subtract); +} + +bool is_ternary(const Primitive& p) { + return typeid(p) == typeid(Select); +} + +bool is_broadcast(const Primitive& p) { + return typeid(p) == typeid(Broadcast); +} + +bool is_noop(const Primitive& p) { + return typeid(p) == typeid(Copy) || typeid(p) == typeid(StopGradient); +} + +bool is_reduction(const Primitive& p) { + return typeid(p) == typeid(Reduce) || typeid(p) == typeid(ArgReduce); +} + +bool is_fusable(const Primitive& p) { + return is_unary(p) || is_binary(p) || is_ternary(p) || is_broadcast(p); +} + +// Any parent in the divider will continue to refer to `x` but any parent not +// in the divider will refer to a copy of the operation. +array split_one( + const array& x, + ParentsMap& parents_map, + const std::unordered_set& divider) { + array y(x.shape(), x.dtype(), x.primitive_ptr(), x.inputs()); + + auto& x_parents = parents_map[x.id()]; + auto& y_parents = parents_map[y.id()]; + + for (auto it = x_parents.begin(); it != x_parents.end();) { + if (divider.find(it->first.id()) != divider.end()) { + it->first.inputs()[it->second] = y; + y_parents.emplace_back(std::move(*it)); + it = x_parents.erase(it); + } else { + it++; + } + } + + return y; +} + +// Extract sub-graphs of the graph that can be compiled +// and replace them with a Compiled Primitive. +// Replace the arrays in `fused_tape` with one Compiled primitive. The arrays in +// `old_outputs` become the outputs of the primitive. +std::vector make_compiled( + std::vector fused_tape, + const std::vector& inputs, + std::vector old_outputs, + const std::unordered_set& input_ids, + std::unordered_map& output_map, + ParentsMap& parents_map) { + std::unordered_set fused_ids; + for (auto& f : fused_tape) { + fused_ids.insert(f.id()); + } + std::unordered_set output_ids; + for (auto& o : old_outputs) { + output_ids.insert(o.id()); + } + + for (auto& f : fused_tape) { + if (output_ids.find(f.id()) != output_ids.end()) { + // Parents are now siblings, update the parent map + auto& pairs = parents_map[f.id()]; + pairs.erase( + std::remove_if( + pairs.begin(), + pairs.end(), + [&](auto& p) { + return fused_ids.find(p.first.id()) != fused_ids.end(); + }), + pairs.end()); + } else { + // Remove inner fused arrays parents from the parents map + // to keep the parents map in a valid state + parents_map.erase(f.id()); + } + } + + std::vector shapes; + std::vector types; + for (auto& o : old_outputs) { + if (o.shape() != old_outputs.back().shape()) { + throw std::runtime_error( + "[compile] Compilation failed. Tried to fuse operations with different output shapes"); + } + shapes.push_back(o.shape()); + types.push_back(o.dtype()); + } + std::unordered_set constant_ids; + for (auto& in : inputs) { + // Scalar constant + if (in.size() == 1 && !in.has_primitive() && + input_ids.find(in.id()) == input_ids.end()) { + constant_ids.insert(in.id()); + } + } + auto compiled_outputs = array::make_arrays( + std::move(shapes), + types, + std::make_shared( + old_outputs.back().primitive().stream(), + inputs, + old_outputs, + std::move(fused_tape), + std::move(constant_ids)), + inputs); + + // Replace inputs old parents with compiled_outputs + for (int i = 0; i < inputs.size(); ++i) { + auto& pairs = parents_map[inputs[i].id()]; + pairs.erase( + std::remove_if( + pairs.begin(), + pairs.end(), + [&](auto& p) { + return fused_ids.find(p.first.id()) != fused_ids.end(); + }), + pairs.end()); + for (auto& o : compiled_outputs) { + pairs.push_back({o, i}); + } + } + + // - Update outputs parents to point to compiled outputs + // - Update any overall graph outputs to be compiled outputs + for (int o = 0; o < old_outputs.size(); ++o) { + merge_one(compiled_outputs[o], old_outputs[o], parents_map); + if (auto it = output_map.find(old_outputs[o].id()); + it != output_map.end()) { + it->second = compiled_outputs[o]; + } + } + return compiled_outputs; +} + +// Groups of arrays to fuse. Each group with more than one array becomes one +// Compiled primitive. +struct FuseGroups { + std::vector> members; + std::unordered_map group_of; + + // The group of `a`, or -1 if `a` is not fusable + int find(const array& a) const { + auto it = group_of.find(a.id()); + return (it == group_of.end()) ? -1 : it->second; + } + + void add(const array& a, int g) { + group_of[a.id()] = g; + members[g].push_back(a); + } + + void make_group(const array& a) { + members.emplace_back(); + add(a, members.size() - 1); + } + + // Move the arrays of group `src` to group `dst` + void merge(int dst, int src) { + for (auto& a : members[src]) { + add(a, dst); + } + members[src].clear(); + } + + // Undo merge(dst, src), where `n` is the size of `dst` before the merge + void unmerge(int dst, int src, int n) { + for (int i = n; i < members[dst].size(); ++i) { + add(members[dst][i], src); + } + members[dst].erase(members[dst].begin() + n, members[dst].end()); + } +}; + +// True if `a` is read outside of its group or is an output of the graph +bool is_group_output( + const array& a, + const FuseGroups& groups, + const ParentsMap& parents_map, + const std::unordered_map& output_map) { + if (output_map.find(a.id()) != output_map.end()) { + return true; + } + int g = groups.find(a); + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + if (groups.find(p) != g) { + return true; + } + } + } + return false; +} + +// True if group `g` can become one Compiled primitive +bool is_valid_group( + int g, + const FuseGroups& groups, + const ParentsMap& parents_map, + const std::unordered_map& output_map) { + // All outputs have the same shape. Do not write out a broadcast. + std::vector outputs; + for (auto& a : groups.members[g]) { + if (!is_group_output(a, groups, parents_map, output_map)) { + continue; + } + if (is_broadcast(a.primitive()) && + output_map.find(a.id()) == output_map.end()) { + return false; + } + if (!outputs.empty() && a.shape() != outputs[0].shape()) { + return false; + } + outputs.push_back(a); + } + + // Count the inputs and find the longest chain of arrays in the group + std::unordered_set inputs; + std::unordered_map depths; + std::function recurse; + recurse = [&](const array& a) -> int { + if (groups.find(a) != g) { + inputs.insert(a.id()); + return 0; + } + if (auto it = depths.find(a.id()); it != depths.end()) { + return it->second; + } + int depth = 0; + for (auto& in : a.inputs()) { + depth = std::max(depth, recurse(in)); + } + depths.insert({a.id(), depth + 1}); + return depth + 1; + }; + int depth = 0; + for (auto& a : groups.members[g]) { + depth = std::max(depth, recurse(a)); + } + return depth <= max_compile_depth && + inputs.size() + outputs.size() <= max_compile_arrays; +} + +// Give group `g` its own copy of the broadcast `b` with split_one. Return +// true if the copy is fused into the group. +bool split_broadcast( + array b, + int g, + FuseGroups& groups, + ParentsMap& parents_map, + const std::unordered_map& output_map) { + std::unordered_set divider; + for (auto& a : groups.members[g]) { + divider.insert(a.id()); + } + array c = split_one(b, parents_map, divider); + for (int i = 0; i < c.inputs().size(); ++i) { + parents_map[c.inputs()[i].id()].push_back({c, i}); + } + groups.add(c, g); + if (is_valid_group(g, groups, parents_map, output_map)) { + return true; + } + + // Undo the copy + groups.members[g].pop_back(); + groups.group_of.erase(c.id()); + for (auto& in : c.inputs()) { + auto& pairs = parents_map[in.id()]; + pairs.erase( + std::remove_if( + pairs.begin(), + pairs.end(), + [&](auto& p) { return p.first.id() == c.id(); }), + pairs.end()); + } + merge_one(b, c, parents_map); + return false; +} + +// Fuse a group into the group that reads it, if all the readers of the group +// are in that group. Return true if a group changed. +bool compile_fuse_vertical( + FuseGroups& groups, + const std::vector& tape, + ParentsMap& parents_map, + const std::unordered_map& output_map) { + bool changed = false; + + // Go through the tape in reverse order so readers are fused first + for (int i = tape.size() - 1; i >= 0; --i) { + auto& a = tape[i]; + int src = groups.find(a); + if (src < 0) { + continue; + } + + // Find the groups that read the group of `a` + std::vector readers; + bool all_fusable = true; + for (auto& m : groups.members[src]) { + auto it = parents_map.find(m.id()); + if (it == parents_map.end()) { + continue; + } + for (auto& [p, idx] : it->second) { + int dst = groups.find(p); + if (dst < 0) { + all_fusable = false; + } else if ( + dst != src && + std::find(readers.begin(), readers.end(), dst) == readers.end()) { + readers.push_back(dst); + } + } + } + + // Split a broadcast with readers in more than one place, so that each + // group can fuse its own copy + if (is_broadcast(a.primitive()) && groups.members[src].size() == 1 && + (readers.size() > 1 || (readers.size() == 1 && !all_fusable))) { + for (int dst : readers) { + changed |= split_broadcast(a, dst, groups, parents_map, output_map); + } + continue; + } + + if (!all_fusable || readers.size() != 1) { + continue; + } + int dst = readers[0]; + if (a.primitive().stream() != groups.members[dst][0].primitive().stream()) { + continue; + } + int n = groups.members[dst].size(); + groups.merge(dst, src); + if (is_valid_group(dst, groups, parents_map, output_map)) { + changed = true; + } else { + groups.unmerge(dst, src, n); + } + } + return changed; +} + +// True if a path through arrays outside of groups `g1` and `g2` connects them. +// The two groups cannot be one kernel then. +bool is_connected( + int g1, + int g2, + const FuseGroups& groups, + const std::unordered_map& position, + const ParentsMap& parents_map) { + auto in_groups = [&](const array& a) { + int g = groups.find(a); + return g == g1 || g == g2; + }; + + // Readers come after their inputs in the tape, so stop the search after the + // last array of the two groups + int last = 0; + for (int g : {g1, g2}) { + for (auto& a : groups.members[g]) { + if (auto it = position.find(a.id()); it != position.end()) { + last = std::max(last, it->second); + } + } + } + + // Start from the readers outside of the two groups + std::vector stack; + for (int g : {g1, g2}) { + for (auto& a : groups.members[g]) { + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + if (!in_groups(p)) { + stack.push_back(p); + } + } + } + } + } + + std::unordered_set visited; + while (!stack.empty()) { + auto a = std::move(stack.back()); + stack.pop_back(); + if (in_groups(a)) { + return true; + } + if (!visited.insert(a.id()).second) { + continue; + } + if (auto it = position.find(a.id()); + it != position.end() && it->second > last) { + continue; + } + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + stack.push_back(p); + } + } + } + return false; +} + +// Fuse two groups which read the same arrays, if no path through other arrays +// connects them and they run at about the same time in the eval order. Return +// true if a group changed. +bool compile_fuse_horizontal( + FuseGroups& groups, + const std::vector& tape, + const std::unordered_map& position, + ParentsMap& parents_map, + const std::unordered_map& output_map) { + // Score each pair of groups with the bytes of the arrays they both read + std::map, size_t> scores; + for (auto& x : tape) { + // Scalars become constants in the kernel, so they are not read + if (x.size() == 1) { + continue; + } + auto it = parents_map.find(x.id()); + if (it == parents_map.end()) { + continue; + } + std::vector readers; + for (auto& [p, idx] : it->second) { + int g = groups.find(p); + if (g >= 0 && g != groups.find(x) && + std::find(readers.begin(), readers.end(), g) == readers.end()) { + readers.push_back(g); + } + } + for (int i = 0; i < readers.size(); ++i) { + for (int j = i + 1; j < readers.size(); ++j) { + auto key = std::minmax(readers[i], readers[j]); + scores[{key.first, key.second}] += x.nbytes(); + } + } + } + + // Eval runs an array as late as possible, just before its readers. So the + // level of an array, its longest path to an output, sets when it runs. + std::unordered_map levels; + std::function level; + level = [&](const array& a) -> int { + if (auto it = levels.find(a.id()); it != levels.end()) { + return it->second; + } + int l = 0; + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + l = std::max(l, level(p) + 1); + } + } + levels.insert({a.id(), l}); + return l; + }; + + // A group runs at the level of its deepest output + auto group_level = [&](int g) { + int l = 0; + for (auto& a : groups.members[g]) { + if (is_group_output(a, groups, parents_map, output_map)) { + l = std::max(l, level(a)); + } + } + return l; + }; + auto distance = [&](int g1, int g2) { + return std::abs(group_level(g1) - group_level(g2)); + }; + auto is_used = [&](int g) { + for (auto& a : groups.members[g]) { + if (is_group_output(a, groups, parents_map, output_map)) { + return true; + } + } + return false; + }; + + // Try the pairs with the most shared bytes first, then the closest pairs. + // Keep one array of each group, since groups change when they merge. + struct Candidate { + size_t score; + int distance; + array a; + array b; + }; + std::vector candidates; + for (auto& [key, score] : scores) { + auto [g1, g2] = key; + candidates.push_back( + {score, + distance(g1, g2), + groups.members[g1][0], + groups.members[g2][0]}); + } + std::sort(candidates.begin(), candidates.end(), [](auto& x, auto& y) { + return (x.score != y.score) ? x.score > y.score : x.distance < y.distance; + }); + + // Merging groups at different levels moves a kernel in the eval order, and + // this can keep large arrays longer in memory + bool changed = false; + for (auto& c : candidates) { + int g1 = groups.find(c.a); + int g2 = groups.find(c.b); + if (g1 == g2 || c.a.primitive().stream() != c.b.primitive().stream() || + distance(g1, g2) > max_fuse_distance || !is_used(g1) || !is_used(g2) || + is_connected(g1, g2, groups, position, parents_map)) { + continue; + } + int size = groups.members[g1].size(); + groups.merge(g1, g2); + if (is_valid_group(g1, groups, parents_map, output_map)) { + changed = true; + } else { + groups.unmerge(g1, g2, size); + } + } + return changed; +} + +// Fuse arrays in groups with the fusion rules, then replace each group with +// one Compiled primitive. +void compile_fuse( + std::vector& tape, + ParentsMap& parents_map, + const std::vector& inputs, + std::vector& outputs) { + // Track outputs to replace with new compiled outputs + std::unordered_map output_map; + for (auto& o : outputs) { + output_map.insert({o.id(), o}); + } + + // Set of inputs to distinguish constants + std::unordered_set input_ids; + for (auto& in : inputs) { + input_ids.insert(in.id()); + } + + // Start with one group for each fusable array + FuseGroups groups; + std::unordered_map position; + for (int i = 0; i < tape.size(); ++i) { + auto& a = tape[i]; + position.insert({a.id(), i}); + if (a.has_primitive() && is_fusable(a.primitive()) && + input_ids.find(a.id()) == input_ids.end()) { + groups.make_group(a); + } + } + + // Merge groups until no rule changes them. A merge can make a new merge + // possible, so repeat the rules. + while (compile_fuse_vertical(groups, tape, parents_map, output_map) || + compile_fuse_horizontal( + groups, tape, position, parents_map, output_map)) { + } + + // Replace each group of more than one array with a Compiled primitive + for (int g = 0; g < groups.members.size(); ++g) { + auto& group = groups.members[g]; + if (group.size() <= 1) { + continue; + } + + // The roots are the arrays which are not read in the group + std::vector roots; + for (auto& a : group) { + bool is_root = true; + if (auto it = parents_map.find(a.id()); it != parents_map.end()) { + for (auto& [p, idx] : it->second) { + if (groups.find(p) == g) { + is_root = false; + break; + } + } + } + if (is_root) { + roots.push_back(a); + } + } + std::sort(roots.begin(), roots.end(), [&](auto& a, auto& b) { + return position.at(a.id()) < position.at(b.id()); + }); + + // Build the tape of the group in the right order and collect the inputs + std::vector inputs; + std::vector fused_tape; + std::unordered_set input_set; + std::unordered_set tape_set; + std::function recurse_tape; + recurse_tape = [&](const array& a) { + if (groups.find(a) != g) { + if (input_set.find(a.id()) == input_set.end()) { + input_set.insert(a.id()); + inputs.push_back(a); + } + return; + } + if (tape_set.find(a.id()) != tape_set.end()) { + return; + } + tape_set.insert(a.id()); + for (auto& in : a.inputs()) { + recurse_tape(in); + } + fused_tape.push_back(a); + }; + for (auto& r : roots) { + recurse_tape(r); + } + + std::vector old_outputs; + for (auto& f : fused_tape) { + if (is_group_output(f, groups, parents_map, output_map)) { + old_outputs.push_back(f); + } + } + + // Nothing reads the group, so it is not used + if (old_outputs.empty()) { + continue; + } + make_compiled( + std::move(fused_tape), + inputs, + std::move(old_outputs), + input_ids, + output_map, + parents_map); + } + + // Build the new tape in execution order + std::vector new_tape; + std::unordered_set cache; + std::function recurse; + recurse = [&](const array& a) { + if (cache.find(a.id()) != cache.end()) { + return; + } + // Don't recurse on inputs + if (input_ids.find(a.id()) == input_ids.end()) { + for (auto& in : a.inputs()) { + recurse(in); + } + } + cache.insert(a.id()); + for (auto& s : a.siblings()) { + cache.insert(s.id()); + } + new_tape.push_back(a); + }; + for (auto& o : outputs) { + o = output_map.at(o.id()); + recurse(o); + } + tape = std::move(new_tape); +} + +} // namespace detail + +} // namespace mlx::core diff --git a/mlx/compile/recompute.cpp b/mlx/compile/recompute.cpp new file mode 100644 index 0000000000..a0e69ff3d6 --- /dev/null +++ b/mlx/compile/recompute.cpp @@ -0,0 +1,349 @@ +// Copyright © 2023-2024 Apple Inc. + +#include +#include +#include +#include +#include +#include + +#include "mlx/compile_impl.h" +#include "mlx/primitives.h" + +namespace mlx::core::detail { + +// Max-flow solver (Dinic) for compile_recompute. +class MaxFlow { + public: + static constexpr int64_t inf = std::numeric_limits::max() / 4; + + explicit MaxFlow(int n) : adj_(n), level_(n), next_(n) {} + + void add_edge(int u, int v, int64_t cap) { + adj_[u].push_back(edges_.size()); + edges_.push_back({v, cap}); + adj_[v].push_back(edges_.size()); + edges_.push_back({u, 0}); + } + + // Returns the max flow, or inf if there is no finite cut. + int64_t solve(int s, int t) { + int64_t flow = 0; + while (make_levels(s, t)) { + std::fill(next_.begin(), next_.end(), 0); + while (int64_t f = augment(s, t)) { + flow += f; + if (flow >= inf) { + return inf; + } + } + } + return flow; + } + + // Vertices that s still reaches in the residual graph. + std::vector source_side(int s) const { + std::vector seen(adj_.size(), false); + std::vector stack = {s}; + seen[s] = true; + while (!stack.empty()) { + int u = stack.back(); + stack.pop_back(); + for (int e : adj_[u]) { + int v = edges_[e].to; + if (edges_[e].cap > 0 && !seen[v]) { + seen[v] = true; + stack.push_back(v); + } + } + } + return seen; + } + + private: + struct Edge { + int to; + int64_t cap; + }; + + bool make_levels(int s, int t) { + std::fill(level_.begin(), level_.end(), -1); + std::queue queue; + level_[s] = 0; + queue.push(s); + while (!queue.empty()) { + int u = queue.front(); + queue.pop(); + for (int e : adj_[u]) { + int v = edges_[e].to; + if (edges_[e].cap > 0 && level_[v] < 0) { + level_[v] = level_[u] + 1; + queue.push(v); + } + } + } + return level_[t] >= 0; + } + + // Push flow along one path of the level graph. A stack is used instead of + // recursion because compiled graphs can be deep. + int64_t augment(int s, int t) { + std::vector path; + int u = s; + while (u != t) { + bool moved = false; + for (; next_[u] < adj_[u].size(); ++next_[u]) { + int e = adj_[u][next_[u]]; + int v = edges_[e].to; + if (edges_[e].cap > 0 && level_[v] == level_[u] + 1) { + path.push_back(e); + u = v; + moved = true; + break; + } + } + if (!moved) { + if (path.empty()) { + return 0; + } + // Dead end: remove u from the level graph and step back. + level_[u] = -1; + u = edges_[path.back() ^ 1].to; + path.pop_back(); + } + } + int64_t f = inf; + for (int e : path) { + f = std::min(f, edges_[e].cap); + } + for (int e : path) { + edges_[e].cap -= f; + edges_[e ^ 1].cap += f; + } + return f; + } + + std::vector edges_; + std::vector> adj_; + std::vector level_; + std::vector next_; +}; + +void compile_recompute(std::vector& tape, ParentsMap& parents_map) { + int n = tape.size(); + std::unordered_map vid; + for (int i = 0; i < n; ++i) { + vid.insert({tape[i].id(), i}); + for (auto& s : tape[i].siblings()) { + vid.insert({s.id(), i}); + } + } + + // Backward nodes are made by autodiff, or computed from such nodes, like an + // optimizer update. + std::vector backward(n, false); + bool has_backward = false; + for (int i = 0; i < n; ++i) { + auto& a = tape[i]; + if (!a.has_primitive()) { + continue; + } + bool b = a.is_backward(); + for (auto& in : a.inputs()) { + b = b || backward[vid.at(in.id())]; + } + backward[i] = b; + has_backward |= b; + } + if (!has_backward) { + return; + } + + auto readers = [&](int i) { + std::vector rs; + for (auto& o : tape[i].outputs()) { + if (auto it = parents_map.find(o.id()); it != parents_map.end()) { + for (auto& [p, _] : it->second) { + if (auto r = vid.find(p.id()); r != vid.end()) { + rs.push_back(r->second); + } + } + } + } + return rs; + }; + auto fusable = [&](int i) { + return tape[i].has_primitive() && is_fusable(tape[i].primitive()); + }; + auto is_view = [&](int i) { + if (!tape[i].has_primitive()) { + return false; + } + auto& p = tape[i].primitive(); + return typeid(p) == typeid(Broadcast) || typeid(p) == typeid(Reshape) || + typeid(p) == typeid(Transpose) || typeid(p) == typeid(ExpandDims) || + typeid(p) == typeid(Squeeze); + }; + + // True if the backward reads node i, directly or through views + std::function backward_reads; + backward_reads = [&](int i) -> bool { + for (int r : readers(i)) { + if (backward[r] || (is_view(r) && backward_reads(r))) { + return true; + } + } + return false; + }; + + // True if the backward reads node i only with fusable ops and not through a + // view. After a transpose or a reshape the copy does not fuse, for example a + // weight cast read by a matmul. After a broadcast the copy runs once for each + // element, for example a scalar learning rate in each optimizer update. + auto fuses_into_backward = [&](int i) { + for (int r : readers(i)) { + if (is_view(r) ? (backward[r] || backward_reads(r)) + : (backward[r] && !fusable(r))) { + return false; + } + } + return true; + }; + + MaxFlow graph(2 * n + 2); + int source = 2 * n; + int sink = 2 * n + 1; + for (int i = 0; i < n; ++i) { + auto& a = tape[i]; + int in = 2 * i; + int out = 2 * i + 1; + if (backward[i]) { + graph.add_edge(in, sink, MaxFlow::inf); + continue; + } + auto rs = readers(i); + for (int r : rs) { + graph.add_edge(out, 2 * r, MaxFlow::inf); + } + if (!a.has_primitive()) { + // Inputs and constants stay alive anyway, so keeping them is free. + graph.add_edge(source, in, MaxFlow::inf); + graph.add_edge(in, out, 0); + continue; + } + if (is_view(i)) { + // Views are free to recompute, so they are never kept. + graph.add_edge(in, out, MaxFlow::inf); + continue; + } + + // Keeping a value costs its size. The cost doubles when a fused kernel + // would otherwise keep the value in registers. + bool in_registers = + fusable(i) && !rs.empty() && std::all_of(rs.begin(), rs.end(), fusable); + int64_t bytes = 0; + for (auto& o : a.outputs()) { + bytes += o.nbytes(); + } + graph.add_edge(in, out, in_registers ? 2 * bytes : bytes); + + // Only cheap elementwise ops are recomputed, and only when the backward + // can fuse them. Other ops must run in the forward. + bool recompute = + fusable(i) && a.siblings().empty() && fuses_into_backward(i); + if (!recompute) { + graph.add_edge(source, in, MaxFlow::inf); + } + } + + auto kept_bytes = graph.solve(source, sink); + if (kept_bytes >= MaxFlow::inf) { + return; + } + auto source_side = graph.source_side(source); + + // The value the backward reads for `x`: x itself if it is kept, otherwise a + // copy computed from kept values. + std::unordered_map copies; + std::vector new_nodes; + std::function backward_value; + backward_value = [&](const array& x) -> array { + int j = vid.at(x.id()); + if (backward[j] || source_side[2 * j]) { + return x; + } + if (auto it = copies.find(j); it != copies.end()) { + return it->second; + } + auto& a = tape[j]; + std::vector inputs; + for (auto& in : a.inputs()) { + inputs.push_back(backward_value(in)); + } + array c(a.shape(), a.dtype(), a.primitive_ptr(), std::move(inputs)); + c.set_backward(true); + for (int k = 0; k < c.inputs().size(); ++k) { + parents_map[c.inputs()[k].id()].push_back({c, k}); + } + copies.insert({j, c}); + new_nodes.push_back(c); + return c; + }; + + // Point the backward readers at the copies. + for (int i = 0; i < n; ++i) { + if (!backward[i]) { + continue; + } + // Give each backward node its own copies, so each kernel can fuse them. + copies.clear(); + for (int k = 0; k < tape[i].inputs().size(); ++k) { + auto x = tape[i].inputs()[k]; + auto c = backward_value(x); + if (c.id() == x.id()) { + continue; + } + auto& from = parents_map[x.id()]; + auto& to = parents_map[c.id()]; + for (auto it = from.begin(); it != from.end();) { + if (it->second == k && vid.at(it->first.id()) == i) { + it->first.inputs()[k] = c; + to.push_back(std::move(*it)); + it = from.erase(it); + } else { + ++it; + } + } + } + } + + if (new_nodes.empty()) { + return; + } + + // Put each copy in the tape before the first node that reads it. + std::unordered_map copy_of_id; + for (auto& c : new_nodes) { + copy_of_id.insert({c.id(), c}); + } + std::vector new_tape; + new_tape.reserve(tape.size() + new_nodes.size()); + std::unordered_set placed; + std::function place_inputs; + place_inputs = [&](const array& a) { + for (auto& in : a.inputs()) { + auto it = copy_of_id.find(in.id()); + if (it != copy_of_id.end() && placed.insert(in.id()).second) { + place_inputs(it->second); + new_tape.push_back(it->second); + } + } + }; + for (auto& a : tape) { + place_inputs(a); + new_tape.push_back(a); + } + tape = std::move(new_tape); +} + +} // namespace mlx::core::detail diff --git a/mlx/compile/simplify.cpp b/mlx/compile/simplify.cpp new file mode 100644 index 0000000000..d1cc47c201 --- /dev/null +++ b/mlx/compile/simplify.cpp @@ -0,0 +1,280 @@ +// Copyright © 2023-2024 Apple Inc. + +#include +#include +#include + +#include "mlx/compile_impl.h" +#include "mlx/primitives.h" +#include "mlx/utils.h" + +namespace mlx::core::detail { + +// Helper like below but only merges the two provided arrays. If the src has +// siblings then these won't be merged to the dst. +void merge_one(array& dst, array& src, ParentsMap& parents_map) { + auto src_parents = parents_map.find(src.id()); + if (src_parents == parents_map.end()) { + return; + } + auto& pairs = parents_map[dst.id()]; + for (auto& parent : src_parents->second) { + parent.first.inputs()[parent.second] = dst; + pairs.push_back(parent); + } + + // If src is a parent of dst, remove it from dst's parents + for (auto it = pairs.begin(); it != pairs.end();) { + if (it->first.id() == src.id()) { + it = pairs.erase(it); + } else { + it++; + } + } + // Remove the source from the map to avoid fusing with it again + parents_map.erase(src_parents); +} + +// Helper that merges two arrays in the graph by setting the parents of the +// source to point to the destination. The arrays are assumed to be coming from +// equivalent primitives so their siblings are merged as well. +void merge(array& dst, array& src, ParentsMap& parents_map) { + // Canonicalize the order of the primitives outputs + auto sources = src.outputs(); + auto dests = dst.outputs(); + // For each src parent, point it to the corresponding dst + for (int i = 0; i < sources.size(); ++i) { + merge_one(dests[i], sources[i], parents_map); + } +} + +static inline uint64_t splitmix64(uint64_t x) noexcept { + x += 0x9e3779b97f4a7c15ull; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ull; + x = (x ^ (x >> 27)) * 0x94d049bb133111ebull; + return x ^ (x >> 31); +} + +struct VecU64Hash { + size_t operator()(const std::vector& s) const noexcept { + uint64_t h = + 0x243f6a8885a308d3ull ^ (uint64_t)s.size() * 0x9e3779b97f4a7c15ull; + for (uint64_t x : s) { + h = splitmix64(x ^ splitmix64(h + 0x9e3779b97f4a7c15ull)); + } + return (size_t)h; + } +}; + +// Simplify the tape. Note, this function modifies in-place both the tape, +// the parents map to remove orphaned arrays, and potentially the outputs +void compile_simplify( + std::vector& tape, + ParentsMap& parents_map, + std::vector& outputs, + int passes) { + // Helpers to identify identical scalars + std::map, array> scalars; + auto is_scalar = [](const array& a) { + // Condition for when it's safe to read an array + return a.is_available() && a.ndim() == 0; + }; + auto get_scalar_rep = [](const array& a) { + uint64_t v = 0; + switch (a.dtype().size()) { + case 1: + v = *a.data(); + break; + case 2: + v = *a.data(); + break; + case 4: + v = *a.data(); + break; + case 8: + v = *a.data(); + break; + } + return std::make_pair(v, a.dtype().val()); + }; + + for (auto& a : tape) { + if (is_scalar(a)) { + scalars.insert({get_scalar_rep(a), a}); + } + } + + // Depth-1 array equivalence check. + auto array_equivalent = [](const array& a, const array& b) { + if (!a.has_primitive() || !b.has_primitive()) { + return false; + } + if (a.primitive_id() == b.primitive_id()) { + return false; + } + const auto& pa = a.primitive(); + const auto& pb = b.primitive(); + if (typeid(pa) != typeid(pb)) { + return false; + } + + if (a.inputs().size() != b.inputs().size()) { + return false; + } + + for (int i = 0; i < a.inputs().size(); i++) { + if (a.inputs()[i].id() != b.inputs()[i].id()) { + return false; + } + } + + return pa.is_equivalent(pb); + }; + + std::unordered_map output_map; + for (auto& o : outputs) { + output_map.insert({o.id(), o}); + } + + // Merge scalars, except outputs + std::vector new_tape; + for (auto& arr : tape) { + // Check if we can merge scalars + if (is_scalar(arr) && output_map.find(arr.id()) == output_map.end()) { + auto scalar = scalars.find(get_scalar_rep(arr)); + if (scalar->second.id() != arr.id()) { + merge(scalar->second, arr, parents_map); + // Don't keep orphaned scalars in the tape + continue; + } + } + new_tape.push_back(std::move(arr)); + } + tape = std::move(new_tape); + + // Remove no-ops + { + for (auto& arr : tape) { + if (!arr.has_primitive() || !is_noop(arr.primitive())) { + new_tape.push_back(std::move(arr)); + continue; + } + merge_one(arr.inputs()[0], arr, parents_map); + if (auto it = output_map.find(arr.id()); it != output_map.end()) { + it->second = arr.inputs()[0]; + } + } + tape = std::move(new_tape); + for (auto& o : outputs) { + o = output_map.at(o.id()); + } + } + + std::unordered_map tape_order; + for (uint32_t i = 0; i < tape.size(); ++i) { + tape_order.insert({tape[i].id(), i}); + } + + // Multi-pass merge only keeping non-orphaned arrays in the tape + for (int pass = 0; pass < passes; ++pass) { + for (auto& arr : tape) { + // Helper to check if we can merge the parents of the + // given array + auto maybe_merge_parents = [&](auto& a) { + auto parents = parents_map.find(a.id()); + if (parents != parents_map.end()) { + auto N = parents->second.size(); + std::vector mask(N, false); + + auto try_merge = [&](int dst_idx, int src_idx) { + if (tape_order[parents->second[src_idx].first.id()] < + tape_order[parents->second[dst_idx].first.id()]) { + std::swap(src_idx, dst_idx); + } + auto& src = parents->second[src_idx].first; + auto& dst = parents->second[dst_idx].first; + if (src.id() != dst.id() && array_equivalent(src, dst) && + output_map.find(src.id()) == output_map.end()) { + merge(dst, src, parents_map); + mask[src_idx] = true; + } + }; + + if (N > 100) { + std::unordered_map< + std::vector, + std::vector, + VecU64Hash> + dst_map; + // Find possibly mergeable groups + for (int i = 0; i < N; i++) { + // Make the hash key + std::vector key; + auto& curr = parents->second[i].first; + key.reserve(curr.inputs().size() + 2); + for (auto& in : curr.inputs()) { + key.push_back(in.id()); + } + auto& p = curr.primitive(); + key.push_back(curr.inputs().size()); + key.push_back(typeid(p).hash_code()); + auto it = dst_map.find(key); + if (it == dst_map.end()) { + bool _; + std::tie(it, _) = dst_map.insert({key, std::vector{}}); + } + it->second.push_back(i); + } + for (auto& [_, group] : dst_map) { + for (int i = 0; i < group.size(); ++i) { + if (mask[group[i]]) { + continue; + } + for (int j = i + 1; j < group.size(); ++j) { + if (mask[group[j]]) { + continue; + } + try_merge(group[i], group[j]); + } + } + } + } else { + for (int i = 0; i < N; ++i) { + if (mask[i]) { + continue; + } + for (int j = i + 1; j < N; ++j) { + if (mask[j]) { + continue; + } + try_merge(i, j); + } + } + } + + // Erase orphaned parents so we don't keep fusing with them + for (int i = N - 1; i >= 0; --i) { + if (mask[i]) { + parents->second.erase(parents->second.begin() + i); + } + } + return false; + } else { + return output_map.find(a.id()) == output_map.end(); + } + }; + bool discard = maybe_merge_parents(arr); + for (auto& s : arr.siblings()) { + discard &= maybe_merge_parents(s); + } + // If an array and its siblings have no parents, and none of them are + // outputs, it is safe to remove it from the tape + if (!discard) { + new_tape.push_back(std::move(arr)); + } + } + tape = std::move(new_tape); + } +} + +} // namespace mlx::core::detail diff --git a/mlx/compile/trace.cpp b/mlx/compile/trace.cpp new file mode 100644 index 0000000000..e51800c91c --- /dev/null +++ b/mlx/compile/trace.cpp @@ -0,0 +1,233 @@ +// Copyright © 2023-2024 Apple Inc. + +#include +#include + +#include "mlx/compile_impl.h" +#include "mlx/primitives.h" +#include "mlx/transforms_impl.h" +#include "mlx/utils.h" + +namespace mlx::core::detail { + +std::tuple, std::vector, std::shared_ptr> +compile_trace( + const ArrayFnWithExtra& fun, + const std::vector& inputs, + bool shapeless) { + // Set the global tracing flag. + detail::InTracing in_tracing{shapeless}; + + // Run the function on placeholder inputs + // to get compute graph + std::vector tracer_inputs; + for (int i = 0; i < inputs.size(); ++i) { + array in(inputs[i].shape(), inputs[i].dtype(), nullptr, {}); + in.set_tracer(true); + tracer_inputs.push_back(std::move(in)); + } + + auto output = fun(tracer_inputs); + return {tracer_inputs, output.first, output.second}; +} + +// Traverses the graph to build a tape and a map of array ids to their parents +std::pair, ParentsMap> compile_dfs( + const std::vector& inputs, + std::vector& outputs, + const std::vector& original_inputs) { + std::vector tape; + std::unordered_map>> + parents_map; + { + std::function recurse; + std::unordered_set input_set; + std::unordered_set original_input_set; + for (int i = 0; i < inputs.size(); ++i) { + input_set.insert(inputs[i].id()); + original_input_set.insert(original_inputs[i].id()); + } + + // DFS the graph to build the tape, and log parents and scalars + std::unordered_set cache; + recurse = [&](const array& a) { + auto id = a.id(); + if (original_input_set.find(id) != original_input_set.end()) { + throw std::invalid_argument( + "[compile] Attempting to compile a function with uncaptured inputs is not allowed."); + } + if (cache.find(id) != cache.end()) { + return; + } + for (int i = 0; i < a.inputs().size(); i++) { + auto& in = a.inputs()[i]; + parents_map[in.id()].push_back({a, i}); + for (auto& s : a.siblings()) { + parents_map[in.id()].push_back({s, i}); + } + // Don't recurse on inputs (but add them to the tape for the purpose + // of future optimizations) + if (input_set.find(a.id()) == input_set.end()) { + recurse(in); + } + } + cache.insert(id); + for (auto& s : a.siblings()) { + cache.insert(s.id()); + } + tape.push_back(a); + }; + for (auto& a : outputs) { + recurse(a); + } + } + + // Deep copy the tape and parents map while preserving inputs and outputs + std::vector new_tape; + std::unordered_set io_set; + std::unordered_map old_to_new; + for (auto& o : outputs) { + old_to_new.insert({o.id(), o}); + io_set.insert(o.id()); + for (auto& s : o.siblings()) { + old_to_new.insert({s.id(), s}); + io_set.insert(s.id()); + } + } + for (auto& i : inputs) { + io_set.insert(i.id()); + old_to_new.insert({i.id(), i}); + } + + new_tape.reserve(tape.size()); + for (auto& arr : tape) { + if (!arr.has_primitive() || (io_set.find(arr.id()) != io_set.end())) { + old_to_new.insert({arr.id(), arr}); + new_tape.push_back(arr); + continue; + } + std::vector inputs; + inputs.reserve(arr.inputs().size()); + for (auto& i : arr.inputs()) { + inputs.push_back(old_to_new.find(i.id())->second); + } + if (arr.siblings().size() > 0) { + std::vector types; + std::vector shapes; + auto out = arr.outputs(); + for (auto& o : out) { + types.push_back(o.dtype()); + shapes.push_back(o.shape()); + } + auto as = array::make_arrays( + std::move(shapes), types, arr.primitive_ptr(), std::move(inputs)); + for (int i = 0; i < out.size(); ++i) { + as[i].set_backward(out[i].is_backward()); + old_to_new.insert({out[i].id(), as[i]}); + } + new_tape.push_back(as[arr.sibling_position()]); + } else { + auto a = array( + arr.shape(), arr.dtype(), arr.primitive_ptr(), std::move(inputs)); + a.set_backward(arr.is_backward()); + old_to_new.insert({arr.id(), a}); + new_tape.push_back(a); + } + } + io_set.clear(); + for (auto& o : outputs) { + if (!(io_set.insert(o.id()).second)) { + continue; + } + for (auto& i : o.inputs()) { + i = old_to_new.find(i.id())->second; + } + for (auto& s : o.siblings()) { + io_set.insert(s.id()); + for (auto& i : s.inputs()) { + i = old_to_new.find(i.id())->second; + } + } + } + tape = std::move(new_tape); + + std::unordered_map>> + new_parents_map; + for (auto& [id, vec] : parents_map) { + for (auto& [a, _] : vec) { + a = old_to_new.find(a.id())->second; + } + new_parents_map[old_to_new.find(id)->second.id()] = std::move(vec); + } + parents_map = std::move(new_parents_map); + return {tape, parents_map}; +} + +std::vector compile_replace( + const std::vector& tape, + const std::vector& trace_inputs, + const std::vector& trace_outputs, + const std::vector& inputs, + bool shapeless) { + std::unordered_map trace_to_real; + for (int i = 0; i < inputs.size(); ++i) { + trace_to_real.insert({trace_inputs[i].id(), inputs[i]}); + } + + auto is_load = [](const Primitive& p) { return typeid(p) == typeid(Load); }; + + for (auto& a : tape) { + // Arrays in the tape without primitives are either: + // - inputs, which are already in the map + // - constants, which can be used directly + // - a load primitive which has no inputs and will become a constant + // after the first eval + if (!a.has_primitive() || is_load(a.primitive())) { + trace_to_real.insert({a.id(), a}); + } else { + // Find real inputs + std::vector real_inputs; + for (auto& in : a.inputs()) { + real_inputs.push_back(trace_to_real.at(in.id())); + } + if (a.siblings().empty()) { + auto shape = + shapeless ? a.primitive().output_shapes(real_inputs)[0] : a.shape(); + auto real_a = array( + std::move(shape), + a.dtype(), + a.primitive_ptr(), + std::move(real_inputs)); + trace_to_real.insert({a.id(), std::move(real_a)}); + } else { + // Ensure the order is correct for multi-output primitives + std::vector types; + auto trace_out = a.outputs(); + for (auto& o : trace_out) { + types.push_back(o.dtype()); + } + std::vector shapes; + if (shapeless) { + shapes = a.primitive().output_shapes(real_inputs); + } else { + for (auto& o : trace_out) { + shapes.push_back(o.shape()); + } + } + auto real_out = array::make_arrays( + std::move(shapes), types, a.primitive_ptr(), real_inputs); + for (int i = 0; i < trace_out.size(); ++i) { + trace_to_real.insert({trace_out[i].id(), std::move(real_out[i])}); + } + } + } + } + + std::vector outputs; + for (auto& o : trace_outputs) { + outputs.push_back(trace_to_real.at(o.id())); + } + return outputs; +} + +} // namespace mlx::core::detail diff --git a/mlx/compile_impl.h b/mlx/compile_impl.h index 1afe07b931..e8f541b47a 100644 --- a/mlx/compile_impl.h +++ b/mlx/compile_impl.h @@ -3,6 +3,7 @@ #pragma once #include +#include #include "mlx/api.h" #include "mlx/array.h" @@ -74,4 +75,31 @@ std::vector compile_replace( void compile_validate_shapeless(const std::vector& tape); +// Kinds of primitives. Fusion uses them to decide what can be in a kernel. +bool is_unary(const Primitive& p); +bool is_binary(const Primitive& p); +bool is_ternary(const Primitive& p); +bool is_broadcast(const Primitive& p); +bool is_noop(const Primitive& p); +bool is_reduction(const Primitive& p); +bool is_fusable(const Primitive& p); + +// Helpers to edit the graph with the tape and the parents map +void merge_one(array& dst, array& src, ParentsMap& parents_map); +void merge(array& dst, array& src, ParentsMap& parents_map); +array split_one( + const array& x, + ParentsMap& parents_map, + const std::unordered_set& divider); + +// Recompute cheap forward values in the backward instead of keeping them +void compile_recompute(std::vector& tape, ParentsMap& parents_map); + +// Fuse arrays into Compiled primitives +void compile_fuse( + std::vector& tape, + ParentsMap& parents_map, + const std::vector& inputs, + std::vector& outputs); + } // namespace mlx::core::detail From 60afaaf74045d26fe3a49285154f4c2707039880 Mon Sep 17 00:00:00 2001 From: Anastasiia Filippova Date: Fri, 20 Nov 2026 17:36:54 +0100 Subject: [PATCH 4/8] refactoring, removing recompute --- mlx/array.cpp | 1 - mlx/array.h | 12 -- mlx/compile/CMakeLists.txt | 1 - mlx/compile/compile.cpp | 6 - mlx/compile/fuse.cpp | 238 +++++++++++++------------ mlx/compile/recompute.cpp | 349 ------------------------------------- mlx/compile/trace.cpp | 2 - mlx/compile_impl.h | 3 - mlx/transforms.cpp | 2 - mlx/transforms_impl.h | 16 -- 10 files changed, 129 insertions(+), 501 deletions(-) delete mode 100644 mlx/compile/recompute.cpp diff --git a/mlx/array.cpp b/mlx/array.cpp index ade99dad77..cf49a0fe5f 100644 --- a/mlx/array.cpp +++ b/mlx/array.cpp @@ -249,7 +249,6 @@ void array::ArrayDesc::init() { for (const auto& in : inputs) { is_tracer |= in.is_tracer(); } - is_backward = detail::InBackward::in_backward(); } array::ArrayDesc::ArrayDesc(Shape shape, Dtype dtype) diff --git a/mlx/array.h b/mlx/array.h index 37c13abd54..59ab941945 100644 --- a/mlx/array.h +++ b/mlx/array.h @@ -426,15 +426,6 @@ class MLX_API array { // Check if the array is a tracer array bool is_tracer() const; - // Mark the array as part of a backward graph (true) or not. - void set_backward(bool is_backward) { - array_desc_->is_backward = is_backward; - } - // Check if the array was made while autodiff built a backward graph - bool is_backward() const { - return array_desc_->is_backward; - } - void set_data(allocator::Data data); void set_data( @@ -482,9 +473,6 @@ class MLX_API array { // and should not be detached from the graph bool is_tracer{false}; - // Indicates an array was made while autodiff built a backward graph - bool is_backward{false}; - // This is a shared pointer so that *different* arrays // can share the underlying data buffer. std::shared_ptr data; diff --git a/mlx/compile/CMakeLists.txt b/mlx/compile/CMakeLists.txt index 9c5f4324fc..c3d0b4a053 100644 --- a/mlx/compile/CMakeLists.txt +++ b/mlx/compile/CMakeLists.txt @@ -3,5 +3,4 @@ target_sources( PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/compile.cpp ${CMAKE_CURRENT_SOURCE_DIR}/trace.cpp ${CMAKE_CURRENT_SOURCE_DIR}/simplify.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/recompute.cpp ${CMAKE_CURRENT_SOURCE_DIR}/fuse.cpp) diff --git a/mlx/compile/compile.cpp b/mlx/compile/compile.cpp index 9b83557ea3..7e3b3e75bd 100644 --- a/mlx/compile/compile.cpp +++ b/mlx/compile/compile.cpp @@ -352,12 +352,6 @@ ArrayFnWithExtra compile( entry.tape, parents_map, entry.outputs, /* passes */ 3); } - // Experimental: recompute cheap forward values in the backward - // instead of keeping them. Enable with MLX_COMPILE_RECOMPUTE=1. - if (config::get("MLX_COMPILE_RECOMPUTE", 0)) { - compile_recompute(entry.tape, parents_map); - } - // Kernel fusion to generate Compiled primitives. The tape and // new outputs must be updated accordingly if (mode != CompileMode::no_fuse) { diff --git a/mlx/compile/fuse.cpp b/mlx/compile/fuse.cpp index 38d32fb08c..b6c8f8b79c 100644 --- a/mlx/compile/fuse.cpp +++ b/mlx/compile/fuse.cpp @@ -8,15 +8,17 @@ #include #include "mlx/compile_impl.h" +#include "mlx/config.h" #include "mlx/primitives.h" -namespace mlx::core { +namespace mlx::core::detail { constexpr int max_compile_depth = 11; constexpr int max_compile_arrays = 24; -constexpr int max_fuse_distance = 0; -namespace detail { +inline int max_fuse_distance() { + return config::get("MLX_MAX_FUSE_DISTANCE", 0); +} bool is_unary(const Primitive& p) { return ( @@ -60,17 +62,7 @@ bool is_broadcast(const Primitive& p) { return typeid(p) == typeid(Broadcast); } -bool is_noop(const Primitive& p) { - return typeid(p) == typeid(Copy) || typeid(p) == typeid(StopGradient); -} - -bool is_reduction(const Primitive& p) { - return typeid(p) == typeid(Reduce) || typeid(p) == typeid(ArgReduce); -} - -bool is_fusable(const Primitive& p) { - return is_unary(p) || is_binary(p) || is_ternary(p) || is_broadcast(p); -} +namespace { // Any parent in the divider will continue to refer to `x` but any parent not // in the divider will refer to a copy of the operation. @@ -96,11 +88,49 @@ array split_one( return y; } -// Extract sub-graphs of the graph that can be compiled -// and replace them with a Compiled Primitive. +// The groups of arrays to fuse. Each group with more than one array becomes one +// Compiled primitive. +struct Groups { + std::vector> members; + std::unordered_map id_to_group; + + // The group of `a`, or -1 if `a` is not fusable + int find(const array& a) const { + auto it = id_to_group.find(a.id()); + return (it == id_to_group.end()) ? -1 : it->second; + } + + void add(const array& a, int g) { + id_to_group[a.id()] = g; + members[g].push_back(a); + } + + // Add a group + void make_group(const array& a) { + members.emplace_back(); + add(a, members.size() - 1); + } + + // Merge 2 groups + void merge(int dst, int src) { + for (auto& a : members[src]) { + add(a, dst); + } + members[src].clear(); + } + + // Undo merge(dst, src), where `n` is the size of `dst` before the merge + void unmerge(int dst, int src, int n) { + for (int i = n; i < members[dst].size(); ++i) { + add(members[dst][i], src); + } + members[dst].erase(members[dst].begin() + n, members[dst].end()); + } +}; + // Replace the arrays in `fused_tape` with one Compiled primitive. The arrays in // `old_outputs` become the outputs of the primitive. -std::vector make_compiled( +std::vector fuse_group( std::vector fused_tape, const std::vector& inputs, std::vector old_outputs, @@ -192,49 +222,10 @@ std::vector make_compiled( return compiled_outputs; } -// Groups of arrays to fuse. Each group with more than one array becomes one -// Compiled primitive. -struct FuseGroups { - std::vector> members; - std::unordered_map group_of; - - // The group of `a`, or -1 if `a` is not fusable - int find(const array& a) const { - auto it = group_of.find(a.id()); - return (it == group_of.end()) ? -1 : it->second; - } - - void add(const array& a, int g) { - group_of[a.id()] = g; - members[g].push_back(a); - } - - void make_group(const array& a) { - members.emplace_back(); - add(a, members.size() - 1); - } - - // Move the arrays of group `src` to group `dst` - void merge(int dst, int src) { - for (auto& a : members[src]) { - add(a, dst); - } - members[src].clear(); - } - - // Undo merge(dst, src), where `n` is the size of `dst` before the merge - void unmerge(int dst, int src, int n) { - for (int i = n; i < members[dst].size(); ++i) { - add(members[dst][i], src); - } - members[dst].erase(members[dst].begin() + n, members[dst].end()); - } -}; - // True if `a` is read outside of its group or is an output of the graph -bool is_group_output( +bool is_read_outside( const array& a, - const FuseGroups& groups, + const Groups& groups, const ParentsMap& parents_map, const std::unordered_map& output_map) { if (output_map.find(a.id()) != output_map.end()) { @@ -251,16 +242,30 @@ bool is_group_output( return false; } +// True if an array of group `g` is read outside of the group +bool has_outputs( + int g, + const Groups& groups, + const ParentsMap& parents_map, + const std::unordered_map& output_map) { + for (auto& a : groups.members[g]) { + if (is_read_outside(a, groups, parents_map, output_map)) { + return true; + } + } + return false; +} + // True if group `g` can become one Compiled primitive -bool is_valid_group( +bool is_fusable_group( int g, - const FuseGroups& groups, + const Groups& groups, const ParentsMap& parents_map, const std::unordered_map& output_map) { // All outputs have the same shape. Do not write out a broadcast. std::vector outputs; for (auto& a : groups.members[g]) { - if (!is_group_output(a, groups, parents_map, output_map)) { + if (!is_read_outside(a, groups, parents_map, output_map)) { continue; } if (is_broadcast(a.primitive()) && @@ -301,11 +306,11 @@ bool is_valid_group( } // Give group `g` its own copy of the broadcast `b` with split_one. Return -// true if the copy is fused into the group. +// true if the group keeps the copy. bool split_broadcast( array b, int g, - FuseGroups& groups, + Groups& groups, ParentsMap& parents_map, const std::unordered_map& output_map) { std::unordered_set divider; @@ -317,13 +322,13 @@ bool split_broadcast( parents_map[c.inputs()[i].id()].push_back({c, i}); } groups.add(c, g); - if (is_valid_group(g, groups, parents_map, output_map)) { + if (is_fusable_group(g, groups, parents_map, output_map)) { return true; } // Undo the copy groups.members[g].pop_back(); - groups.group_of.erase(c.id()); + groups.id_to_group.erase(c.id()); for (auto& in : c.inputs()) { auto& pairs = parents_map[in.id()]; pairs.erase( @@ -337,16 +342,16 @@ bool split_broadcast( return false; } -// Fuse a group into the group that reads it, if all the readers of the group +// Merge a group into the group that reads it, if all the readers of the group // are in that group. Return true if a group changed. -bool compile_fuse_vertical( - FuseGroups& groups, +bool merge_vertical( + Groups& groups, const std::vector& tape, ParentsMap& parents_map, const std::unordered_map& output_map) { bool changed = false; - // Go through the tape in reverse order so readers are fused first + // Go through the tape in reverse order so readers are merged first for (int i = tape.size() - 1; i >= 0; --i) { auto& a = tape[i]; int src = groups.find(a); @@ -356,7 +361,7 @@ bool compile_fuse_vertical( // Find the groups that read the group of `a` std::vector readers; - bool all_fusable = true; + bool all_readers_fusable = true; for (auto& m : groups.members[src]) { auto it = parents_map.find(m.id()); if (it == parents_map.end()) { @@ -365,7 +370,7 @@ bool compile_fuse_vertical( for (auto& [p, idx] : it->second) { int dst = groups.find(p); if (dst < 0) { - all_fusable = false; + all_readers_fusable = false; } else if ( dst != src && std::find(readers.begin(), readers.end(), dst) == readers.end()) { @@ -377,14 +382,14 @@ bool compile_fuse_vertical( // Split a broadcast with readers in more than one place, so that each // group can fuse its own copy if (is_broadcast(a.primitive()) && groups.members[src].size() == 1 && - (readers.size() > 1 || (readers.size() == 1 && !all_fusable))) { + (readers.size() > 1 || (readers.size() == 1 && !all_readers_fusable))) { for (int dst : readers) { changed |= split_broadcast(a, dst, groups, parents_map, output_map); } continue; } - if (!all_fusable || readers.size() != 1) { + if (!all_readers_fusable || readers.size() != 1) { continue; } int dst = readers[0]; @@ -393,7 +398,7 @@ bool compile_fuse_vertical( } int n = groups.members[dst].size(); groups.merge(dst, src); - if (is_valid_group(dst, groups, parents_map, output_map)) { + if (is_fusable_group(dst, groups, parents_map, output_map)) { changed = true; } else { groups.unmerge(dst, src, n); @@ -407,7 +412,7 @@ bool compile_fuse_vertical( bool is_connected( int g1, int g2, - const FuseGroups& groups, + const Groups& groups, const std::unordered_map& position, const ParentsMap& parents_map) { auto in_groups = [&](const array& a) { @@ -463,16 +468,12 @@ bool is_connected( return false; } -// Fuse two groups which read the same arrays, if no path through other arrays -// connects them and they run at about the same time in the eval order. Return -// true if a group changed. -bool compile_fuse_horizontal( - FuseGroups& groups, +// Score each pair of groups with the bytes of the arrays they both read. Pairs +// which read no array together are not in the result. +std::map, size_t> score_pairs( + const Groups& groups, const std::vector& tape, - const std::unordered_map& position, - ParentsMap& parents_map, - const std::unordered_map& output_map) { - // Score each pair of groups with the bytes of the arrays they both read + const ParentsMap& parents_map) { std::map, size_t> scores; for (auto& x : tape) { // Scalars become constants in the kernel, so they are not read @@ -498,11 +499,27 @@ bool compile_fuse_horizontal( } } } + return scores; +} + +// Merge groups which read the same arrays, if no path through other arrays +// connects them and they run at about the same time in the eval order. Return +// true if a group changed. +bool merge_horizontal( + Groups& groups, + const std::vector& tape, + const std::unordered_map& position, + ParentsMap& parents_map, + const std::unordered_map& output_map) { + // Given a tape and groups of array for fusion, for every pair of groups + // (i, j) count the number of bytes it will save in i and j are fused + auto scores = score_pairs(groups, tape, parents_map); // Eval runs an array as late as possible, just before its readers. So the // level of an array, its longest path to an output, sets when it runs. std::unordered_map levels; std::function level; + level = [&](const array& a) -> int { if (auto it = levels.find(a.id()); it != levels.end()) { return it->second; @@ -521,7 +538,7 @@ bool compile_fuse_horizontal( auto group_level = [&](int g) { int l = 0; for (auto& a : groups.members[g]) { - if (is_group_output(a, groups, parents_map, output_map)) { + if (is_read_outside(a, groups, parents_map, output_map)) { l = std::max(l, level(a)); } } @@ -530,17 +547,10 @@ bool compile_fuse_horizontal( auto distance = [&](int g1, int g2) { return std::abs(group_level(g1) - group_level(g2)); }; - auto is_used = [&](int g) { - for (auto& a : groups.members[g]) { - if (is_group_output(a, groups, parents_map, output_map)) { - return true; - } - } - return false; - }; - // Try the pairs with the most shared bytes first, then the closest pairs. - // Keep one array of each group, since groups change when they merge. + // Helper structure to organise horizontal merge proposals + // we sort candidates by the score and if score matches priorities + // closest group. struct Candidate { size_t score; int distance; @@ -561,19 +571,22 @@ bool compile_fuse_horizontal( }); // Merging groups at different levels moves a kernel in the eval order, and - // this can keep large arrays longer in memory + // this can keep large arrays longer in memory. Control it with + // max_fuse_distance bool changed = false; for (auto& c : candidates) { int g1 = groups.find(c.a); int g2 = groups.find(c.b); if (g1 == g2 || c.a.primitive().stream() != c.b.primitive().stream() || - distance(g1, g2) > max_fuse_distance || !is_used(g1) || !is_used(g2) || + distance(g1, g2) > max_fuse_distance() || + !has_outputs(g1, groups, parents_map, output_map) || + !has_outputs(g2, groups, parents_map, output_map) || is_connected(g1, g2, groups, position, parents_map)) { continue; } int size = groups.members[g1].size(); groups.merge(g1, g2); - if (is_valid_group(g1, groups, parents_map, output_map)) { + if (is_fusable_group(g1, groups, parents_map, output_map)) { changed = true; } else { groups.unmerge(g1, g2, size); @@ -582,8 +595,18 @@ bool compile_fuse_horizontal( return changed; } -// Fuse arrays in groups with the fusion rules, then replace each group with -// one Compiled primitive. +} // namespace + +bool is_noop(const Primitive& p) { + return typeid(p) == typeid(Copy) || typeid(p) == typeid(StopGradient); +} + +bool is_fusable(const Primitive& p) { + return is_unary(p) || is_binary(p) || is_ternary(p) || is_broadcast(p); +} + +// Merge the arrays into groups with the rules, then fuse each group into one +// Compiled primitive. void compile_fuse( std::vector& tape, ParentsMap& parents_map, @@ -602,7 +625,7 @@ void compile_fuse( } // Start with one group for each fusable array - FuseGroups groups; + Groups groups; std::unordered_map position; for (int i = 0; i < tape.size(); ++i) { auto& a = tape[i]; @@ -615,9 +638,8 @@ void compile_fuse( // Merge groups until no rule changes them. A merge can make a new merge // possible, so repeat the rules. - while (compile_fuse_vertical(groups, tape, parents_map, output_map) || - compile_fuse_horizontal( - groups, tape, position, parents_map, output_map)) { + while (merge_vertical(groups, tape, parents_map, output_map) || + merge_horizontal(groups, tape, position, parents_map, output_map)) { } // Replace each group of more than one array with a Compiled primitive @@ -676,7 +698,7 @@ void compile_fuse( std::vector old_outputs; for (auto& f : fused_tape) { - if (is_group_output(f, groups, parents_map, output_map)) { + if (is_read_outside(f, groups, parents_map, output_map)) { old_outputs.push_back(f); } } @@ -685,7 +707,7 @@ void compile_fuse( if (old_outputs.empty()) { continue; } - make_compiled( + fuse_group( std::move(fused_tape), inputs, std::move(old_outputs), @@ -721,6 +743,4 @@ void compile_fuse( tape = std::move(new_tape); } -} // namespace detail - -} // namespace mlx::core +} // namespace mlx::core::detail diff --git a/mlx/compile/recompute.cpp b/mlx/compile/recompute.cpp deleted file mode 100644 index a0e69ff3d6..0000000000 --- a/mlx/compile/recompute.cpp +++ /dev/null @@ -1,349 +0,0 @@ -// Copyright © 2023-2024 Apple Inc. - -#include -#include -#include -#include -#include -#include - -#include "mlx/compile_impl.h" -#include "mlx/primitives.h" - -namespace mlx::core::detail { - -// Max-flow solver (Dinic) for compile_recompute. -class MaxFlow { - public: - static constexpr int64_t inf = std::numeric_limits::max() / 4; - - explicit MaxFlow(int n) : adj_(n), level_(n), next_(n) {} - - void add_edge(int u, int v, int64_t cap) { - adj_[u].push_back(edges_.size()); - edges_.push_back({v, cap}); - adj_[v].push_back(edges_.size()); - edges_.push_back({u, 0}); - } - - // Returns the max flow, or inf if there is no finite cut. - int64_t solve(int s, int t) { - int64_t flow = 0; - while (make_levels(s, t)) { - std::fill(next_.begin(), next_.end(), 0); - while (int64_t f = augment(s, t)) { - flow += f; - if (flow >= inf) { - return inf; - } - } - } - return flow; - } - - // Vertices that s still reaches in the residual graph. - std::vector source_side(int s) const { - std::vector seen(adj_.size(), false); - std::vector stack = {s}; - seen[s] = true; - while (!stack.empty()) { - int u = stack.back(); - stack.pop_back(); - for (int e : adj_[u]) { - int v = edges_[e].to; - if (edges_[e].cap > 0 && !seen[v]) { - seen[v] = true; - stack.push_back(v); - } - } - } - return seen; - } - - private: - struct Edge { - int to; - int64_t cap; - }; - - bool make_levels(int s, int t) { - std::fill(level_.begin(), level_.end(), -1); - std::queue queue; - level_[s] = 0; - queue.push(s); - while (!queue.empty()) { - int u = queue.front(); - queue.pop(); - for (int e : adj_[u]) { - int v = edges_[e].to; - if (edges_[e].cap > 0 && level_[v] < 0) { - level_[v] = level_[u] + 1; - queue.push(v); - } - } - } - return level_[t] >= 0; - } - - // Push flow along one path of the level graph. A stack is used instead of - // recursion because compiled graphs can be deep. - int64_t augment(int s, int t) { - std::vector path; - int u = s; - while (u != t) { - bool moved = false; - for (; next_[u] < adj_[u].size(); ++next_[u]) { - int e = adj_[u][next_[u]]; - int v = edges_[e].to; - if (edges_[e].cap > 0 && level_[v] == level_[u] + 1) { - path.push_back(e); - u = v; - moved = true; - break; - } - } - if (!moved) { - if (path.empty()) { - return 0; - } - // Dead end: remove u from the level graph and step back. - level_[u] = -1; - u = edges_[path.back() ^ 1].to; - path.pop_back(); - } - } - int64_t f = inf; - for (int e : path) { - f = std::min(f, edges_[e].cap); - } - for (int e : path) { - edges_[e].cap -= f; - edges_[e ^ 1].cap += f; - } - return f; - } - - std::vector edges_; - std::vector> adj_; - std::vector level_; - std::vector next_; -}; - -void compile_recompute(std::vector& tape, ParentsMap& parents_map) { - int n = tape.size(); - std::unordered_map vid; - for (int i = 0; i < n; ++i) { - vid.insert({tape[i].id(), i}); - for (auto& s : tape[i].siblings()) { - vid.insert({s.id(), i}); - } - } - - // Backward nodes are made by autodiff, or computed from such nodes, like an - // optimizer update. - std::vector backward(n, false); - bool has_backward = false; - for (int i = 0; i < n; ++i) { - auto& a = tape[i]; - if (!a.has_primitive()) { - continue; - } - bool b = a.is_backward(); - for (auto& in : a.inputs()) { - b = b || backward[vid.at(in.id())]; - } - backward[i] = b; - has_backward |= b; - } - if (!has_backward) { - return; - } - - auto readers = [&](int i) { - std::vector rs; - for (auto& o : tape[i].outputs()) { - if (auto it = parents_map.find(o.id()); it != parents_map.end()) { - for (auto& [p, _] : it->second) { - if (auto r = vid.find(p.id()); r != vid.end()) { - rs.push_back(r->second); - } - } - } - } - return rs; - }; - auto fusable = [&](int i) { - return tape[i].has_primitive() && is_fusable(tape[i].primitive()); - }; - auto is_view = [&](int i) { - if (!tape[i].has_primitive()) { - return false; - } - auto& p = tape[i].primitive(); - return typeid(p) == typeid(Broadcast) || typeid(p) == typeid(Reshape) || - typeid(p) == typeid(Transpose) || typeid(p) == typeid(ExpandDims) || - typeid(p) == typeid(Squeeze); - }; - - // True if the backward reads node i, directly or through views - std::function backward_reads; - backward_reads = [&](int i) -> bool { - for (int r : readers(i)) { - if (backward[r] || (is_view(r) && backward_reads(r))) { - return true; - } - } - return false; - }; - - // True if the backward reads node i only with fusable ops and not through a - // view. After a transpose or a reshape the copy does not fuse, for example a - // weight cast read by a matmul. After a broadcast the copy runs once for each - // element, for example a scalar learning rate in each optimizer update. - auto fuses_into_backward = [&](int i) { - for (int r : readers(i)) { - if (is_view(r) ? (backward[r] || backward_reads(r)) - : (backward[r] && !fusable(r))) { - return false; - } - } - return true; - }; - - MaxFlow graph(2 * n + 2); - int source = 2 * n; - int sink = 2 * n + 1; - for (int i = 0; i < n; ++i) { - auto& a = tape[i]; - int in = 2 * i; - int out = 2 * i + 1; - if (backward[i]) { - graph.add_edge(in, sink, MaxFlow::inf); - continue; - } - auto rs = readers(i); - for (int r : rs) { - graph.add_edge(out, 2 * r, MaxFlow::inf); - } - if (!a.has_primitive()) { - // Inputs and constants stay alive anyway, so keeping them is free. - graph.add_edge(source, in, MaxFlow::inf); - graph.add_edge(in, out, 0); - continue; - } - if (is_view(i)) { - // Views are free to recompute, so they are never kept. - graph.add_edge(in, out, MaxFlow::inf); - continue; - } - - // Keeping a value costs its size. The cost doubles when a fused kernel - // would otherwise keep the value in registers. - bool in_registers = - fusable(i) && !rs.empty() && std::all_of(rs.begin(), rs.end(), fusable); - int64_t bytes = 0; - for (auto& o : a.outputs()) { - bytes += o.nbytes(); - } - graph.add_edge(in, out, in_registers ? 2 * bytes : bytes); - - // Only cheap elementwise ops are recomputed, and only when the backward - // can fuse them. Other ops must run in the forward. - bool recompute = - fusable(i) && a.siblings().empty() && fuses_into_backward(i); - if (!recompute) { - graph.add_edge(source, in, MaxFlow::inf); - } - } - - auto kept_bytes = graph.solve(source, sink); - if (kept_bytes >= MaxFlow::inf) { - return; - } - auto source_side = graph.source_side(source); - - // The value the backward reads for `x`: x itself if it is kept, otherwise a - // copy computed from kept values. - std::unordered_map copies; - std::vector new_nodes; - std::function backward_value; - backward_value = [&](const array& x) -> array { - int j = vid.at(x.id()); - if (backward[j] || source_side[2 * j]) { - return x; - } - if (auto it = copies.find(j); it != copies.end()) { - return it->second; - } - auto& a = tape[j]; - std::vector inputs; - for (auto& in : a.inputs()) { - inputs.push_back(backward_value(in)); - } - array c(a.shape(), a.dtype(), a.primitive_ptr(), std::move(inputs)); - c.set_backward(true); - for (int k = 0; k < c.inputs().size(); ++k) { - parents_map[c.inputs()[k].id()].push_back({c, k}); - } - copies.insert({j, c}); - new_nodes.push_back(c); - return c; - }; - - // Point the backward readers at the copies. - for (int i = 0; i < n; ++i) { - if (!backward[i]) { - continue; - } - // Give each backward node its own copies, so each kernel can fuse them. - copies.clear(); - for (int k = 0; k < tape[i].inputs().size(); ++k) { - auto x = tape[i].inputs()[k]; - auto c = backward_value(x); - if (c.id() == x.id()) { - continue; - } - auto& from = parents_map[x.id()]; - auto& to = parents_map[c.id()]; - for (auto it = from.begin(); it != from.end();) { - if (it->second == k && vid.at(it->first.id()) == i) { - it->first.inputs()[k] = c; - to.push_back(std::move(*it)); - it = from.erase(it); - } else { - ++it; - } - } - } - } - - if (new_nodes.empty()) { - return; - } - - // Put each copy in the tape before the first node that reads it. - std::unordered_map copy_of_id; - for (auto& c : new_nodes) { - copy_of_id.insert({c.id(), c}); - } - std::vector new_tape; - new_tape.reserve(tape.size() + new_nodes.size()); - std::unordered_set placed; - std::function place_inputs; - place_inputs = [&](const array& a) { - for (auto& in : a.inputs()) { - auto it = copy_of_id.find(in.id()); - if (it != copy_of_id.end() && placed.insert(in.id()).second) { - place_inputs(it->second); - new_tape.push_back(it->second); - } - } - }; - for (auto& a : tape) { - place_inputs(a); - new_tape.push_back(a); - } - tape = std::move(new_tape); -} - -} // namespace mlx::core::detail diff --git a/mlx/compile/trace.cpp b/mlx/compile/trace.cpp index e51800c91c..d643b18075 100644 --- a/mlx/compile/trace.cpp +++ b/mlx/compile/trace.cpp @@ -122,14 +122,12 @@ std::pair, ParentsMap> compile_dfs( auto as = array::make_arrays( std::move(shapes), types, arr.primitive_ptr(), std::move(inputs)); for (int i = 0; i < out.size(); ++i) { - as[i].set_backward(out[i].is_backward()); old_to_new.insert({out[i].id(), as[i]}); } new_tape.push_back(as[arr.sibling_position()]); } else { auto a = array( arr.shape(), arr.dtype(), arr.primitive_ptr(), std::move(inputs)); - a.set_backward(arr.is_backward()); old_to_new.insert({arr.id(), a}); new_tape.push_back(a); } diff --git a/mlx/compile_impl.h b/mlx/compile_impl.h index e8f541b47a..eb76b4736c 100644 --- a/mlx/compile_impl.h +++ b/mlx/compile_impl.h @@ -92,9 +92,6 @@ array split_one( ParentsMap& parents_map, const std::unordered_set& divider); -// Recompute cheap forward values in the backward instead of keeping them -void compile_recompute(std::vector& tape, ParentsMap& parents_map); - // Fuse arrays into Compiled primitives void compile_fuse( std::vector& tape, diff --git a/mlx/transforms.cpp b/mlx/transforms.cpp index 90254b0046..24c91f3483 100644 --- a/mlx/transforms.cpp +++ b/mlx/transforms.cpp @@ -121,7 +121,6 @@ int& detail::InExportTracing::counter() { return counter_; } thread_local int detail::RetainGraph::tracing_counter{0}; -thread_local int detail::InBackward::counter{0}; array eval_impl(std::vector outputs, bool async) { std::deque tape; @@ -545,7 +544,6 @@ std::pair, std::vector> vjp( // Run the tape backwards, computing vector-jacobian // products for each primitive - detail::InBackward in_backward; std::unordered_map cotan_map; for (auto [out_idx, cotan_idx] : output_cotan_pairs) { auto& o = outputs[out_idx]; diff --git a/mlx/transforms_impl.h b/mlx/transforms_impl.h index 5d17540381..4fc78a1ac2 100644 --- a/mlx/transforms_impl.h +++ b/mlx/transforms_impl.h @@ -80,22 +80,6 @@ struct RetainGraph { static thread_local int tracing_counter; }; -struct InBackward { - InBackward() { - counter++; - } - ~InBackward() { - counter--; - } - - static bool in_backward() { - return counter > 0; - } - - private: - static thread_local int counter; -}; - /** Return true if we are currently performing a function transformation in * order to keep the graph when evaluating tracer arrays. */ inline bool in_tracing() { From 8dac7e41ad3dfb8d3163302f3f7d003d79d44551 Mon Sep 17 00:00:00 2001 From: Anastasiia Filippova Date: Tue, 24 Nov 2026 00:39:17 +0100 Subject: [PATCH 5/8] added tests --- tests/compile_tests.cpp | 173 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) diff --git a/tests/compile_tests.cpp b/tests/compile_tests.cpp index 30c2f887ac..e8e3d6ceac 100644 --- a/tests/compile_tests.cpp +++ b/tests/compile_tests.cpp @@ -7,7 +7,9 @@ #include #include +#include +#include "mlx/config.h" #include "mlx/mlx.h" #include "mlx/primitives.h" @@ -875,3 +877,174 @@ TEST_CASE("test compile throwing first trace does not poison cache") { REQUIRE_EQ(out.size(), 1); CHECK_EQ(out[0].item(), 3.0f); } + +// Horizontal fusion tests +// Three siblings that read x and y +auto siblings_with_shared_input(const std::vector& inputs) { + auto x = inputs[0]; + auto y = inputs[1]; + return std::vector{exp(x) + y, sin(x) * y, cos(x) - y}; +} + +// The backward of silu: y and dx both read x and compute sigmoid(x) +auto silu_backward(const std::vector& inputs) { + auto silu = [](const std::vector& in) { + return std::vector{in[0] * sigmoid(in[0])}; + }; + auto [y, dx] = vjp(silu, {inputs[0]}, {inputs[1]}); + return std::vector{y[0], dx[0]}; +} + +// tanh(x) and x + z both read x, and a path through cumsum, which is not +// fusable, connects them +auto siblings_with_a_path(const std::vector& inputs) { + auto x = inputs[0]; + return std::vector{x + cumsum(tanh(x), -1)}; +} + +// Two siblings read sigmoid(a). One output goes to a matmul, the other is +// returned, so they are at different levels +auto siblings_at_two_levels(const std::vector& inputs) { + auto s = sigmoid(inputs[0]); + return std::vector{ + matmul(exp(s * inputs[1]), inputs[3]), log(s * inputs[2])}; +} + +// The same graph at a different address, so that compile traces it again +auto siblings_at_two_levels_again(const std::vector& inputs) { + return siblings_at_two_levels(inputs); +} + +// RMSNorm that also returns rstd, which has the shape [N, 1] +auto norm_with_rstd(const std::vector& inputs) { + auto x = inputs[0]; + auto rstd = rsqrt(mean(x * x, -1, true) + 1e-5f); + return std::vector{x * rstd, rstd}; +} + +// RMSNorm that returns only the result +auto norm_without_rstd(const std::vector& inputs) { + return std::vector{norm_with_rstd(inputs)[0]}; +} + +// 30 siblings that all read x +auto many_siblings(const std::vector& inputs) { + std::vector outs; + for (int i = 0; i < 30; ++i) { + outs.push_back(sin(inputs[0] + static_cast(i))); + } + return outs; +} + +// Compare the compiled outputs with the outputs of the function +void check_close( + const std::vector& outs, + const std::vector& expected) { + REQUIRE_EQ(outs.size(), expected.size()); + for (int i = 0; i < outs.size(); ++i) { + CHECK(allclose(outs[i], expected[i], 1e-5, 1e-5).item()); + } +} + +TEST_CASE("test compile horizontal fusion") { + // Siblings that read the same inputs become one Compiled primitive, which + // reads x and y once + auto x = array({1.0f, 2.0f, 3.0f}); + auto y = array({0.5f, -1.0f, 2.0f}); + auto outs = compile(siblings_with_shared_input)({x, y}); + auto& p = outs[0].primitive(); + CHECK_EQ(typeid(p), typeid(Compiled)); + CHECK_EQ(&outs[1].primitive(), &p); + CHECK_EQ(&outs[2].primitive(), &p); + CHECK_EQ(outs[0].inputs().size(), 2); + check_close(outs, siblings_with_shared_input({x, y})); +} + +TEST_CASE("test compile horizontal fusion with a shared producer") { + // After y and dx merge, sigmoid(x) joins them, so no input of the kernel + // is computed: they are x, dy and a constant + auto x = array({-1.0f, 0.5f, 2.0f}); + auto dy = array({1.0f, -2.0f, 0.5f}); + auto outs = compile(silu_backward)({x, dy}); + auto& p = outs[0].primitive(); + CHECK_EQ(typeid(p), typeid(Compiled)); + CHECK_EQ(&outs[1].primitive(), &p); + for (auto& in : outs[0].inputs()) { + CHECK_FALSE(in.has_primitive()); + } + check_close(outs, silu_backward({x, dy})); +} + +TEST_CASE("test compile horizontal fusion checks") { + // The checks that stop a merge: a path between the groups, different + // levels, different output shapes and the limit on arrays in one kernel + // A path through cumsum connects tanh(x) and x + z, so one kernel would + // need its own output. The large distance lets only is_connected stop it. + { + auto x = reshape(arange(8.0f), {2, 4}) / 8.0f; + auto scoped = config::scoped_update({{"MLX_MAX_FUSE_DISTANCE", 100}}); + auto out = compile(siblings_with_a_path)({x})[0]; + auto& p = out.primitive(); + CHECK_EQ(typeid(p), typeid(Add)); + check_close({out}, siblings_with_a_path({x})); + } + + // Siblings at different levels merge only if MLX_MAX_FUSE_DISTANCE allows + // it + { + auto a = full({2, 4}, 0.5f); + auto b = full({2, 4}, 0.2f); + auto c = full({2, 4}, 0.7f); + auto w = full({4, 3}, 0.3f); + auto expected = siblings_at_two_levels({a, b, c, w}); + + auto outs = compile(siblings_at_two_levels)({a, b, c, w}); + auto& p = outs[1].primitive(); + CHECK_EQ(typeid(p), typeid(Compiled)); + CHECK(outs[1].siblings().empty()); + check_close(outs, expected); + + { + auto scoped = config::scoped_update({{"MLX_MAX_FUSE_DISTANCE", 1}}); + outs = compile(siblings_at_two_levels_again)({a, b, c, w}); + } + REQUIRE_EQ(outs[1].siblings().size(), 1); + CHECK_EQ(outs[1].inputs().size(), 3); + check_close(outs, expected); + } + + // One kernel has one output shape: if rstd is returned, its group and the + // group of x * rstd stay two kernels + { + auto x = reshape(arange(8.0f), {2, 4}); + auto outs = compile(norm_with_rstd)({x}); + CHECK_NE(&outs[0].primitive(), &outs[1].primitive()); + check_close(outs, norm_with_rstd({x})); + + auto out = compile(norm_without_rstd)({x})[0]; + auto& p = out.primitive(); + CHECK_EQ(typeid(p), typeid(Compiled)); + for (auto& in : out.inputs()) { + if (in.has_primitive()) { + auto& q = in.primitive(); + CHECK_NE(typeid(q), typeid(Compiled)); + } + } + check_close({out}, norm_without_rstd({x})); + } + + // A kernel has at most 24 inputs and outputs, so 30 siblings need more + // than one kernel + { + auto x = array({1.0f, 2.0f, 3.0f}); + auto outs = compile(many_siblings)({x}); + std::unordered_set kernels; + for (auto& o : outs) { + kernels.insert(&o.primitive()); + CHECK_LE(o.inputs().size() + o.siblings().size() + 1, 24); + } + CHECK_GT(kernels.size(), 1); + CHECK_LT(kernels.size(), 30); + check_close(outs, many_siblings({x})); + } +} From 4927cb34dc9f9d15f0f1e09c38099a6673d86cb8 Mon Sep 17 00:00:00 2001 From: Anastasiia Filippova Date: Tue, 24 Nov 2026 14:16:53 +0100 Subject: [PATCH 6/8] fix cycle in horizontal merge --- mlx/compile/fuse.cpp | 29 ++++++++++------------------- tests/compile_tests.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/mlx/compile/fuse.cpp b/mlx/compile/fuse.cpp index b6c8f8b79c..605d9f4c98 100644 --- a/mlx/compile/fuse.cpp +++ b/mlx/compile/fuse.cpp @@ -408,29 +408,19 @@ bool merge_vertical( } // True if a path through arrays outside of groups `g1` and `g2` connects them. -// The two groups cannot be one kernel then. +// The two groups cannot be one kernel then. A kernel reads all its inputs +// before it writes its outputs, so a path to an array of another group +// continues from all arrays of that group. bool is_connected( int g1, int g2, const Groups& groups, - const std::unordered_map& position, const ParentsMap& parents_map) { auto in_groups = [&](const array& a) { int g = groups.find(a); return g == g1 || g == g2; }; - // Readers come after their inputs in the tape, so stop the search after the - // last array of the two groups - int last = 0; - for (int g : {g1, g2}) { - for (auto& a : groups.members[g]) { - if (auto it = position.find(a.id()); it != position.end()) { - last = std::max(last, it->second); - } - } - } - // Start from the readers outside of the two groups std::vector stack; for (int g : {g1, g2}) { @@ -446,6 +436,7 @@ bool is_connected( } std::unordered_set visited; + std::unordered_set visited_groups; while (!stack.empty()) { auto a = std::move(stack.back()); stack.pop_back(); @@ -455,9 +446,10 @@ bool is_connected( if (!visited.insert(a.id()).second) { continue; } - if (auto it = position.find(a.id()); - it != position.end() && it->second > last) { - continue; + if (int g = groups.find(a); g >= 0 && visited_groups.insert(g).second) { + for (auto& m : groups.members[g]) { + stack.push_back(m); + } } if (auto it = parents_map.find(a.id()); it != parents_map.end()) { for (auto& [p, idx] : it->second) { @@ -508,7 +500,6 @@ std::map, size_t> score_pairs( bool merge_horizontal( Groups& groups, const std::vector& tape, - const std::unordered_map& position, ParentsMap& parents_map, const std::unordered_map& output_map) { // Given a tape and groups of array for fusion, for every pair of groups @@ -581,7 +572,7 @@ bool merge_horizontal( distance(g1, g2) > max_fuse_distance() || !has_outputs(g1, groups, parents_map, output_map) || !has_outputs(g2, groups, parents_map, output_map) || - is_connected(g1, g2, groups, position, parents_map)) { + is_connected(g1, g2, groups, parents_map)) { continue; } int size = groups.members[g1].size(); @@ -639,7 +630,7 @@ void compile_fuse( // Merge groups until no rule changes them. A merge can make a new merge // possible, so repeat the rules. while (merge_vertical(groups, tape, parents_map, output_map) || - merge_horizontal(groups, tape, position, parents_map, output_map)) { + merge_horizontal(groups, tape, parents_map, output_map)) { } // Replace each group of more than one array with a Compiled primitive diff --git a/tests/compile_tests.cpp b/tests/compile_tests.cpp index e8e3d6ceac..9f8637598e 100644 --- a/tests/compile_tests.cpp +++ b/tests/compile_tests.cpp @@ -927,6 +927,16 @@ auto norm_without_rstd(const std::vector& inputs) { return std::vector{norm_with_rstd(inputs)[0]}; } +// Two pairs of siblings: each pair can merge, but after both merges each +// kernel reads an output of the other (cycle between kerenls is not allowed) +auto crossing_siblings(const std::vector& inputs) { + auto x = exp(inputs[0]); + auto y = exp(inputs[1]); + auto p = sin(inputs[1]); + auto m = cos(inputs[0]); + return std::vector{x, y, x * p, y * m, cumsum(p, 0), cumsum(m, 0)}; +} + // 30 siblings that all read x auto many_siblings(const std::vector& inputs) { std::vector outs; @@ -1033,6 +1043,25 @@ TEST_CASE("test compile horizontal fusion checks") { check_close({out}, norm_without_rstd({x})); } + // Merges that are correct one by one must not make two kernels that read + // the outputs of each other + { + auto s = array({0.1f, 0.2f, 0.3f}); + auto t = array({-0.4f, 0.5f, 0.6f}); + auto outs = compile(crossing_siblings)({s, t}); + auto reads = [](const array& a, const array& b) { + for (auto& in : a.inputs()) { + if (in.has_primitive() && &in.primitive() == &b.primitive()) { + return true; + } + } + return false; + }; + // outs[2] is x * p and outs[3] is y * m + CHECK_FALSE((reads(outs[2], outs[3]) && reads(outs[3], outs[2]))); + check_close(outs, crossing_siblings({s, t})); + } + // A kernel has at most 24 inputs and outputs, so 30 siblings need more // than one kernel { From 8ba8aa9123a8cd19b20b14ac4d12f92b768d1a3d Mon Sep 17 00:00:00 2001 From: Anastasiia Filippova Date: Tue, 24 Nov 2026 22:35:22 +0100 Subject: [PATCH 7/8] python tests --- python/tests/test_compile.py | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/python/tests/test_compile.py b/python/tests/test_compile.py index 6209805f69..d208bb9f35 100644 --- a/python/tests/test_compile.py +++ b/python/tests/test_compile.py @@ -1738,6 +1738,92 @@ def fun(x): self.assertTrue(mx.array_equal(a, mx.array([2.0, 4.0, 6.0]))) self.assertTrue(mx.array_equal(b, mx.array([1.0, 2.0, 3.0]))) + def test_compile_fuse_siblings(self): + # Graphs where sibling groups merge, and graphs where they must not. + # tests/compile_tests.cpp checks which kernels are made. + def shared_input(x, y, w): + return mx.exp(x) + y, mx.sin(x) * y, mx.cos(x) - y + + def shared_producer(x, y, w): + out, (dx,) = mx.vjp(lambda x: x * mx.sigmoid(x), [x], [y]) + return out[0], dx + + def with_a_path(x, y, w): + return x + mx.cumsum(mx.tanh(x), axis=-1) + + def two_levels(x, y, w): + s = mx.sigmoid(x) + return mx.exp(s * y) @ w, mx.log(s * y + 1) + + def norm_with_rstd(x, y, w): + r = mx.rsqrt(mx.mean(x * x, axis=-1, keepdims=True) + 1e-5) + return x * r, r + + def many_siblings(x, y, w): + return [mx.sin(x + i) for i in range(30)] + + def crossing_siblings(x, y, w): + # Each pair can merge, but not both: the two kernels would read the + # outputs of each other + a, b = mx.exp(x), mx.exp(y) + p, m = mx.sin(y), mx.cos(x) + return a, b, a * p, b * m, mx.cumsum(p, axis=-1), mx.cumsum(m, axis=-1) + + x = mx.random.uniform(shape=(4, 8)) + y = mx.random.uniform(shape=(4, 8)) + w = 0.1 * mx.random.normal((8, 8)) + funs = [ + shared_input, + shared_producer, + with_a_path, + two_levels, + norm_with_rstd, + many_siblings, + crossing_siblings, + ] + for fun in funs: + for distance in [0, 100]: + with self.subTest(fun=fun.__name__, distance=distance): + with mx.config.scoped_update(MLX_MAX_FUSE_DISTANCE=distance): + out = mx.compile(lambda *args: fun(*args))(x, y, w) + out = out if isinstance(out, (list, tuple)) else [out] + expected = fun(x, y, w) + expected = ( + expected if isinstance(expected, (list, tuple)) else [expected] + ) + for a, b in zip(out, expected): + self.assertTrue(mx.allclose(a, b, atol=1e-5)) + + def test_compile_fuse_siblings_shapeless(self): + # Merged siblings give correct outputs when the input shape changes + def fun(x, y): + return mx.exp(x) + y, mx.sin(x) * y + + cfun = mx.compile(fun, shapeless=True) + for shape in [(4,), (3, 5)]: + x = mx.random.uniform(shape=shape) + y = mx.random.uniform(shape=shape) + for a, b in zip(cfun(x, y), fun(x, y)): + self.assertEqual(a.shape, shape) + self.assertTrue(mx.allclose(a, b)) + + def test_compile_fuse_siblings_grad(self): + # Gradients through merged siblings: the backward of a gated MLP has a + # group for each branch + def loss(wg, wu, x): + g = x @ wg + return (g * mx.sigmoid(g) * (x @ wu)).sum() + + wg = mx.random.normal((8, 16)) + wu = mx.random.normal((8, 16)) + x = mx.random.normal((4, 8)) + mx.eval(wg, wu, x) + grad_fn = mx.grad(loss, argnums=(0, 1)) + expected = grad_fn(wg, wu, x) + out = mx.compile(grad_fn)(wg, wu, x) + for a, b in zip(out, expected): + self.assertTrue(mx.allclose(a, b, atol=1e-5)) + if __name__ == "__main__": mlx_tests.MLXTestRunner() From 86c89b3a73eba8c4c5726112471bdf297aadfd07 Mon Sep 17 00:00:00 2001 From: Anastasiia Filippova Date: Thu, 26 Nov 2026 14:08:13 +0100 Subject: [PATCH 8/8] address comments --- mlx/compile/simplify.cpp | 2 +- mlx/compile_impl.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/mlx/compile/simplify.cpp b/mlx/compile/simplify.cpp index 25dd288cd3..b8945c7d7a 100644 --- a/mlx/compile/simplify.cpp +++ b/mlx/compile/simplify.cpp @@ -48,7 +48,7 @@ void merge(array& dst, array& src, ParentsMap& parents_map) { } } -static inline uint64_t splitmix64(uint64_t x) noexcept { +inline uint64_t splitmix64(uint64_t x) noexcept { x += 0x9e3779b97f4a7c15ull; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ull; x = (x ^ (x >> 27)) * 0x94d049bb133111ebull; diff --git a/mlx/compile_impl.h b/mlx/compile_impl.h index eb76b4736c..1de0d03c2a 100644 --- a/mlx/compile_impl.h +++ b/mlx/compile_impl.h @@ -81,7 +81,6 @@ bool is_binary(const Primitive& p); bool is_ternary(const Primitive& p); bool is_broadcast(const Primitive& p); bool is_noop(const Primitive& p); -bool is_reduction(const Primitive& p); bool is_fusable(const Primitive& p); // Helpers to edit the graph with the tape and the parents map