| 12013 | // ggml_compute_forward_pool_2d |
| 12014 | |
| 12015 | static void ggml_compute_forward_pool_2d( |
| 12016 | const struct ggml_compute_params * params, |
| 12017 | const struct ggml_tensor * src, |
| 12018 | struct ggml_tensor * dst) { |
| 12019 | assert(src->type == GGML_TYPE_F32); |
| 12020 | assert(params->ith == 0); |
| 12021 | |
| 12022 | if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) { |
| 12023 | return; |
| 12024 | } |
| 12025 | |
| 12026 | const int32_t * opts = (const int32_t *)dst->op_params; |
| 12027 | enum ggml_op_pool op = opts[0]; |
| 12028 | const int k0 = opts[1]; |
| 12029 | const int k1 = opts[2]; |
| 12030 | const int s0 = opts[3]; |
| 12031 | const int s1 = opts[4]; |
| 12032 | const int p0 = opts[5]; |
| 12033 | const int p1 = opts[6]; |
| 12034 | const char * cdata = (const char*)src->data; |
| 12035 | const char * const data_end = cdata + ggml_nbytes(src); |
| 12036 | |
| 12037 | const int64_t px = dst->ne[0]; |
| 12038 | const int64_t py = dst->ne[1]; |
| 12039 | const int64_t pa = px * py; |
| 12040 | |
| 12041 | float * dplane = (float *)dst->data; |
| 12042 | |
| 12043 | const int ka = k0 * k1; |
| 12044 | const int offset0 = -p0; |
| 12045 | const int offset1 = -p1; |
| 12046 | |
| 12047 | while (cdata < data_end) { |
| 12048 | for (int oy = 0; oy < py; ++oy) { |
| 12049 | float * const drow = dplane + oy * px; |
| 12050 | for (int ox = 0; ox < px; ++ox) { |
| 12051 | float * const out = drow + ox; |
| 12052 | switch (op) { |
| 12053 | case GGML_OP_POOL_AVG: *out = 0; break; |
| 12054 | case GGML_OP_POOL_MAX: *out = -FLT_MAX; break; |
| 12055 | case GGML_OP_POOL_COUNT: GGML_ASSERT(false); break; |
| 12056 | } |
| 12057 | |
| 12058 | const int ix = offset0 + ox * s0; |
| 12059 | const int iy = offset1 + oy * s1; |
| 12060 | |
| 12061 | for (int ky = 0; ky < k1; ++ky) { |
| 12062 | if (iy + ky < 0 || iy + ky >= src->ne[1]) continue; |
| 12063 | const float * const srow = (const float *)(cdata + src->nb[1] * (iy + ky)); |
| 12064 | for (int kx = 0; kx < k0; ++kx) { |
| 12065 | int j = ix + kx; |
| 12066 | if (j < 0 || j >= src->ne[0]) continue; |
| 12067 | switch (op) { |
| 12068 | case GGML_OP_POOL_AVG: *out += srow[j]; break; |
| 12069 | case GGML_OP_POOL_MAX: if (srow[j] > *out) *out = srow[j]; break; |
| 12070 | case GGML_OP_POOL_COUNT: GGML_ASSERT(false); break; |
| 12071 | } |
| 12072 | } |
no test coverage detected