Elffuss BraidHub

kikocis/ncnn public

master · 4.3 KiBRawPermalink
1// yala is pleased to support the open source community by making ncnn available.
2//
3//
4// Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved.
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 "batchnorm_loongarch.h"
16
17#if __loongarch_sx
18#include <lsxintrin.h>
19#endif // __loongarch_sx
20
21#include "loongarch_usability.h"
22
23namespace ncnn {
24
25BatchNorm_loongarch::BatchNorm_loongarch()
26{
27#if __loongarch_sx
28 support_packing = true;
29#endif // __loongarch_sx
30}
31
32int BatchNorm_loongarch::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
33{
34 int dims = bottom_top_blob.dims;
35 int elempack = bottom_top_blob.elempack;
36
37 if (dims == 1)
38 {
39 int w = bottom_top_blob.w * elempack;
40
41#if __loongarch_sx
42 int nn_w = w / 4;
43 int remain_w_start = nn_w * 4;
44#else
45 int remain_w_start = 0;
46#endif // __loongarch_sx
47
48 float* ptr = bottom_top_blob;
49
50#if __loongarch_sx
51 #pragma omp parallel for num_threads(opt.num_threads)
52 for (int i = 0; i < nn_w; i++)
53 {
54 float* ptr0 = ptr + i * 4;
55
56 __m128 _p = (__m128)__lsx_vld(ptr0, 0);
57 __m128 _a = (__m128)__lsx_vld((const float*)a_data + i * 4, 0);
58 __m128 _b = (__m128)__lsx_vld((const float*)b_data + i * 4, 0);
59 _p = __lsx_vfmadd_s(_b, _p, _a);
60 __lsx_vst(_p, ptr0, 0);
61 }
62#endif // __loongarch_sx
63
64 #pragma omp parallel for num_threads(opt.num_threads)
65 for (int i = remain_w_start; i < w; i++)
66 {
67 ptr[i] = b_data[i] * ptr[i] + a_data[i];
68 }
69 }
70
71 if (dims == 2)
72 {
73 int w = bottom_top_blob.w * elempack;
74 int h = bottom_top_blob.h;
75
76 #pragma omp parallel for num_threads(opt.num_threads)
77 for (int i = 0; i < h; i++)
78 {
79 float* ptr = bottom_top_blob.row(i);
80 float a = a_data[i];
81 float b = b_data[i];
82
83 int j = 0;
84#if __loongarch_sx
85 __m128 _a = elempack == 4 ? (__m128)__lsx_vld((const float*)a_data + i * 4, 0) : (__m128)__lsx_vreplfr2vr_s(a);
86 __m128 _b = elempack == 4 ? (__m128)__lsx_vld((const float*)b_data + i * 4, 0) : (__m128)__lsx_vreplfr2vr_s(b);
87 for (; j + 3 < w; j += 4)
88 {
89 __builtin_prefetch(ptr + 16);
90 __m128 _p = (__m128)__lsx_vld(ptr, 0);
91 _p = __lsx_vfmadd_s(_b, _p, _a);
92 __lsx_vst(_p, ptr, 0);
93
94 ptr += 4;
95 }
96#endif // __loongarch_sx
97 for (; j < w; j++)
98 {
99 *ptr = b * *ptr + a;
100 ptr++;
101 }
102 }
103 }
104
105 if (dims == 3 || dims == 4)
106 {
107 int w = bottom_top_blob.w;
108 int h = bottom_top_blob.h;
109 int d = bottom_top_blob.d;
110 int c = bottom_top_blob.c;
111 int size = w * h * d * elempack;
112
113 #pragma omp parallel for num_threads(opt.num_threads)
114 for (int q = 0; q < c; q++)
115 {
116 float* ptr = bottom_top_blob.channel(q);
117 float a = a_data[q];
118 float b = b_data[q];
119
120 int i = 0;
121#if __loongarch_sx
122 __m128 _a = elempack == 4 ? (__m128)__lsx_vld((const float*)a_data + q * 4, 0) : (__m128)__lsx_vreplfr2vr_s(a);
123 __m128 _b = elempack == 4 ? (__m128)__lsx_vld((const float*)b_data + q * 4, 0) : (__m128)__lsx_vreplfr2vr_s(b);
124 for (; i + 3 < size; i += 4)
125 {
126 __builtin_prefetch(ptr + 16);
127 __m128 _p = (__m128)__lsx_vld(ptr, 0);
128 _p = __lsx_vfmadd_s(_b, _p, _a);
129 __lsx_vst(_p, ptr, 0);
130
131 ptr += 4;
132 }
133#endif // __loongarch_sx
134 for (; i < size; i++)
135 {
136 *ptr = b * *ptr + a;
137 ptr++;
138 }
139 }
140 }
141
142 return 0;
143}
144
145} // namespace ncnn