Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / Fill.cc
1 /*
2  * Copyright (c) 2020 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 "GenModelTest.h"
18
19 struct FillVariationParam
20 {
21   TestCaseData tcd;
22   const uint8_t *value_data = nullptr;
23   circle::TensorType data_type = circle::TensorType::TensorType_FLOAT32;
24 };
25
26 class FillVariation : public GenModelTest, public ::testing::WithParamInterface<FillVariationParam>
27 {
28 };
29
30 // value is constant
31 TEST_P(FillVariation, Test)
32 {
33   auto &param = GetParam();
34
35   CircleGen cgen;
36
37   size_t value_size =
38     (param.data_type == circle::TensorType::TensorType_INT64) ? sizeof(int64_t) : sizeof(int32_t);
39   uint32_t value_buf = cgen.addBuffer(param.value_data, value_size);
40
41   int dims = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32});
42   int value = cgen.addTensor({{1}, param.data_type, value_buf});
43   int out = cgen.addTensor({{2, 3}, param.data_type});
44   cgen.addOperatorFill({{dims, value}, {out}});
45   cgen.setInputsAndOutputs({dims}, {out});
46
47   _context = std::make_unique<GenModelTestContext>(cgen.finish());
48   _context->addTestCase(param.tcd);
49   _context->setBackends({"cpu"});
50
51   SUCCEED();
52 }
53
54 const int32_t test_int32 = 13;
55 const int64_t test_int64 = 1052;
56 const float test_float = 5.2;
57
58 // Test with different value type
59 INSTANTIATE_TEST_CASE_P(
60   GenModelTest, FillVariation,
61   ::testing::Values(
62     // float value
63     FillVariationParam{
64       TestCaseData{}.addInput<int32_t>({2, 3}).addOutput<float>({5.2, 5.2, 5.2, 5.2, 5.2, 5.2}),
65       reinterpret_cast<const uint8_t *>(&test_float)},
66     // int32 value
67     FillVariationParam{
68       TestCaseData{}.addInput<int32_t>({2, 3}).addOutput<int32_t>({13, 13, 13, 13, 13, 13}),
69       reinterpret_cast<const uint8_t *>(&test_int32), circle::TensorType::TensorType_INT32},
70     // uint8 value
71     FillVariationParam{
72       TestCaseData{}.addInput<int32_t>({2, 3}).addOutput<int64_t>({1052, 1052, 1052, 1052, 1052,
73                                                                    1052}),
74       reinterpret_cast<const uint8_t *>(&test_int64), circle::TensorType::TensorType_INT64}));
75
76 TEST_F(GenModelTest, OneOp_Fill_Int64_Shape)
77 {
78   CircleGen cgen;
79   std::vector<float> value_data{1.3};
80   uint32_t value_buf = cgen.addBuffer(value_data);
81
82   int dims = cgen.addTensor({{2}, circle::TensorType::TensorType_INT64});
83   int value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32, value_buf});
84   int out = cgen.addTensor({{2, 3}, circle::TensorType::TensorType_FLOAT32});
85   cgen.addOperatorFill({{dims, value}, {out}});
86   cgen.setInputsAndOutputs({dims}, {out});
87
88   _context = std::make_unique<GenModelTestContext>(cgen.finish());
89   _context->addTestCase(
90     TestCaseData{}.addInput<int64_t>({2, 3}).addOutput<float>({1.3, 1.3, 1.3, 1.3, 1.3, 1.3}));
91   _context->setBackends({"cpu"});
92
93   SUCCEED();
94 }
95
96 TEST_F(GenModelTest, neg_OneOp_Fill_Int32_oneoperand)
97 {
98   CircleGen cgen;
99
100   int in = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32});
101   int out = cgen.addTensor({{2, 3}, circle::TensorType::TensorType_INT32});
102   cgen.addOperatorFill({{in}, {out}});
103   cgen.setInputsAndOutputs({in}, {out});
104
105   _context = std::make_unique<GenModelTestContext>(cgen.finish());
106   _context->addTestCase(
107     TestCaseData{}.addInput<int32_t>({2, 3}).addOutput<int32_t>({13, 13, 13, 13, 13, 13}));
108   _context->setBackends({"cpu"});
109   _context->expectFailModelLoad();
110
111   SUCCEED();
112 }
113
114 TEST_F(GenModelTest, neg_OneOp_Fill_Int64_oneoperand)
115 {
116   CircleGen cgen;
117
118   int in = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32});
119   int out = cgen.addTensor({{2, 3}, circle::TensorType::TensorType_INT64});
120   cgen.addOperatorFill({{in}, {out}});
121   cgen.setInputsAndOutputs({in}, {out});
122
123   _context = std::make_unique<GenModelTestContext>(cgen.finish());
124   _context->addTestCase(
125     TestCaseData{}.addInput<int32_t>({2, 3}).addOutput<int64_t>({13, 13, 13, 13, 13, 13}));
126   _context->setBackends({"cpu"});
127   _context->expectFailModelLoad();
128
129   SUCCEED();
130 }
131
132 TEST_F(GenModelTest, neg_OneOp_Fill_Float32_oneoperand)
133 {
134   CircleGen cgen;
135
136   int in = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32});
137   int out = cgen.addTensor({{2, 3}, circle::TensorType::TensorType_FLOAT32});
138   cgen.addOperatorFill({{in}, {out}});
139   cgen.setInputsAndOutputs({in}, {out});
140
141   _context = std::make_unique<GenModelTestContext>(cgen.finish());
142   _context->addTestCase(
143     TestCaseData{}.addInput<int32_t>({2, 3}).addOutput<float>({1.3, 1.3, 1.3, 1.3, 1.3, 1.3}));
144   _context->setBackends({"cpu"});
145   _context->expectFailModelLoad();
146
147   SUCCEED();
148 }