rgerganov commited on
Commit
5838a14
·
unverified ·
1 Parent(s): 328702a

ggml : add RPC backend (llama/6829)

Browse files

* ggml : add RPC backend

The RPC backend proxies all operations to a remote server which runs a
regular backend (CPU, CUDA, Metal, etc).

* set TCP_NODELAY

* add CI workflows

* Address review comments

* fix warning

* implement llama_max_devices() for RPC

* Address review comments

* Address review comments

* wrap sockfd into a struct

* implement get_alignment and get_max_size

* add get_device_memory

* fix warning

* win32 support

* add README

* readme : trim trailing whitespace

* Address review comments

* win32 fix

* Address review comments

* fix compile warnings on macos

Files changed (2) hide show
  1. ggml-rpc.cpp +1023 -0
  2. ggml-rpc.h +24 -0
ggml-rpc.cpp ADDED
@@ -0,0 +1,1023 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "ggml-rpc.h"
2
+ #include "ggml.h"
3
+ #include "ggml-backend-impl.h"
4
+
5
+ #include <cinttypes>
6
+ #include <string>
7
+ #include <vector>
8
+ #include <memory>
9
+ #include <unordered_map>
10
+ #include <unordered_set>
11
+ #ifdef _WIN32
12
+ # define WIN32_LEAN_AND_MEAN
13
+ # ifndef NOMINMAX
14
+ # define NOMINMAX
15
+ # endif
16
+ # include <windows.h>
17
+ # include <winsock2.h>
18
+ #else
19
+ # include <arpa/inet.h>
20
+ # include <sys/socket.h>
21
+ # include <sys/types.h>
22
+ # include <netinet/in.h>
23
+ # include <netinet/tcp.h>
24
+ # include <netdb.h>
25
+ # include <unistd.h>
26
+ #endif
27
+ #include <string.h>
28
+
29
+ #define UNUSED GGML_UNUSED
30
+
31
+ #define GGML_DEBUG 1
32
+ #if (GGML_DEBUG >= 1)
33
+ #define GGML_PRINT_DEBUG(...) printf(__VA_ARGS__)
34
+ #else
35
+ #define GGML_PRINT_DEBUG(...)
36
+ #endif
37
+
38
+ #ifdef _WIN32
39
+ typedef SOCKET sockfd_t;
40
+ using ssize_t = __int64;
41
+ #else
42
+ typedef int sockfd_t;
43
+ #endif
44
+
45
+ // cross-platform socket
46
+ struct socket_t {
47
+ sockfd_t fd;
48
+ socket_t(sockfd_t fd) : fd(fd) {}
49
+ ~socket_t() {
50
+ #ifdef _WIN32
51
+ closesocket(this->fd);
52
+ #else
53
+ close(this->fd);
54
+ #endif
55
+ }
56
+ };
57
+
58
+ // ggml_tensor is serialized into rpc_tensor
59
+ struct rpc_tensor {
60
+ uint64_t id;
61
+ uint32_t type;
62
+ uint64_t buffer;
63
+ uint32_t ne[GGML_MAX_DIMS];
64
+ uint32_t nb[GGML_MAX_DIMS];
65
+ uint32_t op;
66
+ int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
67
+ int32_t flags;
68
+ uint64_t src[GGML_MAX_SRC];
69
+ uint64_t view_src;
70
+ uint64_t view_offs;
71
+ uint64_t data;
72
+ char name[GGML_MAX_NAME];
73
+ };
74
+
75
+ // RPC commands
76
+ enum rpc_cmd {
77
+ ALLOC_BUFFER = 0,
78
+ GET_ALIGNMENT,
79
+ GET_MAX_SIZE,
80
+ BUFFER_GET_BASE,
81
+ FREE_BUFFER,
82
+ BUFFER_CLEAR,
83
+ SET_TENSOR,
84
+ GET_TENSOR,
85
+ COPY_TENSOR,
86
+ GRAPH_COMPUTE,
87
+ GET_DEVICE_MEMORY,
88
+ };
89
+
90
+ // RPC data structures
91
+
92
+ static ggml_guid_t ggml_backend_rpc_guid() {
93
+ static ggml_guid guid = {0x99, 0x68, 0x5b, 0x6c, 0xd2, 0x83, 0x3d, 0x24, 0x25, 0x36, 0x72, 0xe1, 0x5b, 0x0e, 0x14, 0x03};
94
+ return &guid;
95
+ }
96
+
97
+ struct ggml_backend_rpc_buffer_type_context {
98
+ std::shared_ptr<socket_t> sock;
99
+ std::string name;
100
+ size_t alignment;
101
+ size_t max_size;
102
+ };
103
+
104
+ struct ggml_backend_rpc_context {
105
+ std::string endpoint;
106
+ std::string name;
107
+ std::shared_ptr<socket_t> sock;
108
+ ggml_backend_buffer_type_t buft;
109
+ };
110
+
111
+ struct ggml_backend_rpc_buffer_context {
112
+ std::shared_ptr<socket_t> sock;
113
+ std::unordered_map<ggml_backend_buffer_t, void *> base_cache;
114
+ uint64_t remote_ptr;
115
+ std::string name;
116
+ };
117
+
118
+ // RPC helper functions
119
+
120
+ static std::shared_ptr<socket_t> make_socket(sockfd_t fd) {
121
+ #ifdef _WIN32
122
+ if (fd == INVALID_SOCKET) {
123
+ return nullptr;
124
+ }
125
+ #else
126
+ if (fd < 0) {
127
+ return nullptr;
128
+ }
129
+ #endif
130
+ return std::make_shared<socket_t>(fd);
131
+ }
132
+
133
+ static bool set_no_delay(sockfd_t sockfd) {
134
+ int flag = 1;
135
+ // set TCP_NODELAY to disable Nagle's algorithm
136
+ int ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
137
+ return ret >= 0;
138
+ }
139
+
140
+ static std::shared_ptr<socket_t> socket_connect(const char * host, int port) {
141
+ struct sockaddr_in addr;
142
+ auto sockfd = socket(AF_INET, SOCK_STREAM, 0);
143
+ auto sock_ptr = make_socket(sockfd);
144
+ if (sock_ptr == nullptr) {
145
+ return nullptr;
146
+ }
147
+ if (!set_no_delay(sockfd)) {
148
+ fprintf(stderr, "Failed to set TCP_NODELAY\n");
149
+ return nullptr;
150
+ }
151
+ addr.sin_family = AF_INET;
152
+ addr.sin_port = htons(port);
153
+ struct hostent * server = gethostbyname(host);
154
+ if (server == NULL) {
155
+ fprintf(stderr, "Cannot resolve host '%s'\n", host);
156
+ return nullptr;
157
+ }
158
+ memcpy(&addr.sin_addr.s_addr, server->h_addr, server->h_length);
159
+ if (connect(sock_ptr->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
160
+ return nullptr;
161
+ }
162
+ return sock_ptr;
163
+ }
164
+
165
+ static std::shared_ptr<socket_t> socket_accept(sockfd_t srv_sockfd) {
166
+ auto client_socket_fd = accept(srv_sockfd, NULL, NULL);
167
+ auto client_socket = make_socket(client_socket_fd);
168
+ if (client_socket == nullptr) {
169
+ return nullptr;
170
+ }
171
+ if (!set_no_delay(client_socket_fd)) {
172
+ fprintf(stderr, "Failed to set TCP_NODELAY\n");
173
+ return nullptr;
174
+ }
175
+ return client_socket;
176
+ }
177
+
178
+ static std::shared_ptr<socket_t> create_server_socket(const char * host, int port) {
179
+ auto sockfd = socket(AF_INET, SOCK_STREAM, 0);
180
+ auto sock = make_socket(sockfd);
181
+ if (sock == nullptr) {
182
+ return nullptr;
183
+ }
184
+
185
+ struct sockaddr_in serv_addr;
186
+ serv_addr.sin_family = AF_INET;
187
+ serv_addr.sin_addr.s_addr = inet_addr(host);
188
+ serv_addr.sin_port = htons(port);
189
+
190
+ if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
191
+ return nullptr;
192
+ }
193
+ if (listen(sockfd, 1) < 0) {
194
+ return nullptr;
195
+ }
196
+ return sock;
197
+ }
198
+
199
+ static bool send_data(sockfd_t sockfd, const void * data, size_t size) {
200
+ size_t bytes_sent = 0;
201
+ while (bytes_sent < size) {
202
+ ssize_t n = send(sockfd, (const char *)data + bytes_sent, size - bytes_sent, 0);
203
+ if (n < 0) {
204
+ return false;
205
+ }
206
+ bytes_sent += n;
207
+ }
208
+ return true;
209
+ }
210
+
211
+ static bool recv_data(sockfd_t sockfd, void * data, size_t size) {
212
+ size_t bytes_recv = 0;
213
+ while (bytes_recv < size) {
214
+ ssize_t n = recv(sockfd, (char *)data + bytes_recv, size - bytes_recv, 0);
215
+ if (n <= 0) {
216
+ return false;
217
+ }
218
+ bytes_recv += n;
219
+ }
220
+ return true;
221
+ }
222
+
223
+ static bool parse_endpoint(const char * endpoint, std::string & host, int & port) {
224
+ std::string str(endpoint);
225
+ size_t pos = str.find(':');
226
+ if (pos == std::string::npos) {
227
+ return false;
228
+ }
229
+ host = str.substr(0, pos);
230
+ port = std::stoi(str.substr(pos + 1));
231
+ return true;
232
+ }
233
+
234
+ // RPC request : | rpc_cmd (1 byte) | request_size (8 bytes) | request_data (request_size bytes) |
235
+ // RPC response: | response_size (8 bytes) | response_data (response_size bytes) |
236
+ static bool send_rpc_cmd(const std::shared_ptr<socket_t> & sock, enum rpc_cmd cmd, const std::vector<uint8_t> & input, std::vector<uint8_t> & output) {
237
+ uint8_t cmd_byte = cmd;
238
+ if (!send_data(sock->fd, &cmd_byte, sizeof(cmd_byte))) {
239
+ return false;
240
+ }
241
+ uint64_t input_size = input.size();
242
+ if (!send_data(sock->fd, &input_size, sizeof(input_size))) {
243
+ return false;
244
+ }
245
+ if (!send_data(sock->fd, input.data(), input.size())) {
246
+ return false;
247
+ }
248
+ uint64_t output_size;
249
+ if (!recv_data(sock->fd, &output_size, sizeof(output_size))) {
250
+ return false;
251
+ }
252
+ if (output_size == 0) {
253
+ output.clear();
254
+ return true;
255
+ }
256
+ output.resize(output_size);
257
+ if (!recv_data(sock->fd, output.data(), output_size)) {
258
+ return false;
259
+ }
260
+ return true;
261
+ }
262
+
263
+ // RPC client-side implementation
264
+
265
+ GGML_CALL static const char * ggml_backend_rpc_buffer_get_name(ggml_backend_buffer_t buffer) {
266
+ ggml_backend_rpc_buffer_context * ctx = (ggml_backend_rpc_buffer_context *)buffer->context;
267
+ return ctx->name.c_str();
268
+ }
269
+
270
+ GGML_CALL static void ggml_backend_rpc_buffer_free_buffer(ggml_backend_buffer_t buffer) {
271
+ ggml_backend_rpc_buffer_context * ctx = (ggml_backend_rpc_buffer_context *)buffer->context;
272
+ // input serialization format: | remote_ptr (8 bytes) |
273
+ std::vector<uint8_t> input(sizeof(uint64_t), 0);
274
+ uint64_t remote_ptr = ctx->remote_ptr;
275
+ memcpy(input.data(), &remote_ptr, sizeof(remote_ptr));
276
+ std::vector<uint8_t> output;
277
+ bool status = send_rpc_cmd(ctx->sock, FREE_BUFFER, input, output);
278
+ GGML_ASSERT(status);
279
+ GGML_ASSERT(output.empty());
280
+ delete ctx;
281
+ }
282
+
283
+ GGML_CALL static void * ggml_backend_rpc_buffer_get_base(ggml_backend_buffer_t buffer) {
284
+ ggml_backend_rpc_buffer_context * ctx = (ggml_backend_rpc_buffer_context *)buffer->context;
285
+ if (ctx->base_cache.find(buffer) != ctx->base_cache.end()) {
286
+ return ctx->base_cache[buffer];
287
+ }
288
+ // input serialization format: | remote_ptr (8 bytes) |
289
+ std::vector<uint8_t> input(sizeof(uint64_t), 0);
290
+ uint64_t remote_ptr = ctx->remote_ptr;
291
+ memcpy(input.data(), &remote_ptr, sizeof(remote_ptr));
292
+ std::vector<uint8_t> output;
293
+ bool status = send_rpc_cmd(ctx->sock, BUFFER_GET_BASE, input, output);
294
+ GGML_ASSERT(status);
295
+ GGML_ASSERT(output.size() == sizeof(uint64_t));
296
+ // output serialization format: | base_ptr (8 bytes) |
297
+ uint64_t base_ptr;
298
+ memcpy(&base_ptr, output.data(), sizeof(base_ptr));
299
+ void * base = reinterpret_cast<void *>(base_ptr);
300
+ ctx->base_cache[buffer] = base;
301
+ return base;
302
+ }
303
+
304
+ static rpc_tensor serialize_tensor(const ggml_tensor * tensor) {
305
+ rpc_tensor result;
306
+ result.id = reinterpret_cast<uint64_t>(tensor);
307
+ result.type = tensor->type;
308
+ if (tensor->buffer) {
309
+ ggml_backend_buffer_t buffer = tensor->buffer;
310
+ ggml_backend_rpc_buffer_context * ctx = (ggml_backend_rpc_buffer_context *)buffer->context;
311
+ result.buffer = ctx->remote_ptr;
312
+ } else {
313
+ result.buffer = 0;
314
+ }
315
+ for (uint32_t i = 0; i < GGML_MAX_DIMS; i++) {
316
+ result.ne[i] = tensor->ne[i];
317
+ result.nb[i] = tensor->nb[i];
318
+ }
319
+ result.op = tensor->op;
320
+ for (uint32_t i = 0; i < GGML_MAX_OP_PARAMS / sizeof(int32_t); i++) {
321
+ result.op_params[i] = tensor->op_params[i];
322
+ }
323
+ result.flags = tensor->flags;
324
+ for (uint32_t i = 0; i < GGML_MAX_SRC; i++) {
325
+ result.src[i] = reinterpret_cast<uint64_t>(tensor->src[i]);
326
+ }
327
+ result.view_src = reinterpret_cast<uint64_t>(tensor->view_src);
328
+ result.view_offs = tensor->view_offs;
329
+ result.data = reinterpret_cast<uint64_t>(tensor->data);
330
+ snprintf(result.name, GGML_MAX_NAME, "%s", tensor->name);
331
+ return result;
332
+ }
333
+
334
+ static ggml_tensor * deserialize_tensor(struct ggml_context * ctx, const rpc_tensor * tensor) {
335
+ ggml_tensor * result = ggml_new_tensor_4d(ctx, (ggml_type) tensor->type,
336
+ tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
337
+ for (uint32_t i = 0; i < GGML_MAX_DIMS; i++) {
338
+ result->nb[i] = tensor->nb[i];
339
+ }
340
+ result->buffer = reinterpret_cast<ggml_backend_buffer_t>(tensor->buffer);
341
+ result->op = (ggml_op) tensor->op;
342
+ for (uint32_t i = 0; i < GGML_MAX_OP_PARAMS / sizeof(int32_t); i++) {
343
+ result->op_params[i] = tensor->op_params[i];
344
+ }
345
+ result->flags = tensor->flags;
346
+ result->data = reinterpret_cast<void *>(tensor->data);
347
+ ggml_set_name(result, tensor->name);
348
+ return result;
349
+ }
350
+
351
+ GGML_CALL static void ggml_backend_rpc_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
352
+ UNUSED(buffer);
353
+ if (ggml_is_quantized(tensor->type)) {
354
+ // TODO: this check is due to MATRIX_ROW_PADDING in CUDA and should be generalized
355
+ GGML_ASSERT(tensor->ne[0] % 512 == 0 && "unsupported quantized tensor");
356
+ }
357
+ }
358
+
359
+ GGML_CALL static void ggml_backend_rpc_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
360
+ ggml_backend_rpc_buffer_context * ctx = (ggml_backend_rpc_buffer_context *)buffer->context;
361
+ // input serialization format: | rpc_tensor | offset (8 bytes) | data (size bytes) |
362
+ size_t input_size = sizeof(rpc_tensor) + sizeof(uint64_t) + size;
363
+ std::vector<uint8_t> input(input_size, 0);
364
+ rpc_tensor rpc_tensor = serialize_tensor(tensor);
365
+ memcpy(input.data(), &rpc_tensor, sizeof(rpc_tensor));
366
+ memcpy(input.data() + sizeof(rpc_tensor), &offset, sizeof(offset));
367
+ memcpy(input.data() + sizeof(rpc_tensor) + sizeof(offset), data, size);
368
+ std::vector<uint8_t> output;
369
+ bool status = send_rpc_cmd(ctx->sock, SET_TENSOR, input, output);
370
+ GGML_ASSERT(status);
371
+ }
372
+
373
+ GGML_CALL static void ggml_backend_rpc_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
374
+ ggml_backend_rpc_buffer_context * ctx = (ggml_backend_rpc_buffer_context *)buffer->context;
375
+ // input serialization format: | rpc_tensor | offset (8 bytes) | size (8 bytes) |
376
+ int input_size = sizeof(rpc_tensor) + 2*sizeof(uint64_t);
377
+ std::vector<uint8_t> input(input_size, 0);
378
+ rpc_tensor rpc_tensor = serialize_tensor(tensor);
379
+ memcpy(input.data(), &rpc_tensor, sizeof(rpc_tensor));
380
+ memcpy(input.data() + sizeof(rpc_tensor), &offset, sizeof(offset));
381
+ memcpy(input.data() + sizeof(rpc_tensor) + sizeof(offset), &size, sizeof(size));
382
+ std::vector<uint8_t> output;
383
+ bool status = send_rpc_cmd(ctx->sock, GET_TENSOR, input, output);
384
+ GGML_ASSERT(status);
385
+ GGML_ASSERT(output.size() == size);
386
+ // output serialization format: | data (size bytes) |
387
+ memcpy(data, output.data(), size);
388
+ }
389
+
390
+ GGML_CALL static bool ggml_backend_rpc_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
391
+ // check if src and dst are on the same server
392
+ ggml_backend_buffer_t src_buffer = src->buffer;
393
+ ggml_backend_rpc_buffer_context * src_ctx = (ggml_backend_rpc_buffer_context *)src_buffer->context;
394
+ ggml_backend_buffer_t dst_buffer = dst->buffer;
395
+ ggml_backend_rpc_buffer_context * dst_ctx = (ggml_backend_rpc_buffer_context *)dst_buffer->context;
396
+ if (src_ctx->sock != dst_ctx->sock) {
397
+ return false;
398
+ }
399
+ ggml_backend_rpc_buffer_context * ctx = (ggml_backend_rpc_buffer_context *)buffer->context;
400
+ // input serialization format: | rpc_tensor src | rpc_tensor dst |
401
+ int input_size = 2*sizeof(rpc_tensor);
402
+ std::vector<uint8_t> input(input_size, 0);
403
+ rpc_tensor rpc_src = serialize_tensor(src);
404
+ rpc_tensor rpc_dst = serialize_tensor(dst);
405
+ memcpy(input.data(), &rpc_src, sizeof(rpc_src));
406
+ memcpy(input.data() + sizeof(rpc_src), &rpc_dst, sizeof(rpc_dst));
407
+ std::vector<uint8_t> output;
408
+ bool status = send_rpc_cmd(ctx->sock, COPY_TENSOR, input, output);
409
+ GGML_ASSERT(status);
410
+ // output serialization format: | result (1 byte) |
411
+ GGML_ASSERT(output.size() == 1);
412
+ return output[0];
413
+ }
414
+
415
+ GGML_CALL static void ggml_backend_rpc_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
416
+ ggml_backend_rpc_buffer_context * ctx = (ggml_backend_rpc_buffer_context *)buffer->context;
417
+ // serialization format: | bufptr (8 bytes) | value (1 byte) |
418
+ int input_size = sizeof(uint64_t) + sizeof(uint8_t);
419
+ std::vector<uint8_t> input(input_size, 0);
420
+ memcpy(input.data(), &ctx->remote_ptr, sizeof(ctx->remote_ptr));
421
+ memcpy(input.data() + sizeof(ctx->remote_ptr), &value, sizeof(value));
422
+ std::vector<uint8_t> output;
423
+ bool status = send_rpc_cmd(ctx->sock, BUFFER_CLEAR, input, output);
424
+ GGML_ASSERT(status);
425
+ }
426
+
427
+ static ggml_backend_buffer_i ggml_backend_rpc_buffer_interface = {
428
+ /* .get_name = */ ggml_backend_rpc_buffer_get_name,
429
+ /* .free_buffer = */ ggml_backend_rpc_buffer_free_buffer,
430
+ /* .get_base = */ ggml_backend_rpc_buffer_get_base,
431
+ /* .init_tensor = */ ggml_backend_rpc_buffer_init_tensor,
432
+ /* .set_tensor = */ ggml_backend_rpc_buffer_set_tensor,
433
+ /* .get_tensor = */ ggml_backend_rpc_buffer_get_tensor,
434
+ /* .cpy_tensor = */ ggml_backend_rpc_buffer_cpy_tensor,
435
+ /* .clear = */ ggml_backend_rpc_buffer_clear,
436
+ /* .reset = */ NULL,
437
+ };
438
+
439
+ GGML_CALL static const char * ggml_backend_rpc_buffer_type_name(ggml_backend_buffer_type_t buft) {
440
+ ggml_backend_rpc_buffer_type_context * buft_ctx = (ggml_backend_rpc_buffer_type_context *)buft->context;
441
+ return buft_ctx->name.c_str();
442
+ }
443
+
444
+ GGML_CALL static ggml_backend_buffer_t ggml_backend_rpc_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
445
+ ggml_backend_rpc_buffer_type_context * buft_ctx = (ggml_backend_rpc_buffer_type_context *)buft->context;
446
+ // input serialization format: | size (8 bytes) |
447
+ int input_size = sizeof(uint64_t);
448
+ std::vector<uint8_t> input(input_size, 0);
449
+ memcpy(input.data(), &size, sizeof(size));
450
+ std::vector<uint8_t> output;
451
+ bool status = send_rpc_cmd(buft_ctx->sock, ALLOC_BUFFER, input, output);
452
+ GGML_ASSERT(status);
453
+ GGML_ASSERT(output.size() == 2*sizeof(uint64_t));
454
+ // output serialization format: | remote_ptr (8 bytes) | remote_size (8 bytes) |
455
+ uint64_t remote_ptr;
456
+ memcpy(&remote_ptr, output.data(), sizeof(remote_ptr));
457
+ size_t remote_size;
458
+ memcpy(&remote_size, output.data() + sizeof(uint64_t), sizeof(remote_size));
459
+
460
+ ggml_backend_buffer_t buffer = ggml_backend_buffer_init(buft,
461
+ ggml_backend_rpc_buffer_interface,
462
+ new ggml_backend_rpc_buffer_context{buft_ctx->sock, {}, remote_ptr, "RPC"},
463
+ remote_size);
464
+
465
+ return buffer;
466
+ }
467
+
468
+ static size_t get_alignment(const std::shared_ptr<socket_t> & sock) {
469
+ // input serialization format: | 0 bytes |
470
+ std::vector<uint8_t> input;
471
+ std::vector<uint8_t> output;
472
+ bool status = send_rpc_cmd(sock, GET_ALIGNMENT, input, output);
473
+ GGML_ASSERT(status);
474
+ GGML_ASSERT(output.size() == sizeof(uint64_t));
475
+ // output serialization format: | alignment (8 bytes) |
476
+ uint64_t alignment;
477
+ memcpy(&alignment, output.data(), sizeof(alignment));
478
+ return alignment;
479
+ }
480
+
481
+ GGML_CALL static size_t ggml_backend_rpc_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
482
+ ggml_backend_rpc_buffer_type_context * buft_ctx = (ggml_backend_rpc_buffer_type_context *)buft->context;
483
+ return buft_ctx->alignment;
484
+ }
485
+
486
+ static size_t get_max_size(const std::shared_ptr<socket_t> & sock) {
487
+ // input serialization format: | 0 bytes |
488
+ std::vector<uint8_t> input;
489
+ std::vector<uint8_t> output;
490
+ bool status = send_rpc_cmd(sock, GET_MAX_SIZE, input, output);
491
+ GGML_ASSERT(status);
492
+ GGML_ASSERT(output.size() == sizeof(uint64_t));
493
+ // output serialization format: | max_size (8 bytes) |
494
+ uint64_t max_size;
495
+ memcpy(&max_size, output.data(), sizeof(max_size));
496
+ return max_size;
497
+ }
498
+
499
+ GGML_CALL static size_t ggml_backend_rpc_get_max_size(ggml_backend_buffer_type_t buft) {
500
+ ggml_backend_rpc_buffer_type_context * buft_ctx = (ggml_backend_rpc_buffer_type_context *)buft->context;
501
+ return buft_ctx->max_size;
502
+ }
503
+
504
+ GGML_CALL static size_t ggml_backend_rpc_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
505
+ UNUSED(buft);
506
+ return ggml_nbytes(tensor);
507
+ }
508
+
509
+ GGML_CALL static bool ggml_backend_rpc_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
510
+ if (!ggml_backend_is_rpc(backend)) {
511
+ return false;
512
+ }
513
+ ggml_backend_rpc_buffer_type_context * buft_ctx = (ggml_backend_rpc_buffer_type_context *)buft->context;
514
+ ggml_backend_rpc_context * rpc_ctx = (ggml_backend_rpc_context *)backend->context;
515
+ return buft_ctx->sock == rpc_ctx->sock;
516
+ }
517
+
518
+ static ggml_backend_buffer_type_i ggml_backend_rpc_buffer_type_interface = {
519
+ /* .get_name = */ ggml_backend_rpc_buffer_type_name,
520
+ /* .alloc_buffer = */ ggml_backend_rpc_buffer_type_alloc_buffer,
521
+ /* .get_alignment = */ ggml_backend_rpc_buffer_type_get_alignment,
522
+ /* .get_max_size = */ ggml_backend_rpc_get_max_size,
523
+ /* .get_alloc_size = */ ggml_backend_rpc_buffer_type_get_alloc_size,
524
+ /* .supports_backend = */ ggml_backend_rpc_buffer_type_supports_backend,
525
+ /* .is_host = */ NULL,
526
+ };
527
+
528
+
529
+ GGML_CALL static const char * ggml_backend_rpc_name(ggml_backend_t backend) {
530
+ ggml_backend_rpc_context * rpc_ctx = (ggml_backend_rpc_context *)backend->context;
531
+
532
+ return rpc_ctx->name.c_str();
533
+ }
534
+
535
+ GGML_CALL static void ggml_backend_rpc_free(ggml_backend_t backend) {
536
+ ggml_backend_rpc_context * rpc_ctx = (ggml_backend_rpc_context *)backend->context;
537
+ ggml_backend_rpc_buffer_type_context * buft_ctx = (ggml_backend_rpc_buffer_type_context *)rpc_ctx->buft->context;
538
+ delete buft_ctx;
539
+ delete rpc_ctx->buft;
540
+ delete rpc_ctx;
541
+ delete backend;
542
+ }
543
+
544
+ GGML_CALL static ggml_backend_buffer_type_t ggml_backend_rpc_get_default_buffer_type(ggml_backend_t backend) {
545
+ ggml_backend_rpc_context * ctx = (ggml_backend_rpc_context *)backend->context;
546
+ return ctx->buft;
547
+ }
548
+
549
+ GGML_CALL static void ggml_backend_rpc_synchronize(ggml_backend_t backend) {
550
+ UNUSED(backend);
551
+ // this is no-op because we don't have any async operations
552
+ }
553
+
554
+ static void add_tensor(ggml_tensor * tensor, std::vector<rpc_tensor> & tensors, std::unordered_set<ggml_tensor*> & visited) {
555
+ if (tensor == nullptr) {
556
+ return;
557
+ }
558
+ if (visited.find(tensor) != visited.end()) {
559
+ return;
560
+ }
561
+ visited.insert(tensor);
562
+ for (int i = 0; i < GGML_MAX_SRC; i++) {
563
+ add_tensor(tensor->src[i], tensors, visited);
564
+ }
565
+ add_tensor(tensor->view_src, tensors, visited);
566
+ tensors.push_back(serialize_tensor(tensor));
567
+ }
568
+
569
+ static void serialize_graph(const ggml_cgraph * cgraph, std::vector<uint8_t> & output) {
570
+ uint32_t n_nodes = cgraph->n_nodes;
571
+ std::vector<rpc_tensor> tensors;
572
+ std::unordered_set<ggml_tensor*> visited;
573
+ for (uint32_t i = 0; i < n_nodes; i++) {
574
+ add_tensor(cgraph->nodes[i], tensors, visited);
575
+ }
576
+ // serialization format:
577
+ // | n_nodes (4 bytes) | nodes (n_nodes * sizeof(uint64_t) | n_tensors (4 bytes) | tensors (n_tensors * sizeof(rpc_tensor)) |
578
+ uint32_t n_tensors = tensors.size();
579
+ int output_size = sizeof(uint32_t) + n_nodes * sizeof(uint64_t) + sizeof(uint32_t) + n_tensors * sizeof(rpc_tensor);
580
+ output.resize(output_size, 0);
581
+ memcpy(output.data(), &n_nodes, sizeof(n_nodes));
582
+ uint64_t * out_nodes = (uint64_t *)(output.data() + sizeof(n_nodes));
583
+ for (uint32_t i = 0; i < n_nodes; i++) {
584
+ out_nodes[i] = reinterpret_cast<uint64_t>(cgraph->nodes[i]);
585
+ }
586
+ uint32_t * out_ntensors = (uint32_t *)(output.data() + sizeof(n_nodes) + n_nodes * sizeof(uint64_t));
587
+ *out_ntensors = n_tensors;
588
+ rpc_tensor * out_tensors = (rpc_tensor *)(output.data() + sizeof(n_nodes) + n_nodes * sizeof(uint64_t) + sizeof(uint32_t));
589
+ memcpy(out_tensors, tensors.data(), n_tensors * sizeof(rpc_tensor));
590
+ }
591
+
592
+ GGML_CALL static enum ggml_status ggml_backend_rpc_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
593
+ ggml_backend_rpc_context * rpc_ctx = (ggml_backend_rpc_context *)backend->context;
594
+ std::vector<uint8_t> input;
595
+ serialize_graph(cgraph, input);
596
+ std::vector<uint8_t> output;
597
+ bool status = send_rpc_cmd(rpc_ctx->sock, GRAPH_COMPUTE, input, output);
598
+ GGML_ASSERT(status);
599
+ GGML_ASSERT(output.size() == 1);
600
+ return (enum ggml_status)output[0];
601
+ }
602
+
603
+ GGML_CALL static bool ggml_backend_rpc_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
604
+ UNUSED(backend);
605
+ UNUSED(op);
606
+ GGML_ASSERT(false && "not implemented");
607
+ return false;
608
+ }
609
+
610
+ static ggml_backend_i ggml_backend_rpc_interface = {
611
+ /* .get_name = */ ggml_backend_rpc_name,
612
+ /* .free = */ ggml_backend_rpc_free,
613
+ /* .get_default_buffer_type = */ ggml_backend_rpc_get_default_buffer_type,
614
+ /* .set_tensor_async = */ NULL,
615
+ /* .get_tensor_async = */ NULL,
616
+ /* .cpy_tensor_async = */ NULL,
617
+ /* .synchronize = */ ggml_backend_rpc_synchronize,
618
+ /* .graph_plan_create = */ NULL,
619
+ /* .graph_plan_free = */ NULL,
620
+ /* .graph_plan_compute = */ NULL,
621
+ /* .graph_compute = */ ggml_backend_rpc_graph_compute,
622
+ /* .supports_op = */ ggml_backend_rpc_supports_op,
623
+ /* .offload_op = */ NULL,
624
+ /* .event_new = */ NULL,
625
+ /* .event_free = */ NULL,
626
+ /* .event_record = */ NULL,
627
+ /* .event_wait = */ NULL,
628
+ /* .event_synchronize = */ NULL,
629
+ };
630
+
631
+ static std::unordered_map<std::string, ggml_backend_t> instances;
632
+
633
+ GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_rpc_buffer_type(const char * endpoint) {
634
+ ggml_backend_t backend = ggml_backend_rpc_init(endpoint);
635
+ return backend != nullptr ? ggml_backend_rpc_get_default_buffer_type(backend) : nullptr;
636
+ }
637
+
638
+ GGML_CALL ggml_backend_t ggml_backend_rpc_init(const char * endpoint) {
639
+ std::string endpoint_str(endpoint);
640
+ if (instances.find(endpoint_str) != instances.end()) {
641
+ return instances[endpoint_str];
642
+ }
643
+ #ifdef _WIN32
644
+ {
645
+ WSADATA wsaData;
646
+ int res = WSAStartup(MAKEWORD(2, 2), &wsaData);
647
+ if (res != 0) {
648
+ return nullptr;
649
+ }
650
+ }
651
+ #endif
652
+ GGML_PRINT_DEBUG("Connecting to %s\n", endpoint);
653
+ std::string host;
654
+ int port;
655
+ if (!parse_endpoint(endpoint, host, port)) {
656
+ return nullptr;
657
+ }
658
+ auto sock = socket_connect(host.c_str(), port);
659
+ if (sock == nullptr) {
660
+ return nullptr;
661
+ }
662
+ size_t alignment = get_alignment(sock);
663
+ size_t max_size = get_max_size(sock);
664
+ ggml_backend_rpc_buffer_type_context * buft_ctx = new ggml_backend_rpc_buffer_type_context {
665
+ /* .sock = */ sock,
666
+ /* .name = */ "RPC" + std::to_string(sock->fd),
667
+ /* .alignment = */ alignment,
668
+ /* .max_size = */ max_size
669
+ };
670
+
671
+ ggml_backend_buffer_type_t buft = new ggml_backend_buffer_type {
672
+ /* .iface = */ ggml_backend_rpc_buffer_type_interface,
673
+ /* .context = */ buft_ctx
674
+ };
675
+
676
+ ggml_backend_rpc_context * ctx = new ggml_backend_rpc_context {
677
+ /* .endpoint = */ endpoint,
678
+ /* .name = */ "RPC" + std::to_string(sock->fd),
679
+ /* .sock = */ sock,
680
+ /* .buft = */ buft
681
+ };
682
+
683
+ instances[endpoint] = new ggml_backend {
684
+ /* .guid = */ ggml_backend_rpc_guid(),
685
+ /* .interface = */ ggml_backend_rpc_interface,
686
+ /* .context = */ ctx
687
+ };
688
+
689
+ return instances[endpoint];
690
+ }
691
+
692
+ GGML_API GGML_CALL bool ggml_backend_is_rpc(ggml_backend_t backend) {
693
+ return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_rpc_guid());
694
+ }
695
+
696
+ static void get_device_memory(const std::shared_ptr<socket_t> & sock, size_t * free, size_t * total) {
697
+ // input serialization format: | 0 bytes |
698
+ std::vector<uint8_t> input;
699
+ std::vector<uint8_t> output;
700
+ bool status = send_rpc_cmd(sock, GET_DEVICE_MEMORY, input, output);
701
+ GGML_ASSERT(status);
702
+ GGML_ASSERT(output.size() == 2*sizeof(uint64_t));
703
+ // output serialization format: | free (8 bytes) | total (8 bytes) |
704
+ uint64_t free_mem;
705
+ memcpy(&free_mem, output.data(), sizeof(free_mem));
706
+ uint64_t total_mem;
707
+ memcpy(&total_mem, output.data() + sizeof(uint64_t), sizeof(total_mem));
708
+ *free = free_mem;
709
+ *total = total_mem;
710
+ }
711
+
712
+ GGML_API GGML_CALL void ggml_backend_rpc_get_device_memory(const char * endpoint, size_t * free, size_t * total) {
713
+ ggml_backend_t backend = ggml_backend_rpc_init(endpoint);
714
+ if (backend == nullptr) {
715
+ *free = 0;
716
+ *total = 0;
717
+ return;
718
+ }
719
+ ggml_backend_rpc_context * ctx = (ggml_backend_rpc_context *)backend->context;
720
+ get_device_memory(ctx->sock, free, total);
721
+ }
722
+
723
+ // RPC server-side implementation
724
+
725
+ static void rpc_alloc_buffer(ggml_backend_t backend, const std::vector<uint8_t> & input, std::vector<uint8_t> & output) {
726
+ // input serialization format: | size (8 bytes) |
727
+ uint64_t size;
728
+ memcpy(&size, input.data(), sizeof(size));
729
+ ggml_backend_buffer_type_t buft = ggml_backend_get_default_buffer_type(backend);
730
+ ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer(buft, size);
731
+ uint64_t remote_ptr = reinterpret_cast<uint64_t>(buffer);
732
+ uint64_t remote_size = buffer->size;
733
+ GGML_PRINT_DEBUG("[%s] size: %" PRIu64 " -> remote_ptr: %" PRIx64 ", remote_size: %" PRIu64 "\n", __func__, size, remote_ptr, remote_size);
734
+ // output serialization format: | remote_ptr (8 bytes) | remote_size (8 bytes) |
735
+ output.resize(2*sizeof(uint64_t), 0);
736
+ memcpy(output.data(), &remote_ptr, sizeof(remote_ptr));
737
+ memcpy(output.data() + sizeof(uint64_t), &remote_size, sizeof(remote_size));
738
+ }
739
+
740
+ static void rpc_get_alignment(ggml_backend_t backend, std::vector<uint8_t> & output) {
741
+ ggml_backend_buffer_type_t buft = ggml_backend_get_default_buffer_type(backend);
742
+ size_t alignment = ggml_backend_buft_get_alignment(buft);
743
+ GGML_PRINT_DEBUG("[%s] alignment: %lu\n", __func__, alignment);
744
+ // output serialization format: | alignment (8 bytes) |
745
+ output.resize(sizeof(uint64_t), 0);
746
+ memcpy(output.data(), &alignment, sizeof(alignment));
747
+ }
748
+
749
+ static void rpc_get_max_size(ggml_backend_t backend, std::vector<uint8_t> & output) {
750
+ ggml_backend_buffer_type_t buft = ggml_backend_get_default_buffer_type(backend);
751
+ size_t max_size = ggml_backend_buft_get_max_size(buft);
752
+ GGML_PRINT_DEBUG("[%s] max_size: %lu\n", __func__, max_size);
753
+ // output serialization format: | max_size (8 bytes) |
754
+ output.resize(sizeof(uint64_t), 0);
755
+ memcpy(output.data(), &max_size, sizeof(max_size));
756
+ }
757
+
758
+ static void rpc_buffer_get_base(const std::vector<uint8_t> & input, std::vector<uint8_t> & output) {
759
+ // input serialization format: | remote_ptr (8 bytes) |
760
+ uint64_t remote_ptr;
761
+ memcpy(&remote_ptr, input.data(), sizeof(remote_ptr));
762
+ GGML_PRINT_DEBUG("[%s] remote_ptr: %" PRIx64 "\n", __func__, remote_ptr);
763
+ ggml_backend_buffer_t buffer = reinterpret_cast<ggml_backend_buffer_t>(remote_ptr);
764
+ void * base = ggml_backend_buffer_get_base(buffer);
765
+ // output serialization format: | base_ptr (8 bytes) |
766
+ uint64_t base_ptr = reinterpret_cast<uint64_t>(base);
767
+ output.resize(sizeof(uint64_t), 0);
768
+ memcpy(output.data(), &base_ptr, sizeof(base_ptr));
769
+ }
770
+
771
+ static void rpc_free_buffer(const std::vector<uint8_t> & input) {
772
+ // input serialization format: | remote_ptr (8 bytes) |
773
+ uint64_t remote_ptr;
774
+ memcpy(&remote_ptr, input.data(), sizeof(remote_ptr));
775
+ GGML_PRINT_DEBUG("[%s] remote_ptr: %" PRIx64 "\n", __func__, remote_ptr);
776
+ ggml_backend_buffer_t buffer = reinterpret_cast<ggml_backend_buffer_t>(remote_ptr);
777
+ ggml_backend_buffer_free(buffer);
778
+ }
779
+
780
+ static void rpc_buffer_clear(const std::vector<uint8_t> & input) {
781
+ // input serialization format: | remote_ptr (8 bytes) | value (1 byte) |
782
+ uint64_t remote_ptr;
783
+ memcpy(&remote_ptr, input.data(), sizeof(remote_ptr));
784
+ uint8_t value;
785
+ memcpy(&value, input.data() + sizeof(uint64_t), sizeof(value));
786
+ GGML_PRINT_DEBUG("[%s] remote_ptr: %" PRIx64 ", value: %u\n", __func__, remote_ptr, value);
787
+ ggml_backend_buffer_t buffer = reinterpret_cast<ggml_backend_buffer_t>(remote_ptr);
788
+ ggml_backend_buffer_clear(buffer, value);
789
+ }
790
+
791
+ static void rpc_set_tensor(const std::vector<uint8_t> & input) {
792
+ // serialization format: | rpc_tensor | offset (8 bytes) | data (size bytes) |
793
+ const rpc_tensor * in_tensor = (const rpc_tensor *)input.data();
794
+ uint64_t offset;
795
+ memcpy(&offset, input.data() + sizeof(rpc_tensor), sizeof(offset));
796
+ size_t size = input.size() - sizeof(rpc_tensor) - sizeof(offset);
797
+
798
+ struct ggml_init_params params {
799
+ /*.mem_size =*/ ggml_tensor_overhead(),
800
+ /*.mem_buffer =*/ NULL,
801
+ /*.no_alloc =*/ true,
802
+ };
803
+ struct ggml_context * ctx = ggml_init(params);
804
+ ggml_tensor * tensor = deserialize_tensor(ctx, in_tensor);
805
+ GGML_PRINT_DEBUG("[%s] buffer: %p, data: %p, offset: %" PRIu64 ", size: %zu\n", __func__, (void*)tensor->buffer, tensor->data, offset, size);
806
+ const void * data = input.data() + sizeof(rpc_tensor) + sizeof(offset);
807
+ ggml_backend_tensor_set(tensor, data, offset, size);
808
+ ggml_free(ctx);
809
+ }
810
+
811
+ static void rpc_get_tensor(const std::vector<uint8_t> & input, std::vector<uint8_t> & output) {
812
+ // serialization format: | rpc_tensor | offset (8 bytes) | size (8 bytes) |
813
+ const rpc_tensor * in_tensor = (const rpc_tensor *)input.data();
814
+ uint64_t offset;
815
+ memcpy(&offset, input.data() + sizeof(rpc_tensor), sizeof(offset));
816
+ uint64_t size;
817
+ memcpy(&size, input.data() + sizeof(rpc_tensor) + sizeof(offset), sizeof(size));
818
+
819
+ struct ggml_init_params params {
820
+ /*.mem_size =*/ ggml_tensor_overhead(),
821
+ /*.mem_buffer =*/ NULL,
822
+ /*.no_alloc =*/ true,
823
+ };
824
+ struct ggml_context * ctx = ggml_init(params);
825
+ ggml_tensor * tensor = deserialize_tensor(ctx, in_tensor);
826
+ GGML_PRINT_DEBUG("[%s] buffer: %p, data: %p, offset: %" PRIu64 ", size: %" PRIu64 "\n", __func__, (void*)tensor->buffer, tensor->data, offset, size);
827
+ // output serialization format: | data (size bytes) |
828
+ output.resize(size, 0);
829
+ ggml_backend_tensor_get(tensor, output.data(), offset, size);
830
+ ggml_free(ctx);
831
+ }
832
+
833
+ static void rpc_copy_tensor(const std::vector<uint8_t> & input, std::vector<uint8_t> & output) {
834
+ // serialization format: | rpc_tensor src | rpc_tensor dst |
835
+ const rpc_tensor * rpc_src = (const rpc_tensor *)input.data();
836
+ const rpc_tensor * rpc_dst = (const rpc_tensor *)(input.data() + sizeof(rpc_src));
837
+
838
+ struct ggml_init_params params {
839
+ /*.mem_size =*/ 2*ggml_tensor_overhead(),
840
+ /*.mem_buffer =*/ NULL,
841
+ /*.no_alloc =*/ true,
842
+ };
843
+ struct ggml_context * ctx = ggml_init(params);
844
+ ggml_tensor * src = deserialize_tensor(ctx, rpc_src);
845
+ ggml_tensor * dst = deserialize_tensor(ctx, rpc_dst);
846
+ GGML_PRINT_DEBUG("[%s] src->buffer: %p, dst->buffer: %p\n", __func__, (void*)src->buffer, (void*)dst->buffer);
847
+ bool result = ggml_backend_buffer_copy_tensor(src, dst);
848
+ // output serialization format: | result (1 byte) |
849
+ output.resize(1, 0);
850
+ output[0] = result;
851
+ ggml_free(ctx);
852
+ }
853
+
854
+ static struct ggml_tensor * create_node(uint64_t id,
855
+ struct ggml_context * ctx,
856
+ const std::unordered_map<uint64_t, const rpc_tensor*> & tensor_ptrs,
857
+ std::unordered_map<uint64_t, struct ggml_tensor*> & tensor_map) {
858
+ if (id == 0) {
859
+ return nullptr;
860
+ }
861
+ if (tensor_map.find(id) != tensor_map.end()) {
862
+ return tensor_map[id];
863
+ }
864
+ const rpc_tensor * tensor = tensor_ptrs.at(id);
865
+ struct ggml_tensor * result = deserialize_tensor(ctx, tensor);
866
+ tensor_map[id] = result;
867
+ for (int i = 0; i < GGML_MAX_SRC; i++) {
868
+ result->src[i] = create_node(tensor->src[i], ctx, tensor_ptrs, tensor_map);
869
+ }
870
+ result->view_src = create_node(tensor->view_src, ctx, tensor_ptrs, tensor_map);
871
+ result->view_offs = tensor->view_offs;
872
+ return result;
873
+ }
874
+
875
+ static void rpc_graph_compute(ggml_backend_t backend, const std::vector<uint8_t> & input, std::vector<uint8_t> & output) {
876
+ // serialization format:
877
+ // | n_nodes (4 bytes) | nodes (n_nodes * sizeof(uint64_t) | n_tensors (4 bytes) | tensors (n_tensors * sizeof(rpc_tensor)) |
878
+ uint32_t n_nodes;
879
+ memcpy(&n_nodes, input.data(), sizeof(n_nodes));
880
+ const uint64_t * nodes = (const uint64_t *)(input.data() + sizeof(n_nodes));
881
+ uint32_t n_tensors;
882
+ memcpy(&n_tensors, input.data() + sizeof(n_nodes) + n_nodes*sizeof(uint64_t), sizeof(n_tensors));
883
+ const rpc_tensor * tensors = (const rpc_tensor *)(input.data() + sizeof(n_nodes) + n_nodes*sizeof(uint64_t) + sizeof(n_tensors));
884
+ GGML_PRINT_DEBUG("[%s] n_nodes: %u, n_tensors: %u\n", __func__, n_nodes, n_tensors);
885
+
886
+ static size_t buf_size = ggml_tensor_overhead()*(n_nodes + n_tensors) + ggml_graph_overhead_custom(n_nodes, false);
887
+ struct ggml_init_params params = {
888
+ /*.mem_size =*/ buf_size,
889
+ /*.mem_buffer =*/ NULL,
890
+ /*.no_alloc =*/ true,
891
+ };
892
+ struct ggml_context * ctx = ggml_init(params);
893
+ struct ggml_cgraph * graph = ggml_new_graph_custom(ctx, n_nodes, false);
894
+ graph->n_nodes = n_nodes;
895
+ std::unordered_map<uint64_t, const rpc_tensor*> tensor_ptrs;
896
+ for (uint32_t i = 0; i < n_tensors; i++) {
897
+ tensor_ptrs[tensors[i].id] = &tensors[i];
898
+ }
899
+ std::unordered_map<uint64_t, ggml_tensor*> tensor_map;
900
+ for (uint32_t i = 0; i < n_nodes; i++) {
901
+ graph->nodes[i] = create_node(nodes[i], ctx, tensor_ptrs, tensor_map);
902
+ }
903
+ ggml_status status = ggml_backend_graph_compute(backend, graph);
904
+ // output serialization format: | status (1 byte) |
905
+ output.resize(1, 0);
906
+ output[0] = status;
907
+ ggml_free(ctx);
908
+ }
909
+
910
+ static void rpc_serve_client(ggml_backend_t backend, sockfd_t sockfd, size_t free_mem, size_t total_mem) {
911
+ while (true) {
912
+ uint8_t cmd;
913
+ if (!recv_data(sockfd, &cmd, 1)) {
914
+ break;
915
+ }
916
+ std::vector<uint8_t> input;
917
+ std::vector<uint8_t> output;
918
+ uint64_t input_size;
919
+ if (!recv_data(sockfd, &input_size, sizeof(input_size))) {
920
+ break;
921
+ }
922
+ input.resize(input_size);
923
+ if (!recv_data(sockfd, input.data(), input_size)) {
924
+ break;
925
+ }
926
+ switch (cmd) {
927
+ case ALLOC_BUFFER: {
928
+ rpc_alloc_buffer(backend, input, output);
929
+ break;
930
+ }
931
+ case GET_ALIGNMENT: {
932
+ rpc_get_alignment(backend, output);
933
+ break;
934
+ }
935
+ case GET_MAX_SIZE: {
936
+ rpc_get_max_size(backend, output);
937
+ break;
938
+ }
939
+ case BUFFER_GET_BASE: {
940
+ rpc_buffer_get_base(input, output);
941
+ break;
942
+ }
943
+ case FREE_BUFFER: {
944
+ rpc_free_buffer(input);
945
+ break;
946
+ }
947
+ case BUFFER_CLEAR: {
948
+ rpc_buffer_clear(input);
949
+ break;
950
+ }
951
+ case SET_TENSOR: {
952
+ rpc_set_tensor(input);
953
+ break;
954
+ }
955
+ case GET_TENSOR: {
956
+ rpc_get_tensor(input, output);
957
+ break;
958
+ }
959
+ case COPY_TENSOR: {
960
+ rpc_copy_tensor(input, output);
961
+ break;
962
+ }
963
+ case GRAPH_COMPUTE: {
964
+ rpc_graph_compute(backend, input, output);
965
+ break;
966
+ }
967
+ case GET_DEVICE_MEMORY: {
968
+ // output serialization format: | free (8 bytes) | total (8 bytes) |
969
+ output.resize(2*sizeof(uint64_t), 0);
970
+ memcpy(output.data(), &free_mem, sizeof(free_mem));
971
+ memcpy(output.data() + sizeof(uint64_t), &total_mem, sizeof(total_mem));
972
+ break;
973
+ }
974
+ default: {
975
+ fprintf(stderr, "Unknown command: %d\n", cmd);
976
+ return;
977
+ }
978
+ }
979
+ uint64_t output_size = output.size();
980
+ if (!send_data(sockfd, &output_size, sizeof(output_size))) {
981
+ break;
982
+ }
983
+ if (!send_data(sockfd, output.data(), output_size)) {
984
+ break;
985
+ }
986
+ }
987
+ }
988
+
989
+ void start_rpc_server(ggml_backend_t backend, const char * endpoint, size_t free_mem, size_t total_mem) {
990
+ std::string host;
991
+ int port;
992
+ if (!parse_endpoint(endpoint, host, port)) {
993
+ return;
994
+ }
995
+ #ifdef _WIN32
996
+ {
997
+ WSADATA wsaData;
998
+ int res = WSAStartup(MAKEWORD(2, 2), &wsaData);
999
+ if (res != 0) {
1000
+ fprintf(stderr, "WSAStartup failed: %d\n", res);
1001
+ return;
1002
+ }
1003
+ }
1004
+ #endif
1005
+ auto server_socket = create_server_socket(host.c_str(), port);
1006
+ if (server_socket == nullptr) {
1007
+ fprintf(stderr, "Failed to create server socket\n");
1008
+ return;
1009
+ }
1010
+ while (true) {
1011
+ auto client_socket = socket_accept(server_socket->fd);
1012
+ if (client_socket == nullptr) {
1013
+ fprintf(stderr, "Failed to accept client connection\n");
1014
+ return;
1015
+ }
1016
+ printf("Accepted client connection, free_mem=%zu, total_mem=%zu\n", free_mem, total_mem);
1017
+ rpc_serve_client(backend, client_socket->fd, free_mem, total_mem);
1018
+ printf("Client connection closed\n");
1019
+ }
1020
+ #ifdef _WIN32
1021
+ WSACleanup();
1022
+ #endif
1023
+ }
ggml-rpc.h ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_RPC_MAX_SERVERS 16
11
+
12
+ // backend API
13
+ GGML_API GGML_CALL ggml_backend_t ggml_backend_rpc_init(const char * endpoint);
14
+ GGML_API GGML_CALL bool ggml_backend_is_rpc(ggml_backend_t backend);
15
+
16
+ GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_rpc_buffer_type(const char * endpoint);
17
+
18
+ GGML_API GGML_CALL void ggml_backend_rpc_get_device_memory(const char * endpoint, size_t * free, size_t * total);
19
+
20
+ GGML_API GGML_CALL void start_rpc_server(ggml_backend_t backend, const char * endpoint, size_t free_mem, size_t total_mem);
21
+
22
+ #ifdef __cplusplus
23
+ }
24
+ #endif