test-customop.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "ggml/ggml.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #if defined(_WIN32)
  7. #include <windows.h>
  8. typedef volatile LONG atomic_int;
  9. static LONG atomic_fetch_add(atomic_int * ptr, LONG inc) {
  10. return InterlockedExchangeAdd(ptr, inc);
  11. }
  12. #else
  13. #include <stdatomic.h>
  14. #endif
  15. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  16. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  17. struct ggml_context * make_ctx(void) {
  18. struct ggml_init_params params = {
  19. /*.mem_size =*/ 1 * 1024 * 1024,
  20. /*.mem_buffer =*/ NULL,
  21. /*.no_alloc =*/ false,
  22. };
  23. return ggml_init(params);
  24. }
  25. char g_userdata[] = "ggml";
  26. atomic_int g_custom1_count = 0;
  27. atomic_int g_custom2_count = 0;
  28. atomic_int g_custom3_count = 0;
  29. void custom1(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata) {
  30. // check that the userdata is correct
  31. assert(userdata == NULL);
  32. assert(ggml_are_same_shape(dst, a));
  33. atomic_fetch_add(&g_custom1_count, 1);
  34. const float * a_data = ggml_get_data_f32(a);
  35. float * dst_data = ggml_get_data_f32(dst);
  36. // this assumes that the tensors are contiguous
  37. assert(ggml_is_contiguous(dst));
  38. assert(ggml_is_contiguous(a));
  39. // parallelize by elements
  40. const int ne = (int)ggml_nelements(dst);
  41. const int dr = (ne + nth - 1) / nth;
  42. const int ie0 = dr * ith;
  43. const int ie1 = MIN(ie0 + dr, ne);
  44. for (int i = ie0; i < ie1; ++i) {
  45. dst_data[i] = a_data[i] * 2;
  46. }
  47. }
  48. void custom2(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata) {
  49. // check that the userdata is correct
  50. assert(userdata == g_userdata);
  51. assert(strcmp(userdata, "ggml") == 0);
  52. assert(ggml_are_same_shape(dst, a));
  53. assert(ggml_are_same_shape(dst, b));
  54. atomic_fetch_add(&g_custom2_count, 1);
  55. const float * a_data = ggml_get_data_f32(a);
  56. const float * b_data = ggml_get_data_f32(b);
  57. float * dst_data = ggml_get_data_f32(dst);
  58. // parallelize by rows
  59. const int nr = (int)ggml_nrows(dst);
  60. // number of rows per thread
  61. const int dr = (nr + nth - 1) / nth;
  62. // row range for this thread
  63. const int ir0 = dr * ith;
  64. const int ir1 = MIN(ir0 + dr, nr);
  65. // number of columns
  66. const int nc = (int)dst->ne[0];
  67. // this assumes that the tensors are contiguous
  68. assert(ggml_is_contiguous(dst));
  69. assert(ggml_is_contiguous(a));
  70. assert(ggml_is_contiguous(b));
  71. for (int ir = ir0; ir < ir1; ++ir) {
  72. for (int ic = 0; ic < nc; ++ic) {
  73. const int i = ir * nc + ic;
  74. dst_data[i] = a_data[i] + b_data[i];
  75. }
  76. }
  77. }
  78. void custom3(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata) {
  79. // check that the userdata is correct
  80. assert(userdata == g_userdata);
  81. assert(strcmp(userdata, "ggml") == 0);
  82. assert(ggml_are_same_shape(dst, a));
  83. assert(ggml_are_same_shape(dst, b));
  84. assert(ggml_are_same_shape(dst, c));
  85. atomic_fetch_add(&g_custom3_count, 1);
  86. const float * a_data = ggml_get_data_f32(a);
  87. const float * b_data = ggml_get_data_f32(b);
  88. const float * c_data = ggml_get_data_f32(c);
  89. float * dst_data = ggml_get_data_f32(dst);
  90. // dont parallelize
  91. assert(ith == 0);
  92. // number of elements
  93. const int ne = (int)ggml_nelements(dst);
  94. // this assumes that the tensors are contiguous
  95. assert(ggml_is_contiguous(dst));
  96. assert(ggml_is_contiguous(a));
  97. assert(ggml_is_contiguous(b));
  98. assert(ggml_is_contiguous(c));
  99. for (int i = 0; i < ne; ++i) {
  100. dst_data[i] = a_data[i] + b_data[i] + c_data[i];
  101. }
  102. }
  103. int main(int argc, const char** argv) {
  104. float buf1_f32[1024];
  105. for (int i = 0; i < 1024; ++i) {
  106. buf1_f32[i] = (float)(i + 1);
  107. }
  108. float buf2_f32[1024];
  109. for (int i = 0; i < 1024; ++i) {
  110. buf2_f32[i] = (float)(i + 1) * 2;
  111. }
  112. float buf3_f32[1024];
  113. for (int i = 0; i < 1024; ++i) {
  114. buf3_f32[i] = (float)(i + 1) * 3;
  115. }
  116. // map_custom1
  117. // 2 tasks, no userdata, parallelized by elements
  118. {
  119. struct ggml_context * ctx = make_ctx();
  120. struct ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
  121. memcpy(t->data, buf1_f32, ggml_nbytes(t));
  122. struct ggml_tensor * m1 = ggml_map_custom1(ctx, t, custom1, 2, NULL);
  123. struct ggml_cgraph graph = ggml_build_forward(m1);
  124. ggml_graph_compute_with_ctx(ctx, &graph, 4);
  125. const float * output = ggml_get_data_f32(m1);
  126. for (int i = 0; i < ggml_nelements(m1); ++i) {
  127. assert(output[i] == buf1_f32[i] * 2);
  128. }
  129. assert(g_custom1_count == 2);
  130. ggml_free(ctx);
  131. }
  132. // map_custom2
  133. // max tasks (4), userdata, parallelized by rows
  134. {
  135. struct ggml_context * ctx = make_ctx();
  136. struct ggml_tensor * t1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
  137. memcpy(t1->data, buf1_f32, ggml_nbytes(t1));
  138. struct ggml_tensor * t2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
  139. memcpy(t2->data, buf2_f32, ggml_nbytes(t2));
  140. struct ggml_tensor * m2 = ggml_map_custom2(ctx, t1, t2, custom2, GGML_N_TASKS_MAX, g_userdata);
  141. struct ggml_cgraph graph = ggml_build_forward(m2);
  142. ggml_graph_compute_with_ctx(ctx, &graph, 4);
  143. const float * output = ggml_get_data_f32(m2);
  144. for (int i = 0; i < ggml_nelements(m2); ++i) {
  145. assert(output[i] == buf1_f32[i] + buf2_f32[i]);
  146. }
  147. assert(g_custom2_count == 4);
  148. ggml_free(ctx);
  149. }
  150. // map_custom3
  151. // 1 task, userdata, not parallelized
  152. {
  153. struct ggml_context * ctx = make_ctx();
  154. struct ggml_tensor * t1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
  155. memcpy(t1->data, buf1_f32, ggml_nbytes(t1));
  156. struct ggml_tensor * t2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
  157. memcpy(t2->data, buf2_f32, ggml_nbytes(t2));
  158. struct ggml_tensor * t3 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
  159. memcpy(t3->data, buf3_f32, ggml_nbytes(t3));
  160. struct ggml_tensor * m3 = ggml_map_custom3(ctx, t1, t2, t3, custom3, 1, g_userdata);
  161. struct ggml_cgraph graph = ggml_build_forward(m3);
  162. ggml_graph_compute_with_ctx(ctx, &graph, 4);
  163. const float * output = ggml_get_data_f32(m3);
  164. for (int i = 0; i < ggml_nelements(m3); ++i) {
  165. assert(output[i] == buf1_f32[i] + buf2_f32[i] + buf3_f32[i]);
  166. }
  167. assert(g_custom3_count == 1);
  168. ggml_free(ctx);
  169. }
  170. return 0;
  171. }