Skip to content

Commit 78d16d0

Browse files
committed
build: integrate the remoting-backend skeleton
1 parent a54229d commit 78d16d0

File tree

9 files changed

+154
-0
lines changed

9 files changed

+154
-0
lines changed

CMakePresets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
{ "name": "sycl_f16", "hidden": true, "cacheVariables": { "GGML_SYCL_F16": "ON" } },
3232
{ "name": "vulkan", "hidden": true, "cacheVariables": { "GGML_VULKAN": "ON" } },
3333
{ "name": "remoting_frontend", "hidden": true, "cacheVariables": { "GGML_REMOTING_FRONTEND": "ON" } },
34+
{ "name": "remoting_backend", "hidden": true, "cacheVariables": { "GGML_REMOTING_BACKEND": "ON" } },
3435

3536
{
3637
"name": "x64-windows-llvm", "hidden": true,

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,11 @@ ifdef GGML_REMOTING_FRONTEND
721721
OBJ_GGML_EXT += ggml/src/ggml-remotingfrontend/ggml-remoting-frontend.o
722722
endif
723723

724+
ifdef GGML_REMOTING_BACKEND
725+
MK_CPPFLAGS += -DGGML_USE_REMOTINGBACKEND
726+
OBJ_GGML_EXT += ggml/src/ggml-remotingbackend/ggml-remoting-backend.o
727+
endif
728+
724729
ifdef GGML_VULKAN
725730
MK_CPPFLAGS += -DGGML_USE_VULKAN
726731
MK_LDFLAGS += $(shell pkg-config --libs vulkan)
@@ -763,6 +768,9 @@ ggml/src/ggml-vulkan.o: ggml/src/ggml-vulkan/ggml-vulkan.cpp ggml/include/ggml-v
763768
ggml/src/ggml-remotingfrontend/frontend.o: ggml/src/ggml-remotingfrontend/frontend.cpp
764769
$(CXX) $(CXXFLAGS) -c $< -o $@
765770

771+
ggml/src/ggml-remotingbackend/backend.o: ggml/src/ggml-remotingbackend/backend.cpp
772+
$(CXX) $(CXXFLAGS) -c $< -o $@
773+
766774
$(_ggml_vk_header): $(_ggml_vk_source)
767775

768776
$(_ggml_vk_source): $(_ggml_vk_shader_deps) vulkan-shaders-gen

ggml/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ option(GGML_VULKAN_PERF "ggml: enable Vulkan perf output"
180180
option(GGML_VULKAN_VALIDATE "ggml: enable Vulkan validation" OFF)
181181
option(GGML_VULKAN_RUN_TESTS "ggml: run Vulkan tests" OFF)
182182
option(GGML_REMOTING_FRONTEND "ggml: use the API Remoting frontend" OFF)
183+
option(GGML_REMOTING_BACKEND "ggml: use the API Remoting backend" OFF)
183184
option(GGML_KOMPUTE "ggml: use Kompute" OFF)
184185
option(GGML_METAL "ggml: use Metal" ${GGML_METAL_DEFAULT})
185186
option(GGML_METAL_USE_BF16 "ggml: use bfloat if available" OFF)
@@ -271,6 +272,7 @@ set(GGML_PUBLIC_HEADERS
271272
include/ggml-sycl.h
272273
include/ggml-vulkan.h
273274
ggml/include/ggml-remoting-frontend.h
275+
ggml/include/ggml-remoting-backend.h
274276
include/gguf.h)
275277

276278
set_target_properties(ggml PROPERTIES PUBLIC_HEADER "${GGML_PUBLIC_HEADERS}")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "ggml.h"
4+
#include "ggml-backend.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
#define GGML_REMOTING_BACKEND_NAME "RemotingBackend"
11+
12+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_remoting_backend_reg();
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif

ggml/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ ggml_add_backend(RPC)
310310
ggml_add_backend(SYCL)
311311
ggml_add_backend(Vulkan)
312312
ggml_add_backend(RemotingFrontend)
313+
ggml_add_backend(RemotingBackend)
313314
ggml_add_backend(OpenCL)
314315

315316
foreach (target ggml-base ggml)

ggml/src/ggml-backend-reg.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
#include "ggml-remoting-frontend.h"
5050
#endif
5151

52+
#ifdef GGML_USE_REMOTINGBACKEND
53+
#include "ggml-remoting-backend.h"
54+
#endif
55+
5256
#ifdef GGML_USE_OPENCL
5357
#include "ggml-opencl.h"
5458
#endif
@@ -179,6 +183,9 @@ struct ggml_backend_registry {
179183
#ifdef GGML_USE_REMOTINGFRONTEND
180184
register_backend(ggml_backend_remoting_frontend_reg());
181185
#endif
186+
#ifdef GGML_USE_REMOTINGBACKEND
187+
register_backend(ggml_backend_remoting_backend_reg());
188+
#endif
182189
#ifdef GGML_USE_OPENCL
183190
register_backend(ggml_backend_opencl_reg());
184191
#endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.19)
2+
cmake_policy(SET CMP0114 NEW)
3+
4+
message(STATUS "Enable API Remoting backend")
5+
6+
ggml_add_backend_library(ggml-remotingbackend
7+
backend.cpp
8+
../../include/ggml-remoting-backend.h
9+
)
10+
11+
target_compile_options(ggml-remotingbackend PRIVATE -std=c++20)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <cstdio>
2+
#include <cstdarg>
3+
4+
static inline void LOG(const char* fmt, ...) {
5+
va_list args;
6+
va_start(args, fmt);
7+
vprintf(fmt, args);
8+
va_end(args);
9+
10+
printf("\n");
11+
}
12+
13+
static inline void FATAL(const char* fmt, ...) {
14+
printf("FATAL: ");
15+
va_list args;
16+
va_start(args, fmt);
17+
vprintf(fmt, args);
18+
va_end(args);
19+
20+
printf("\n");
21+
22+
if (!fmt)
23+
return; // avoid the noreturn attribute
24+
25+
exit(1);
26+
}
27+
28+
extern "C" {
29+
void ggml_backend_remoting_backend_say_hello();
30+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include <dlfcn.h>
3+
4+
#include "ggml-remoting-backend.h"
5+
6+
#include "ggml-impl.h"
7+
#include "ggml-backend-impl.h"
8+
#include "ggml-backend.h"
9+
10+
#include "backend-internal.h"
11+
12+
#define UNUSED GGML_UNUSED
13+
14+
static size_t ggml_backend_remoting_reg_get_device_count(ggml_backend_reg_t reg) {
15+
UNUSED(reg);
16+
return 0;
17+
}
18+
19+
static const char * ggml_backend_remoting_reg_get_name(ggml_backend_reg_t reg) {
20+
UNUSED(reg);
21+
return GGML_REMOTING_BACKEND_NAME;
22+
}
23+
24+
static ggml_backend_dev_t ggml_backend_remoting_reg_get_device(ggml_backend_reg_t reg, size_t device) {
25+
UNUSED(reg);
26+
UNUSED(device);
27+
28+
return NULL;
29+
}
30+
31+
static const struct ggml_backend_reg_i ggml_backend_remoting_reg_i = {
32+
/* .get_name = */ ggml_backend_remoting_reg_get_name,
33+
/* .get_device_count = */ ggml_backend_remoting_reg_get_device_count,
34+
/* .get_device = */ ggml_backend_remoting_reg_get_device,
35+
/* .get_proc_address = */ NULL,
36+
};
37+
38+
ggml_backend_reg_t ggml_backend_remoting_backend_reg() {
39+
static ggml_backend_reg reg = {
40+
/* .api_version = */ GGML_BACKEND_API_VERSION,
41+
/* .iface = */ ggml_backend_remoting_reg_i,
42+
/* .context = */ nullptr,
43+
};
44+
45+
LOG("%s, hello :wave:", __func__);
46+
47+
return &reg;
48+
}
49+
50+
typedef ggml_backend_reg_t (*backend_reg_fct_t)(void);
51+
52+
#define METAL_LIBRARY_PATH "/Users/kevinpouget/remoting/llama_cpp/build.remoting-backend/bin/libggml-metal.dylib"
53+
#define ENTRYPOINT_FCT_NAME "ggml_backend_metal_reg"
54+
55+
extern "C" {
56+
void ggml_backend_remoting_backend_say_hello() {
57+
LOG("%s: hello :wave: \\o/", __func__);
58+
59+
void * library_handle = dlopen(METAL_LIBRARY_PATH, RTLD_LAZY);
60+
61+
if (!library_handle) {
62+
FATAL("Cannot open library: %s\n", dlerror());
63+
return;
64+
}
65+
66+
backend_reg_fct_t entrypoint_fct = (backend_reg_fct_t) dlsym(library_handle, ENTRYPOINT_FCT_NAME);
67+
const char* dlsym_error = dlerror();
68+
if (dlsym_error) {
69+
FATAL("Cannot load symbol: %s\n", dlsym_error);
70+
return;
71+
}
72+
73+
ggml_backend_reg_t reg = entrypoint_fct();
74+
LOG("%s: --> %s", __func__, reg->iface.get_name(reg));
75+
76+
dlclose(library_handle);
77+
}
78+
}

0 commit comments

Comments
 (0)