mlx/mlx/transforms_impl.h
Awni Hannun 1ccaf80575
Dynamic broadcasting for shapeless compile/export (#1722)
* working towards dynamic broadcast

* shapeless broadcast

* fix build + nits

* use broadcast arrays in quantize matmul

* some cleanup / consistency

* mend

* some comments

* add vjp, jvp for broadcast axes
2025-01-09 11:04:24 -08:00

75 lines
1.8 KiB
C++

// Copyright © 2023-2024 Apple Inc.
#pragma once
namespace mlx::core::detail {
std::pair<std::vector<array>, std::vector<array>> vmap_trace(
const std::function<std::vector<array>(const std::vector<array>&)>& fun,
const std::vector<array>& inputs,
const std::vector<int>& in_axes);
std::vector<array> vmap_replace(
const std::vector<array>& inputs,
const std::vector<array>& s_inputs,
const std::vector<array>& s_outputs,
const std::vector<int>& in_axes,
const std::vector<int>& out_axes);
// Create an InTracing object during tracing operations to signify to the rest
// of the codebase that we are during tracing so evals should not throw away
// the graph.
struct InTracing {
explicit InTracing(bool dynamic = false) {
trace_stack.push_back(dynamic);
}
~InTracing() {
trace_stack.pop_back();
}
static bool in_tracing() {
return !trace_stack.empty();
}
static bool in_dynamic_tracing() {
// compile is always and only the outer-most transform
return in_tracing() && trace_stack.front();
}
private:
static std::vector<bool> trace_stack;
};
struct RetainGraph {
RetainGraph() {
tracing_counter++;
}
~RetainGraph() {
tracing_counter--;
}
static bool retain_graph() {
return tracing_counter > 0;
}
private:
static int tracing_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() {
return detail::InTracing::in_tracing();
}
/** Return true if we are in a dynamic (shapeless) trace used for compiling or
* exporting graphs with dynamic shapes. */
inline bool in_dynamic_tracing() {
return detail::InTracing::in_dynamic_tracing();
}
inline bool retain_graph() {
return detail::RetainGraph::retain_graph();
}
} // namespace mlx::core::detail