Elffuss BraidHub

kikocis/ncnn public

e91df65e9177 · 8.6 KiBRawPermalink
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 "gemm.h"
16
17namespace ncnn {
18
19Gemm::Gemm()
20{
21 one_blob_only = false;
22 support_inplace = false;
23}
24
25int Gemm::load_param(const ParamDict& pd)
26{
27 alpha = pd.get(0, 1.f);
28 beta = pd.get(1, 1.f);
29 transA = pd.get(2, 0);
30 transB = pd.get(3, 0);
31 constantA = pd.get(4, 0);
32 constantB = pd.get(5, 0);
33 constantC = pd.get(6, 0);
34 constantM = pd.get(7, 0);
35 constantN = pd.get(8, 0);
36 constantK = pd.get(9, 0);
37 constant_broadcast_type_C = pd.get(10, 0);
38 output_N1M = pd.get(11, 0);
39 output_elempack = pd.get(12, 0);
40 output_elemtype = pd.get(13, 0);
41 output_transpose = pd.get(14, 0);
42 constant_TILE_M = pd.get(20, 0);
43 constant_TILE_N = pd.get(21, 0);
44 constant_TILE_K = pd.get(22, 0);
45
46 if (constantA == 1 && (constantM == 0 || constantK == 0))
47 {
48 NCNN_LOGE("constantM and constantK must be non-zero when constantA enabled");
49 return -1;
50 }
51
52 if (constantB == 1 && (constantN == 0 || constantK == 0))
53 {
54 NCNN_LOGE("constantN and constantK must be non-zero when constantB enabled");
55 return -1;
56 }
57
58 if (constantC == 1 && (constant_broadcast_type_C < -1 || constant_broadcast_type_C > 4))
59 {
60 NCNN_LOGE("constant_broadcast_type_C must be -1 or 0~4 when constantC enabled");
61 return -1;
62 }
63
64 if (constantA == 0 && constantB == 1 && constantC == 1)
65 one_blob_only = true;
66
67 if (constantA == 1 && constantB == 0 && constantC == 1)
68 one_blob_only = true;
69
70 if (constantA == 1 && constantB == 1 && constantC == 0)
71 one_blob_only = true;
72
73 return 0;
74}
75
76int Gemm::load_model(const ModelBin& mb)
77{
78 if (constantA == 1)
79 {
80 if (transA == 0)
81 A_data = mb.load(constantK, constantM, 0);
82 else
83 A_data = mb.load(constantM, constantK, 0);
84 if (A_data.empty())
85 return -100;
86 }
87
88 if (constantB == 1)
89 {
90 if (transB == 0)
91 B_data = mb.load(constantN, constantK, 0);
92 else
93 B_data = mb.load(constantK, constantN, 0);
94 if (B_data.empty())
95 return -100;
96 }
97
98 if (constantC == 1 && constant_broadcast_type_C != -1)
99 {
100 if (constant_broadcast_type_C == 0)
101 C_data = mb.load(1, 0);
102 if (constant_broadcast_type_C == 1)
103 C_data = mb.load(constantM, 0);
104 if (constant_broadcast_type_C == 2)
105 C_data = mb.load(1, constantM, 0);
106 if (constant_broadcast_type_C == 3)
107 C_data = mb.load(constantN, constantM, 0);
108 if (constant_broadcast_type_C == 4)
109 C_data = mb.load(constantN, 1, 0);
110 if (C_data.empty())
111 return -100;
112 }
113
114 return 0;
115}
116
117int Gemm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
118{
119 std::vector<Mat> bottom_blobs(1, bottom_blob);
120 std::vector<Mat> top_blobs(1, top_blob);
121 int ret = forward(bottom_blobs, top_blobs, opt);
122 top_blob = top_blobs[0];
123 return ret;
124}
125
126int Gemm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
127{
128 const Mat& A0 = constantA ? A_data : bottom_blobs[0];
129 const Mat& B0 = constantB ? B_data : constantA ? bottom_blobs[0] : bottom_blobs[1];
130
131 size_t elemsize = A0.elemsize;
132
133 Mat A;
134 if (transA == 0)
135 {
136 A = A0;
137 }
138 else
139 {
140 // transpose A to row-major
141 A.create((A0.dims == 3 ? A0.c : A0.h), A0.w, elemsize, opt.workspace_allocator);
142
143 const int A0_hstep = A0.dims == 3 ? (int)A0.cstep : A0.w;
144
145 for (int i = 0; i < A.h; i++)
146 {
147 float* ptr = A.row(i);
148 for (int j = 0; j < A.w; j++)
149 {
150 ptr[j] = A0[j * A0_hstep + i];
151 }
152 }
153 }
154
155 Mat B;
156 if (transB == 0)
157 {
158 // transpose B to col-major
159 B.create((B0.dims == 3 ? B0.c : B0.h), B0.w, elemsize, opt.workspace_allocator);
160
161 const int B0_hstep = B0.dims == 3 ? (int)B0.cstep : B0.w;
162
163 for (int i = 0; i < B.h; i++)
164 {
165 float* ptr = B.row(i);
166 for (int j = 0; j < B.w; j++)
167 {
168 ptr[j] = B0[j * B0_hstep + i];
169 }
170 }
171 }
172 else
173 {
174 B = B0;
175 }
176
177 const int M = A.dims == 3 ? A.c : A.h;
178 const int K = A.w; // assert A.w == B.w
179 const int N = B.dims == 3 ? B.c : B.h;
180
181 const float* ptrC = 0;
182 int broadcast_type_C = 0;
183 if (constantC)
184 {
185 ptrC = C_data;
186 broadcast_type_C = constant_broadcast_type_C;
187 }
188 else
189 {
190 if (constantA && constantB)
191 {
192 ptrC = bottom_blobs.size() == 1 ? bottom_blobs[0] : 0;
193 }
194 else if (constantA)
195 {
196 ptrC = bottom_blobs.size() == 2 ? bottom_blobs[1] : 0;
197 }
198 else if (constantB)
199 {
200 ptrC = bottom_blobs.size() == 2 ? bottom_blobs[1] : 0;
201 }
202 else
203 {
204 ptrC = bottom_blobs.size() == 3 ? bottom_blobs[2] : 0;
205 }
206
207 if (ptrC)
208 {
209 const Mat& C = bottom_blobs[bottom_blobs.size() - 1];
210
211 if (C.dims == 1 && C.w == 1)
212 {
213 // scalar
214 broadcast_type_C = 0;
215 }
216 if (C.dims == 1 && C.w == M)
217 {
218 // M
219 // auto broadcast from h to w is the ncnn-style convention
220 broadcast_type_C = 1;
221 }
222 if (C.dims == 1 && C.w == N)
223 {
224 // N
225 broadcast_type_C = 4;
226 }
227 if (C.dims == 2 && C.w == 1 && C.h == M)
228 {
229 // Mx1
230 broadcast_type_C = 2;
231 }
232 if (C.dims == 2 && C.w == N && C.h == M)
233 {
234 // MxN
235 broadcast_type_C = 3;
236 }
237 if (C.dims == 2 && C.w == N && C.h == 1)
238 {
239 // 1xN
240 broadcast_type_C = 4;
241 }
242 }
243 }
244
245 Mat& top_blob = top_blobs[0];
246 if (output_transpose)
247 {
248 if (output_N1M)
249 top_blob.create(M, 1, N, elemsize, opt.blob_allocator);
250 else
251 top_blob.create(M, N, elemsize, opt.blob_allocator);
252 }
253 else
254 {
255 if (output_N1M)
256 top_blob.create(N, 1, M, elemsize, opt.blob_allocator);
257 else
258 top_blob.create(N, M, elemsize, opt.blob_allocator);
259 }
260 if (top_blob.empty())
261 return -100;
262
263 #pragma omp parallel for num_threads(opt.num_threads)
264 for (int i = 0; i < M; i++)
265 {
266 const int out_hstep = top_blob.dims == 3 ? (int)top_blob.cstep : top_blob.w;
267
268 const int A_hstep = A.dims == 3 ? (int)A.cstep : A.w;
269 const int B_hstep = B.dims == 3 ? (int)B.cstep : B.w;
270
271 const float* ptrA = (const float*)A + i * A_hstep;
272
273 for (int j = 0; j < N; j++)
274 {
275 const float* ptrB = (const float*)B + j * B_hstep;
276
277 float sum = 0.f;
278 if (ptrC)
279 {
280 if (broadcast_type_C == 0)
281 {
282 sum = ptrC[0];
283 }
284 if (broadcast_type_C == 1)
285 {
286 sum = ptrC[i];
287 }
288 if (broadcast_type_C == 2)
289 {
290 sum = ptrC[i];
291 }
292 if (broadcast_type_C == 3)
293 {
294 sum = ptrC[i * N + j];
295 }
296 if (broadcast_type_C == 4)
297 {
298 sum = ptrC[j];
299 }
300
301 sum *= beta;
302 }
303
304 for (int k = 0; k < K; k++)
305 {
306 sum += ptrA[k] * ptrB[k];
307 }
308
309 sum *= alpha;
310
311 if (output_transpose)
312 {
313 top_blob[j * out_hstep + i] = sum;
314 }
315 else
316 {
317 top_blob[i * out_hstep + j] = sum;
318 }
319 }
320 }
321
322 return 0;
323}
324
325} // namespace ncnn