Skip to content

Instantly share code, notes, and snippets.

@nbx3
Created January 6, 2025 14:02
Show Gist options
  • Save nbx3/b367a2e6ec50e9803f95a400d4ffe076 to your computer and use it in GitHub Desktop.
Save nbx3/b367a2e6ec50e9803f95a400d4ffe076 to your computer and use it in GitHub Desktop.
Patch for llama.cpp multi gpu support
diff --git a/cmake/llama-config.cmake.in b/cmake/llama-config.cmake.in
index 5c55bc6b..69bc027f 100644
--- a/cmake/llama-config.cmake.in
+++ b/cmake/llama-config.cmake.in
@@ -46,6 +46,7 @@ set(GGML_VULKAN_PERF @GGML_VULKAN_PERF@)
set(GGML_VULKAN_VALIDATE @GGML_VULKAN_VALIDATE@)
set(GGML_VULKAN_RUN_TESTS @GGML_VULKAN_RUN_TESTS@)
+set(GGML_METAL_MULTIDEV @GGML_METAL_MULTIDEV@)
set(GGML_METAL_USE_BF16 @GGML_METAL_USE_BF16@)
set(GGML_METAL_NDEBUG @GGML_METAL_NDEBUG@)
set(GGML_METAL_SHADER_DEBUG @GGML_METAL_SHADER_DEBUG@)
diff --git a/ggml/CMakeLists.txt b/ggml/CMakeLists.txt
index e33d9748..ad46e743 100644
--- a/ggml/CMakeLists.txt
+++ b/ggml/CMakeLists.txt
@@ -166,6 +166,7 @@ option(GGML_KOMPUTE "ggml: use Kompute"
option(GGML_METAL "ggml: use Metal" ${GGML_METAL_DEFAULT})
option(GGML_METAL_USE_BF16 "ggml: use bfloat if available" OFF)
option(GGML_METAL_NDEBUG "ggml: disable Metal debugging" OFF)
+option(GGML_METAL_MULTIDEV "ggml: enable Metal multi-device selection" OFF)
option(GGML_METAL_SHADER_DEBUG "ggml: compile Metal with -fno-fast-math" OFF)
option(GGML_METAL_EMBED_LIBRARY "ggml: embed Metal library" ${GGML_METAL})
set (GGML_METAL_MACOSX_VERSION_MIN "" CACHE STRING
diff --git a/ggml/src/ggml-metal/CMakeLists.txt b/ggml/src/ggml-metal/CMakeLists.txt
index 1bad2720..cf67d6f0 100644
--- a/ggml/src/ggml-metal/CMakeLists.txt
+++ b/ggml/src/ggml-metal/CMakeLists.txt
@@ -18,6 +18,10 @@ if (GGML_METAL_NDEBUG)
add_compile_definitions(GGML_METAL_NDEBUG)
endif()
+if (GGML_METAL_MULTIDEV)
+ add_compile_definitions(GGML_METAL_MULTIDEV)
+endif()
+
if (GGML_METAL_USE_BF16)
add_compile_definitions(GGML_METAL_USE_BF16)
endif()
diff --git a/ggml/src/ggml-metal/ggml-metal.m b/ggml/src/ggml-metal/ggml-metal.m
index 28f590f9..6e67274f 100644
--- a/ggml/src/ggml-metal/ggml-metal.m
+++ b/ggml/src/ggml-metal/ggml-metal.m
@@ -58,7 +58,14 @@ static id<MTLDevice> ggml_backend_metal_device_acq(struct ggml_backend_metal_dev
assert(ctx != NULL);
if (ctx->mtl_device == nil) {
+#if defined(GGML_METAL_MULTIDEV)
+ NSArray<id<MTLDevice>> *devices = MTLCopyAllDevices();
+ NSString *user_device_string = [[NSProcessInfo processInfo] environment][@"GGML_METAL_DEVICE"];
+ int user_device_int = [user_device_string intValue];
+ ctx->mtl_device = devices[user_device_int];
+#else
ctx->mtl_device = MTLCreateSystemDefaultDevice();
+#endif
ctx->has_simdgroup_reduction = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment