forked from ml-explore/mlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_loader.cpp
More file actions
254 lines (210 loc) · 7.33 KB
/
kernel_loader.cpp
File metadata and controls
254 lines (210 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (c) 2024-2026 Lux Industries Inc.
// SPDX-License-Identifier: BSD-3-Clause-Eco
//
// Kernel Loader - Implementation of kernel caching and registry
#include "lux/gpu/kernel_loader.h"
#include <unordered_map>
#include <string>
#include <mutex>
#include <cstring>
namespace lux::gpu {
// =============================================================================
// Kernel Variant Hash
// =============================================================================
struct VariantHash {
size_t operator()(const LuxKernelVariant& v) const {
// FNV-1a hash
size_t h = 14695981039346656037ULL;
if (v.name) {
for (const char* p = v.name; *p; ++p) {
h ^= static_cast<size_t>(*p);
h *= 1099511628211ULL;
}
}
h ^= static_cast<size_t>(v.dtype);
h *= 1099511628211ULL;
h ^= static_cast<size_t>(v.size_hint);
h *= 1099511628211ULL;
h ^= static_cast<size_t>(v.flags);
h *= 1099511628211ULL;
return h;
}
};
struct VariantEqual {
bool operator()(const LuxKernelVariant& a, const LuxKernelVariant& b) const {
if (a.dtype != b.dtype) return false;
if (a.size_hint != b.size_hint) return false;
if (a.flags != b.flags) return false;
if (a.name == nullptr && b.name == nullptr) return true;
if (a.name == nullptr || b.name == nullptr) return false;
return std::strcmp(a.name, b.name) == 0;
}
};
// =============================================================================
// Kernel Cache Implementation
// =============================================================================
} // namespace lux::gpu
// =============================================================================
// LuxKernelCache Implementation (C-linkage compatible)
// =============================================================================
struct LuxKernelCache {
std::mutex mutex;
std::unordered_map<LuxKernelVariant, LuxKernel*, lux::gpu::VariantHash, lux::gpu::VariantEqual> cache;
size_t total_memory = 0;
// Store name strings to ensure lifetime
std::unordered_map<std::string, std::string> name_storage;
};
// =============================================================================
// C API Implementation
// =============================================================================
extern "C" {
LuxKernelCache* lux_kernel_cache_create(void) {
return new LuxKernelCache();
}
void lux_kernel_cache_destroy(LuxKernelCache* cache) {
if (!cache) return;
// Note: We don't destroy kernels here - they may be in use.
// Caller is responsible for clearing cache before destruction.
delete cache;
}
LuxKernel* lux_kernel_cache_get(
LuxKernelCache* cache,
const LuxKernelVariant* variant
) {
if (!cache || !variant) return nullptr;
std::lock_guard<std::mutex> lock(cache->mutex);
auto it = cache->cache.find(*variant);
return (it != cache->cache.end()) ? it->second : nullptr;
}
void lux_kernel_cache_put(
LuxKernelCache* cache,
const LuxKernelVariant* variant,
LuxKernel* kernel
) {
if (!cache || !variant || !kernel) return;
std::lock_guard<std::mutex> lock(cache->mutex);
// Store name string to ensure lifetime
std::string name_key;
if (variant->name) {
name_key = variant->name;
cache->name_storage[name_key] = name_key;
}
// Create stable variant with stored name
LuxKernelVariant stored = *variant;
if (variant->name) {
stored.name = cache->name_storage[name_key].c_str();
}
cache->cache[stored] = kernel;
}
void lux_kernel_cache_clear(LuxKernelCache* cache) {
if (!cache) return;
std::lock_guard<std::mutex> lock(cache->mutex);
cache->cache.clear();
cache->name_storage.clear();
cache->total_memory = 0;
}
void lux_kernel_cache_stats(
LuxKernelCache* cache,
size_t* count,
size_t* memory_bytes
) {
if (!cache) {
if (count) *count = 0;
if (memory_bytes) *memory_bytes = 0;
return;
}
std::lock_guard<std::mutex> lock(cache->mutex);
if (count) *count = cache->cache.size();
if (memory_bytes) *memory_bytes = cache->total_memory;
}
// =============================================================================
// Kernel Registry
// =============================================================================
// Forward declarations of backend registries (defined in generated files)
extern const LuxKernelRegistry* lux_kernel_registry_metal_get(void);
extern const LuxKernelRegistry* lux_kernel_registry_cuda_get(void);
extern const LuxKernelRegistry* lux_kernel_registry_webgpu_get(void);
const LuxKernelRegistry* lux_kernel_registry_get(const char* backend) {
if (!backend) return nullptr;
if (std::strcmp(backend, "metal") == 0) {
return lux_kernel_registry_metal_get();
} else if (std::strcmp(backend, "cuda") == 0) {
return lux_kernel_registry_cuda_get();
} else if (std::strcmp(backend, "webgpu") == 0) {
return lux_kernel_registry_webgpu_get();
}
return nullptr;
}
const LuxEmbeddedKernel* lux_kernel_registry_find(
const LuxKernelRegistry* registry,
const char* name
) {
if (!registry || !name) return nullptr;
for (size_t i = 0; i < registry->count; ++i) {
if (std::strcmp(registry->kernels[i].name, name) == 0) {
return ®istry->kernels[i];
}
}
return nullptr;
}
// =============================================================================
// Kernel Compilation API (backend-specific)
// These are placeholder stubs. Real implementations live in each backend plugin.
// =============================================================================
LuxKernel* lux_kernel_compile(
void* device_context,
const LuxKernelSource* source
) {
// Backend-specific implementation required
// This stub returns nullptr as the core library cannot compile kernels
// without knowing the target backend. Use backend-specific compilation
// or load pre-compiled binaries.
(void)device_context;
(void)source;
return nullptr;
}
LuxKernel* lux_kernel_load_binary(
void* device_context,
const void* binary,
size_t binary_len,
const char* entry_point
) {
// Backend-specific implementation required
(void)device_context;
(void)binary;
(void)binary_len;
(void)entry_point;
return nullptr;
}
void lux_kernel_destroy(LuxKernel* kernel) {
// Kernels are backend-owned; this stub does nothing.
// Each backend tracks and destroys its own kernels.
(void)kernel;
}
const char* lux_kernel_entry_point(LuxKernel* kernel) {
// Backend-specific; stub returns nullptr
(void)kernel;
return nullptr;
}
} // extern "C"
// =============================================================================
// Weak symbols for backend registries (overridden by backends)
// =============================================================================
#if !defined(_WIN32)
__attribute__((weak))
#endif
const LuxKernelRegistry* lux_kernel_registry_metal_get(void) {
return nullptr;
}
#if !defined(_WIN32)
__attribute__((weak))
#endif
const LuxKernelRegistry* lux_kernel_registry_cuda_get(void) {
return nullptr;
}
#if !defined(_WIN32)
__attribute__((weak))
#endif
const LuxKernelRegistry* lux_kernel_registry_webgpu_get(void) {
return nullptr;
}