test-conv-transpose.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "ggml/ggml.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. struct ggml_context* make_ctx(void) {
  6. struct ggml_init_params params = {
  7. .mem_size = 2 * 1024 * 1024,
  8. };
  9. return ggml_init(params);
  10. }
  11. // void printf_tensor(struct ggml_tensor * t) {
  12. // if (t->type == GGML_TYPE_F32) {
  13. // const float * t_d = ggml_get_data_f32(t);
  14. // for (int i = 0; i < t->ne[2]; ++i) {
  15. // for (int j = 0; j < t->ne[1]; ++j) {
  16. // for (int k = 0; k < t->ne[0]; ++k) {
  17. // printf("%.1f ", t_d[i * t->ne[1] * t->ne[0] + j * t->ne[0] + k]);
  18. // }
  19. // printf("\n");
  20. // }
  21. // printf("---\n");
  22. // }
  23. // }
  24. // else if (t->type == GGML_TYPE_F16) {
  25. // const ggml_fp16_t * t_d = ggml_get_data(t);
  26. // for (int i = 0; i < t->ne[2]; ++i) {
  27. // for (int j = 0; j < t->ne[1]; ++j) {
  28. // for (int k = 0; k < t->ne[0]; ++k) {
  29. // printf("%.1f ", ggml_fp16_to_fp32(t_d[i * t->ne[1] * t->ne[0] + j * t->ne[0] + k]));
  30. // }
  31. // printf("\n");
  32. // }
  33. // printf("---\n");
  34. // }
  35. // }
  36. // else {
  37. // printf("unknown type\n");
  38. // }
  39. // }
  40. void check_tensor(struct ggml_tensor * t, float * expected_t_d, int ne0, int ne1, int ne2) {
  41. GGML_ASSERT(t->type == GGML_TYPE_F32);
  42. GGML_ASSERT(t->ne[0] == ne0);
  43. GGML_ASSERT(t->ne[1] == ne1);
  44. GGML_ASSERT(t->ne[2] == ne2);
  45. for (int i2 = 0; i2 < ne2; ++i2) {
  46. for (int i1 = 0; i1 < ne1; ++i1) {
  47. for (int i0 = 0; i0 < ne0; ++i0) {
  48. float expected = *(expected_t_d + i2 * ne1 * ne0 + i1 * ne0 + i0);
  49. float actual = ggml_get_data_f32(t)[i2 * ne1 * ne0 + i1 * ne0 + i0];
  50. GGML_ASSERT(expected == actual);
  51. }
  52. }
  53. }
  54. }
  55. int main(int argc, const char** argv) {
  56. float buf_f32[1024];
  57. for (int i = 0; i < 1024; ++i) {
  58. buf_f32[i] = (float)i;
  59. }
  60. ggml_fp16_t buf_f16[1024];
  61. for (int i = 0; i < 1024; ++i) {
  62. buf_f16[i] = ggml_fp32_to_fp16((float)i);
  63. }
  64. float expected_out_1[3][3][4] = {
  65. {
  66. {72.0, 162.0, 188.0, 106.0},
  67. {192.0, 430.0, 490.0, 274.0},
  68. {132.0, 292.0, 326.0, 180.0},
  69. },
  70. {
  71. {96.0, 218.0, 260.0, 146.0},
  72. {264.0, 590.0, 682.0, 378.0},
  73. {180.0, 396.0, 446.0, 244.0},
  74. },
  75. {
  76. {120.0, 274.0, 332.0, 186.0},
  77. {336.0, 750.0, 874.0, 482.0},
  78. {228.0, 500.0, 566.0, 308.0},
  79. },
  80. };
  81. float expected_out_2[3][4][6] = {
  82. {
  83. {72.0, 78.0, 84.0, 92.0, 96.0, 106.0},
  84. {84.0, 90.0, 100.0, 108.0, 116.0, 126.0},
  85. {108.0, 120.0, 120.0, 134.0, 132.0, 148.0},
  86. {132.0, 144.0, 148.0, 162.0, 164.0, 180.0},
  87. },
  88. {
  89. {96.0, 102.0, 116.0, 124.0, 136.0, 146.0},
  90. {108.0, 114.0, 132.0, 140.0, 156.0, 166.0},
  91. {156.0, 168.0, 176.0, 190.0, 196.0, 212.0},
  92. {180.0, 192.0, 204.0, 218.0, 228.0, 244.0},
  93. },
  94. {
  95. {120.0, 126.0, 148.0, 156.0, 176.0, 186.0},
  96. {132.0, 138.0, 164.0, 172.0, 196.0, 206.0},
  97. {204.0, 216.0, 232.0, 246.0, 260.0, 276.0},
  98. {228.0, 240.0, 260.0, 274.0, 292.0, 308.0},
  99. },
  100. };
  101. float expected_out_3[3][5][8] = {
  102. {
  103. {72.0, 78.0, 0.0, 84.0, 92.0, 0.0, 96.0, 106.0},
  104. {84.0, 90.0, 0.0, 100.0, 108.0, 0.0, 116.0, 126.0},
  105. {0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  106. {108.0, 120.0, 0.0, 120.0, 134.0, 0.0, 132.0, 148.0},
  107. {132.0, 144.0, 0.0, 148.0, 162.0, 0.0, 164.0, 180.0},
  108. },
  109. {
  110. {96.0, 102.0, 0.0, 116.0, 124.0, 0.0, 136.0, 146.0},
  111. {108.0, 114.0, 0.0, 132.0, 140.0, 0.0, 156.0, 166.0},
  112. {0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  113. {156.0, 168.0, 0.0, 176.0, 190.0, 0.0, 196.0, 212.0},
  114. {180.0, 192.0, 0.0, 204.0, 218.0, 0.0, 228.0, 244.0},
  115. },
  116. {
  117. {120.0, 126.0, 0.0, 148.0, 156.0, 0.0, 176.0, 186.0},
  118. {132.0, 138.0, 0.0, 164.0, 172.0, 0.0, 196.0, 206.0},
  119. {0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  120. {204.0, 216.0, 0.0, 232.0, 246.0, 0.0, 260.0, 276.0},
  121. {228.0, 240.0, 0.0, 260.0, 274.0, 0.0, 292.0, 308.0},
  122. },
  123. };
  124. // conv transpose 2d with stride 1, 2 & 3
  125. {
  126. struct ggml_context * ctx = make_ctx();
  127. struct ggml_tensor * t = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, 3, 2, 2, 1); // w x h x cin
  128. memcpy(t->data, buf_f32, ggml_nbytes(t));
  129. struct ggml_tensor * k = ggml_new_tensor_4d(ctx, GGML_TYPE_F16, 2, 2, 3, 2); // w x h cin x cout
  130. memcpy(k->data, buf_f16, ggml_nbytes(k));
  131. struct ggml_tensor * out_1 = ggml_conv_transpose_2d_p0(ctx, k, t, 1);
  132. struct ggml_tensor * out_2 = ggml_conv_transpose_2d_p0(ctx, k, t, 2);
  133. struct ggml_tensor * out_3 = ggml_conv_transpose_2d_p0(ctx, k, t, 3);
  134. struct ggml_cgraph gf_1 = ggml_build_forward(out_1);
  135. struct ggml_cgraph gf_2 = ggml_build_forward(out_2);
  136. struct ggml_cgraph gf_3 = ggml_build_forward(out_3);
  137. ggml_graph_compute_with_ctx(ctx, &gf_1, 1);
  138. ggml_graph_compute_with_ctx(ctx, &gf_2, 1);
  139. ggml_graph_compute_with_ctx(ctx, &gf_3, 1);
  140. // printf("in\n");
  141. // printf_tensor(t);
  142. // printf("\n\nkernel\n");
  143. // printf_tensor(k);
  144. // printf("\n\nout\n");
  145. // printf_tensor(out);
  146. // printf("\n\nout_2\n");
  147. // printf_tensor(out_2);
  148. // printf("\n\nout_3\n");
  149. // printf_tensor(out_3);
  150. check_tensor(out_1, (float*)expected_out_1, 4, 3, 3);
  151. check_tensor(out_2, (float*)expected_out_2, 6, 4, 3);
  152. check_tensor(out_3, (float*)expected_out_3, 8, 5, 3);
  153. }
  154. return 0;
  155. }