diff --git a/mlx/backend/metal/CMakeLists.txt b/mlx/backend/metal/CMakeLists.txt index cccaad8a5c..c1b4ab962f 100644 --- a/mlx/backend/metal/CMakeLists.txt +++ b/mlx/backend/metal/CMakeLists.txt @@ -126,6 +126,7 @@ target_sources( ${CMAKE_CURRENT_SOURCE_DIR}/binary.cpp ${CMAKE_CURRENT_SOURCE_DIR}/device_info.cpp ${CMAKE_CURRENT_SOURCE_DIR}/compiled.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/compiled_reduce.cpp ${CMAKE_CURRENT_SOURCE_DIR}/conv.cpp ${CMAKE_CURRENT_SOURCE_DIR}/copy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/custom_kernel.cpp diff --git a/mlx/backend/metal/compiled.cpp b/mlx/backend/metal/compiled.cpp index 853cfbf840..77c348dafe 100644 --- a/mlx/backend/metal/compiled.cpp +++ b/mlx/backend/metal/compiled.cpp @@ -4,6 +4,7 @@ #include "mlx/backend/common/compiled.h" #include "mlx/backend/common/utils.h" +#include "mlx/backend/metal/compiled.h" #include "mlx/backend/metal/device.h" #include "mlx/backend/metal/jit/includes.h" #include "mlx/backend/metal/utils.h" @@ -13,6 +14,29 @@ namespace mlx::core { +void build_tape( + std::string& os, + const std::vector& tape, + NodeNamer& namer) { + for (auto& x : tape) { + os += fmt::format( + " {0} tmp_{1} = ", get_type_string(x.dtype()), namer.get_name(x)); + if (is_static_cast(x.primitive())) { + os += fmt::format( + "cast_to<{0}>(tmp_{1});\n", + get_type_string(x.dtype()), + namer.get_name(x.inputs()[0])); + } else { + os += x.primitive().name(); + os += "()("; + for (int i = 0; i < x.inputs().size() - 1; i++) { + os += fmt::format("tmp_{0}, ", namer.get_name(x.inputs()[i])); + } + os += fmt::format("tmp_{0});\n", namer.get_name(x.inputs().back())); + } + } +} + inline void build_kernel( std::string& os, const std::string& kernel_name, @@ -203,23 +227,7 @@ inline void build_kernel( } // Actually write the computation - for (auto& x : tape) { - os += fmt::format( - " {0} tmp_{1} = ", get_type_string(x.dtype()), namer.get_name(x)); - if (is_static_cast(x.primitive())) { - os += fmt::format( - "cast_to<{0}>(tmp_{1});\n", - get_type_string(x.dtype()), - namer.get_name(x.inputs()[0])); - } else { - os += x.primitive().name(); - os += "()("; - for (int i = 0; i < x.inputs().size() - 1; i++) { - os += fmt::format("tmp_{0}, ", namer.get_name(x.inputs()[i])); - } - os += fmt::format("tmp_{0});\n", namer.get_name(x.inputs().back())); - } - } + build_tape(os, tape, namer); // Write the outputs from tmps for (auto& x : outputs) { @@ -265,6 +273,18 @@ void Compiled::eval_gpu( return; } + if (auto& p = tape_.back().primitive(); typeid(p) == typeid(Reduce)) { + return compiled_reduce( + stream(), + kernel_lib_, + inputs_, + tape_, + constant_ids_, + is_constant_, + inputs, + outputs[0]); + } + // Get the kernel if someone else built it already auto& s = stream(); auto& d = metal::device(s.device); diff --git a/mlx/backend/metal/compiled.h b/mlx/backend/metal/compiled.h new file mode 100644 index 0000000000..139cc481fc --- /dev/null +++ b/mlx/backend/metal/compiled.h @@ -0,0 +1,31 @@ +// Copyright © 2026 Apple Inc. + +#pragma once + +#include +#include +#include + +#include "mlx/array.h" +#include "mlx/graph_utils.h" + +namespace mlx::core { + +// Write the computation of the tape into tmp_* variables. +void build_tape( + std::string& os, + const std::vector& tape, + NodeNamer& namer); + +// Evaluate a Compiled primitive whose tape ends with a Reduce. +void compiled_reduce( + const Stream& s, + const std::string& lib_name, + const std::vector& trace_inputs, + const std::vector& tape, + const std::unordered_set& constant_ids, + const std::function& is_constant, + const std::vector& inputs, + array& out); + +} // namespace mlx::core diff --git a/mlx/backend/metal/compiled_reduce.cpp b/mlx/backend/metal/compiled_reduce.cpp new file mode 100644 index 0000000000..4123a55087 --- /dev/null +++ b/mlx/backend/metal/compiled_reduce.cpp @@ -0,0 +1,797 @@ +// Copyright © 2026 Apple Inc. + +#include + +#include +#include + +#include "mlx/backend/common/compiled.h" +#include "mlx/backend/common/reduce.h" +#include "mlx/backend/common/utils.h" +#include "mlx/backend/gpu/copy.h" +#include "mlx/backend/metal/compiled.h" +#include "mlx/backend/metal/device.h" +#include "mlx/backend/metal/jit/includes.h" +#include "mlx/backend/metal/kernels/defines.h" +#include "mlx/backend/metal/reduce.h" +#include "mlx/backend/metal/utils.h" +#include "mlx/graph_utils.h" +#include "mlx/primitives.h" +#include "mlx/utils.h" + +namespace mlx::core { + +namespace { + +// Threadgroups to aim for before the reduction is split. +constexpr int64_t target_groups = 1024; +// Row kernel: minimum number of elements for one threadgroup of a split row. +constexpr int64_t row_min_block = 8192; +// Row kernel: maximum threadgroup size. More work per thread is faster. +constexpr int64_t row_max_threads = 256; +// Row kernel: one thread reduces rows of up to this many reads. +constexpr int64_t row_small_reads = 8; +// Column kernel: threads along the reduction. +constexpr int col_threads = 256; +// Column kernel: minimum number of reduced rows per thread for one split. +constexpr int64_t col_min_rows = 32; + +inline int64_t ceil_div(int64_t n, int64_t m) { + return (n + m - 1) / m; +} + +std::string reduce_op_name(const array& reduce) { + auto type = static_cast(reduce.primitive()).state().first; + bool is_bool = reduce.dtype() == bool_; + switch (type) { + case Reduce::And: + return "and"; + case Reduce::Or: + return "or"; + case Reduce::Sum: + return "sum"; + case Reduce::Prod: + return "prod"; + case Reduce::Min: + return is_bool ? "and" : "min"; + case Reduce::Max: + return is_bool ? "or" : "max"; + } + return ""; +} + +// The space before the reduction as kept x reduced, with each dimension +// collapsed. +// - Row: the reduced axes make one row with stride 1 or 0 for each strided +// input. The pattern has 'c' for stride 1 and 'b' for stride 0. +// - Col: the reduced axes make one dimension with any stride. The last kept +// dimension has stride 1 or 0 for each strided input (the pattern). +// The shape and strides are for the kept dimensions, without the last one for +// Col. +struct ReduceLayout { + enum Type { None, Row, Col }; + Type type{None}; + std::string pattern; + Shape shape; + Strides strides; + Strides reduce_strides; + int64_t outer{1}; + int64_t cols{1}; + int64_t reduce_size{1}; +}; + +ReduceLayout get_reduce_layout( + const std::vector& inputs, + const std::function& is_constant, + const Shape& shape, + const std::vector& axes) { + ReduceLayout layout; + + // Broadcast the strides of the strided inputs to the full shape + std::vector strides; + for (size_t i = 0; i < inputs.size(); ++i) { + auto& x = inputs[i]; + if (is_constant(i) || is_scalar(x)) { + continue; + } + Strides xstrides(shape.size(), 0); + int offset = shape.size() - x.ndim(); + for (int j = 0; j < x.ndim(); ++j) { + if (x.shape(j) != 1) { + xstrides[offset + j] = x.strides()[j]; + } + } + strides.push_back(std::move(xstrides)); + } + if (strides.empty()) { + return layout; + } + + // A full reduction can read contiguous inputs in memory order + if (axes.size() == shape.size()) { + auto [contiguous, negative_strides] = + compiled_check_contiguity(inputs, shape); + if (contiguous && !negative_strides) { + layout.type = ReduceLayout::Row; + layout.pattern = std::string(strides.size(), 'c'); + for (auto s : shape) { + layout.reduce_size *= s; + } + return layout; + } + } + + // Separate and collapse the kept and the reduced axes + std::vector reduced(shape.size(), false); + for (auto ax : axes) { + reduced[ax] = true; + } + Shape keep_shape; + Shape red_shape; + std::vector keep_strides(strides.size()); + std::vector red_strides(strides.size()); + for (int d = 0; d < shape.size(); ++d) { + (reduced[d] ? red_shape : keep_shape).push_back(shape[d]); + for (int i = 0; i < strides.size(); ++i) { + (reduced[d] ? red_strides : keep_strides)[i].push_back(strides[i][d]); + } + } + std::tie(red_shape, red_strides) = + collapse_contiguous_dims(red_shape, red_strides); + std::tie(keep_shape, keep_strides) = + collapse_contiguous_dims(keep_shape, keep_strides); + if (red_shape.size() > 1) { + return layout; + } + layout.reduce_size = red_shape.empty() ? 1 : red_shape[0]; + + // The pattern of stride 1 ('c') or 0 ('b') of a dimension + auto get_pattern = [&](const std::vector& st, int dim) { + std::string pattern; + for (auto& s : st) { + auto stride = dim < 0 ? 0 : s[dim]; + if (stride != 0 && stride != 1) { + return std::string(); + } + pattern += (stride == 1) ? 'c' : 'b'; + } + return pattern; + }; + + int keep_ndim = keep_shape.size(); + layout.pattern = get_pattern(red_strides, int(red_shape.size()) - 1); + if (!layout.pattern.empty()) { + layout.type = ReduceLayout::Row; + } else if (keep_ndim > 0) { + layout.pattern = get_pattern(keep_strides, keep_ndim - 1); + if (layout.pattern.empty()) { + return layout; + } + layout.type = ReduceLayout::Col; + layout.cols = keep_shape.back(); + keep_shape.pop_back(); + for (int i = 0; i < keep_strides.size(); ++i) { + keep_strides[i].pop_back(); + layout.reduce_strides.push_back(red_strides[i][0]); + } + } else { + return layout; + } + + layout.shape = std::move(keep_shape); + for (auto s : layout.shape) { + layout.outer *= s; + } + for (auto& st : keep_strides) { + layout.strides.insert(layout.strides.end(), st.begin(), st.end()); + } + if (layout.outer > UINT32_MAX) { + layout.type = ReduceLayout::None; + } + return layout; +} + +// Source parts shared by the row and column kernels. +struct ReduceSource { + NodeNamer namer; + std::string out_t; + std::string acc_t; + std::string op; + // Inputs which are read for each element (pattern 'c') + std::vector elem_inputs; + // Inputs which are read once per row or column (pattern 'b') + std::vector fixed_inputs; +}; + +// Sums and products of 16 bit floats accumulate in float32. +Dtype accumulate_type(const array& reduce) { + auto op = reduce_op_name(reduce); + bool half = reduce.dtype() == float16 || reduce.dtype() == bfloat16; + return (half && (op == "sum" || op == "prod")) ? float32 : reduce.dtype(); +} + +ReduceSource reduce_source(const std::vector& tape, Dtype out_type) { + ReduceSource src; + auto& reduce = tape.back(); + src.out_t = get_type_string(out_type); + src.acc_t = get_type_string(accumulate_type(reduce)); + auto op = reduce_op_name(reduce); + op[0] = std::toupper(op[0]); + src.op = op + "<" + src.acc_t + ">"; + return src; +} + +// Write the input and common arguments and return the next buffer index. +int write_arguments( + std::string& os, + ReduceSource& src, + const std::string& kernel_name, + const std::vector& inputs, + const std::function& is_constant) { + os += fmt::format( + "[[host_name(\"{0}\")]]\n[[kernel]] void {0}(\n", kernel_name); + int cnt = 0; + for (size_t i = 0; i < inputs.size(); ++i) { + if (is_constant(i)) { + continue; + } + os += fmt::format( + " device const {0}* {1} [[buffer({2})]],\n", + get_type_string(inputs[i].dtype()), + src.namer.get_name(inputs[i]), + cnt++); + } + os += fmt::format( + " device {0}* out [[buffer({1})]],\n" + " constant const int* shape [[buffer({2})]],\n" + " constant const int64_t* strides [[buffer({3})]],\n" + " constant const int& ndim [[buffer({4})]],\n" + " constant const int64_t& reduce_size [[buffer({5})]],\n" + " constant const int64_t& block_size [[buffer({6})]],\n", + src.out_t, + cnt, + cnt + 1, + cnt + 2, + cnt + 3, + cnt + 4, + cnt + 5); + return cnt + 6; +} + +// Move the strided inputs to the start of their block. `start` is the offset +// of the block for inputs with pattern 'c' and `offset(j)` for all strided +// inputs. Load the constants and scalars. +void write_setup( + std::string& os, + ReduceSource& src, + const std::vector& inputs, + const std::function& is_constant, + const std::string& pattern, + const std::string& start, + const std::function& offset) { + std::string loc_init; + std::string loc_update; + std::string loads; + int j = 0; + for (size_t i = 0; i < inputs.size(); ++i) { + auto& x = inputs[i]; + auto& xname = src.namer.get_name(x); + auto type_str = get_type_string(x.dtype()); + if (is_constant(i)) { + std::ostringstream ss; + print_constant(ss, x); + loads += fmt::format( + " auto tmp_{0} = static_cast<{1}>({2});\n", + xname, + type_str, + ss.str()); + } else if (is_scalar(x)) { + loads += fmt::format(" {0} tmp_{1} = {1}[0];\n", type_str, xname); + } else { + loc_init += fmt::format(" int64_t loc_{0} = {1};\n", xname, offset(j)); + loc_update += fmt::format( + " loc_{0} += l * strides[{1} * ndim + d];\n", xname, j); + if (pattern[j] == 'c') { + loads += fmt::format(" {0} += loc_{0} + {1};\n", xname, start); + src.elem_inputs.push_back(x); + } else { + loads += fmt::format(" {0} += loc_{0};\n", xname); + src.fixed_inputs.push_back(x); + } + j++; + } + } + os += loc_init; + os += " int64_t r = row;\n"; + os += " for (int d = ndim - 1; d >= 0; --d) {\n"; + os += " int64_t l = r % shape[d];\n"; + os += loc_update; + os += " r /= shape[d];\n }\n"; + os += loads; +} + +// The body for one element. `elem` and `fixed` give the index into inputs +// with pattern 'c' and 'b'. +std::string reduce_body( + ReduceSource& src, + const std::vector& tape, + const std::string& total, + const std::function& elem, + const std::function& fixed) { + std::string body; + auto load = [&](const array& x, const std::string& idx) { + body += fmt::format( + " {0} tmp_{1} = {1}[{2}];\n", + get_type_string(x.dtype()), + src.namer.get_name(x), + idx); + }; + for (auto& x : src.elem_inputs) { + load(x, elem(src.namer.get_name(x))); + } + for (auto& x : src.fixed_inputs) { + auto idx = fixed(src.namer.get_name(x)); + if (!idx.empty()) { + load(x, idx); + } + } + build_tape(body, std::vector(tape.begin(), tape.end() - 1), src.namer); + body += fmt::format( + " {0} = op({0}, cast_to<{1}>(tmp_{2}));\n", + total, + src.acc_t, + src.namer.get_name(tape.back().inputs()[0])); + return body; +} + +// Reduce rows. In the small variant one thread reduces one row. Otherwise +// one threadgroup reduces one block of one row. The block is the full row +// unless the row is split, then the output holds partials. +std::string build_row_kernel( + const std::string& kernel_name, + const std::vector& inputs, + const std::vector& tape, + const std::function& is_constant, + const std::string& pattern, + Dtype out_type, + bool small, + int n_reads) { + std::string os; + auto src = reduce_source(tape, out_type); + write_arguments(os, src, kernel_name, inputs, is_constant); + if (small) { + os += " uint tid [[thread_position_in_grid]]) {\n"; + os += " int64_t row = tid;\n"; + os += " int64_t k0 = 0;\n"; + os += " int n = reduce_size;\n"; + } else { + os += fmt::format( + " uint3 gid [[threadgroup_position_in_grid]],\n" + " uint3 gsize [[threadgroups_per_grid]],\n" + " uint3 lsize [[threads_per_threadgroup]],\n" + " uint lid [[thread_index_in_threadgroup]],\n" + " uint simd_lane_id [[thread_index_in_simdgroup]],\n" + " uint simd_group_id [[simdgroup_index_in_threadgroup]],\n" + " uint simd_per_group [[simdgroups_per_threadgroup]]) {{\n" + " threadgroup {0} shared_vals[simd_size];\n" + " int64_t row = gid.y;\n" + " int64_t k0 = gid.x * block_size;\n" + " int n = static_cast(metal::min(block_size, " + "reduce_size - k0));\n", + src.acc_t); + } + os += fmt::format( + " constexpr int N_READS = {0};\n {1} op;\n", n_reads, src.op); + write_setup( + os, src, inputs, is_constant, pattern, "k0", [](int) { return "0"; }); + for (auto& x : src.fixed_inputs) { + os += fmt::format( + " {0} tmp_{1} = {1}[0];\n", + get_type_string(x.dtype()), + src.namer.get_name(x)); + } + + // Each thread reduces N_READS consecutive elements at a time + auto body = reduce_body( + src, + tape, + "total", + [](auto&) { return "k + i"; }, + [](auto&) { return ""; }); + os += fmt::format(" {0} total = {1}::init;\n", src.acc_t, src.op); + if (small) { + os += " for (int k = 0; k < n; k += N_READS) {\n"; + } else { + os += " for (int k = lid * N_READS; k < n; k += lsize.x * N_READS) {\n"; + } + os += " if (k + N_READS <= n) {\n"; + os += " for (int i = 0; i < N_READS; i++) {\n" + body + " }\n"; + os += " } else {\n"; + os += " for (int i = 0; k + i < n; i++) {\n" + body + " }\n"; + os += " }\n }\n"; + + // Reduce in the threadgroup and write the result + if (small) { + os += fmt::format(" out[row] = static_cast<{0}>(total);\n}}\n", src.out_t); + return os; + } + os += fmt::format( + " total = op.simd_reduce(total);\n" + " if (simd_per_group > 1) {{\n" + " if (simd_lane_id == 0) {{\n" + " shared_vals[simd_group_id] = total;\n" + " }}\n" + " threadgroup_barrier(mem_flags::mem_threadgroup);\n" + " total = lid < simd_per_group ? shared_vals[lid] : {0}::init;\n" + " total = op.simd_reduce(total);\n" + " }}\n" + " if (lid == 0) {{\n" + " out[row * gsize.x + gid.x] = static_cast<{1}>(total);\n" + " }}\n" + "}}\n", + src.op, + src.out_t); + return os; +} + +// Reduce columns. A threadgroup of lsize.x x BM threads reduces +// BN = lsize.x * N_READS columns over one block of the reduced rows. Each +// thread reduces N_READS consecutive columns. BM is a power of 2. The output +// holds partials if the rows are split. +std::string build_col_kernel( + const std::string& kernel_name, + const std::vector& inputs, + const std::vector& tape, + const std::function& is_constant, + const std::string& pattern, + Dtype out_type, + int n_reads) { + std::string os; + auto src = reduce_source(tape, out_type); + int cnt = write_arguments(os, src, kernel_name, inputs, is_constant); + os += fmt::format( + " constant const int64_t* reduce_strides [[buffer({0})]],\n" + " constant const int64_t& cols [[buffer({1})]],\n" + " uint3 gid [[threadgroup_position_in_grid]],\n" + " uint3 gsize [[threadgroups_per_grid]],\n" + " uint3 lid [[thread_position_in_threadgroup]],\n" + " uint3 lsize [[threads_per_threadgroup]]) {{\n" + " constexpr int N_READS = {2};\n" + " int BM = lsize.y;\n" + " int BN = lsize.x * N_READS;\n" + " {4} op;\n" + " threadgroup {5} shared_vals[{3} * N_READS];\n" + " int64_t row = gid.y;\n" + " int64_t m0 = int64_t(gid.x) * BN + lid.x * N_READS;\n" + " int64_t k0 = int64_t(gid.z) * block_size;\n" + " int n = static_cast(metal::min(block_size, " + "reduce_size - k0));\n" + " int n_cols = static_cast(metal::clamp(cols - m0, " + "int64_t(0), int64_t(N_READS)));\n", + cnt, + cnt + 1, + n_reads, + col_threads, + src.op, + src.acc_t); + write_setup(os, src, inputs, is_constant, pattern, "m0", [](int j) { + return fmt::format("k0 * reduce_strides[{0}]", j); + }); + int j = 0; + for (size_t i = 0; i < inputs.size(); ++i) { + if (!is_constant(i) && !is_scalar(inputs[i])) { + os += fmt::format( + " int64_t rs_{0} = reduce_strides[{1}];\n", + src.namer.get_name(inputs[i]), + j++); + } + } + + // Loop over the rows of the block + auto body = reduce_body( + src, + tape, + "totals[i]", + [](auto& name) { return fmt::format("k * rs_{0} + i", name); }, + [](auto&) { return ""; }); + std::string fixed_loads; + for (auto& x : src.fixed_inputs) { + auto& name = src.namer.get_name(x); + fixed_loads += fmt::format( + " {0} tmp_{1} = {1}[k * rs_{1}];\n", get_type_string(x.dtype()), name); + } + os += fmt::format( + " {0} totals[N_READS];\n" + " for (int i = 0; i < N_READS; i++) {{\n" + " totals[i] = {1}::init;\n" + " }}\n", + src.acc_t, + src.op); + os += " if (n_cols == N_READS) {\n"; + os += " for (int k = lid.y; k < n; k += BM) {\n" + fixed_loads; + os += " for (int i = 0; i < N_READS; i++) {\n" + body + " }\n }\n"; + os += " } else {\n"; + os += " for (int k = lid.y; k < n; k += BM) {\n" + fixed_loads; + os += " for (int i = 0; i < n_cols; i++) {\n" + body + " }\n }\n"; + os += " }\n"; + + // Reduce across the threadgroup rows and write the result + os += fmt::format( + " for (int i = 0; i < N_READS; i++) {{\n" + " shared_vals[lid.y * BN + lid.x * N_READS + i] = totals[i];\n" + " }}\n" + " for (int s = BM / 2; s > 0; s /= 2) {{\n" + " threadgroup_barrier(mem_flags::mem_threadgroup);\n" + " if (lid.y < s) {{\n" + " for (int i = 0; i < N_READS; i++) {{\n" + " int idx = lid.y * BN + lid.x * N_READS + i;\n" + " totals[i] = op(totals[i], shared_vals[idx + s * BN]);\n" + " shared_vals[idx] = totals[i];\n" + " }}\n" + " }}\n" + " }}\n" + " if (lid.y == 0) {{\n" + " out += (int64_t(gid.z) * gsize.y + row) * cols + m0;\n" + " for (int i = 0; i < n_cols; i++) {{\n" + " out[i] = static_cast<{0}>(totals[i]);\n" + " }}\n" + " }}\n" + "}}\n", + src.out_t); + return os; +} + +MTL::ComputePipelineState* get_reduce_kernel( + metal::Device& d, + const std::string& kernel_name, + const std::function& build) { + auto lib = d.get_library(kernel_name, [&]() { + std::string kernel = metal::utils(); + concatenate( + kernel, + metal::unary_ops(), + metal::binary_ops(), + metal::ternary_ops(), + metal::reduce_utils(), + build()); + return kernel; + }); + return d.get_kernel(kernel_name, lib); +} + +// Compute the elementwise part in a temporary and reduce it with the +// regular reduction. +void compiled_reduce_fallback( + const Stream& s, + const std::vector& trace_inputs, + const std::vector& tape, + const std::unordered_set& constant_ids, + const std::vector& inputs, + const Shape& shape, + array& out) { + auto& reduce = tape.back(); + auto& reduce_in = reduce.inputs()[0]; + array tmp(shape, reduce_in.dtype(), nullptr, {}); + if (tmp.size() > 0) { + Compiled elementwise( + s, + trace_inputs, + {reduce_in}, + std::vector(tape.begin(), tape.end() - 1), + constant_ids); + std::vector outs{tmp}; + elementwise.eval_gpu(inputs, outs); + } else { + tmp.set_data(allocator::malloc(0)); + } + metal::get_command_encoder(s).add_temporary(tmp); + auto [type, axes] = static_cast(reduce.primitive()).state(); + Reduce(s, type, axes).eval_gpu({tmp}, out); +} + +} // namespace + +void compiled_reduce( + const Stream& s, + const std::string& lib_name, + const std::vector& trace_inputs, + const std::vector& tape, + const std::unordered_set& constant_ids, + const std::function& is_constant, + const std::vector& inputs, + array& out) { + Shape shape; + for (auto& in : inputs) { + shape = broadcast_shapes(shape, in.shape()); + } + auto axes = + static_cast(tape.back().primitive()).state().second; + bool empty_in = + std::any_of(shape.begin(), shape.end(), [](auto d) { return d == 0; }); + if (empty_in && out.size() > 0) { + return compiled_reduce_fallback( + s, trace_inputs, tape, constant_ids, inputs, shape, out); + } + auto layout = get_reduce_layout(inputs, is_constant, shape, axes); + if (layout.type == ReduceLayout::None) { + return compiled_reduce_fallback( + s, trace_inputs, tape, constant_ids, inputs, shape, out); + } + bool row = layout.type == ReduceLayout::Row; + + // Read 32 (row) or 8 (column) bytes of the widest input with pattern 'c' + // per thread and step + int max_itemsize = 1; + int n_buffers = row ? 6 : 8; + for (int i = 0, j = 0; i < inputs.size(); ++i) { + if (is_constant(i)) { + continue; + } + n_buffers++; + if (!is_scalar(inputs[i]) && layout.pattern[j++] == 'c') { + max_itemsize = std::max(max_itemsize, int(inputs[i].itemsize())); + } + } + int n_reads = row ? std::clamp(32 / max_itemsize, REDUCE_N_READS, 16) + : std::clamp(8 / max_itemsize, 2, 8); + + // Narrow columns use fewer threads along the columns and more along the + // reduction + int64_t col_lanes = next_power_of_2(ceil_div(layout.cols, n_reads)); + col_lanes = std::clamp(col_lanes, 1, 32); + int64_t col_bm = col_threads / col_lanes; + int64_t col_groups = ceil_div(layout.cols, col_lanes * n_reads); + + // Metal has 31 buffer slots and grid dimensions below 2^32 threads + if (n_buffers > 31 || (!row && layout.outer * col_bm > int64_t(UINT32_MAX))) { + return compiled_reduce_fallback( + s, trace_inputs, tape, constant_ids, inputs, shape, out); + } + + out.set_data(allocator::malloc(std::max(out.nbytes(), size_t(4)))); + if (out.size() == 0) { + return; + } + + int64_t outer = layout.outer; + int64_t size = layout.reduce_size; + bool small = row && size <= row_small_reads * n_reads; + + // Split the reduction when there are few threadgroups. Blocks are below + // 2^30 so int32 indices in the kernels do not overflow. + int64_t groups = row ? outer : outer * col_groups; + int64_t min_block = row ? row_min_block : col_bm * col_min_rows; + int64_t n_splits = 1; + if (!small && groups < target_groups) { + n_splits = + std::min(ceil_div(size, min_block), ceil_div(target_groups, groups)); + } + n_splits = std::max(n_splits, ceil_div(size, int64_t(1) << 30)); + int64_t block_size = ceil_div(size, std::max(n_splits, int64_t(1))); + n_splits = block_size > 0 ? ceil_div(size, block_size) : 1; + + // Partial results keep the accumulation type + Dtype out_type = n_splits > 1 ? accumulate_type(tape.back()) : out.dtype(); + + auto& d = metal::device(s.device); + auto kernel_name = fmt::format( + "{}_reduce_{}_{}_{}_{}", + lib_name, + row ? (small ? "small" : "row") : "col", + layout.pattern, + n_reads, + type_to_name(out_type)); + auto kernel = get_reduce_kernel(d, kernel_name, [&]() { + if (row) { + return build_row_kernel( + kernel_name, + trace_inputs, + tape, + is_constant, + layout.pattern, + out_type, + small, + n_reads); + } else { + return build_col_kernel( + kernel_name, + trace_inputs, + tape, + is_constant, + layout.pattern, + out_type, + n_reads); + } + }); + + auto& compute_encoder = metal::get_command_encoder(s); + auto make_temporary = [&](Shape shape, Dtype dtype) { + array a(std::move(shape), dtype, nullptr, {}); + a.set_data(allocator::malloc(a.nbytes())); + compute_encoder.add_temporary(a); + return a; + }; + array partial = out; + if (n_splits > 1) { + partial = make_temporary( + row ? Shape{int(outer), int(n_splits)} + : Shape{int(n_splits), int(out.size())}, + out_type); + } + + // Encode dummy values for no kept axes + int ndim = layout.shape.size(); + if (ndim == 0) { + layout.shape.push_back(0); + layout.strides.push_back(0); + } + + compute_encoder.set_compute_pipeline_state(kernel); + int cnt = 0; + for (int i = 0; i < inputs.size(); ++i) { + if (!is_constant(i)) { + compute_encoder.set_input_array(inputs[i], cnt++); + } + } + compute_encoder.set_output_array(partial, cnt++); + compute_encoder.set_vector_bytes(layout.shape, cnt++); + compute_encoder.set_vector_bytes(layout.strides, cnt++); + compute_encoder.set_bytes(ndim, cnt++); + compute_encoder.set_bytes(size, cnt++); + compute_encoder.set_bytes(block_size, cnt++); + if (small) { + int64_t max_threads = kernel->maxTotalThreadsPerThreadgroup(); + compute_encoder.dispatch_threads( + MTL::Size(outer, 1, 1), MTL::Size(std::min(outer, max_threads), 1, 1)); + } else if (row) { + int64_t max_threads = kernel->maxTotalThreadsPerThreadgroup() / 32 * 32; + int64_t threads = ceil_div(block_size, n_reads); + threads = std::clamp( + ceil_div(threads, 32) * 32, + int64_t(32), + std::min(max_threads, row_max_threads)); + compute_encoder.dispatch_threadgroups( + MTL::Size(n_splits, outer, 1), MTL::Size(threads, 1, 1)); + } else { + // Keep BM a power of 2 within the kernel limit + while (col_lanes * col_bm > kernel->maxTotalThreadsPerThreadgroup()) { + col_bm /= 2; + } + compute_encoder.set_vector_bytes(layout.reduce_strides, cnt++); + compute_encoder.set_bytes(layout.cols, cnt++); + compute_encoder.dispatch_threadgroups( + MTL::Size(col_groups, outer, n_splits), + MTL::Size(col_lanes, col_bm, 1)); + } + if (n_splits == 1) { + return; + } + + // Reduce the partial results into a flat view of the output, or into a + // temporary of the accumulation type which is then cast to the output + array result = out; + if (out_type != out.dtype()) { + result = make_temporary(out.shape(), out_type); + } + array flat({int(out.size())}, out_type, nullptr, {}); + flat.copy_shared_buffer(result, {1}, result.flags(), result.size()); + auto op_name = reduce_op_name(tape.back()); + if (row && outer == 1) { + all_reduce_dispatch(partial, flat, op_name, compute_encoder, d, s); + } else { + std::vector partial_axes = {row ? 1 : 0}; + auto plan = get_reduction_plan(partial, partial_axes); + if (row) { + row_reduce_general_dispatch( + partial, flat, op_name, plan, partial_axes, compute_encoder, d, s); + } else { + strided_reduce_general_dispatch( + partial, flat, op_name, plan, partial_axes, compute_encoder, d, s); + } + } + if (out_type != out.dtype()) { + copy_gpu_inplace(result, out, CopyType::Vector, s); + } +} + +} // namespace mlx::core diff --git a/mlx/compile.cpp b/mlx/compile.cpp index 462a772797..1eb0ff7e66 100644 --- a/mlx/compile.cpp +++ b/mlx/compile.cpp @@ -11,6 +11,7 @@ #include "mlx/allocator.h" #include "mlx/backend/common/compiled.h" +#include "mlx/backend/metal/metal.h" #include "mlx/compile.h" #include "mlx/compile_impl.h" #include "mlx/fast_primitives.h" @@ -71,14 +72,26 @@ 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); } +// A reduction that can end a fused graph. Only the Metal backend supports it. +bool is_fusable_reduce(const array& a) { + if (std::getenv("MLX_DISABLE_REDUCE_FUSION")) { + return false; + } + if (!a.has_primitive()) { + return false; + } + if (auto& p = a.primitive(); typeid(p) != typeid(Reduce)) { + return false; + } + return a.primitive().stream().device == Device::gpu && + metal::is_available() && !issubdtype(a.dtype(), complexfloating) && + !issubdtype(a.inputs()[0].dtype(), complexfloating); +} + Compiled::Compiled( Stream stream, std::vector inputs, @@ -209,6 +222,11 @@ std::vector Compiled::output_shapes(const std::vector& inputs) { out_shape[i] = std::max(out_shape[i], in.shape()[i - dd]); } } + if (auto r = dynamic_cast(&tape_.back().primitive())) { + for (auto ax : r->state().second) { + out_shape[ax] = 1; + } + } // All outputs have the same shape return std::vector(outputs_.size(), out_shape); } @@ -843,6 +861,13 @@ void compile_fuse( // - Collect inputs to the new compiled primitive // - Add fusable primitives to a tape in the correct order + // A reduction root fuses the elementwise graph that makes its input. Its + // inner arrays have a different shape, so they cannot be outputs. + bool reduce_root = is_fusable_reduce(arr); + + // Broadcasts split during the recursion as (original, copy) pairs + std::vector> splits; + std::function recurse; std::unordered_set cache; std::unordered_set input_set; @@ -859,10 +884,13 @@ void compile_fuse( // - Constant input // - Stream mismatch // - Non fusable primitive - // - Is global output but has a different shape + // - Is global output but has a different shape or feeds a reduction + bool is_output = output_map.find(a.id()) != output_map.end(); + bool is_root_reduce = reduce_root && depth == 0; 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)) { + a.primitive().stream() != s || + !(is_fusable(a.primitive()) || is_root_reduce) || + (is_output && (a.shape() != shape || (reduce_root && depth > 0)))) { // Possible input input_set.insert(a.id()); return; @@ -888,7 +916,10 @@ void compile_fuse( 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); + // `a` may alias a parent input which the split overwrites + array original = a; + array b = split_one(original, parents_map, cache); + splits.emplace_back(std::move(original), b); recurse(b, depth, s, shape); } else { // Possible input @@ -954,6 +985,22 @@ void compile_fuse( }; recurse_tape(arr); + // The backend infers the shape before the reduction from the inputs. If + // that is not possible, undo the broadcast splits and do not fuse. + if (reduce_root) { + Shape in_shape; + for (auto& in : inputs) { + in_shape = broadcast_shapes(in_shape, in.shape()); + } + if (in_shape != arr.inputs()[0].shape()) { + for (auto it = splits.rbegin(); it != splits.rend(); ++it) { + merge_one(it->first, it->second, parents_map); + } + new_tape.push_back(arr); + continue; + } + } + std::vector old_outputs; // Add to global cache and add any global outputs to outputs // of new primitive diff --git a/python/tests/test_compile.py b/python/tests/test_compile.py index 6209805f69..802d34bd7b 100644 --- a/python/tests/test_compile.py +++ b/python/tests/test_compile.py @@ -1728,6 +1728,162 @@ def fun(x): x = mx.array([1.0, float("nan"), 3.0]) self.assertTrue(mx.array_equal(mx.compile(fun)(x), mx.array([False, True]))) + def test_compile_fused_reduce(self): + def is_fused(fun, *args): + out = mx.compile(fun)(*args) + s = StringIO() + mx.export_to_dot(s, out) + return any( + ("Compiled" in line and line.split('"')[1].endswith(r)) + for line in s.getvalue().splitlines() + if "label" in line + for r in ["Sum", "Prod", "Max", "Min", "And", "Or"] + ) + + fusion = mx.default_device() == mx.gpu and mx.metal.is_available() + + def check(fun, *args, fused=True, atol=1e-5, rtol=1e-5): + mx.eval(args) + expected = fun(*args) + out = mx.compile(fun)(*args) + self.assertEqual(out.shape, expected.shape) + self.assertEqual(out.dtype, expected.dtype) + self.assertTrue( + mx.allclose(out, expected, atol=atol, rtol=rtol, equal_nan=True) + ) + if fusion: + self.assertEqual(is_fused(fun, *args), fused) + + x = mx.random.normal((33, 47, 65)) + y = mx.random.normal((33, 47, 65)) + + # Reduction types and axes + for op in [mx.sum, mx.prod, mx.max, mx.min]: + for axis in [None, -1, (1, 2), (0, 1, 2)]: + check(lambda a, b: op(mx.tanh(a) * b, axis=axis), x, y) + for op in [mx.all, mx.any]: + check(lambda a, b: op(a > b, axis=-1), x, y) + check(lambda a, b: op(a > b), x, y) + check(lambda a, b: mx.sum(a * b, axis=-1, keepdims=True), x, y) + check(lambda a, b: mx.mean(a * b, axis=(1, 2)), x, y) + + # Broadcast and scalar inputs + w = mx.random.normal((65,)) + mu = mx.random.normal((33, 47, 1)) + s = mx.array(2.0) + check(lambda a, b: mx.sum(a * b, axis=-1), x, w) + check(lambda a, b: mx.sum(a * b, axis=-1), x, mu) + check(lambda a, b, c: mx.sum((a - b) * c + 1.0, axis=-1), x, mu, s) + check(lambda a: mx.var(a, axis=-1), x, atol=1e-4) + check(lambda a, b: mx.sum(a * b, axis=(1, 2)), x, w) + + # Short, long and split rows + for shape in [(1000, 3), (1000, 64), (7, 100000), (1 << 20,)]: + a = mx.random.normal(shape) + b = mx.random.normal(shape) + check(lambda a, b: mx.sum(a * b, axis=-1), a, b, atol=1e-3, rtol=1e-4) + check(lambda a, b: mx.max(mx.abs(a - b), axis=-1), a, b) + + # Column reductions + check(lambda a, b: mx.sum(a * b, axis=0), x, y) + check(lambda a, b: mx.sum(a * b, axis=1), x, y) + check(lambda a, b: mx.max(a * b, axis=(0, 1)), x, y) + check(lambda a, b: mx.sum(a * b, axis=0), x, w) + check(lambda a, b: mx.sum(a * b, axis=(0, 1)), x, mu) + check(lambda a, b: mx.sum(a * b, axis=-1), x.swapaxes(1, 2), y.swapaxes(1, 2)) + for shape in [(100000, 3), (100000, 200), (50, 1000), (3, 64)]: + a = mx.random.normal(shape) + b = mx.random.normal(shape) + check(lambda a, b: mx.sum(a * b, axis=0), a, b, atol=1e-3, rtol=1e-4) + check(lambda a, b: mx.min(a - b, axis=0), a, b) + + # Layouts which use the unfused kernels at run time + check(lambda a, b: mx.sum(a * b), x, w) + check(lambda a, b: mx.sum(a * b, axis=-1), x[::-1], y) + check(lambda a, b: mx.sum(a * b, axis=0), x[:, :, ::-1], y) + check(lambda a, b: mx.sum(a * b, axis=(0, 2)), x, y) + check(lambda a, b: mx.max(a * b, axis=(0, 2)), x[::-1], y) + + # Non-contiguous inputs which are still fused + check(lambda a, b: mx.sum(a * b), x.T, y.T) + check(lambda a, b: mx.sum(a * b, axis=-1), x[:, 3:40, 5:], y[:, 3:40, 5:]) + + # Types + for t in [mx.float16, mx.bfloat16, mx.int32, mx.uint8, mx.bool_]: + half = t in [mx.float16, mx.bfloat16] + a = (x if half else x * 4).astype(t) + b = (y if half else y * 4).astype(t) + tol = 0.5 if half else 1e-5 + check(lambda a, b: mx.sum(a + b, axis=-1), a, b, atol=tol, rtol=tol) + check(lambda a, b: mx.max(a * b, axis=-1), a, b, atol=tol, rtol=tol) + check(lambda a, b: mx.sum(a * b, axis=0), a, b, atol=tol, rtol=tol) + # CUDA codegen does not support complex casts + if fusion: + check(lambda a: mx.sum(mx.abs(a), axis=-1), x.astype(mx.complex64)) + + # Empty arrays + check(lambda a, b: mx.sum(a * b, axis=-1), x[:, :, :0], y[:, :, :0]) + check(lambda a, b: mx.max(a * b, axis=-1), x[:0], y[:0]) + check(lambda a, b: mx.sum(a * b, axis=0), x[::2, 0, :0], w[:0]) + + # NaN propagates + z = mx.where(x > 2.0, mx.nan, x) + check(lambda a, b: mx.max(a * b, axis=-1), z, y) + + # Many inputs + xs = [mx.random.normal((64, 300)) for _ in range(28)] + + def many(*a): + acc = a[0] + for b in a[1:]: + acc = acc * 0.5 + b + return acc + + check(lambda *a: mx.sum(many(*a), axis=-1), *xs, atol=1e-4) + check(lambda *a: mx.sum(many(*a), axis=0), *xs, atol=1e-4) + + # Intermediate outputs are not fused into the reduction + def fun(a, b): + c = a * b + return mx.sum(c, axis=-1), c + + mx.eval(x, y) + out = mx.compile(fun)(x, y) + expected = fun(x, y) + self.assertTrue(mx.allclose(out[0], expected[0], atol=1e-5)) + self.assertTrue(mx.array_equal(out[1], expected[1])) + + # The shape before the reduction can not be inferred from the inputs + check( + lambda a: mx.sum(mx.broadcast_to(a[:, None], (33, 7)) * 2.0, axis=-1), + x[:, 0, 0], + fused=False, + ) + + def test_compile_fused_reduce_shapeless(self): + def fun(a, b): + return mx.sum(mx.exp(a) * b, axis=-1) + + cfun = mx.compile(fun, shapeless=True) + shapes = [(4, 8), (16, 3), (2, 1000)] + if mx.default_device() == mx.gpu: + shapes.append((1, 1)) + for shape in shapes: + a = mx.random.normal(shape) + b = mx.random.normal(shape) + out = cfun(a, b) + self.assertEqual(out.shape, shape[:1]) + self.assertTrue(mx.allclose(out, fun(a, b), atol=1e-4, rtol=1e-4)) + + def fun(a, b): + return mx.max(a - b, axis=0) + + cfun = mx.compile(fun, shapeless=True) + for shape in [(4, 8), (16, 3)]: + a = mx.random.normal(shape) + b = mx.random.normal(shape[-1:]) + self.assertTrue(mx.allclose(cfun(a, b), fun(a, b))) + def test_compile_output_through_noop_is_kept(self): def fun(x): a = x.astype(mx.float32) * 2 diff --git a/tests/compile_tests.cpp b/tests/compile_tests.cpp index 30c2f887ac..27a0533fab 100644 --- a/tests/compile_tests.cpp +++ b/tests/compile_tests.cpp @@ -321,6 +321,8 @@ TEST_CASE("test compile unary fused") { } { + // Reductions fuse on the GPU, so use the CPU to keep the Reduce + StreamContext ctx(Device::cpu); auto cfun = compile(unary_fused_2); auto x = array({1.0, 2.0}); auto out = cfun({x}); @@ -331,6 +333,7 @@ TEST_CASE("test compile unary fused") { // it if we change compile conditions CHECK_EQ(typeid(p), typeid(Reduce)); auto cout = out[0].inputs()[0]; + REQUIRE(cout.has_primitive()); auto& cp = cout.primitive(); CHECK_EQ(typeid(cp), typeid(Compiled)); CHECK_EQ(cout.inputs()[0].id(), x.id()); @@ -345,6 +348,7 @@ TEST_CASE("test compile unary fused") { CHECK_EQ(typeid(p), typeid(Compiled)); auto sout = out[0].inputs()[0]; CHECK_EQ(out[0].inputs().size(), 1); + REQUIRE(sout.has_primitive()); auto& sp = sout.primitive(); CHECK_EQ(typeid(sp), typeid(Reduce)); CHECK_EQ(sout.inputs()[0].id(), x.id()); @@ -421,6 +425,8 @@ TEST_CASE("test compile binary fused") { } { + // Reductions fuse on the GPU, so use the CPU to keep the Reduce + StreamContext ctx(Device::cpu); auto cfun = compile(binary_fused_3); auto x = array({1.0, 2.0}); auto y = array({1.0, 2.0}); @@ -430,6 +436,7 @@ TEST_CASE("test compile binary fused") { CHECK_EQ(typeid(p), typeid(Reduce)); auto cout = out.inputs()[0]; + REQUIRE(cout.has_primitive()); auto& cp = cout.primitive(); CHECK_EQ(typeid(cp), typeid(Compiled)); CHECK_EQ(cout.inputs()[0].id(), x.id());