| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 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 "mish_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #include "arm_usability.h" |
| 20 | #include "neon_mathfun.h" |
| 21 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 22 | #include "neon_mathfun_fp16s.h" |
| 23 | #endif |
| 24 | #endif // __ARM_NEON |
| 25 | |
| 26 | namespace ncnn { |
| 27 | |
| 28 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 29 | int Mish_arm::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const |
| 30 | { |
| 31 | int w = bottom_top_blob.w; |
| 32 | int h = bottom_top_blob.h; |
| 33 | int d = bottom_top_blob.d; |
| 34 | int channels = bottom_top_blob.c; |
| 35 | int size = w * h * d; |
| 36 | int elempack = bottom_top_blob.elempack; |
| 37 | |
| 38 | if (elempack == 4) |
| 39 | { |
| 40 | #pragma omp parallel for num_threads(opt.num_threads) |
| 41 | for (int q = 0; q < channels; q++) |
| 42 | { |
| 43 | __fp16* ptr = bottom_top_blob.channel(q); |
| 44 | |
| 45 | for (int i = 0; i < size; i++) |
| 46 | { |
| 47 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 48 | _p = vmulq_f32(_p, tanh_ps(log_ps(vaddq_f32(exp_ps(_p), vdupq_n_f32(1.f))))); |
| 49 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 50 | |
| 51 | ptr += 4; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | #pragma omp parallel for num_threads(opt.num_threads) |
| 59 | for (int q = 0; q < channels; q++) |
| 60 | { |
| 61 | __fp16* ptr = bottom_top_blob.channel(q); |
| 62 | |
| 63 | int i = 0; |
| 64 | for (; i + 3 < size; i += 4) |
| 65 | { |
| 66 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 67 | _p = vmulq_f32(_p, tanh_ps(log_ps(vaddq_f32(exp_ps(_p), vdupq_n_f32(1.f))))); |
| 68 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 69 | |
| 70 | ptr += 4; |
| 71 | } |
| 72 | for (; i < size; i++) |
| 73 | { |
| 74 | float v = (float)*ptr; |
| 75 | v = v * tanhf(logf(expf(v) + 1.f)); |
| 76 | *ptr = (__fp16)v; |
| 77 | ptr++; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | int Mish_arm::forward_inplace_fp16sa(Mat& bottom_top_blob, const Option& opt) const |
| 85 | { |
| 86 | int w = bottom_top_blob.w; |
| 87 | int h = bottom_top_blob.h; |
| 88 | int d = bottom_top_blob.d; |
| 89 | int channels = bottom_top_blob.c; |
| 90 | int size = w * h * d; |
| 91 | int elempack = bottom_top_blob.elempack; |
| 92 | |
| 93 | if (elempack == 8) |
| 94 | { |
| 95 | #pragma omp parallel for num_threads(opt.num_threads) |
| 96 | for (int q = 0; q < channels; q++) |
| 97 | { |
| 98 | __fp16* ptr = bottom_top_blob.channel(q); |
| 99 | |
| 100 | for (int i = 0; i < size; i++) |
| 101 | { |
| 102 | float16x8_t _p = vld1q_f16(ptr); |
| 103 | _p = vmulq_f16(_p, tanh_ps_f16(log_ps_f16(vaddq_f16(exp_ps_f16(_p), vdupq_n_f16(1.f))))); |
| 104 | vst1q_f16(ptr, _p); |
| 105 | |
| 106 | ptr += 8; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | if (elempack == 4) |
| 114 | { |
| 115 | #pragma omp parallel for num_threads(opt.num_threads) |
| 116 | for (int q = 0; q < channels; q++) |
| 117 | { |
| 118 | __fp16* ptr = bottom_top_blob.channel(q); |
| 119 | |
| 120 | for (int i = 0; i < size; i++) |
| 121 | { |
| 122 | float16x4_t _p = vld1_f16(ptr); |
| 123 | _p = vmul_f16(_p, tanh_ps_f16(log_ps_f16(vadd_f16(exp_ps_f16(_p), vdup_n_f16(1.f))))); |
| 124 | vst1_f16(ptr, _p); |
| 125 | |
| 126 | ptr += 4; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | #pragma omp parallel for num_threads(opt.num_threads) |
| 134 | for (int q = 0; q < channels; q++) |
| 135 | { |
| 136 | __fp16* ptr = bottom_top_blob.channel(q); |
| 137 | |
| 138 | int i = 0; |
| 139 | for (; i + 3 < size; i += 4) |
| 140 | { |
| 141 | float16x4_t _p = vld1_f16(ptr); |
| 142 | _p = vmul_f16(_p, tanh_ps_f16(log_ps_f16(vadd_f16(exp_ps_f16(_p), vdup_n_f16(1.f))))); |
| 143 | vst1_f16(ptr, _p); |
| 144 | |
| 145 | ptr += 4; |
| 146 | } |
| 147 | for (; i < size; i++) |
| 148 | { |
| 149 | __fp16 v = *ptr; |
| 150 | v = v * (__fp16)tanhf(logf(expf(v) + 1.f)); |
| 151 | *ptr = v; |
| 152 | ptr++; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 159 | |
| 160 | } // namespace ncnn |