Skip to content

LLM inference in C/C++

C++ ggml

rpc : fix pre-rdma macOS versions (#27815)

Ryan C2bf041515208
diff --git a/ggml/src/ggml-rpc/CMakeLists.txt b/ggml/src/ggml-rpc/CMakeLists.txt
index b2f086380d..af3bd0290f 100644
--- a/ggml/src/ggml-rpc/CMakeLists.txt
+++ b/ggml/src/ggml-rpc/CMakeLists.txt
@@ -34,10 +34,14 @@ if (GGML_RPC_RDMA)
find_library(RDMA_LIB ${RDMA_LIB_NAME} REQUIRED)
endif()
target_compile_definitions(ggml-rpc PRIVATE GGML_RPC_RDMA)
- target_link_libraries(ggml-rpc PRIVATE ${RDMA_LIB})
if (APPLE)
+ # librdma.dylib only exists on macOS 26.2 and later. Link it weakly so a build made
+ # where it exists still loads where it does not; checked at runtime before use.
+ target_link_options(ggml-rpc PRIVATE "LINKER:-weak_library,${RDMA_LIB}")
target_compile_definitions(ggml-rpc PRIVATE GGML_RPC_RDMA_APPLE)
target_sources(ggml-rpc PRIVATE transport-apple.cpp)
+ else()
+ target_link_libraries(ggml-rpc PRIVATE ${RDMA_LIB})
endif()
message(STATUS " RDMA transport enabled (${RDMA_DESC})")
else()
diff --git a/ggml/src/ggml-rpc/transport-apple.cpp b/ggml/src/ggml-rpc/transport-apple.cpp
index c8be77a6dc..2cfaa5d3fd 100644
--- a/ggml/src/ggml-rpc/transport-apple.cpp
+++ b/ggml/src/ggml-rpc/transport-apple.cpp
@@ -8,6 +8,7 @@
#include <cstdlib>
#include <cstring>
#include <string>
+#include <dlfcn.h>
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>
@@ -184,11 +185,28 @@ static uint8_t rdma_first_active_port(struct ibv_context * ctx, struct ibv_port_
return 0;
}
+// librdma.dylib is weak-linked, so its symbols are null when it is absent. Nothing may
+// call one before this has returned true.
+static bool rdma_library_present() {
+ static const bool present = [] {
+ void * handle = dlopen("/usr/lib/librdma.dylib", RTLD_LAZY);
+ if (handle == nullptr) {
+ return false;
+ }
+ dlclose(handle);
+ return true;
+ }();
+ return present;
+}
+
// Called before the endpoints are exchanged: pick the local device facing this
// peer, create a UC QP and register the frame rings. RDMA is point-to-point, so
// the device is the one whose GID equals the bootstrap connection's local
// address, i.e. the one cabled to the peer.
std::unique_ptr<apple_rdma> apple_rdma::probe(int fd, const uint8_t * target_gid, uint8_t * caps) {
+ if (!rdma_library_present()) {
+ return nullptr;
+ }
int ndev = 0;
ibv_device ** devs = ibv_get_device_list(&ndev);
if (!devs) return nullptr;