| 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 "prelu_x86.h" |
| 16 | |
| 17 | #if __SSE2__ |
| 18 | #include <emmintrin.h> |
| 19 | #if __AVX__ |
| 20 | #include <immintrin.h> |
| 21 | #endif // __AVX__ |
| 22 | #endif // __SSE2__ |
| 23 | #include "x86_activation.h" |
| 24 | |
| 25 | namespace ncnn { |
| 26 | |
| 27 | PReLU_x86::PReLU_x86() |
| 28 | { |
| 29 | #if __SSE2__ |
| 30 | support_packing = true; |
| 31 | #endif // __SSE2__ |
| 32 | } |
| 33 | |
| 34 | int PReLU_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 35 | { |
| 36 | int dims = bottom_top_blob.dims; |
| 37 | int w = bottom_top_blob.w; |
| 38 | int h = bottom_top_blob.h; |
| 39 | int channels = bottom_top_blob.c; |
| 40 | int elempack = bottom_top_blob.elempack; |
| 41 | |
| 42 | if (dims == 1) |
| 43 | { |
| 44 | const int size = w * elempack; |
| 45 | |
| 46 | if (num_slope > 1) |
| 47 | { |
| 48 | float* ptr = bottom_top_blob; |
| 49 | const float* slope = slope_data; |
| 50 | |
| 51 | int nn_size = 0; |
| 52 | int remain_size_start = 0; |
| 53 | #if __SSE2__ |
| 54 | #if __AVX__ |
| 55 | #if __AVX512F__ |
| 56 | nn_size = (size - remain_size_start) / 16; |
| 57 | #pragma omp parallel for num_threads(opt.num_threads) |
| 58 | for (int ii = 0; ii < nn_size; ii++) |
| 59 | { |
| 60 | int i = remain_size_start + ii * 16; |
| 61 | __m512 _p512 = _mm512_loadu_ps(ptr + i); |
| 62 | __m512 _slope512 = _mm512_loadu_ps(slope + i); |
| 63 | _mm512_storeu_ps(ptr + i, prelu_avx512(_p512, _slope512)); |
| 64 | } |
| 65 | remain_size_start += nn_size * 16; |
| 66 | #endif // __AVX512F__ |
| 67 | nn_size = (size - remain_size_start) / 8; |
| 68 | #pragma omp parallel for num_threads(opt.num_threads) |
| 69 | for (int ii = 0; ii < nn_size; ii++) |
| 70 | { |
| 71 | int i = remain_size_start + ii * 8; |
| 72 | __m256 _p256 = _mm256_loadu_ps(ptr + i); |
| 73 | __m256 _slope256 = _mm256_loadu_ps(slope + i); |
| 74 | _mm256_storeu_ps(ptr + i, prelu_avx(_p256, _slope256)); |
| 75 | } |
| 76 | remain_size_start += nn_size * 8; |
| 77 | #endif // __AVX__ |
| 78 | nn_size = (size - remain_size_start) / 4; |
| 79 | #pragma omp parallel for num_threads(opt.num_threads) |
| 80 | for (int ii = 0; ii < nn_size; ii++) |
| 81 | { |
| 82 | int i = remain_size_start + ii * 4; |
| 83 | __m128 _p128 = _mm_load_ps(ptr + i); |
| 84 | __m128 _slope128 = _mm_loadu_ps(slope + i); |
| 85 | _mm_store_ps(ptr + i, prelu_sse(_p128, _slope128)); |
| 86 | } |
| 87 | remain_size_start += nn_size * 4; |
| 88 | #endif // __SSE2__ |
| 89 | #pragma omp parallel for num_threads(opt.num_threads) |
| 90 | for (int i = remain_size_start; i < size; i++) |
| 91 | { |
| 92 | if (ptr[i] < 0) |
| 93 | ptr[i] *= slope_data[i]; |
| 94 | } |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | float* ptr = bottom_top_blob; |
| 99 | const float slope = slope_data[0]; |
| 100 | |
| 101 | int nn_size = 0; |
| 102 | int remain_size_start = 0; |
| 103 | #if __SSE2__ |
| 104 | #if __AVX__ |
| 105 | #if __AVX512F__ |
| 106 | nn_size = (size - remain_size_start) / 16; |
| 107 | #pragma omp parallel for num_threads(opt.num_threads) |
| 108 | for (int ii = 0; ii < nn_size; ii++) |
| 109 | { |
| 110 | int i = remain_size_start + ii * 16; |
| 111 | __m512 _p512 = _mm512_loadu_ps(ptr + i); |
| 112 | __m512 _slope512 = _mm512_set1_ps(slope); |
| 113 | _mm512_storeu_ps(ptr + i, prelu_avx512(_p512, _slope512)); |
| 114 | } |
| 115 | remain_size_start += nn_size * 16; |
| 116 | #endif // __AVX512F__ |
| 117 | nn_size = (size - remain_size_start) / 8; |
| 118 | #pragma omp parallel for num_threads(opt.num_threads) |
| 119 | for (int ii = 0; ii < nn_size; ii++) |
| 120 | { |
| 121 | int i = remain_size_start + ii * 8; |
| 122 | __m256 _p256 = _mm256_loadu_ps(ptr + i); |
| 123 | __m256 _slope256 = _mm256_set1_ps(slope); |
| 124 | _mm256_storeu_ps(ptr + i, prelu_avx(_p256, _slope256)); |
| 125 | } |
| 126 | remain_size_start += nn_size * 8; |
| 127 | #endif // __AVX__ |
| 128 | nn_size = (size - remain_size_start) / 4; |
| 129 | #pragma omp parallel for num_threads(opt.num_threads) |
| 130 | for (int ii = 0; ii < nn_size; ii++) |
| 131 | { |
| 132 | int i = remain_size_start + ii * 4; |
| 133 | __m128 _p128 = _mm_load_ps(ptr + i); |
| 134 | __m128 _slope128 = _mm_set1_ps(slope); |
| 135 | _mm_store_ps(ptr + i, prelu_sse(_p128, _slope128)); |
| 136 | } |
| 137 | remain_size_start += nn_size * 4; |
| 138 | #endif // __SSE2__ |
| 139 | #pragma omp parallel for num_threads(opt.num_threads) |
| 140 | for (int i = remain_size_start; i < size; i++) |
| 141 | { |
| 142 | if (ptr[i] < 0) |
| 143 | ptr[i] *= slope; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (dims == 2) |
| 149 | { |
| 150 | const int size = w * elempack; |
| 151 | |
| 152 | #pragma omp parallel for num_threads(opt.num_threads) |
| 153 | for (int i = 0; i < h; i++) |
| 154 | { |
| 155 | float* ptr = bottom_top_blob.row(i); |
| 156 | int j = 0; |
| 157 | |
| 158 | float slope = num_slope > 1 ? slope_data[i] : slope_data[0]; |
| 159 | #if __SSE2__ |
| 160 | __m128 _slope128 = num_slope > 1 && (elempack == 4) ? _mm_loadu_ps((const float*)slope_data + i * 4) : _mm_set1_ps(slope); |
| 161 | #if __AVX__ |
| 162 | __m256 _slope256 = num_slope > 1 && (elempack == 8) ? _mm256_loadu_ps((const float*)slope_data + i * 8) : _mm256_insertf128_ps(_mm256_castps128_ps256(_slope128), _slope128, 1); |
| 163 | #if __AVX512F__ |
| 164 | __m512 _slope512 = num_slope > 1 && (elempack == 16) ? _mm512_loadu_ps((const float*)slope_data + i * 16) : _mm512_insertf32x8(_mm512_castps256_ps512(_slope256), _slope256, 1); |
| 165 | |
| 166 | for (; j + 15 < size; j += 16) |
| 167 | { |
| 168 | __m512 _p512 = _mm512_loadu_ps(ptr); |
| 169 | _mm512_storeu_ps(ptr, prelu_avx512(_p512, _slope512)); |
| 170 | ptr += 16; |
| 171 | } |
| 172 | #endif // __AVX512F__ |
| 173 | for (; j + 7 < size; j += 8) |
| 174 | { |
| 175 | __m256 _p256 = _mm256_loadu_ps(ptr); |
| 176 | _mm256_storeu_ps(ptr, prelu_avx(_p256, _slope256)); |
| 177 | ptr += 8; |
| 178 | } |
| 179 | #endif // __AVX__ |
| 180 | for (; j + 3 < size; j += 4) |
| 181 | { |
| 182 | __m128 _p128 = _mm_loadu_ps(ptr); |
| 183 | _mm_storeu_ps(ptr, prelu_sse(_p128, _slope128)); |
| 184 | ptr += 4; |
| 185 | } |
| 186 | #endif // __SSE2__ |
| 187 | for (; j < size; j++) |
| 188 | { |
| 189 | if (*ptr < 0) |
| 190 | *ptr *= slope; |
| 191 | ptr++; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (dims == 3) |
| 197 | { |
| 198 | const int size = w * h * elempack; |
| 199 | |
| 200 | #pragma omp parallel for num_threads(opt.num_threads) |
| 201 | for (int q = 0; q < channels; q++) |
| 202 | { |
| 203 | float* ptr = bottom_top_blob.channel(q); |
| 204 | int i = 0; |
| 205 | |
| 206 | float slope = num_slope > 1 ? slope_data[q] : slope_data[0]; |
| 207 | #if __SSE2__ |
| 208 | __m128 _slope128 = num_slope > 1 && (elempack == 4) ? _mm_loadu_ps((const float*)slope_data + q * 4) : _mm_set1_ps(slope); |
| 209 | #if __AVX__ |
| 210 | __m256 _slope256 = num_slope > 1 && (elempack == 8) ? _mm256_loadu_ps((const float*)slope_data + q * 8) : _mm256_insertf128_ps(_mm256_castps128_ps256(_slope128), _slope128, 1); |
| 211 | #if __AVX512F__ |
| 212 | __m512 _slope512 = num_slope > 1 && (elempack == 16) ? _mm512_loadu_ps((const float*)slope_data + q * 16) : _mm512_insertf32x8(_mm512_castps256_ps512(_slope256), _slope256, 1); |
| 213 | |
| 214 | for (; i + 15 < size; i += 16) |
| 215 | { |
| 216 | __m512 _p512 = _mm512_loadu_ps(ptr); |
| 217 | _mm512_storeu_ps(ptr, prelu_avx512(_p512, _slope512)); |
| 218 | ptr += 16; |
| 219 | } |
| 220 | #endif // __AVX512F__ |
| 221 | for (; i + 7 < size; i += 8) |
| 222 | { |
| 223 | __m256 _p256 = _mm256_loadu_ps(ptr); |
| 224 | _mm256_storeu_ps(ptr, prelu_avx(_p256, _slope256)); |
| 225 | ptr += 8; |
| 226 | } |
| 227 | #endif // __AVX__ |
| 228 | for (; i + 3 < size; i += 4) |
| 229 | { |
| 230 | __m128 _p128 = _mm_load_ps(ptr); |
| 231 | _mm_store_ps(ptr, prelu_sse(_p128, _slope128)); |
| 232 | ptr += 4; |
| 233 | } |
| 234 | #endif // __SSE2__ |
| 235 | for (; i < size; i++) |
| 236 | { |
| 237 | if (*ptr < 0) |
| 238 | *ptr *= slope; |
| 239 | ptr++; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | } // namespace ncnn |