Skip to content

Commit 0e7aa4e

Browse files
Shinnosuke TakagiShinnosuke Takagi
authored andcommitted
ggml-rpc: chunk send()/recv() to avoid EINVAL for very large tensors over RPC (macOS & others). Fixes #15055
1 parent e54d41b commit 0e7aa4e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

ggml/src/ggml-rpc/ggml-rpc.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
namespace fs = std::filesystem;
3434

35+
static constexpr size_t RPC_IO_CHUNK = 1024ull * 1024ull * 1024ull; // 1 GiB
36+
3537
#ifdef _WIN32
3638
typedef SOCKET sockfd_t;
3739
using ssize_t = __int64;
@@ -323,27 +325,43 @@ static std::shared_ptr<socket_t> create_server_socket(const char * host, int por
323325
static bool send_data(sockfd_t sockfd, const void * data, size_t size) {
324326
size_t bytes_sent = 0;
325327
while (bytes_sent < size) {
326-
ssize_t n = send(sockfd, (const char *)data + bytes_sent, size - bytes_sent, 0);
328+
size_t size_to_send = size - bytes_sent;
329+
if (size_to_send > RPC_IO_CHUNK) size_to_send = RPC_IO_CHUNK;
330+
ssize_t n = send(sockfd, (const char *)data + bytes_sent, size_to_send, 0);
327331
if (n < 0) {
332+
#ifndef _WIN32
333+
perror("send");
334+
#else
335+
fprintf(stderr, "send failed (bytes_sent=%zu, size_to_send=%zu)\n", bytes_sent, size_to_send);
336+
#endif
328337
return false;
329338
}
330339
bytes_sent += n;
331340
}
332341
return true;
333342
}
334343

344+
335345
static bool recv_data(sockfd_t sockfd, void * data, size_t size) {
336346
size_t bytes_recv = 0;
337347
while (bytes_recv < size) {
338-
ssize_t n = recv(sockfd, (char *)data + bytes_recv, size - bytes_recv, 0);
348+
size_t size_to_recv = size - bytes_recv;
349+
if (size_to_recv > RPC_IO_CHUNK) size_to_recv = RPC_IO_CHUNK;
350+
ssize_t n = recv(sockfd, (char *)data + bytes_recv, size_to_recv, 0);
339351
if (n <= 0) {
352+
#ifndef _WIN32
353+
perror("recv");
354+
#else
355+
fprintf(stderr, "recv failed (bytes_recv=%zu, size_to_recv=%zu)\n", bytes_recv, size_to_recv);
356+
#endif
340357
return false;
341358
}
342359
bytes_recv += n;
343360
}
344361
return true;
345362
}
346363

364+
347365
static bool send_msg(sockfd_t sockfd, const void * msg, size_t msg_size) {
348366
if (!send_data(sockfd, &msg_size, sizeof(msg_size))) {
349367
return false;

0 commit comments

Comments
 (0)