| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 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 "scale_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #endif // __ARM_NEON |
| 20 | |
| 21 | namespace ncnn { |
| 22 | |
| 23 | Scale_arm::Scale_arm() |
| 24 | { |
| 25 | #if __ARM_NEON |
| 26 | support_packing = true; |
| 27 | #endif // __ARM_NEON |
| 28 | } |
| 29 | |
| 30 | int Scale_arm::forward_inplace(std::vector<Mat>& bottom_top_blobs, const Option& opt) const |
| 31 | { |
| 32 | Mat& bottom_top_blob = bottom_top_blobs[0]; |
| 33 | const Mat& scale_blob = bottom_top_blobs[1]; |
| 34 | |
| 35 | int dims = bottom_top_blob.dims; |
| 36 | int elempack = bottom_top_blob.elempack; |
| 37 | |
| 38 | #if __ARM_NEON |
| 39 | if (elempack == 4) |
| 40 | { |
| 41 | if (dims == 1) |
| 42 | { |
| 43 | int w = bottom_top_blob.w; |
| 44 | |
| 45 | const float* scale = scale_blob; |
| 46 | if (bias_term) |
| 47 | { |
| 48 | const float* bias = bias_data; |
| 49 | #pragma omp parallel for num_threads(opt.num_threads) |
| 50 | for (int i = 0; i < w; i++) |
| 51 | { |
| 52 | float* ptr = (float*)bottom_top_blob + i * 4; |
| 53 | |
| 54 | float32x4_t _p = vld1q_f32(ptr); |
| 55 | float32x4_t _s = vld1q_f32(scale + i * 4); |
| 56 | float32x4_t _bias = vld1q_f32(bias + i * 4); |
| 57 | _p = vmlaq_f32(_bias, _p, _s); |
| 58 | vst1q_f32(ptr, _p); |
| 59 | } |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | #pragma omp parallel for num_threads(opt.num_threads) |
| 64 | for (int i = 0; i < w; i++) |
| 65 | { |
| 66 | float* ptr = (float*)bottom_top_blob + i * 4; |
| 67 | |
| 68 | float32x4_t _p = vld1q_f32(ptr); |
| 69 | float32x4_t _s = vld1q_f32(scale + i * 4); |
| 70 | _p = vmulq_f32(_p, _s); |
| 71 | vst1q_f32(ptr, _p); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (dims == 2) |
| 77 | { |
| 78 | int w = bottom_top_blob.w; |
| 79 | int h = bottom_top_blob.h; |
| 80 | |
| 81 | if (bias_term) |
| 82 | { |
| 83 | #pragma omp parallel for num_threads(opt.num_threads) |
| 84 | for (int i = 0; i < h; i++) |
| 85 | { |
| 86 | float* ptr = bottom_top_blob.row(i); |
| 87 | float32x4_t _s = vld1q_f32((const float*)scale_blob + i * 4); |
| 88 | float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4); |
| 89 | |
| 90 | for (int j = 0; j < w; j++) |
| 91 | { |
| 92 | float32x4_t _p = vld1q_f32(ptr); |
| 93 | _p = vmlaq_f32(_bias, _p, _s); |
| 94 | vst1q_f32(ptr, _p); |
| 95 | |
| 96 | ptr += 4; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | #pragma omp parallel for num_threads(opt.num_threads) |
| 103 | for (int i = 0; i < h; i++) |
| 104 | { |
| 105 | float* ptr = bottom_top_blob.row(i); |
| 106 | float32x4_t _s = vld1q_f32((const float*)scale_blob + i * 4); |
| 107 | |
| 108 | for (int j = 0; j < w; j++) |
| 109 | { |
| 110 | float32x4_t _p = vld1q_f32(ptr); |
| 111 | _p = vmulq_f32(_p, _s); |
| 112 | vst1q_f32(ptr, _p); |
| 113 | |
| 114 | ptr += 4; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (dims == 3) |
| 121 | { |
| 122 | int w = bottom_top_blob.w; |
| 123 | int h = bottom_top_blob.h; |
| 124 | int channels = bottom_top_blob.c; |
| 125 | int size = w * h; |
| 126 | |
| 127 | if (bias_term) |
| 128 | { |
| 129 | #pragma omp parallel for num_threads(opt.num_threads) |
| 130 | for (int q = 0; q < channels; q++) |
| 131 | { |
| 132 | float* ptr = bottom_top_blob.channel(q); |
| 133 | float32x4_t _s = vld1q_f32((const float*)scale_blob + q * 4); |
| 134 | float32x4_t _bias = vld1q_f32((const float*)bias_data + q * 4); |
| 135 | |
| 136 | for (int i = 0; i < size; i++) |
| 137 | { |
| 138 | float32x4_t _p = vld1q_f32(ptr); |
| 139 | _p = vmlaq_f32(_bias, _p, _s); |
| 140 | vst1q_f32(ptr, _p); |
| 141 | |
| 142 | ptr += 4; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | #pragma omp parallel for num_threads(opt.num_threads) |
| 149 | for (int q = 0; q < channels; q++) |
| 150 | { |
| 151 | float* ptr = bottom_top_blob.channel(q); |
| 152 | float32x4_t _s = vld1q_f32((const float*)scale_blob + q * 4); |
| 153 | |
| 154 | for (int i = 0; i < size; i++) |
| 155 | { |
| 156 | float32x4_t _p = vld1q_f32(ptr); |
| 157 | _p = vmulq_f32(_p, _s); |
| 158 | vst1q_f32(ptr, _p); |
| 159 | |
| 160 | ptr += 4; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | #endif // __ARM_NEON |
| 169 | |
| 170 | if (dims != 3) |
| 171 | return Scale::forward_inplace(bottom_top_blobs, opt); |
| 172 | |
| 173 | int w = bottom_top_blob.w; |
| 174 | int h = bottom_top_blob.h; |
| 175 | int channels = bottom_top_blob.c; |
| 176 | int size = w * h; |
| 177 | |
| 178 | if (bias_term) |
| 179 | { |
| 180 | const float* scale_ptr = scale_blob; |
| 181 | const float* bias_ptr = bias_data; |
| 182 | #pragma omp parallel for num_threads(opt.num_threads) |
| 183 | for (int q = 0; q < channels; q++) |
| 184 | { |
| 185 | float* ptr = bottom_top_blob.channel(q); |
| 186 | |
| 187 | float s = scale_ptr[q]; |
| 188 | float bias = bias_ptr[q]; |
| 189 | |
| 190 | #if __ARM_NEON |
| 191 | int nn = size >> 2; |
| 192 | int remain = size - (nn << 2); |
| 193 | #else |
| 194 | int remain = size; |
| 195 | #endif // __ARM_NEON |
| 196 | |
| 197 | #if __ARM_NEON |
| 198 | float32x4_t _s = vdupq_n_f32(s); |
| 199 | float32x4_t _bias = vdupq_n_f32(bias); |
| 200 | for (; nn > 0; nn--) |
| 201 | { |
| 202 | float32x4_t _p = vld1q_f32(ptr); |
| 203 | _p = vmlaq_f32(_bias, _p, _s); |
| 204 | vst1q_f32(ptr, _p); |
| 205 | |
| 206 | ptr += 4; |
| 207 | } |
| 208 | #endif // __ARM_NEON |
| 209 | |
| 210 | for (; remain > 0; remain--) |
| 211 | { |
| 212 | *ptr = *ptr * s + bias; |
| 213 | |
| 214 | ptr++; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | const float* scale_ptr = scale_blob; |
| 221 | #pragma omp parallel for num_threads(opt.num_threads) |
| 222 | for (int q = 0; q < channels; q++) |
| 223 | { |
| 224 | float* ptr = bottom_top_blob.channel(q); |
| 225 | |
| 226 | float s = scale_ptr[q]; |
| 227 | |
| 228 | #if __ARM_NEON |
| 229 | int nn = size >> 2; |
| 230 | int remain = size - (nn << 2); |
| 231 | #else |
| 232 | int remain = size; |
| 233 | #endif // __ARM_NEON |
| 234 | |
| 235 | #if __ARM_NEON |
| 236 | float32x4_t _s = vdupq_n_f32(s); |
| 237 | for (; nn > 0; nn--) |
| 238 | { |
| 239 | float32x4_t _p = vld1q_f32(ptr); |
| 240 | _p = vmulq_f32(_p, _s); |
| 241 | vst1q_f32(ptr, _p); |
| 242 | |
| 243 | ptr += 4; |
| 244 | } |
| 245 | #endif // __ARM_NEON |
| 246 | |
| 247 | for (; remain > 0; remain--) |
| 248 | { |
| 249 | *ptr *= s; |
| 250 | |
| 251 | ptr++; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | } // namespace ncnn |