| 1 | // yala is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // |
| 4 | // Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved. |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "pooling_loongarch.h" |
| 16 | |
| 17 | #include <float.h> |
| 18 | |
| 19 | #if __loongarch_sx |
| 20 | #include <lsxintrin.h> |
| 21 | #endif // __loongarch_sx |
| 22 | |
| 23 | #include "loongarch_usability.h" |
| 24 | |
| 25 | namespace ncnn { |
| 26 | |
| 27 | Pooling_loongarch::Pooling_loongarch() |
| 28 | { |
| 29 | #if __loongarch_sx |
| 30 | support_packing = true; |
| 31 | #endif // __loongarch_sx |
| 32 | } |
| 33 | |
| 34 | int Pooling_loongarch::create_pipeline(const Option& /*opt*/) |
| 35 | { |
| 36 | if (adaptive_pooling) |
| 37 | { |
| 38 | support_packing = false; |
| 39 | |
| 40 | support_bf16_storage = false; |
| 41 | support_fp16_storage = false; |
| 42 | support_int8_storage = false; |
| 43 | support_tensor_storage = false; |
| 44 | } |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int Pooling_loongarch::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 49 | { |
| 50 | if (adaptive_pooling) |
| 51 | { |
| 52 | return Pooling::forward(bottom_blob, top_blob, opt); |
| 53 | } |
| 54 | |
| 55 | // max value in NxN window |
| 56 | // avg value in NxN window |
| 57 | |
| 58 | int w = bottom_blob.w; |
| 59 | int h = bottom_blob.h; |
| 60 | int channels = bottom_blob.c; |
| 61 | size_t elemsize = bottom_blob.elemsize; |
| 62 | int elempack = bottom_blob.elempack; |
| 63 | |
| 64 | #if __loongarch_sx |
| 65 | // NCNN_LOGE("Pooling input %d x %d pad = %d %d %d %d ksize=%d %d stride=%d %d", w, h, pad_left, pad_right, pad_top, pad_bottom, kernel_w, kernel_h, stride_w, stride_h); |
| 66 | |
| 67 | if (elempack == 4) |
| 68 | { |
| 69 | if (global_pooling) |
| 70 | { |
| 71 | top_blob.create(channels, elemsize, elempack, opt.blob_allocator); |
| 72 | if (top_blob.empty()) |
| 73 | return -100; |
| 74 | |
| 75 | int size = w * h; |
| 76 | |
| 77 | if (pooling_type == PoolMethod_MAX) |
| 78 | { |
| 79 | #pragma omp parallel for num_threads(opt.num_threads) |
| 80 | for (int q = 0; q < channels; q++) |
| 81 | { |
| 82 | const float* ptr = bottom_blob.channel(q); |
| 83 | |
| 84 | __m128 _max = (__m128)__lsx_vld(ptr, 0); |
| 85 | for (int i = 0; i < size; i++) |
| 86 | { |
| 87 | __m128 _val = (__m128)__lsx_vld(ptr, 0); |
| 88 | _max = __lsx_vfmax_s(_max, _val); |
| 89 | ptr += 4; |
| 90 | } |
| 91 | |
| 92 | float* outptr = top_blob; |
| 93 | __lsx_vst(_max, outptr + q * 4, 0); |
| 94 | } |
| 95 | } |
| 96 | else if (pooling_type == PoolMethod_AVE) |
| 97 | { |
| 98 | #pragma omp parallel for num_threads(opt.num_threads) |
| 99 | for (int q = 0; q < channels; q++) |
| 100 | { |
| 101 | const float* ptr = bottom_blob.channel(q); |
| 102 | |
| 103 | __m128 _sum = (__m128)__lsx_vreplgr2vr_w(0); |
| 104 | for (int i = 0; i < size; i++) |
| 105 | { |
| 106 | __m128 _val = (__m128)__lsx_vld(ptr, 0); |
| 107 | _sum = __lsx_vfadd_s(_sum, _val); |
| 108 | ptr += 4; |
| 109 | } |
| 110 | |
| 111 | __m128 _avg = __lsx_vfmul_s(_sum, __lsx_vreplfr2vr_s(1.f / size)); |
| 112 | |
| 113 | float* outptr = top_blob; |
| 114 | __lsx_vst(_avg, outptr + q * 4, 0); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | Mat bottom_blob_bordered; |
| 122 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 123 | if (bottom_blob_bordered.empty()) |
| 124 | return -100; |
| 125 | |
| 126 | w = bottom_blob_bordered.w; |
| 127 | h = bottom_blob_bordered.h; |
| 128 | |
| 129 | int outw = (w - kernel_w) / stride_w + 1; |
| 130 | int outh = (h - kernel_h) / stride_h + 1; |
| 131 | |
| 132 | top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator); |
| 133 | if (top_blob.empty()) |
| 134 | return -100; |
| 135 | |
| 136 | const int maxk = kernel_w * kernel_h; |
| 137 | |
| 138 | // kernel offsets |
| 139 | std::vector<int> _space_ofs(maxk); |
| 140 | int* space_ofs = &_space_ofs[0]; |
| 141 | { |
| 142 | int p1 = 0; |
| 143 | int p2 = 0; |
| 144 | int gap = w - kernel_w; |
| 145 | for (int i = 0; i < kernel_h; i++) |
| 146 | { |
| 147 | for (int j = 0; j < kernel_w; j++) |
| 148 | { |
| 149 | space_ofs[p1] = p2; |
| 150 | p1++; |
| 151 | p2++; |
| 152 | } |
| 153 | p2 += gap; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (pooling_type == PoolMethod_MAX) |
| 158 | { |
| 159 | #pragma omp parallel for num_threads(opt.num_threads) |
| 160 | for (int q = 0; q < channels; q++) |
| 161 | { |
| 162 | const Mat m = bottom_blob_bordered.channel(q); |
| 163 | float* outptr = top_blob.channel(q); |
| 164 | |
| 165 | for (int i = 0; i < outh; i++) |
| 166 | { |
| 167 | for (int j = 0; j < outw; j++) |
| 168 | { |
| 169 | const float* sptr = m.row(i * stride_h) + j * stride_w * 4; |
| 170 | |
| 171 | __m128 _max = (__m128)__lsx_vld(sptr, 0); |
| 172 | |
| 173 | for (int k = 0; k < maxk; k++) |
| 174 | { |
| 175 | __m128 _val = (__m128)__lsx_vld(sptr + space_ofs[k] * 4, 0); |
| 176 | _max = __lsx_vfmax_s(_max, _val); |
| 177 | } |
| 178 | |
| 179 | __lsx_vst(_max, outptr + j * 4, 0); |
| 180 | } |
| 181 | |
| 182 | outptr += outw * 4; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | else if (pooling_type == PoolMethod_AVE) |
| 187 | { |
| 188 | if (avgpool_count_include_pad == 0) |
| 189 | { |
| 190 | int wtailpad = 0; |
| 191 | int htailpad = 0; |
| 192 | |
| 193 | if (pad_mode == 0) // full padding |
| 194 | { |
| 195 | wtailpad = bottom_blob_bordered.w - bottom_blob.w - pad_left - pad_right; |
| 196 | htailpad = bottom_blob_bordered.h - bottom_blob.h - pad_top - pad_bottom; |
| 197 | } |
| 198 | |
| 199 | #pragma omp parallel for num_threads(opt.num_threads) |
| 200 | for (int q = 0; q < channels; q++) |
| 201 | { |
| 202 | const Mat m = bottom_blob_bordered.channel(q); |
| 203 | float* outptr = top_blob.channel(q); |
| 204 | |
| 205 | for (int i = 0; i < outh; i++) |
| 206 | { |
| 207 | int sy0 = i * stride_h; |
| 208 | |
| 209 | for (int j = 0; j < outw; j++) |
| 210 | { |
| 211 | int sx0 = j * stride_w; |
| 212 | |
| 213 | __m128 _sum = (__m128)__lsx_vreplgr2vr_w(0); |
| 214 | int area = 0; |
| 215 | |
| 216 | for (int ki = 0; ki < kernel_h; ki++) |
| 217 | { |
| 218 | int sy = sy0 + ki; |
| 219 | |
| 220 | if (sy < pad_top) |
| 221 | continue; |
| 222 | |
| 223 | if (sy >= h - pad_bottom - htailpad) |
| 224 | break; |
| 225 | |
| 226 | for (int kj = 0; kj < kernel_w; kj++) |
| 227 | { |
| 228 | int sx = sx0 + kj; |
| 229 | |
| 230 | if (sx < pad_left) |
| 231 | continue; |
| 232 | |
| 233 | if (sx >= w - pad_right - wtailpad) |
| 234 | break; |
| 235 | |
| 236 | __m128 _val = (__m128)__lsx_vld(m.row(sy) + sx * 4, 0); |
| 237 | _sum = __lsx_vfadd_s(_sum, _val); |
| 238 | area += 1; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | __m128 _avg = __lsx_vfmul_s(_sum, __lsx_vreplfr2vr_s(1.f / area)); |
| 243 | __lsx_vst(_avg, outptr + j * 4, 0); |
| 244 | } |
| 245 | |
| 246 | outptr += outw * 4; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | else // if (avgpool_count_include_pad == 1) |
| 251 | { |
| 252 | #pragma omp parallel for num_threads(opt.num_threads) |
| 253 | for (int q = 0; q < channels; q++) |
| 254 | { |
| 255 | const Mat m = bottom_blob_bordered.channel(q); |
| 256 | float* outptr = top_blob.channel(q); |
| 257 | |
| 258 | const float inv_maxk = 1.f / maxk; |
| 259 | |
| 260 | for (int i = 0; i < outh; i++) |
| 261 | { |
| 262 | for (int j = 0; j < outw; j++) |
| 263 | { |
| 264 | const float* sptr = m.row(i * stride_h) + j * stride_w * 4; |
| 265 | |
| 266 | __m128 _sum = (__m128)__lsx_vreplgr2vr_w(0); |
| 267 | |
| 268 | for (int k = 0; k < maxk; k++) |
| 269 | { |
| 270 | __m128 _val = (__m128)__lsx_vld(sptr + space_ofs[k] * 4, 0); |
| 271 | _sum = __lsx_vfadd_s(_sum, _val); |
| 272 | } |
| 273 | |
| 274 | __m128 _avg = __lsx_vfmul_s(_sum, __lsx_vreplfr2vr_s(inv_maxk)); |
| 275 | __lsx_vst(_avg, outptr + j * 4, 0); |
| 276 | } |
| 277 | |
| 278 | outptr += outw * 4; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | #endif // __loongarch_sx |
| 287 | |
| 288 | return Pooling::forward(bottom_blob, top_blob, opt); |
| 289 | } |
| 290 | |
| 291 | } // namespace ncnn |