Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/tests/test-conv-transpose.c # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / tests / test-conv-transpose.c` 248 lines ยท 8458 bytes Raw Blame History 1 #include "ggml.h" 2 #include "ggml-cpu.h" 3 4 #include 5 #include 6 #include 7 8 struct ggml_context* make_ctx(void) { 9 struct ggml_init_params params = { 10 .mem_size = 2 * 1024 * 1024, 11 }; 12 13 return ggml_init(params); 14 } 15 16 void printf_tensor(struct ggml_tensor * t) { 17 if (t->type == GGML_TYPE_F32) { 18 const float * t_d = ggml_get_data_f32(t); 19 for (int i = 0; i < t->ne[2]; ++i) { 20 for (int j = 0; j < t->ne[1]; ++j) { 21 for (int k = 0; k < t->ne[0]; ++k) { 22 printf("%.1f ", t_d[i * t->ne[1] * t->ne[0] + j * t->ne[0] + k]); 23 } 24 printf("\n"); 25 } 26 printf("---\n"); 27 } 28 } 29 else if (t->type == GGML_TYPE_F16) { 30 const ggml_fp16_t * t_d = ggml_get_data(t); 31 for (int i = 0; i < t->ne[2]; ++i) { 32 for (int j = 0; j < t->ne[1]; ++j) { 33 for (int k = 0; k < t->ne[0]; ++k) { 34 printf("%.1f ", ggml_fp16_to_fp32(t_d[i * t->ne[1] * t->ne[0] + j * t->ne[0] + k])); 35 } 36 printf("\n"); 37 } 38 printf("---\n"); 39 } 40 } 41 else { 42 printf("unknown type\n"); 43 } 44 } 45 46 void check_tensor(struct ggml_tensor * t, float * expected_t_d, int ne0, int ne1, int ne2) { 47 GGML_ASSERT(t->type == GGML_TYPE_F32); 48 GGML_ASSERT(t->ne[0] == ne0); 49 GGML_ASSERT(t->ne[1] == ne1); 50 GGML_ASSERT(t->ne[2] == ne2); 51 for (int i2 = 0; i2 < ne2; ++i2) { 52 for (int i1 = 0; i1 < ne1; ++i1) { 53 for (int i0 = 0; i0 < ne0; ++i0) { 54 float expected = *(expected_t_d + i2 * ne1 * ne0 + i1 * ne0 + i0); 55 float actual = ggml_get_data_f32(t)[i2 * ne1 * ne0 + i1 * ne0 + i0]; 56 if (expected != actual) { 57 printf("expected %.1f, got %.1f\n", expected, actual); 58 } 59 GGML_ASSERT(expected == actual); 60 } 61 } 62 } 63 } 64 65 void test_conv_transpose_1d(void) { 66 67 float buf_f32[1024]; 68 for (int i = 0; i < 1024; ++i) { 69 buf_f32[i] = (float)i; 70 } 71 72 ggml_fp16_t buf_f16[1024]; 73 for (int i = 0; i < 1024; ++i) { 74 buf_f16[i] = ggml_fp32_to_fp16((float)i); 75 } 76 77 float expected_out_1[3][4] = { 78 {18.0, 45.0, 59.0, 37.0}, 79 {24.0, 61.0, 83.0, 51.0}, 80 {30.0, 77.0, 107.0, 65.0}, 81 }; 82 float expected_out_2[3][6] = { 83 {18.0, 21.0, 24.0, 29.0, 30.0, 37.0}, 84 {24.0, 27.0, 34.0, 39.0, 44.0, 51.0}, 85 {30.0, 33.0, 44.0, 49.0, 58.0, 65.0}, 86 }; 87 float expected_out_3[3][8] = { 88 {18.0, 21.0, 0.0, 24.0, 29.0, 0.0, 30.0, 37.0}, 89 {24.0, 27.0, 0.0, 34.0, 39.0, 0.0, 44.0, 51.0}, 90 {30.0, 33.0, 0.0, 44.0, 49.0, 0.0, 58.0, 65.0}, 91 }; 92 93 // conv transpose 1d with stride 1, 2 & 3 94 { 95 struct ggml_context * ctx = make_ctx(); 96 97 struct ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 3, 2); // l x cin 98 memcpy(t->data, buf_f32, ggml_nbytes(t)); 99 100 struct ggml_tensor * k = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, 2, 3, 2); // k x cout x cin 101 memcpy(k->data, buf_f16, ggml_nbytes(k)); 102 103 struct ggml_tensor * out_1 = ggml_conv_transpose_1d(ctx, k, t, 1 /* s0 */, 0 /* p0 */, 1 /* d0 */); 104 struct ggml_tensor * out_2 = ggml_conv_transpose_1d(ctx, k, t, 2 /* s0 */, 0 /* p0 */, 1 /* d0 */); 105 struct ggml_tensor * out_3 = ggml_conv_transpose_1d(ctx, k, t, 3 /* s0 */, 0 /* p0 */, 1 /* d0 */); 106 107 struct ggml_cgraph * gf_1 = ggml_new_graph(ctx); 108 struct ggml_cgraph * gf_2 = ggml_new_graph(ctx); 109 struct ggml_cgraph * gf_3 = ggml_new_graph(ctx); 110 111 ggml_build_forward_expand(gf_1, out_1); 112 ggml_build_forward_expand(gf_2, out_2); 113 ggml_build_forward_expand(gf_3, out_3); 114 115 ggml_graph_compute_with_ctx(ctx, gf_1, 1); 116 ggml_graph_compute_with_ctx(ctx, gf_2, 1); 117 ggml_graph_compute_with_ctx(ctx, gf_3, 1); 118 119 check_tensor(out_1, (float*)expected_out_1, 4, 3, 1); 120 check_tensor(out_2, (float*)expected_out_2, 6, 3, 1); 121 check_tensor(out_3, (float*)expected_out_3, 8, 3, 1); 122 } 123 } 124 125 void test_conv_transpose_2d(void) { 126 127 float buf_f32[1024]; 128 for (int i = 0; i < 1024; ++i) { 129 buf_f32[i] = (float)i; 130 } 131 132 ggml_fp16_t buf_f16[1024]; 133 for (int i = 0; i < 1024; ++i) { 134 buf_f16[i] = ggml_fp32_to_fp16((float)i); 135 } 136 137 float expected_out_1[3][3][4] = { 138 { 139 {72.0, 162.0, 188.0, 106.0}, 140 {192.0, 430.0, 490.0, 274.0}, 141 {132.0, 292.0, 326.0, 180.0}, 142 }, 143 { 144 {96.0, 218.0, 260.0, 146.0}, 145 {264.0, 590.0, 682.0, 378.0}, 146 {180.0, 396.0, 446.0, 244.0}, 147 }, 148 { 149 {120.0, 274.0, 332.0, 186.0}, 150 {336.0, 750.0, 874.0, 482.0}, 151 {228.0, 500.0, 566.0, 308.0}, 152 }, 153 }; 154 155 float expected_out_2[3][4][6] = { 156 { 157 {72.0, 78.0, 84.0, 92.0, 96.0, 106.0}, 158 {84.0, 90.0, 100.0, 108.0, 116.0, 126.0}, 159 {108.0, 120.0, 120.0, 134.0, 132.0, 148.0}, 160 {132.0, 144.0, 148.0, 162.0, 164.0, 180.0}, 161 }, 162 { 163 {96.0, 102.0, 116.0, 124.0, 136.0, 146.0}, 164 {108.0, 114.0, 132.0, 140.0, 156.0, 166.0}, 165 {156.0, 168.0, 176.0, 190.0, 196.0, 212.0}, 166 {180.0, 192.0, 204.0, 218.0, 228.0, 244.0}, 167 }, 168 { 169 {120.0, 126.0, 148.0, 156.0, 176.0, 186.0}, 170 {132.0, 138.0, 164.0, 172.0, 196.0, 206.0}, 171 {204.0, 216.0, 232.0, 246.0, 260.0, 276.0}, 172 {228.0, 240.0, 260.0, 274.0, 292.0, 308.0}, 173 }, 174 }; 175 176 float expected_out_3[3][5][8] = { 177 { 178 {72.0, 78.0, 0.0, 84.0, 92.0, 0.0, 96.0, 106.0}, 179 {84.0, 90.0, 0.0, 100.0, 108.0, 0.0, 116.0, 126.0}, 180 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 181 {108.0, 120.0, 0.0, 120.0, 134.0, 0.0, 132.0, 148.0}, 182 {132.0, 144.0, 0.0, 148.0, 162.0, 0.0, 164.0, 180.0}, 183 }, 184 { 185 {96.0, 102.0, 0.0, 116.0, 124.0, 0.0, 136.0, 146.0}, 186 {108.0, 114.0, 0.0, 132.0, 140.0, 0.0, 156.0, 166.0}, 187 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 188 {156.0, 168.0, 0.0, 176.0, 190.0, 0.0, 196.0, 212.0}, 189 {180.0, 192.0, 0.0, 204.0, 218.0, 0.0, 228.0, 244.0}, 190 }, 191 { 192 {120.0, 126.0, 0.0, 148.0, 156.0, 0.0, 176.0, 186.0}, 193 {132.0, 138.0, 0.0, 164.0, 172.0, 0.0, 196.0, 206.0}, 194 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 195 {204.0, 216.0, 0.0, 232.0, 246.0, 0.0, 260.0, 276.0}, 196 {228.0, 240.0, 0.0, 260.0, 274.0, 0.0, 292.0, 308.0}, 197 }, 198 }; 199 200 // conv transpose 2d with stride 1, 2 & 3 201 { 202 struct ggml_context * ctx = make_ctx(); 203 204 struct ggml_tensor * t = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, 3, 2, 2, 1); // w x h x cin 205 memcpy(t->data, buf_f32, ggml_nbytes(t)); 206 207 struct ggml_tensor * k = ggml_new_tensor_4d(ctx, GGML_TYPE_F16, 2, 2, 3, 2); // w x h cin x cout 208 memcpy(k->data, buf_f16, ggml_nbytes(k)); 209 210 struct ggml_tensor * out_1 = ggml_conv_transpose_2d_p0(ctx, k, t, 1); 211 struct ggml_tensor * out_2 = ggml_conv_transpose_2d_p0(ctx, k, t, 2); 212 struct ggml_tensor * out_3 = ggml_conv_transpose_2d_p0(ctx, k, t, 3); 213 214 struct ggml_cgraph * gf_1 = ggml_new_graph(ctx); 215 struct ggml_cgraph * gf_2 = ggml_new_graph(ctx); 216 struct ggml_cgraph * gf_3 = ggml_new_graph(ctx); 217 218 ggml_build_forward_expand(gf_1, out_1); 219 ggml_build_forward_expand(gf_2, out_2); 220 ggml_build_forward_expand(gf_3, out_3); 221 222 ggml_graph_compute_with_ctx(ctx, gf_1, 1); 223 ggml_graph_compute_with_ctx(ctx, gf_2, 1); 224 ggml_graph_compute_with_ctx(ctx, gf_3, 1); 225 226 // printf("in\n"); 227 // printf_tensor(t); 228 // printf("\n\nkernel\n"); 229 // printf_tensor(k); 230 // printf("\n\nout\n"); 231 // printf_tensor(out); 232 // printf("\n\nout_2\n"); 233 // printf_tensor(out_2); 234 // printf("\n\nout_3\n"); 235 // printf_tensor(out_3); 236 237 check_tensor(out_1, (float*)expected_out_1, 4, 3, 3); 238 check_tensor(out_2, (float*)expected_out_2, 6, 4, 3); 239 check_tensor(out_3, (float*)expected_out_3, 8, 5, 3); 240 241 } 242 } 243 244 int main(int argc, const char * argv[]) { 245 test_conv_transpose_1d(); 246 test_conv_transpose_2d(); 247 return 0; 248 }