llama_cpp 0.16.1 → 0.17.0

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