| 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 "selu_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include "neon_mathfun.h" |
| 19 | |
| 20 | #include <arm_neon.h> |
| 21 | #endif // __ARM_NEON |
| 22 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | int SELU_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 26 | { |
| 27 | int w = bottom_top_blob.w; |
| 28 | int h = bottom_top_blob.h; |
| 29 | int d = bottom_top_blob.d; |
| 30 | int channels = bottom_top_blob.c; |
| 31 | int size = w * h * d; |
| 32 | float alphaxlambda = alpha * lambda; |
| 33 | |
| 34 | #pragma omp parallel for num_threads(opt.num_threads) |
| 35 | for (int q = 0; q < channels; q++) |
| 36 | { |
| 37 | float* ptr = bottom_top_blob.channel(q); |
| 38 | #if __ARM_NEON |
| 39 | int nn = size >> 2; |
| 40 | int remain = size - (nn << 2); |
| 41 | #else |
| 42 | int remain = size; |
| 43 | #endif // __ARM_NEON |
| 44 | |
| 45 | #if __ARM_NEON |
| 46 | float32x4_t _one = vdupq_n_f32(1.f); |
| 47 | float32x4_t _zero = vdupq_n_f32(0.f); |
| 48 | float32x4_t _alphaxlambda = vdupq_n_f32(alphaxlambda); |
| 49 | float32x4_t _lambda = vdupq_n_f32(lambda); |
| 50 | for (; nn > 0; nn--) |
| 51 | { |
| 52 | float32x4_t _p = vld1q_f32(ptr); |
| 53 | uint32x4_t _lemask = vcleq_f32(_p, _zero); |
| 54 | |
| 55 | float32x4_t _nps = exp_ps(_p); |
| 56 | _nps = vsubq_f32(_nps, _one); |
| 57 | _nps = vmulq_f32(_nps, _alphaxlambda); |
| 58 | |
| 59 | _p = vmulq_f32(_p, _lambda); |
| 60 | |
| 61 | _p = vbslq_f32(_lemask, _nps, _p); |
| 62 | vst1q_f32(ptr, _p); |
| 63 | |
| 64 | ptr += 4; |
| 65 | } |
| 66 | #endif // __ARM_NEON |
| 67 | for (; remain > 0; remain--) |
| 68 | { |
| 69 | if (*ptr < 0.f) |
| 70 | *ptr = (expf(*ptr) - 1.f) * alphaxlambda; |
| 71 | else |
| 72 | *ptr *= lambda; |
| 73 | |
| 74 | ptr++; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | } // namespace ncnn |