Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / moco / import / src / Nodes / Const.test.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "moco/Import/Nodes/Const.h"
18 #include "TestHelper.h"
19
20 #include <gtest/gtest.h>
21
22 using namespace moco::test;
23
24 namespace
25 {
26
27 // Test case for "input_tensor.float_val_size() == num_elements"
28
29 // clang-format off
30 const char *const_float_01_pbtxtdata = STRING_CONTENT(
31   name: "const/float"
32   op: "Const"
33   attr {
34     key: "dtype"
35     value {
36       type: DT_FLOAT
37     }
38   }
39   attr {
40     key: "value"
41     value {
42       tensor {
43         dtype: DT_FLOAT
44         tensor_shape {
45           dim {
46             size: 2
47           }
48           dim {
49             size: 3
50           }
51         }
52         float_val: 1.1
53         float_val: 2.2
54         float_val: 3.3
55         float_val: 4.4
56         float_val: 5.5
57         float_val: 6.6
58       }
59     }
60   }
61 );
62 // clang-format on
63
64 } // namespace
65
66 TEST(TensorFlowImport, const_float_01)
67 {
68   TFNodeBuildTester tester;
69   moco::ConstGraphBuilder graphbuilder;
70   tensorflow::NodeDef nodedef;
71
72   EXPECT_TRUE(plier::tf::parse_nodedef(const_float_01_pbtxtdata, nodedef));
73
74   // what to test:
75   // - there should exist TFConst
76   // - values should match
77
78   tester.inputs({});
79   tester.output("const/float");
80   tester.run(nodedef, graphbuilder);
81
82   auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
83   ASSERT_NE(test_node, nullptr);
84   ASSERT_EQ(test_node->size<loco::DataType::FLOAT32>(), 6);
85   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(0), 1.1f);
86   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(1), 2.2f);
87   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(2), 3.3f);
88   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(3), 4.4f);
89   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(4), 5.5f);
90   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(5), 6.6f);
91 }
92
93 namespace
94 {
95 // Test case for "input_tensor.float_val_size() == 1"
96
97 // clang-format off
98 const char *const_float_02_pbtxtdata = STRING_CONTENT(
99   name: "const/float"
100   op: "Const"
101   attr {
102     key: "dtype"
103     value {
104       type: DT_FLOAT
105     }
106   }
107   attr {
108     key: "value"
109     value {
110       tensor {
111         dtype: DT_FLOAT
112         tensor_shape {
113           dim {
114             size: 2
115           }
116           dim {
117             size: 3
118           }
119         }
120         float_val: 1.1
121       }
122     }
123   }
124 );
125 // clang-format on
126
127 } // namespace
128
129 TEST(TensorFlowImport, const_float_02)
130 {
131   TFNodeBuildTester tester;
132   moco::ConstGraphBuilder graphbuilder;
133   tensorflow::NodeDef nodedef;
134
135   EXPECT_TRUE(plier::tf::parse_nodedef(const_float_02_pbtxtdata, nodedef));
136
137   // what to test:
138   // - there should exist TFConst
139   // - values should match
140
141   tester.inputs({});
142   tester.output("const/float");
143   tester.run(nodedef, graphbuilder);
144
145   auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
146   ASSERT_NE(test_node, nullptr);
147   ASSERT_EQ(test_node->size<loco::DataType::FLOAT32>(), 6);
148   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(0), 1.1f);
149   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(1), 1.1f);
150   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(2), 1.1f);
151   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(3), 1.1f);
152   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(4), 1.1f);
153   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(5), 1.1f);
154 }
155
156 namespace
157 {
158 // Test case for "input_tensor.tensor_content().size() == num_elements * sizeof(float)"
159 // Generated with tfkit tool: "cat ./test.pbtxt | ./tfkit pack"
160
161 // clang-format off
162 const char *const_float_03_pbtxtdata = STRING_CONTENT(
163   name: "const/float"
164   op: "Const"
165   attr {
166     key: "dtype"
167     value {
168       type: DT_FLOAT
169     }
170   }
171   attr {
172     key: "value"
173     value {
174       tensor {
175         dtype: DT_FLOAT
176         tensor_shape {
177           dim {
178             size: 2
179           }
180           dim {
181             size: 3
182           }
183         }
184         tensor_content: "\315\314\214?\315\314\014@33S@\315\314\214@\000\000\260@33\323@"
185       }
186     }
187   }
188 );
189 // clang-format on
190
191 } // namespace
192
193 TEST(TensorFlowImport, const_float_03)
194 {
195   TFNodeBuildTester tester;
196   moco::ConstGraphBuilder graphbuilder;
197   tensorflow::NodeDef nodedef;
198
199   EXPECT_TRUE(plier::tf::parse_nodedef(const_float_03_pbtxtdata, nodedef));
200
201   // what to test:
202   // - there should exist TFConst
203   // - values should match
204
205   tester.inputs({});
206   tester.output("const/float");
207   tester.run(nodedef, graphbuilder);
208
209   auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
210   ASSERT_NE(test_node, nullptr);
211   ASSERT_EQ(test_node->size<loco::DataType::FLOAT32>(), 6);
212   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(0), 1.1f);
213   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(1), 2.2f);
214   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(2), 3.3f);
215   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(3), 4.4f);
216   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(4), 5.5f);
217   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(5), 6.6f);
218 }
219
220 namespace
221 {
222 // Test case for "input_tensor.float_val_size() < num_elements"
223
224 // clang-format off
225 const char *const_float_04_pbtxtdata = STRING_CONTENT(
226   name: "const/float"
227   op: "Const"
228   attr {
229     key: "dtype"
230     value {
231       type: DT_FLOAT
232     }
233   }
234   attr {
235     key: "value"
236     value {
237       tensor {
238         dtype: DT_FLOAT
239         tensor_shape {
240           dim {
241             size: 2
242           }
243           dim {
244             size: 3
245           }
246         }
247         float_val: 1.1
248         float_val: 2.2
249       }
250     }
251   }
252 );
253 // clang-format on
254
255 } // namespace
256
257 TEST(TensorFlowImport, const_float_04)
258 {
259   TFNodeBuildTester tester;
260   moco::ConstGraphBuilder graphbuilder;
261   tensorflow::NodeDef nodedef;
262
263   EXPECT_TRUE(plier::tf::parse_nodedef(const_float_04_pbtxtdata, nodedef));
264
265   // what to test:
266   // - there should exist TFConst
267   // - values should match
268
269   tester.inputs({});
270   tester.output("const/float");
271   tester.run(nodedef, graphbuilder);
272
273   auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
274   ASSERT_NE(test_node, nullptr);
275   ASSERT_EQ(test_node->size<loco::DataType::FLOAT32>(), 6);
276   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(0), 1.1f);
277   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(1), 2.2f);
278   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(2), 2.2f);
279   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(3), 2.2f);
280   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(4), 2.2f);
281   ASSERT_EQ(test_node->at<loco::DataType::FLOAT32>(5), 2.2f);
282 }
283
284 namespace
285 {
286 // Test case for "input_tensor.int_val_size() < num_elements"
287
288 // clang-format off
289 const char *const_int32_04_pbtxtdata = STRING_CONTENT(
290   name: "const/int"
291   op: "Const"
292   attr {
293     key: "dtype"
294     value {
295       type: DT_INT32
296     }
297   }
298   attr {
299     key: "value"
300     value {
301       tensor {
302         dtype: DT_INT32
303         tensor_shape {
304           dim {
305             size: 2
306           }
307           dim {
308             size: 3
309           }
310         }
311         int_val: 1
312         int_val: 2
313       }
314     }
315   }
316 );
317 // clang-format on
318
319 } // namespace
320
321 TEST(TensorFlowImport, const_int32_04)
322 {
323   TFNodeBuildTester tester;
324   moco::ConstGraphBuilder graphbuilder;
325   tensorflow::NodeDef nodedef;
326
327   EXPECT_TRUE(plier::tf::parse_nodedef(const_int32_04_pbtxtdata, nodedef));
328
329   // what to test:
330   // - there should exist TFConst
331   // - values should match
332
333   tester.inputs({});
334   tester.output("const/int");
335   tester.run(nodedef, graphbuilder);
336
337   auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
338   ASSERT_NE(test_node, nullptr);
339   ASSERT_EQ(test_node->size<loco::DataType::S32>(), 6);
340   ASSERT_EQ(test_node->at<loco::DataType::S32>(0), 1);
341   ASSERT_EQ(test_node->at<loco::DataType::S32>(1), 2);
342   ASSERT_EQ(test_node->at<loco::DataType::S32>(2), 2);
343   ASSERT_EQ(test_node->at<loco::DataType::S32>(3), 2);
344   ASSERT_EQ(test_node->at<loco::DataType::S32>(4), 2);
345   ASSERT_EQ(test_node->at<loco::DataType::S32>(5), 2);
346 }
347
348 namespace
349 {
350 // Test case for "scalar"
351
352 // clang-format off
353 const char *const_int32_scalar_pbtxtdata = STRING_CONTENT(
354   name: "const/int"
355   op: "Const"
356   attr {
357     key: "dtype"
358     value {
359       type: DT_INT32
360     }
361   }
362   attr {
363     key: "value"
364     value {
365       tensor {
366         dtype: DT_INT32
367         tensor_shape {
368         }
369         int_val: 3
370       }
371     }
372   }
373 );
374 // clang-format on
375
376 } // namespace
377
378 TEST(TensorFlowImport, const_int32_scalar)
379 {
380   TFNodeBuildTester tester;
381   moco::ConstGraphBuilder graphbuilder;
382   tensorflow::NodeDef nodedef;
383
384   EXPECT_TRUE(plier::tf::parse_nodedef(const_int32_scalar_pbtxtdata, nodedef));
385
386   // what to test:
387   // - there should exist TFConst
388   // - there should be one element and value should be 3
389
390   tester.inputs({});
391   tester.output("const/int");
392   tester.run(nodedef, graphbuilder);
393
394   auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
395   ASSERT_NE(test_node, nullptr);
396   ASSERT_EQ(test_node->size<loco::DataType::S32>(), 1);
397   ASSERT_EQ(test_node->at<loco::DataType::S32>(0), 3);
398 }
399
400 namespace
401 {
402
403 // clang-format off
404 const char *const_int8_01_pbtxtdata = STRING_CONTENT(
405   name: "const/int8"
406   op: "Const"
407   attr {
408     key: "dtype"
409     value {
410       type: DT_INT8
411     }
412   }
413   attr {
414     key: "value"
415     value {
416       tensor {
417         dtype: DT_INT8
418         tensor_shape {
419           dim {
420             size: 2
421           }
422           dim {
423             size: 3
424           }
425         }
426         int_val: 0
427         int_val: -1
428         int_val: 1
429         int_val: 2
430         int_val: 3
431         int_val: 4
432       }
433     }
434   }
435 );
436 // clang-format on
437
438 } // namespace
439
440 TEST(TensorFlowImport, const_int8_01)
441 {
442   TFNodeBuildTester tester;
443   moco::ConstGraphBuilder graphbuilder;
444   tensorflow::NodeDef nodedef;
445
446   EXPECT_TRUE(plier::tf::parse_nodedef(const_int8_01_pbtxtdata, nodedef));
447
448   // what to test:
449   // - there should exist TFConst
450   // - values should match
451
452   tester.inputs({});
453   tester.output("const/int8");
454   tester.run(nodedef, graphbuilder);
455
456   auto test_node = loco::must_cast<moco::TFConst *>(tester.output());
457   ASSERT_NE(test_node, nullptr);
458   ASSERT_EQ(test_node->size<loco::DataType::S8>(), 6);
459   ASSERT_EQ(test_node->at<loco::DataType::S8>(0), 0);
460   ASSERT_EQ(test_node->at<loco::DataType::S8>(1), -1);
461   ASSERT_EQ(test_node->at<loco::DataType::S8>(2), 1);
462   ASSERT_EQ(test_node->at<loco::DataType::S8>(3), 2);
463   ASSERT_EQ(test_node->at<loco::DataType::S8>(4), 3);
464   ASSERT_EQ(test_node->at<loco::DataType::S8>(5), 4);
465 }