Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mlx/backend/metal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 36 additions & 17 deletions mlx/backend/metal/compiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -13,6 +14,29 @@

namespace mlx::core {

void build_tape(
std::string& os,
const std::vector<array>& 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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -265,6 +273,17 @@ 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_,
inputs,
outputs[0]);
}

// Get the kernel if someone else built it already
auto& s = stream();
auto& d = metal::device(s.device);
Expand Down
29 changes: 29 additions & 0 deletions mlx/backend/metal/compiled.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright © 2026 Apple Inc.

#pragma once

#include <string>

#include "mlx/array.h"
#include "mlx/graph_utils.h"
#include "mlx/small_unordered_set.h"

namespace mlx::core {

// Write the computation of the tape into tmp_* variables.
void build_tape(
std::string& os,
const std::vector<array>& 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<array>& trace_inputs,
const std::vector<array>& tape,
const SmallUnorderedSet<uintptr_t>& constant_ids,
const std::vector<array>& inputs,
array& out);

} // namespace mlx::core
Loading
Loading