ggml-mpi.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "ggml-mpi.h"
  2. #include "ggml.h"
  3. #include <mpi.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  7. #define UNUSED GGML_UNUSED
  8. struct ggml_mpi_context {
  9. int rank;
  10. int size;
  11. };
  12. void ggml_mpi_backend_init(void) {
  13. MPI_Init(NULL, NULL);
  14. }
  15. void ggml_mpi_backend_free(void) {
  16. MPI_Finalize();
  17. }
  18. struct ggml_mpi_context * ggml_mpi_init(void) {
  19. struct ggml_mpi_context * ctx = calloc(1, sizeof(struct ggml_mpi_context));
  20. MPI_Comm_rank(MPI_COMM_WORLD, &ctx->rank);
  21. MPI_Comm_size(MPI_COMM_WORLD, &ctx->size);
  22. return ctx;
  23. }
  24. void ggml_mpi_free(struct ggml_mpi_context * ctx) {
  25. free(ctx);
  26. }
  27. int ggml_mpi_rank(struct ggml_mpi_context * ctx) {
  28. return ctx->rank;
  29. }
  30. void ggml_mpi_eval_init(
  31. struct ggml_mpi_context * ctx_mpi,
  32. int * n_tokens,
  33. int * n_past,
  34. int * n_threads) {
  35. UNUSED(ctx_mpi);
  36. // synchronize the worker node parameters with the root node
  37. MPI_Barrier(MPI_COMM_WORLD);
  38. MPI_Bcast(n_tokens, 1, MPI_INT, 0, MPI_COMM_WORLD);
  39. MPI_Bcast(n_past, 1, MPI_INT, 0, MPI_COMM_WORLD);
  40. MPI_Bcast(n_threads, 1, MPI_INT, 0, MPI_COMM_WORLD);
  41. }
  42. static int ggml_graph_get_node_idx(struct ggml_cgraph * gf, const char * name) {
  43. struct ggml_tensor * t = ggml_graph_get_tensor(gf, name);
  44. if (t == NULL) {
  45. fprintf(stderr, "%s: tensor %s not found\n", __func__, name);
  46. return -1;
  47. }
  48. for (int i = 0; i < gf->n_nodes; i++) {
  49. if (gf->nodes[i] == t) {
  50. return i;
  51. }
  52. }
  53. fprintf(stderr, "%s: tensor %s not found in graph (should not happen)\n", __func__, name);
  54. return -1;
  55. }
  56. static void ggml_mpi_tensor_send(struct ggml_tensor * t, int mpi_rank_dst) {
  57. MPI_Datatype mpi_type;
  58. switch (t->type) {
  59. case GGML_TYPE_I32: mpi_type = MPI_INT32_T; break;
  60. case GGML_TYPE_F32: mpi_type = MPI_FLOAT; break;
  61. default: GGML_ASSERT(false && "not implemented");
  62. }
  63. const int retval = MPI_Send(t->data, ggml_nelements(t), mpi_type, mpi_rank_dst, 0, MPI_COMM_WORLD);
  64. GGML_ASSERT(retval == MPI_SUCCESS);
  65. }
  66. static void ggml_mpi_tensor_recv(struct ggml_tensor * t, int mpi_rank_src) {
  67. MPI_Datatype mpi_type;
  68. switch (t->type) {
  69. case GGML_TYPE_I32: mpi_type = MPI_INT32_T; break;
  70. case GGML_TYPE_F32: mpi_type = MPI_FLOAT; break;
  71. default: GGML_ASSERT(false && "not implemented");
  72. }
  73. MPI_Status status; UNUSED(status);
  74. const int retval = MPI_Recv(t->data, ggml_nelements(t), mpi_type, mpi_rank_src, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  75. GGML_ASSERT(retval == MPI_SUCCESS);
  76. }
  77. // TODO: there are many improvements that can be done to this implementation
  78. void ggml_mpi_graph_compute_pre(
  79. struct ggml_mpi_context * ctx_mpi,
  80. struct ggml_cgraph * gf,
  81. int n_layers) {
  82. const int mpi_rank = ctx_mpi->rank;
  83. const int mpi_size = ctx_mpi->size;
  84. struct ggml_tensor * inp_tokens = ggml_graph_get_tensor(gf, "inp_tokens");
  85. if (inp_tokens == NULL) {
  86. fprintf(stderr, "%s: tensor 'inp_tokens' not found\n", __func__);
  87. return;
  88. }
  89. struct ggml_tensor * inp0 = ggml_graph_get_tensor(gf, "layer_inp_0");
  90. if (inp0 == NULL) {
  91. fprintf(stderr, "%s: tensor 'inp0' not found\n", __func__);
  92. return;
  93. }
  94. GGML_ASSERT(inp0 == gf->nodes[0]);
  95. // distribute the compute graph into slices across the MPI nodes
  96. //
  97. // the main node (0) processes the last layers + the remainder of the compute graph
  98. // and is responsible to pass the input tokens to the first node (1)
  99. //
  100. // node 1: [( 0) * n_per_node, ( 1) * n_per_node)
  101. // node 2: [( 1) * n_per_node, ( 2) * n_per_node)
  102. // ...
  103. // node n-1: [(n-2) * n_per_node, (n-1) * n_per_node)
  104. // node 0: [(n-1) * n_per_node, n_nodes)
  105. //
  106. if (mpi_rank > 0) {
  107. if (mpi_rank == 1) {
  108. // the first node (1) receives the input tokens from the main node (0)
  109. ggml_mpi_tensor_recv(inp_tokens, 0);
  110. } else {
  111. // recv input data for each node into the "inp0" tensor (i.e. the first node in the compute graph)
  112. ggml_mpi_tensor_recv(inp0, mpi_rank - 1);
  113. }
  114. } else if (mpi_size > 1) {
  115. // node 0 sends the input tokens to node 1
  116. ggml_mpi_tensor_send(inp_tokens, 1);
  117. // recv the output data from the last node
  118. ggml_mpi_tensor_recv(inp0, mpi_size - 1);
  119. }
  120. {
  121. const int n_per_node = (n_layers + (mpi_size - 1)) / mpi_size;
  122. const int mpi_idx = mpi_rank > 0 ? mpi_rank - 1 : mpi_size - 1;
  123. const int il0 = (mpi_idx + 0) * n_per_node;
  124. const int il1 = MIN(n_layers, (mpi_idx + 1) * n_per_node);
  125. char name_l0[GGML_MAX_NAME];
  126. char name_l1[GGML_MAX_NAME];
  127. snprintf(name_l0, sizeof(name_l0), "layer_inp_%d", il0);
  128. snprintf(name_l1, sizeof(name_l1), "layer_inp_%d", il1);
  129. const int idx_l0 = ggml_graph_get_node_idx(gf, name_l0);
  130. const int idx_l1 = mpi_rank > 0 ? ggml_graph_get_node_idx(gf, name_l1) + 1 : gf->n_nodes;
  131. if (idx_l0 < 0 || idx_l1 < 0) {
  132. fprintf(stderr, "%s: layer input nodes not found\n", __func__);
  133. return;
  134. }
  135. // attach the input data to all nodes that need it
  136. // TODO: not great - should be able to do this without modifying the compute graph (see next TODO below)
  137. for (int i = idx_l0; i < idx_l1; i++) {
  138. if (gf->nodes[i]->src0 == gf->nodes[idx_l0]) {
  139. gf->nodes[i]->src0 = inp0;
  140. }
  141. if (gf->nodes[i]->src1 == gf->nodes[idx_l0]) {
  142. gf->nodes[i]->src1 = inp0;
  143. }
  144. }
  145. // TODO: instead of rearranging the nodes, we should be able to execute a subset of the compute graph
  146. for (int i = 1; i < idx_l1 - idx_l0; i++) {
  147. gf->nodes[i] = gf->nodes[idx_l0 + i];
  148. gf->grads[i] = gf->grads[idx_l0 + i];
  149. }
  150. // the first node performs the "get_rows" operation, the rest of the nodes get the data from the previous node
  151. if (mpi_idx != 0) {
  152. gf->nodes[0]->op = GGML_OP_NONE;
  153. }
  154. gf->n_nodes = idx_l1 - idx_l0;
  155. //fprintf(stderr, "%s: node %d: processing %d nodes [%d, %d)\n", __func__, mpi_rank, gf->n_nodes, il0, il1);
  156. }
  157. }
  158. void ggml_mpi_graph_compute_post(
  159. struct ggml_mpi_context * ctx_mpi,
  160. struct ggml_cgraph * gf,
  161. int n_layers) {
  162. UNUSED(n_layers);
  163. const int mpi_rank = ctx_mpi->rank;
  164. const int mpi_size = ctx_mpi->size;
  165. // send the output data to the next node
  166. if (mpi_rank > 0) {
  167. ggml_mpi_tensor_send(gf->nodes[gf->n_nodes - 1], (mpi_rank + 1) % mpi_size);
  168. }
  169. }