Elffuss BraidHub

kikocis/ncnn public

master · 8.4 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 <string.h>
16#include "c_api.h"
17
18static int test_c_api_0()
19{
20 ncnn_mat_t a = ncnn_mat_create_1d(2, NULL);
21 ncnn_mat_t b = ncnn_mat_create_1d(2, NULL);
22 ncnn_mat_t c = 0;
23
24 ncnn_option_t opt = ncnn_option_create();
25
26 // set a and b
27 {
28 ncnn_mat_fill_float(a, 2.f);
29 ncnn_mat_fill_float(b, 3.f);
30 }
31
32 // c = a + b
33 {
34 ncnn_layer_t op = ncnn_layer_create_by_type("BinaryOp");
35
36 // load param
37 {
38 ncnn_paramdict_t pd = ncnn_paramdict_create();
39 ncnn_paramdict_set_int(pd, 0, 0); // op_type = ADD
40
41 op->load_param(op, pd);
42
43 ncnn_paramdict_destroy(pd);
44 }
45
46 // load model
47 {
48 ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(0, 0);
49
50 op->load_model(op, mb);
51
52 ncnn_modelbin_destroy(mb);
53 }
54
55 op->create_pipeline(op, opt);
56
57 const ncnn_mat_t bottom_blobs[2] = {a, b};
58 ncnn_mat_t top_blobs[1] = {0};
59 op->forward_n(op, bottom_blobs, 2, top_blobs, 1, opt);
60 c = top_blobs[0];
61
62 op->destroy_pipeline(op, opt);
63
64 ncnn_layer_destroy(op);
65 }
66
67 // check c == a + b
68 bool success = false;
69 if (c)
70 {
71 int dims = ncnn_mat_get_dims(c);
72 int w = ncnn_mat_get_w(c);
73 const float* c_data = (const float*)ncnn_mat_get_data(c);
74
75 success = dims == 1 && w == 2 && c_data[0] == 5.f && c_data[1] == 5.f;
76 }
77
78 ncnn_option_destroy(opt);
79
80 ncnn_mat_destroy(a);
81 ncnn_mat_destroy(b);
82 ncnn_mat_destroy(c);
83
84 if (!success)
85 {
86 fprintf(stderr, "test_c_api_0 failed\n");
87 }
88
89 return success ? 0 : -1;
90}
91
92static int test_c_api_1()
93{
94 ncnn_mat_t a = ncnn_mat_create_1d(24, NULL);
95
96 // set a
97 {
98 const float data[] = {
99 0, 1, 2, 3, 4, 5, 6, 7,
100 10, 11, 12, 13, 14, 15, 16, 17,
101 20, 21, 22, 23, 24, 25, 26, 27
102 };
103
104 float* a_data = (float*)ncnn_mat_get_data(a);
105 memcpy(a_data, data, 24 * sizeof(float));
106 }
107
108 ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, NULL);
109 ncnn_mat_t c = 0;
110
111 ncnn_option_t opt = ncnn_option_create();
112
113 // c = reorg(b, 2)
114 {
115 ncnn_layer_t op = ncnn_layer_create_by_type("Reorg");
116
117 // load param
118 {
119 ncnn_paramdict_t pd = ncnn_paramdict_create();
120 ncnn_paramdict_set_int(pd, 0, 2); // stride
121
122 op->load_param(op, pd);
123
124 ncnn_paramdict_destroy(pd);
125 }
126
127 // load model
128 {
129 ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(0, 0);
130
131 op->load_model(op, mb);
132
133 ncnn_modelbin_destroy(mb);
134 }
135
136 op->create_pipeline(op, opt);
137
138 op->forward_1(op, b, &c, opt);
139
140 op->destroy_pipeline(op, opt);
141
142 ncnn_layer_destroy(op);
143 }
144
145 // check c
146 bool success = false;
147 if (c)
148 {
149 int dims = ncnn_mat_get_dims(c);
150 int w = ncnn_mat_get_w(c);
151 int h = ncnn_mat_get_h(c);
152 int ch = ncnn_mat_get_c(c);
153
154 success = dims == 3 && w == 2 && h == 1 && ch == 12;
155
156 const float expected[] = {
157 0, 2,
158 1, 3,
159 4, 6,
160 5, 7,
161 10, 12,
162 11, 13,
163 14, 16,
164 15, 17,
165 20, 22,
166 21, 23,
167 24, 26,
168 25, 27
169 };
170 ncnn_mat_t c2 = 0;
171 ncnn_flatten(c, &c2, opt);
172 const float* c2_data = (const float*)ncnn_mat_get_data(c2);
173 if (memcmp(c2_data, expected, 24) != 0)
174 {
175 success = false;
176 }
177 ncnn_mat_destroy(c2);
178 }
179
180 ncnn_option_destroy(opt);
181
182 ncnn_mat_destroy(a);
183 ncnn_mat_destroy(b);
184 ncnn_mat_destroy(c);
185
186 if (!success)
187 {
188 fprintf(stderr, "test_c_api_1 failed\n");
189 }
190
191 return success ? 0 : -1;
192}
193
194static int mylayer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
195{
196 int w = ncnn_mat_get_w(bottom_top_blob);
197 int h = ncnn_mat_get_h(bottom_top_blob);
198 int channels = ncnn_mat_get_c(bottom_top_blob);
199 int size = w * h;
200
201 #pragma omp parallel for num_threads(ncnn_option_get_num_threads(opt))
202 for (int q = 0; q < channels; q++)
203 {
204 float* ptr = (float*)ncnn_mat_get_channel_data(bottom_top_blob, q);
205 for (int i = 0; i < size; i++)
206 {
207 *ptr = *ptr + 100.f;
208 ptr++;
209 }
210 }
211
212 return 0;
213}
214
215static ncnn_layer_t mylayer_creator(void* /*userdata*/)
216{
217 ncnn_layer_t layer = ncnn_layer_create();
218
219 ncnn_layer_set_one_blob_only(layer, 1);
220 ncnn_layer_set_support_inplace(layer, 1);
221
222 layer->forward_inplace_1 = mylayer_forward_inplace_1;
223
224 return layer;
225}
226
227static void mylayer_destroyer(ncnn_layer_t layer, void* /*userdata*/)
228{
229 ncnn_layer_destroy(layer);
230}
231
232static size_t emptydr_read(ncnn_datareader_t /*dr*/, void* buf, size_t size)
233{
234 memset(buf, 0, size);
235 return size;
236}
237
238static int test_c_api_2()
239{
240 // datareader from empty
241 ncnn_datareader_t emptydr = ncnn_datareader_create();
242 {
243 emptydr->read = emptydr_read;
244 }
245
246 ncnn_allocator_t blob_allocator = ncnn_allocator_create_pool_allocator();
247 ncnn_allocator_t workspace_allocator = ncnn_allocator_create_unlocked_pool_allocator();
248
249 ncnn_option_t opt = ncnn_option_create();
250 {
251 ncnn_option_set_num_threads(opt, 1);
252
253 ncnn_option_set_blob_allocator(opt, blob_allocator);
254 ncnn_option_set_workspace_allocator(opt, workspace_allocator);
255 }
256
257 ncnn_net_t net = ncnn_net_create();
258 {
259 ncnn_net_set_option(net, opt);
260
261 ncnn_net_register_custom_layer_by_type(net, "MyLayer", mylayer_creator, mylayer_destroyer, 0);
262
263 const char param_txt[] = "7767517\n2 2\nInput input 0 1 data\nMyLayer mylayer 1 1 data output\n";
264
265 ncnn_net_load_param_memory(net, param_txt);
266 ncnn_net_load_model_datareader(net, emptydr);
267 }
268
269 ncnn_mat_t a = ncnn_mat_create_1d(24, blob_allocator);
270
271 // set a
272 {
273 const float data[] = {
274 0, 1, 2, 3, 4, 5, 6, 7,
275 10, 11, 12, 13, 14, 15, 16, 17,
276 20, 21, 22, 23, 24, 25, 26, 27
277 };
278
279 float* a_data = (float*)ncnn_mat_get_data(a);
280 memcpy(a_data, data, 24 * sizeof(float));
281 }
282
283 ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, blob_allocator);
284 ncnn_mat_t c = 0;
285
286 {
287 ncnn_extractor_t ex = ncnn_extractor_create(net);
288
289 ncnn_extractor_input(ex, "data", b);
290
291 ncnn_extractor_extract(ex, "output", &c);
292
293 ncnn_extractor_destroy(ex);
294 }
295
296 ncnn_net_destroy(net);
297
298 // check c
299 bool success = false;
300 if (c)
301 {
302 int dims = ncnn_mat_get_dims(c);
303 int w = ncnn_mat_get_w(c);
304 int h = ncnn_mat_get_h(c);
305 int ch = ncnn_mat_get_c(c);
306
307 success = dims == 3 && w == 4 && h == 2 && ch == 3;
308
309 const float expected[] = {
310 100, 101, 102, 103, 104, 105, 106, 107,
311 110, 111, 112, 113, 114, 115, 116, 117,
312 120, 121, 122, 123, 124, 125, 126, 127
313 };
314 ncnn_mat_t c2 = 0;
315 ncnn_flatten(c, &c2, opt);
316 const float* c2_data = (const float*)ncnn_mat_get_data(c2);
317 if (memcmp(c2_data, expected, 24) != 0)
318 {
319 success = false;
320 }
321 ncnn_mat_destroy(c2);
322 }
323
324 ncnn_mat_destroy(a);
325 ncnn_mat_destroy(b);
326 ncnn_mat_destroy(c);
327
328 ncnn_option_destroy(opt);
329
330 ncnn_allocator_destroy(blob_allocator);
331 ncnn_allocator_destroy(workspace_allocator);
332
333 ncnn_datareader_destroy(emptydr);
334
335 if (!success)
336 {
337 fprintf(stderr, "test_c_api_2 failed\n");
338 }
339
340 return success ? 0 : -1;
341}
342
343int main()
344{
345 return test_c_api_0() || test_c_api_1() || test_c_api_2();
346}