Skip to content

Commit 5c716bd

Browse files
olilarkinzcbenz
authored andcommitted
Add set_metallib_path() for custom metallib location
Allows overriding the default metallib search paths before Device initialization. Needed when MLX is embedded in plugin bundles (AU, VST3) where the standard search locations don't apply.
1 parent 602b535 commit 5c716bd

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

mlx/backend/metal/device.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ struct hash<NS::SharedPtr<T>> {
3030

3131
namespace mlx::core::metal {
3232

33+
static std::string g_metallib_override_path;
34+
35+
void set_metallib_path(const std::string& path) {
36+
g_metallib_override_path = path;
37+
}
38+
3339
namespace {
3440

3541
constexpr const char* default_mtllib_path = METAL_PATH;
@@ -162,6 +168,15 @@ std::pair<MTL::Library*, NS::Error*> load_swiftpm_library(
162168
}
163169

164170
MTL::Library* load_default_library(MTL::Device* device) {
171+
// Check override path first
172+
if (!g_metallib_override_path.empty()) {
173+
auto [lib, error] =
174+
load_library_from_path(device, g_metallib_override_path.c_str());
175+
if (lib) {
176+
return lib;
177+
}
178+
}
179+
165180
NS::Error* error[5];
166181
MTL::Library* lib;
167182
// First try the colocated mlx.metallib

mlx/backend/metal/metal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
namespace mlx::core::metal {
1212

13+
/* Set a custom path to mlx.metallib. Must be called before any MLX operation.
14+
*/
15+
void set_metallib_path(const std::string& path);
16+
1317
/* Check if the Metal backend is available. */
1418
MLX_API bool is_available();
1519

0 commit comments

Comments
 (0)