2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "moco/Import/Nodes/Const.h"
18 #include "TestHelper.h"
20 #include <gtest/gtest.h>
22 using namespace moco::test;
27 // Test case for "input_tensor.float_val_size() == num_elements"
30 const char *const_float_01_pbtxtdata = STRING_CONTENT(
66 TEST(TensorFlowImport, const_float_01)
68 TFNodeBuildTester tester;
69 moco::ConstGraphBuilder graphbuilder;
70 tensorflow::NodeDef nodedef;
72 EXPECT_TRUE(plier::tf::parse_nodedef(const_float_01_pbtxtdata, nodedef));
75 // - there should exist TFConst
76 // - values should match
79 tester.output("const/float");
80 tester.run(nodedef, graphbuilder);
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);
95 // Test case for "input_tensor.float_val_size() == 1"
98 const char *const_float_02_pbtxtdata = STRING_CONTENT(
129 TEST(TensorFlowImport, const_float_02)
131 TFNodeBuildTester tester;
132 moco::ConstGraphBuilder graphbuilder;
133 tensorflow::NodeDef nodedef;
135 EXPECT_TRUE(plier::tf::parse_nodedef(const_float_02_pbtxtdata, nodedef));
138 // - there should exist TFConst
139 // - values should match
142 tester.output("const/float");
143 tester.run(nodedef, graphbuilder);
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);
158 // Test case for "input_tensor.tensor_content().size() == num_elements * sizeof(float)"
159 // Generated with tfkit tool: "cat ./test.pbtxt | ./tfkit pack"
162 const char *const_float_03_pbtxtdata = STRING_CONTENT(
184 tensor_content: "\315\314\214?\315\314\014@33S@\315\314\214@\000\000\260@33\323@"
193 TEST(TensorFlowImport, const_float_03)
195 TFNodeBuildTester tester;
196 moco::ConstGraphBuilder graphbuilder;
197 tensorflow::NodeDef nodedef;
199 EXPECT_TRUE(plier::tf::parse_nodedef(const_float_03_pbtxtdata, nodedef));
202 // - there should exist TFConst
203 // - values should match
206 tester.output("const/float");
207 tester.run(nodedef, graphbuilder);
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);
222 // Test case for "input_tensor.float_val_size() < num_elements"
225 const char *const_float_04_pbtxtdata = STRING_CONTENT(
257 TEST(TensorFlowImport, const_float_04)
259 TFNodeBuildTester tester;
260 moco::ConstGraphBuilder graphbuilder;
261 tensorflow::NodeDef nodedef;
263 EXPECT_TRUE(plier::tf::parse_nodedef(const_float_04_pbtxtdata, nodedef));
266 // - there should exist TFConst
267 // - values should match
270 tester.output("const/float");
271 tester.run(nodedef, graphbuilder);
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);
286 // Test case for "input_tensor.int_val_size() < num_elements"
289 const char *const_int32_04_pbtxtdata = STRING_CONTENT(
321 TEST(TensorFlowImport, const_int32_04)
323 TFNodeBuildTester tester;
324 moco::ConstGraphBuilder graphbuilder;
325 tensorflow::NodeDef nodedef;
327 EXPECT_TRUE(plier::tf::parse_nodedef(const_int32_04_pbtxtdata, nodedef));
330 // - there should exist TFConst
331 // - values should match
334 tester.output("const/int");
335 tester.run(nodedef, graphbuilder);
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);
350 // Test case for "scalar"
353 const char *const_int32_scalar_pbtxtdata = STRING_CONTENT(
378 TEST(TensorFlowImport, const_int32_scalar)
380 TFNodeBuildTester tester;
381 moco::ConstGraphBuilder graphbuilder;
382 tensorflow::NodeDef nodedef;
384 EXPECT_TRUE(plier::tf::parse_nodedef(const_int32_scalar_pbtxtdata, nodedef));
387 // - there should exist TFConst
388 // - there should be one element and value should be 3
391 tester.output("const/int");
392 tester.run(nodedef, graphbuilder);
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);
404 const char *const_int8_01_pbtxtdata = STRING_CONTENT(
440 TEST(TensorFlowImport, const_int8_01)
442 TFNodeBuildTester tester;
443 moco::ConstGraphBuilder graphbuilder;
444 tensorflow::NodeDef nodedef;
446 EXPECT_TRUE(plier::tf::parse_nodedef(const_int8_01_pbtxtdata, nodedef));
449 // - there should exist TFConst
450 // - values should match
453 tester.output("const/int8");
454 tester.run(nodedef, graphbuilder);
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);