Elffuss BraidHub

kikocis/ncnn public

master · 6.0 KiBRawPermalink
1// Copyright (c) 2022 Xiaomi Corp. (author: Fangjun Kuang)
2//
3// Licensed under the BSD 3-Clause License (the "License"); you may not use this
4// file except in compliance with the License. You may obtain a copy of the
5// License at
6//
7// https://opensource.org/licenses/BSD-3-Clause
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#include "glu.h"
16
17namespace ncnn {
18
19GLU::GLU()
20{
21 one_blob_only = true;
22 support_inplace = false;
23}
24
25int GLU::load_param(const ParamDict& pd)
26{
27 axis = pd.get(0, 0);
28
29 return 0;
30}
31
32int GLU::forward(const Mat& bottom_blob, Mat& top_blob,
33 const Option& opt) const
34{
35 int dims = bottom_blob.dims;
36 int positive_axis = axis < 0 ? dims + axis : axis;
37
38 if (dims == 1)
39 { // ignore axis
40 int w = bottom_blob.w;
41 int out_w = w / 2;
42 top_blob.create(out_w, sizeof(float), opt.blob_allocator);
43
44 const float* in_ptr = bottom_blob;
45 float* out_ptr = top_blob;
46
47 #pragma omp parallel for num_threads(opt.num_threads)
48 for (int x = 0; x < out_w; ++x)
49 {
50 float sigmoid = 1.f / (1.f + expf(-in_ptr[x + out_w]));
51
52 out_ptr[x] = in_ptr[x] * sigmoid;
53 }
54
55 return 0;
56 } // if (dims == 1)
57
58 if (dims == 2 && positive_axis == 0)
59 {
60 int w = bottom_blob.w;
61 int h = bottom_blob.h;
62 int out_w = w;
63 int out_h = h / 2;
64 top_blob.create(out_w, out_h, sizeof(float), opt.blob_allocator);
65
66 int offset = out_w * out_h;
67
68#if 0
69 // this one is equivalent to the else branch. It is more readable
70 // but less efficient
71 #pragma omp parallel for num_threads(opt.num_threads)
72 for (int y = 0; y < out_h; ++y) {
73 const float *in_ptr = bottom_blob.row(y);
74 float *out_ptr = top_blob.row(y);
75
76 for (int x = 0; x < w; ++x) {
77 float sigmoid =
78 1.f / (1.f + expf(-in_ptr[x + offset]));
79
80 out_ptr[x] = in_ptr[x] * sigmoid;
81 }
82 }
83#else
84 int size = offset;
85 const float* in_ptr = bottom_blob;
86 float* out_ptr = top_blob;
87
88 #pragma omp parallel for num_threads(opt.num_threads)
89 for (int i = 0; i < size; ++i)
90 {
91 float sigmoid = 1.f / (1.f + expf(-in_ptr[i + offset]));
92 out_ptr[i] = in_ptr[i] * sigmoid;
93 }
94#endif
95
96 return 0;
97 } // if (dims == 2 && positive_axis == 0)
98
99 if (dims == 2 && positive_axis == 1)
100 {
101 int w = bottom_blob.w;
102 int h = bottom_blob.h;
103 int out_w = w / 2;
104 int out_h = h;
105
106 top_blob.create(out_w, out_h, sizeof(float), opt.blob_allocator);
107
108 #pragma omp parallel for num_threads(opt.num_threads)
109 for (int y = 0; y < h; ++y)
110 {
111 const float* in_ptr = bottom_blob.row(y);
112 float* out_ptr = top_blob.row(y);
113
114 for (int x = 0; x < out_w; ++x)
115 {
116 float sigmoid = 1.f / (1.f + expf(-in_ptr[x + out_w]));
117 out_ptr[x] = in_ptr[x] * sigmoid;
118 }
119 }
120
121 return 0;
122 } // if (dims == 2 && positive_axis == 1)
123
124 if (dims == 3 && positive_axis == 0)
125 {
126 int w = bottom_blob.w;
127 int h = bottom_blob.h;
128 int c = bottom_blob.c;
129
130 int out_w = w;
131 int out_h = h;
132 int out_c = c / 2;
133
134 top_blob.create(out_w, out_h, out_c, sizeof(float), opt.blob_allocator);
135
136 int offset = out_c * bottom_blob.cstep;
137 int size = w * h;
138
139 #pragma omp parallel for num_threads(opt.num_threads)
140 for (int q = 0; q < out_c; ++q)
141 {
142 const float* in_ptr = bottom_blob.channel(q);
143 float* out_ptr = top_blob.channel(q);
144
145 for (int i = 0; i < size; ++i)
146 {
147 float sigmoid = 1.f / (1.f + expf(-in_ptr[i + offset]));
148 out_ptr[i] = in_ptr[i] * sigmoid;
149 }
150 }
151 return 0;
152 } // if (dims == 3 && positive_axis == 0) {
153
154 if (dims == 3 && positive_axis == 1)
155 {
156 int w = bottom_blob.w;
157 int h = bottom_blob.h;
158 int c = bottom_blob.c;
159
160 int out_w = w;
161 int out_h = h / 2;
162 int out_c = c;
163
164 top_blob.create(out_w, out_h, out_c, sizeof(float), opt.blob_allocator);
165
166 int offset = out_h * out_w;
167 int size = offset;
168
169 #pragma omp parallel for num_threads(opt.num_threads)
170 for (int q = 0; q < c; ++q)
171 {
172 const float* in_ptr = bottom_blob.channel(q);
173 float* out_ptr = top_blob.channel(q);
174
175 for (int i = 0; i < size; ++i)
176 {
177 float sigmoid = 1.f / (1.f + expf(-in_ptr[i + offset]));
178 out_ptr[i] = in_ptr[i] * sigmoid;
179 }
180 }
181 return 0;
182 } // if (dims == 3 && positive_axis == 1)
183
184 if (dims == 3 && positive_axis == 2)
185 {
186 int w = bottom_blob.w;
187 int h = bottom_blob.h;
188 int c = bottom_blob.c;
189
190 int out_w = w / 2;
191 int out_h = h;
192 int out_c = c;
193
194 top_blob.create(out_w, out_h, out_c, sizeof(float), opt.blob_allocator);
195
196 #pragma omp parallel for num_threads(opt.num_threads)
197 for (int q = 0; q < c; ++q)
198 {
199 const float* in_ptr = bottom_blob.channel(q);
200 float* out_ptr = top_blob.channel(q);
201 for (int y = 0; y < h; ++y)
202 {
203 for (int x = 0; x < out_w; ++x)
204 {
205 float sigmoid = 1.f / (1.f + expf(-in_ptr[x + out_w]));
206 out_ptr[x] = in_ptr[x] * sigmoid;
207 }
208 in_ptr += w;
209 out_ptr += out_w;
210 }
211 }
212 return 0;
213 } // if (dims == 3 && positive_axis == 2)
214
215 return -100;
216}
217
218} // namespace ncnn