1
0

test-opt.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "ggml.h"
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cassert>
  6. #define MAX_NARGS 2
  7. #if defined(__GNUC__)
  8. #pragma GCC diagnostic ignored "-Wdouble-promotion"
  9. #endif
  10. //
  11. // logging
  12. //
  13. #define GGML_DEBUG 0
  14. #if (GGML_DEBUG >= 1)
  15. #define GGML_PRINT_DEBUG(...) printf(__VA_ARGS__)
  16. #else
  17. #define GGML_PRINT_DEBUG(...)
  18. #endif
  19. #if (GGML_DEBUG >= 5)
  20. #define GGML_PRINT_DEBUG_5(...) printf(__VA_ARGS__)
  21. #else
  22. #define GGML_PRINT_DEBUG_5(...)
  23. #endif
  24. #if (GGML_DEBUG >= 10)
  25. #define GGML_PRINT_DEBUG_10(...) printf(__VA_ARGS__)
  26. #else
  27. #define GGML_PRINT_DEBUG_10(...)
  28. #endif
  29. #define GGML_PRINT(...) printf(__VA_ARGS__)
  30. static float frand(void) {
  31. return (float)rand()/(float)RAND_MAX;
  32. }
  33. static struct ggml_tensor * get_random_tensor(
  34. struct ggml_context * ctx0, int ndims, int64_t ne[], float fmin, float fmax
  35. ) {
  36. struct ggml_tensor * result = ggml_new_tensor(ctx0, GGML_TYPE_F32, ndims, ne);
  37. switch (ndims) {
  38. case 1:
  39. for (int i0 = 0; i0 < ne[0]; i0++) {
  40. ((float *)result->data)[i0] = frand()*(fmax - fmin) + fmin;
  41. }
  42. break;
  43. case 2:
  44. for (int i1 = 0; i1 < ne[1]; i1++) {
  45. for (int i0 = 0; i0 < ne[0]; i0++) {
  46. ((float *)result->data)[i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
  47. }
  48. }
  49. break;
  50. case 3:
  51. for (int i2 = 0; i2 < ne[2]; i2++) {
  52. for (int i1 = 0; i1 < ne[1]; i1++) {
  53. for (int i0 = 0; i0 < ne[0]; i0++) {
  54. ((float *)result->data)[i2*ne[1]*ne[0] + i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
  55. }
  56. }
  57. }
  58. break;
  59. case 4:
  60. for (int i3 = 0; i3 < ne[3]; i3++) {
  61. for (int i2 = 0; i2 < ne[2]; i2++) {
  62. for (int i1 = 0; i1 < ne[1]; i1++) {
  63. for (int i0 = 0; i0 < ne[0]; i0++) {
  64. ((float *)result->data)[i3*ne[2]*ne[1]*ne[0] + i2*ne[1]*ne[0] + i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
  65. }
  66. }
  67. }
  68. }
  69. break;
  70. default:
  71. assert(false);
  72. }
  73. return result;
  74. }
  75. int main(void) {
  76. struct ggml_init_params params = {
  77. /* .mem_size = */ 1024*1024*1024,
  78. /* .mem_buffer = */ NULL,
  79. /* .no_alloc = */ false,
  80. };
  81. struct ggml_context * ctx = ggml_init(params);
  82. int64_t ne1[4] = {4, 128, 1, 1};
  83. int64_t ne2[4] = {4, 256, 1, 1};
  84. int64_t ne3[4] = {128, 256, 1, 1};
  85. struct ggml_tensor * a = get_random_tensor(ctx, 2, ne1, -1, +1);
  86. struct ggml_tensor * b = get_random_tensor(ctx, 2, ne2, -1, +1);
  87. ggml_set_param(ctx, a);
  88. ggml_set_param(ctx, b);
  89. struct ggml_tensor * c = get_random_tensor(ctx, 2, ne3, -1, +1);
  90. struct ggml_tensor * ab = ggml_mul_mat(ctx, a, b);
  91. struct ggml_tensor * d = ggml_sub(ctx, c, ab);
  92. struct ggml_tensor * e = ggml_sum(ctx, ggml_sqr(ctx, d));
  93. struct ggml_cgraph * ge = ggml_new_graph_custom(ctx, GGML_DEFAULT_GRAPH_SIZE, true);
  94. ggml_build_forward_expand(ge, e);
  95. ggml_graph_reset(ge);
  96. ggml_graph_compute_with_ctx(ctx, ge, /*n_threads*/ 1);
  97. const float fe = ggml_get_f32_1d(e, 0);
  98. printf("%s: e = %.4f\n", __func__, fe);
  99. struct ggml_opt_params opt_params = ggml_opt_default_params(GGML_OPT_ADAM);
  100. ggml_opt(ctx, opt_params, e);
  101. ggml_graph_reset(ge);
  102. ggml_graph_compute_with_ctx(ctx, ge, /*n_threads*/ 1);
  103. const float fe_opt = ggml_get_f32_1d(e, 0);
  104. printf("%s: original e = %.4f\n", __func__, fe);
  105. printf("%s: optimized e = %.4f\n", __func__, fe_opt);
  106. const bool success = (fe_opt <= fe);
  107. assert(success);
  108. ggml_free(ctx);
  109. return success ? 0 : -1;
  110. }
  111. // int64_t ne1[4] = {4, 128, 1, 1};
  112. // int64_t ne2[4] = {4, 256, 1, 1};;
  113. // int64_t ne3[4] = {128, 256, 1, 1};
  114. // main: original e = 25890.9375
  115. // main: optimized e = 10094.7031
  116. // int64_t ne1[4] = {8, 128, 1, 1};
  117. // int64_t ne2[4] = {8, 256, 1, 1};;
  118. // int64_t ne3[4] = {128, 256, 1, 1};
  119. // main: original e = 39429.5078
  120. // main: optimized e = 9275.8936
  121. // int64_t ne1[4] = {16, 128, 1, 1};
  122. // int64_t ne2[4] = {16, 256, 1, 1};;
  123. // int64_t ne3[4] = {128, 256, 1, 1};
  124. // main: original e = 68371.1328
  125. // main: optimized e = 7854.4502
  126. // int64_t ne1[4] = {32, 128, 1, 1};
  127. // int64_t ne2[4] = {32, 256, 1, 1};;
  128. // int64_t ne3[4] = {128, 256, 1, 1};
  129. // main: original e = 126061.1953
  130. // main: optimized e = 5451.0166
  131. // int64_t ne1[4] = {4, 1024, 1, 1};
  132. // int64_t ne2[4] = {4, 2048, 1, 1};;
  133. // int64_t ne3[4] = {1024, 2048, 1, 1};
  134. // main: original e = 1620817.8750
  135. // main: optimized e = 698387.6875
  136. // another run on M1
  137. // int64_t ne1[4] = {4, 1024, 1, 1};
  138. // int64_t ne2[4] = {4, 2048, 1, 1};;
  139. // int64_t ne3[4] = {1024, 2048, 1, 1};
  140. // main: original e = 1629595.6250
  141. // main: optimized e = 698169.1250
  142. // int64_t ne1[4] = {32, 1024, 1, 1};
  143. // int64_t ne2[4] = {32, 2048, 1, 1};;
  144. // int64_t ne3[4] = {1024, 2048, 1, 1};
  145. // main: original e = 8146770.5000
  146. // main: optimized e = 651119.1250