| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 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 "batchnorm_mips.h" |
| 16 | |
| 17 | #if __mips_msa |
| 18 | #include <msa.h> |
| 19 | #endif // __mips_msa |
| 20 | |
| 21 | #include "mips_usability.h" |
| 22 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | BatchNorm_mips::BatchNorm_mips() |
| 26 | { |
| 27 | #if __mips_msa |
| 28 | support_packing = true; |
| 29 | #endif // __mips_msa |
| 30 | } |
| 31 | |
| 32 | int BatchNorm_mips::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 33 | { |
| 34 | int dims = bottom_top_blob.dims; |
| 35 | int elempack = bottom_top_blob.elempack; |
| 36 | |
| 37 | if (dims == 1) |
| 38 | { |
| 39 | int w = bottom_top_blob.w * elempack; |
| 40 | |
| 41 | #if __mips_msa |
| 42 | int nn_w = w / 4; |
| 43 | int remain_w_start = nn_w * 4; |
| 44 | #else |
| 45 | int remain_w_start = 0; |
| 46 | #endif // __mips_msa |
| 47 | |
| 48 | float* ptr = bottom_top_blob; |
| 49 | |
| 50 | #if __mips_msa |
| 51 | #pragma omp parallel for num_threads(opt.num_threads) |
| 52 | for (int i = 0; i < nn_w; i++) |
| 53 | { |
| 54 | float* ptr0 = ptr + i * 4; |
| 55 | |
| 56 | v4f32 _p = (v4f32)__msa_ld_w(ptr0, 0); |
| 57 | v4f32 _a = (v4f32)__msa_ld_w((const float*)a_data + i * 4, 0); |
| 58 | v4f32 _b = (v4f32)__msa_ld_w((const float*)b_data + i * 4, 0); |
| 59 | _p = __msa_fmadd_w(_a, _p, _b); |
| 60 | __msa_st_w((v4i32)_p, ptr0, 0); |
| 61 | } |
| 62 | #endif // __mips_msa |
| 63 | |
| 64 | #pragma omp parallel for num_threads(opt.num_threads) |
| 65 | for (int i = remain_w_start; i < w; i++) |
| 66 | { |
| 67 | ptr[i] = b_data[i] * ptr[i] + a_data[i]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (dims == 2) |
| 72 | { |
| 73 | int w = bottom_top_blob.w * elempack; |
| 74 | int h = bottom_top_blob.h; |
| 75 | |
| 76 | #pragma omp parallel for num_threads(opt.num_threads) |
| 77 | for (int i = 0; i < h; i++) |
| 78 | { |
| 79 | float* ptr = bottom_top_blob.row(i); |
| 80 | float a = a_data[i]; |
| 81 | float b = b_data[i]; |
| 82 | |
| 83 | int j = 0; |
| 84 | #if __mips_msa |
| 85 | v4f32 _a = elempack == 4 ? (v4f32)__msa_ld_w((const float*)a_data + i * 4, 0) : (v4f32)__msa_fill_w_f32(a); |
| 86 | v4f32 _b = elempack == 4 ? (v4f32)__msa_ld_w((const float*)b_data + i * 4, 0) : (v4f32)__msa_fill_w_f32(b); |
| 87 | for (; j + 3 < w; j += 4) |
| 88 | { |
| 89 | __builtin_prefetch(ptr + 16); |
| 90 | v4f32 _p = (v4f32)__msa_ld_w(ptr, 0); |
| 91 | _p = __msa_fmadd_w(_a, _p, _b); |
| 92 | __msa_st_w((v4i32)_p, ptr, 0); |
| 93 | |
| 94 | ptr += 4; |
| 95 | } |
| 96 | #endif // __mips_msa |
| 97 | for (; j < w; j++) |
| 98 | { |
| 99 | *ptr = b * *ptr + a; |
| 100 | ptr++; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (dims == 3 || dims == 4) |
| 106 | { |
| 107 | int w = bottom_top_blob.w; |
| 108 | int h = bottom_top_blob.h; |
| 109 | int d = bottom_top_blob.d; |
| 110 | int c = bottom_top_blob.c; |
| 111 | int size = w * h * d * elempack; |
| 112 | |
| 113 | #pragma omp parallel for num_threads(opt.num_threads) |
| 114 | for (int q = 0; q < c; q++) |
| 115 | { |
| 116 | float* ptr = bottom_top_blob.channel(q); |
| 117 | float a = a_data[q]; |
| 118 | float b = b_data[q]; |
| 119 | |
| 120 | int i = 0; |
| 121 | #if __mips_msa |
| 122 | v4f32 _a = elempack == 4 ? (v4f32)__msa_ld_w((const float*)a_data + q * 4, 0) : (v4f32)__msa_fill_w_f32(a); |
| 123 | v4f32 _b = elempack == 4 ? (v4f32)__msa_ld_w((const float*)b_data + q * 4, 0) : (v4f32)__msa_fill_w_f32(b); |
| 124 | for (; i + 3 < size; i += 4) |
| 125 | { |
| 126 | __builtin_prefetch(ptr + 16); |
| 127 | v4f32 _p = (v4f32)__msa_ld_w(ptr, 0); |
| 128 | _p = __msa_fmadd_w(_a, _p, _b); |
| 129 | __msa_st_w((v4i32)_p, ptr, 0); |
| 130 | |
| 131 | ptr += 4; |
| 132 | } |
| 133 | #endif // __mips_msa |
| 134 | for (; i < size; i++) |
| 135 | { |
| 136 | *ptr = b * *ptr + a; |
| 137 | ptr++; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | } // namespace ncnn |