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-pool.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-pool.c` 274 lines ยท 9006 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 int main(int argc, const char** argv) { 17 18 float buf_f32[1024]; 19 ggml_fp16_t buf_f16[1024]; 20 for (int i = 0; i < 1024; ++i) { 21 buf_f32[i] = (float)(i + 1); 22 buf_f16[i] = ggml_fp32_to_fp16(buf_f32[i]); 23 } 24 25 // avg pool 1d - Float 32 26 { 27 struct ggml_context * ctx = make_ctx(); 28 struct ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2); 29 memcpy(t->data, buf_f32, ggml_nbytes(t)); 30 31 struct ggml_tensor * t_pooled = ggml_pool_1d(ctx, t, GGML_OP_POOL_AVG, 3, 3, 0); 32 GGML_ASSERT(t_pooled->ne[0] == 3); 33 GGML_ASSERT(t_pooled->ne[1] == 2); 34 GGML_ASSERT(t_pooled->ne[2] == 1); 35 36 struct ggml_cgraph * graph = ggml_new_graph(ctx); 37 ggml_build_forward_expand(graph, t_pooled); 38 39 ggml_graph_compute_with_ctx(ctx, graph, 4); 40 41 const float * output = ggml_get_data_f32(t_pooled); 42 43 GGML_ASSERT(output[0] == 2); 44 GGML_ASSERT(output[1] == 5); 45 GGML_ASSERT(output[2] == 8); 46 GGML_ASSERT(output[3] == 12); 47 GGML_ASSERT(output[4] == 15); 48 GGML_ASSERT(output[5] == 18); 49 50 ggml_free(ctx); 51 } 52 53 // avg pool 1d - Float 16 54 { 55 struct ggml_context * ctx = make_ctx(); 56 struct ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F16, 10, 2); 57 memcpy(t->data, buf_f16, ggml_nbytes(t)); 58 59 struct ggml_tensor * t_pooled = ggml_pool_1d(ctx, t, GGML_OP_POOL_AVG, 3, 3, 0); 60 GGML_ASSERT(t_pooled->ne[0] == 3); 61 GGML_ASSERT(t_pooled->ne[1] == 2); 62 GGML_ASSERT(t_pooled->ne[2] == 1); 63 64 struct ggml_cgraph * graph = ggml_new_graph(ctx); 65 ggml_build_forward_expand(graph, t_pooled); 66 67 ggml_graph_compute_with_ctx(ctx, graph, 4); 68 69 const float * output = ggml_get_data_f32(t_pooled); 70 71 GGML_ASSERT(output[0] == 2); 72 GGML_ASSERT(output[1] == 5); 73 GGML_ASSERT(output[2] == 8); 74 GGML_ASSERT(output[3] == 12); 75 GGML_ASSERT(output[4] == 15); 76 GGML_ASSERT(output[5] == 18); 77 78 ggml_free(ctx); 79 } 80 81 // max pool 1d - Float 32 82 { 83 struct ggml_context * ctx = make_ctx(); 84 struct ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2); 85 memcpy(t->data, buf_f32, ggml_nbytes(t)); 86 87 struct ggml_tensor * t_pooled = ggml_pool_1d(ctx, t, GGML_OP_POOL_MAX, 3, 3, 0); 88 GGML_ASSERT(t_pooled->ne[0] == 3); 89 GGML_ASSERT(t_pooled->ne[1] == 2); 90 GGML_ASSERT(t_pooled->ne[2] == 1); 91 92 struct ggml_cgraph * graph = ggml_new_graph(ctx); 93 ggml_build_forward_expand(graph, t_pooled); 94 95 ggml_graph_compute_with_ctx(ctx, graph, 4); 96 97 const float * output = ggml_get_data_f32(t_pooled); 98 GGML_ASSERT(output[0] == 3); 99 GGML_ASSERT(output[1] == 6); 100 GGML_ASSERT(output[2] == 9); 101 GGML_ASSERT(output[3] == 13); 102 GGML_ASSERT(output[4] == 16); 103 GGML_ASSERT(output[5] == 19); 104 105 ggml_free(ctx); 106 } 107 108 // max pool 1d - Float 16 109 { 110 struct ggml_context * ctx = make_ctx(); 111 struct ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F16, 10, 2); 112 memcpy(t->data, buf_f16, ggml_nbytes(t)); 113 114 struct ggml_tensor * t_pooled = ggml_pool_1d(ctx, t, GGML_OP_POOL_MAX, 3, 3, 0); 115 GGML_ASSERT(t_pooled->ne[0] == 3); 116 GGML_ASSERT(t_pooled->ne[1] == 2); 117 GGML_ASSERT(t_pooled->ne[2] == 1); 118 119 struct ggml_cgraph * graph = ggml_new_graph(ctx); 120 ggml_build_forward_expand(graph, t_pooled); 121 122 ggml_graph_compute_with_ctx(ctx, graph, 4); 123 124 const float * output = ggml_get_data_f32(t_pooled); 125 GGML_ASSERT(output[0] == 3); 126 GGML_ASSERT(output[1] == 6); 127 GGML_ASSERT(output[2] == 9); 128 GGML_ASSERT(output[3] == 13); 129 GGML_ASSERT(output[4] == 16); 130 GGML_ASSERT(output[5] == 19); 131 132 ggml_free(ctx); 133 } 134 135 // avg pool 2d - Float 32 136 { 137 struct ggml_context * ctx = make_ctx(); 138 struct ggml_tensor * t = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 10, 10, 2); 139 memcpy(t->data, buf_f32, ggml_nbytes(t)); 140 141 struct ggml_tensor * t_pooled = ggml_pool_2d(ctx, t, GGML_OP_POOL_AVG, 3, 4, 3, 4, 0, 0); 142 GGML_ASSERT(t_pooled->ne[0] == 3); 143 GGML_ASSERT(t_pooled->ne[1] == 2); 144 GGML_ASSERT(t_pooled->ne[2] == 2); 145 GGML_ASSERT(t_pooled->ne[3] == 1); 146 147 struct ggml_cgraph * graph = ggml_new_graph(ctx); 148 ggml_build_forward_expand(graph, t_pooled); 149 150 ggml_graph_compute_with_ctx(ctx, graph, 4); 151 152 const float * output = ggml_get_data_f32(t_pooled); 153 GGML_ASSERT(output[0] == 17); 154 GGML_ASSERT(output[1] == 20); 155 GGML_ASSERT(output[2] == 23); 156 GGML_ASSERT(output[3] == 57); 157 GGML_ASSERT(output[4] == 60); 158 GGML_ASSERT(output[5] == 63); 159 GGML_ASSERT(output[6] == 117); 160 GGML_ASSERT(output[7] == 120); 161 GGML_ASSERT(output[8] == 123); 162 GGML_ASSERT(output[9] == 157); 163 GGML_ASSERT(output[10] == 160); 164 GGML_ASSERT(output[11] == 163); 165 166 167 ggml_free(ctx); 168 } 169 170 // avg pool 2d - Float 16 171 { 172 struct ggml_context * ctx = make_ctx(); 173 struct ggml_tensor * t = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, 10, 10, 2); 174 memcpy(t->data, buf_f16, ggml_nbytes(t)); 175 176 struct ggml_tensor * t_pooled = ggml_pool_2d(ctx, t, GGML_OP_POOL_AVG, 3, 4, 3, 4, 0, 0); 177 GGML_ASSERT(t_pooled->ne[0] == 3); 178 GGML_ASSERT(t_pooled->ne[1] == 2); 179 GGML_ASSERT(t_pooled->ne[2] == 2); 180 GGML_ASSERT(t_pooled->ne[3] == 1); 181 182 struct ggml_cgraph * graph = ggml_new_graph(ctx); 183 ggml_build_forward_expand(graph, t_pooled); 184 185 ggml_graph_compute_with_ctx(ctx, graph, 4); 186 187 const float * output = ggml_get_data_f32(t_pooled); 188 GGML_ASSERT(output[0] == 17); 189 GGML_ASSERT(output[1] == 20); 190 GGML_ASSERT(output[2] == 23); 191 GGML_ASSERT(output[3] == 57); 192 GGML_ASSERT(output[4] == 60); 193 GGML_ASSERT(output[5] == 63); 194 GGML_ASSERT(output[6] == 117); 195 GGML_ASSERT(output[7] == 120); 196 GGML_ASSERT(output[8] == 123); 197 GGML_ASSERT(output[9] == 157); 198 GGML_ASSERT(output[10] == 160); 199 GGML_ASSERT(output[11] == 163); 200 201 202 ggml_free(ctx); 203 } 204 205 // max pool 2d - Float 32 206 { 207 struct ggml_context * ctx = make_ctx(); 208 struct ggml_tensor * t = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 10, 10, 2); 209 memcpy(t->data, buf_f32, ggml_nbytes(t)); 210 211 struct ggml_tensor * t_pooled = ggml_pool_2d(ctx, t, GGML_OP_POOL_MAX, 3, 4, 3, 4, 0, 0); 212 GGML_ASSERT(t_pooled->ne[0] == 3); 213 GGML_ASSERT(t_pooled->ne[1] == 2); 214 GGML_ASSERT(t_pooled->ne[2] == 2); 215 GGML_ASSERT(t_pooled->ne[3] == 1); 216 217 struct ggml_cgraph * graph = ggml_new_graph(ctx); 218 ggml_build_forward_expand(graph, t_pooled); 219 220 ggml_graph_compute_with_ctx(ctx, graph, 4); 221 222 const float * output = ggml_get_data_f32(t_pooled); 223 GGML_ASSERT(output[0] == 33); 224 GGML_ASSERT(output[1] == 36); 225 GGML_ASSERT(output[2] == 39); 226 GGML_ASSERT(output[3] == 73); 227 GGML_ASSERT(output[4] == 76); 228 GGML_ASSERT(output[5] == 79); 229 GGML_ASSERT(output[6] == 133); 230 GGML_ASSERT(output[7] == 136); 231 GGML_ASSERT(output[8] == 139); 232 GGML_ASSERT(output[9] == 173); 233 GGML_ASSERT(output[10] == 176); 234 GGML_ASSERT(output[11] == 179); 235 236 ggml_free(ctx); 237 } 238 239 // max pool 2d - Float 16 240 { 241 struct ggml_context * ctx = make_ctx(); 242 struct ggml_tensor * t = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, 10, 10, 2); 243 memcpy(t->data, buf_f16, ggml_nbytes(t)); 244 245 struct ggml_tensor * t_pooled = ggml_pool_2d(ctx, t, GGML_OP_POOL_MAX, 3, 4, 3, 4, 0, 0); 246 GGML_ASSERT(t_pooled->ne[0] == 3); 247 GGML_ASSERT(t_pooled->ne[1] == 2); 248 GGML_ASSERT(t_pooled->ne[2] == 2); 249 GGML_ASSERT(t_pooled->ne[3] == 1); 250 251 struct ggml_cgraph * graph = ggml_new_graph(ctx); 252 ggml_build_forward_expand(graph, t_pooled); 253 254 ggml_graph_compute_with_ctx(ctx, graph, 4); 255 256 const float * output = ggml_get_data_f32(t_pooled); 257 GGML_ASSERT(output[0] == 33); 258 GGML_ASSERT(output[1] == 36); 259 GGML_ASSERT(output[2] == 39); 260 GGML_ASSERT(output[3] == 73); 261 GGML_ASSERT(output[4] == 76); 262 GGML_ASSERT(output[5] == 79); 263 GGML_ASSERT(output[6] == 133); 264 GGML_ASSERT(output[7] == 136); 265 GGML_ASSERT(output[8] == 139); 266 GGML_ASSERT(output[9] == 173); 267 GGML_ASSERT(output[10] == 176); 268 GGML_ASSERT(output[11] == 179); 269 270 ggml_free(ctx); 271 } 272 273 return 0; 274 }