| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 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_x86.h" |
| 16 | |
| 17 | #if __SSE2__ |
| 18 | #include <emmintrin.h> |
| 19 | #include "sse_mathfun.h" |
| 20 | #if __AVX__ |
| 21 | #include <immintrin.h> |
| 22 | #include "avx_mathfun.h" |
| 23 | #if __AVX512F__ |
| 24 | #include "avx512_mathfun.h" |
| 25 | #endif // __AVX512F__ |
| 26 | #endif // __AVX__ |
| 27 | #endif // __SSE2__ |
| 28 | |
| 29 | namespace ncnn { |
| 30 | |
| 31 | SELU_x86::SELU_x86() |
| 32 | { |
| 33 | #if __SSE2__ |
| 34 | support_packing = true; |
| 35 | #endif // __SSE2__ |
| 36 | } |
| 37 | |
| 38 | int SELU_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 39 | { |
| 40 | int w = bottom_top_blob.w; |
| 41 | int h = bottom_top_blob.h; |
| 42 | int d = bottom_top_blob.d; |
| 43 | int elempack = bottom_top_blob.elempack; |
| 44 | int channels = bottom_top_blob.c; |
| 45 | int size = w * h * d * elempack; |
| 46 | |
| 47 | #pragma omp parallel for num_threads(opt.num_threads) |
| 48 | for (int q = 0; q < channels; q++) |
| 49 | { |
| 50 | float* ptr = bottom_top_blob.channel(q); |
| 51 | |
| 52 | int i = 0; |
| 53 | |
| 54 | #if __SSE2__ |
| 55 | #if __AVX__ |
| 56 | #if __AVX512F__ |
| 57 | __m512 _zero512 = _mm512_setzero_ps(); |
| 58 | __m512 _one512 = _mm512_set1_ps(1.f); |
| 59 | __m512 _alpha512 = _mm512_set1_ps(alpha); |
| 60 | __m512 _lambda512 = _mm512_set1_ps(lambda); |
| 61 | for (; i + 15 < size; i += 16) |
| 62 | { |
| 63 | __m512 _p = _mm512_loadu_ps(ptr); |
| 64 | |
| 65 | __m512 _pos = _mm512_max_ps(_zero512, _p); |
| 66 | __m512 _neg = _mm512_min_ps(_zero512, _p); |
| 67 | |
| 68 | __m512 _blob = exp512_ps(_neg); |
| 69 | _blob = _mm512_sub_ps(_blob, _one512); |
| 70 | _blob = _mm512_mul_ps(_alpha512, _blob); |
| 71 | _blob = _mm512_mul_ps(_lambda512, _mm512_add_ps(_pos, _blob)); |
| 72 | |
| 73 | _mm512_storeu_ps(ptr, _blob); |
| 74 | |
| 75 | ptr += 16; |
| 76 | } |
| 77 | #endif // __AVX512F__ |
| 78 | __m256 _zero256 = _mm256_setzero_ps(); |
| 79 | __m256 _one256 = _mm256_set1_ps(1.f); |
| 80 | __m256 _alpha256 = _mm256_set1_ps(alpha); |
| 81 | __m256 _lambda256 = _mm256_set1_ps(lambda); |
| 82 | for (; i + 7 < size; i += 8) |
| 83 | { |
| 84 | __m256 _p = _mm256_loadu_ps(ptr); |
| 85 | |
| 86 | __m256 _pos = _mm256_max_ps(_zero256, _p); |
| 87 | __m256 _neg = _mm256_min_ps(_zero256, _p); |
| 88 | |
| 89 | __m256 _blob = exp256_ps(_neg); |
| 90 | _blob = _mm256_sub_ps(_blob, _one256); |
| 91 | _blob = _mm256_mul_ps(_alpha256, _blob); |
| 92 | _blob = _mm256_mul_ps(_lambda256, _mm256_add_ps(_pos, _blob)); |
| 93 | |
| 94 | _mm256_storeu_ps(ptr, _blob); |
| 95 | |
| 96 | ptr += 8; |
| 97 | } |
| 98 | #endif // __AVX__ |
| 99 | __m128 _zero128 = _mm_setzero_ps(); |
| 100 | __m128 _one128 = _mm_set1_ps(1.f); |
| 101 | __m128 _alpha128 = _mm_set1_ps(alpha); |
| 102 | __m128 _lambda128 = _mm_set1_ps(lambda); |
| 103 | for (; i + 3 < size; i += 4) |
| 104 | { |
| 105 | __m128 _p = _mm_loadu_ps(ptr); |
| 106 | |
| 107 | __m128 _pos = _mm_max_ps(_zero128, _p); |
| 108 | __m128 _neg = _mm_min_ps(_zero128, _p); |
| 109 | |
| 110 | __m128 _blob = exp_ps(_neg); |
| 111 | _blob = _mm_sub_ps(_blob, _one128); |
| 112 | _blob = _mm_mul_ps(_alpha128, _blob); |
| 113 | _blob = _mm_mul_ps(_lambda128, _mm_add_ps(_pos, _blob)); |
| 114 | |
| 115 | _mm_storeu_ps(ptr, _blob); |
| 116 | |
| 117 | ptr += 4; |
| 118 | } |
| 119 | #endif // __SSE2__ |
| 120 | float alphaxlambda = alpha * lambda; |
| 121 | for (; i < size; i++) |
| 122 | { |
| 123 | // y = lambda * ( max(0, x) + min(0, alpha * (exp(x) - 1)) ) |
| 124 | if (*ptr < 0) |
| 125 | *ptr = (expf(*ptr) - 1.f) * alphaxlambda; |
| 126 | else |
| 127 | *ptr = *ptr * lambda; |
| 128 | ptr++; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | } // namespace ncnn |