2024-03-04 20:02:26 -08:00
|
|
|
// Copyright © 2023-2024 Apple Inc.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2025-01-07 17:27:55 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
2024-12-24 11:19:13 -08:00
|
|
|
#include "mlx/array.h"
|
2024-03-04 20:02:26 -08:00
|
|
|
|
|
|
|
|
namespace mlx::core::detail {
|
|
|
|
|
|
2025-09-30 13:33:27 -07:00
|
|
|
using ArraysAndExtra = std::pair<std::vector<array>, std::shared_ptr<void>>;
|
|
|
|
|
using ArrayFnWithExtra =
|
|
|
|
|
std::function<ArraysAndExtra(const std::vector<array>&)>;
|
|
|
|
|
|
2024-10-14 16:17:03 -07:00
|
|
|
// This is not part of the general C++ API as calling with a bad id is a bad
|
|
|
|
|
// idea.
|
|
|
|
|
std::function<std::vector<array>(const std::vector<array>&)> compile(
|
2024-12-06 13:13:21 -08:00
|
|
|
std::function<std::vector<array>(const std::vector<array>&)> fun,
|
2024-10-14 16:17:03 -07:00
|
|
|
std::uintptr_t fun_id,
|
|
|
|
|
bool shapeless = false,
|
|
|
|
|
std::vector<uint64_t> constants = {});
|
|
|
|
|
|
2025-09-30 13:33:27 -07:00
|
|
|
ArrayFnWithExtra compile(
|
|
|
|
|
ArrayFnWithExtra fun,
|
|
|
|
|
std::uintptr_t fun_id,
|
|
|
|
|
bool shapeless,
|
|
|
|
|
std::vector<uint64_t> constants);
|
|
|
|
|
|
2024-10-14 16:17:03 -07:00
|
|
|
// Erase cached compile functions
|
|
|
|
|
void compile_erase(std::uintptr_t fun_id);
|
2024-03-04 20:02:26 -08:00
|
|
|
|
2024-10-14 16:17:03 -07:00
|
|
|
// Clear the compiler cache causing a recompilation of all compiled functions
|
|
|
|
|
// when called again.
|
|
|
|
|
void compile_clear_cache();
|
|
|
|
|
|
|
|
|
|
bool compile_available_for_device(const Device& device);
|
2024-12-24 11:19:13 -08:00
|
|
|
|
2025-09-30 13:33:27 -07:00
|
|
|
std::tuple<std::vector<array>, std::vector<array>, std::shared_ptr<void>>
|
|
|
|
|
compile_trace(
|
|
|
|
|
const ArrayFnWithExtra& fun,
|
2025-01-09 11:04:24 -08:00
|
|
|
const std::vector<array>& inputs,
|
|
|
|
|
bool shapeless);
|
2024-12-24 11:19:13 -08:00
|
|
|
|
|
|
|
|
using ParentsMap =
|
|
|
|
|
std::unordered_map<std::uintptr_t, std::vector<std::pair<array, int>>>;
|
|
|
|
|
|
|
|
|
|
// Traverses the graph to build a tape and a map of array ids to their parents
|
|
|
|
|
std::pair<std::vector<array>, ParentsMap> compile_dfs(
|
|
|
|
|
const std::vector<array>& inputs,
|
|
|
|
|
const std::vector<array>& outputs,
|
|
|
|
|
const std::vector<array>& original_inputs);
|
|
|
|
|
|
2025-01-09 11:23:19 -08:00
|
|
|
// Simplify the tape.
|
2024-12-24 11:19:13 -08:00
|
|
|
void compile_simplify(
|
|
|
|
|
std::vector<array>& tape,
|
|
|
|
|
ParentsMap& parents_map,
|
2025-01-09 11:23:19 -08:00
|
|
|
std::vector<array>& outputs,
|
2024-12-24 11:19:13 -08:00
|
|
|
int passes);
|
|
|
|
|
|
|
|
|
|
std::vector<array> compile_replace(
|
|
|
|
|
const std::vector<array>& tape,
|
|
|
|
|
const std::vector<array>& trace_inputs,
|
|
|
|
|
const std::vector<array>& trace_outputs,
|
|
|
|
|
const std::vector<array>& inputs,
|
|
|
|
|
bool shapeless);
|
|
|
|
|
|
|
|
|
|
void compile_validate_shapeless(const std::vector<array>& tape);
|
|
|
|
|
|
2024-10-14 16:17:03 -07:00
|
|
|
} // namespace mlx::core::detail
|