MLX
Loading...
Searching...
No Matches
device.h
Go to the documentation of this file.
1// Copyright © 2023-2024 Apple Inc.
2
3#pragma once
4
5#include <Metal/Metal.hpp>
6#include <dlfcn.h>
7#include <filesystem>
8#include <functional>
9#include <mutex>
10#include <shared_mutex>
11#include <string>
12#include <unordered_map>
13#include <unordered_set>
14
15#include "mlx/array.h"
16#include "mlx/device.h"
17
18namespace fs = std::filesystem;
19
20namespace mlx::core::metal {
21
22// Note, this function must be left inline in a header so that it is not
23// dynamically linked.
24inline std::string get_colocated_mtllib_path(const std::string& lib_name) {
25 Dl_info info;
26 std::string mtllib_path;
27 std::string lib_ext = lib_name + ".metallib";
28
29 int success = dladdr((void*)get_colocated_mtllib_path, &info);
30 if (success) {
31 auto mtllib = fs::path(info.dli_fname).remove_filename() / lib_ext;
32 mtllib_path = mtllib.c_str();
33 }
34
35 return mtllib_path;
36}
37
38using MTLFCList =
39 std::vector<std::tuple<const void*, MTL::DataType, NS::UInteger>>;
40
42 CommandEncoder(MTL::CommandBuffer* cbuf);
45
48 enc.concurrent = true;
49 }
51 enc.concurrent = false;
52 enc.outputs.insert(
53 enc.concurrent_outputs.begin(), enc.concurrent_outputs.end());
54 enc.concurrent_outputs.clear();
55 }
56
57 private:
58 CommandEncoder& enc;
59 };
60
61 MTL::ComputeCommandEncoder* operator->() {
62 return enc;
63 }
64
65 void set_input_array(const array& a, int idx, int64_t offset = 0);
66 void set_output_array(array& a, int idx, int64_t offset = 0);
67 void dispatchThreadgroups(MTL::Size grid_dims, MTL::Size group_dims);
68 void dispatchThreads(MTL::Size grid_dims, MTL::Size group_dims);
69
73
75
76 private:
77 void maybe_split();
78
79 int num_dispatches{0};
80 MTL::CommandBuffer* cbuf;
81 MTL::ComputeCommandEncoder* enc;
82 bool concurrent{false};
83 std::unordered_set<MTL::Resource*> outputs;
84 std::unordered_set<MTL::Resource*> concurrent_outputs;
85};
86
87class Device {
88 public:
90 Device(const Device&) = delete;
91 Device& operator=(const Device&) = delete;
93
94 MTL::Device* mtl_device() {
95 return device_;
96 };
97
98 void new_queue(int index);
99 MTL::CommandBuffer* get_command_buffer(int index);
102 void commit_command_buffer(int index);
104 void end_encoding(int index);
105
107 const std::string& lib_name,
108 const std::string& lib_path);
109
110 // Note, this should remain in the header so that it is not dynamically
111 // linked
112 void register_library(const std::string& lib_name) {
113 if (auto it = library_map_.find(lib_name); it == library_map_.end()) {
114 register_library(lib_name, get_colocated_mtllib_path(lib_name));
115 }
116 }
117
118 MTL::Library* get_library(
119 const std::string& name,
120 const std::function<std::string(void)>& builder);
121
122 MTL::ComputePipelineState* get_kernel(
123 const std::string& base_name,
124 MTL::Library* mtl_lib,
125 const std::string& hash_name = "",
126 const MTLFCList& func_consts = {},
127 const std::vector<MTL::Function*>& linked_functions = {});
128
129 MTL::ComputePipelineState* get_kernel(
130 const std::string& base_name,
131 const std::string& lib_name = "mlx",
132 const std::string& hash_name = "",
133 const MTLFCList& func_consts = {},
134 const std::vector<MTL::Function*>& linked_functions = {});
135
136 MTL::ArgumentEncoder* argument_encoder(
137 const std::vector<MTL::ArgumentDescriptor*>& arg_descs) const;
138
139 private:
140 MTL::Library* get_library_cache_(const std::string& name);
141
142 MTL::Library* get_library_(const std::string& name);
143 MTL::Library* build_library_(const std::string& source_string);
144
145 MTL::Function* get_function_(const std::string& name, MTL::Library* mtl_lib);
146
147 MTL::Function* get_function_(
148 const std::string& name,
149 const std::string& specialized_name,
150 const MTLFCList& func_consts,
151 MTL::Library* mtl_lib);
152
153 MTL::LinkedFunctions* get_linked_functions_(
154 const std::vector<MTL::Function*>& funcs);
155
156 MTL::ComputePipelineState* get_kernel_(
157 const std::string& name,
158 const MTL::Function* mtl_function);
159
160 MTL::ComputePipelineState* get_kernel_(
161 const std::string& name,
162 const MTL::Function* mtl_function,
163 const MTL::LinkedFunctions* linked_functions);
164
165 MTL::ComputePipelineState* get_kernel_(
166 const std::string& base_name,
167 MTL::Library* mtl_lib,
168 const std::string& hash_name,
169 const MTLFCList& func_consts = {},
170 const std::vector<MTL::Function*>& linked_functions = {});
171
172 MTL::Device* device_;
173 std::unordered_map<int32_t, MTL::CommandQueue*> queue_map_;
174 std::unordered_map<int32_t, std::pair<int, MTL::CommandBuffer*>> buffer_map_;
175 std::unordered_map<int32_t, std::unique_ptr<CommandEncoder>> encoder_map_;
176
177 std::shared_mutex kernel_mtx_;
178 std::unordered_map<std::string, MTL::ComputePipelineState*> kernel_map_;
179
180 std::shared_mutex library_mtx_;
181 std::unordered_map<std::string, MTL::Library*> library_map_;
182};
183
185
186} // namespace mlx::core::metal
Definition array.h:20
Definition device.h:87
int get_command_buffer_ops(int index)
MTL::Device * mtl_device()
Definition device.h:94
void register_library(const std::string &lib_name, const std::string &lib_path)
MTL::CommandBuffer * get_command_buffer(int index)
void end_encoding(int index)
MTL::ComputePipelineState * get_kernel(const std::string &base_name, MTL::Library *mtl_lib, const std::string &hash_name="", const MTLFCList &func_consts={}, const std::vector< MTL::Function * > &linked_functions={})
MTL::ArgumentEncoder * argument_encoder(const std::vector< MTL::ArgumentDescriptor * > &arg_descs) const
MTL::Library * get_library(const std::string &name, const std::function< std::string(void)> &builder)
void increment_command_buffer_ops(int index)
void new_queue(int index)
void commit_command_buffer(int index)
void register_library(const std::string &lib_name)
Definition device.h:112
Device(const Device &)=delete
Device & operator=(const Device &)=delete
MTL::ComputePipelineState * get_kernel(const std::string &base_name, const std::string &lib_name="mlx", const std::string &hash_name="", const MTLFCList &func_consts={}, const std::vector< MTL::Function * > &linked_functions={})
CommandEncoder & get_command_encoder(int index)
Definition allocator.h:12
std::string get_colocated_mtllib_path(const std::string &lib_name)
Definition device.h:24
std::vector< std::tuple< const void *, MTL::DataType, NS::UInteger > > MTLFCList
Definition device.h:38
Device & device(mlx::core::Device)
Definition device.h:7
ConcurrentContext(CommandEncoder &enc)
Definition device.h:47
Definition device.h:41
void dispatchThreads(MTL::Size grid_dims, MTL::Size group_dims)
CommandEncoder(MTL::CommandBuffer *cbuf)
CommandEncoder & operator=(const CommandEncoder &)=delete
ConcurrentContext start_concurrent()
Definition device.h:70
void set_output_array(array &a, int idx, int64_t offset=0)
void dispatchThreadgroups(MTL::Size grid_dims, MTL::Size group_dims)
MTL::ComputeCommandEncoder * operator->()
Definition device.h:61
void set_input_array(const array &a, int idx, int64_t offset=0)
CommandEncoder(const CommandEncoder &)=delete