Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / Tile.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 TEST_F(GenModelTest, OneOp_Tile_ConstMul)
20 {
21   CircleGen cgen;
22   std::vector<int32_t> mul_data{1, 2};
23   uint32_t mul_buf = cgen.addBuffer(mul_data);
24   int in = cgen.addTensor({{2, 3}, circle::TensorType::TensorType_FLOAT32});
25   int mul = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32, mul_buf});
26   int out = cgen.addTensor({{2, 6}, circle::TensorType::TensorType_FLOAT32});
27   cgen.addOperatorTile({{in, mul}, {out}});
28   cgen.setInputsAndOutputs({in}, {out});
29
30   _context = std::make_unique<GenModelTestContext>(cgen.finish());
31   _context->addTestCase(
32     uniformTCD<float>({{1, 2, 3, 4, 5, 6}}, {{1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6}}));
33   _context->setBackends({"cpu"});
34
35   SUCCEED();
36 }
37
38 TEST_F(GenModelTest, OneOp_Tile_MulToConst)
39 {
40   CircleGen cgen;
41   std::vector<int32_t> multiplies_data{2, 3, 1};
42   uint32_t multiplies_buf = cgen.addBuffer(multiplies_data);
43   int multiplies = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32, multiplies_buf});
44   int in = cgen.addTensor({{1, 2, 3}, circle::TensorType::TensorType_FLOAT32});
45   int out = cgen.addTensor({{2, 6, 3}, circle::TensorType::TensorType_FLOAT32});
46   cgen.addOperatorTile({{in, multiplies}, {out}});
47   cgen.setInputsAndOutputs({in}, {out});
48
49   _context = std::make_unique<GenModelTestContext>(cgen.finish());
50   _context->addTestCase(
51     uniformTCD<float>({{11, 12, 13, 21, 22, 23}},
52                       {{11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23,
53                         11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23}}));
54   _context->setBackends({"cpu"});
55
56   SUCCEED();
57 }
58
59 TEST_F(GenModelTest, OneOp_Tile_MulToVar)
60 {
61   CircleGen cgen;
62   int multiplies = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32});
63   int in = cgen.addTensor({{1, 2, 3}, circle::TensorType::TensorType_FLOAT32});
64   int out = cgen.addTensor({{2, 6, 3}, circle::TensorType::TensorType_FLOAT32});
65   cgen.addOperatorTile({{in, multiplies}, {out}});
66   cgen.setInputsAndOutputs({in, multiplies}, {out});
67
68   _context = std::make_unique<GenModelTestContext>(cgen.finish());
69   _context->addTestCase(
70     TestCaseData{}
71       .addInput<float>({11, 12, 13, 21, 22, 23})
72       .addInput<int32_t>({2, 3, 1})
73       .addOutput<float>({11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23,
74                          11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23}));
75   _context->setBackends({"cpu"});
76
77   SUCCEED();
78 }
79
80 TEST_F(GenModelTest, OneOp_Tile_VarMul)
81 {
82   CircleGen cgen;
83   int in = cgen.addTensor({{2, 3}, circle::TensorType::TensorType_FLOAT32});
84   int mul = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32});
85   int out = cgen.addTensor({{2, 6}, circle::TensorType::TensorType_FLOAT32});
86   cgen.addOperatorTile({{in, mul}, {out}});
87   cgen.setInputsAndOutputs({in, mul}, {out});
88
89   _context = std::make_unique<GenModelTestContext>(cgen.finish());
90   _context->addTestCase(TestCaseData{}
91                           .addInput<float>({1, 2, 3, 4, 5, 6})
92                           .addInput<int32_t>({1, 2})
93                           .addOutput<float>({1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6}));
94   _context->setBackends({"cpu"});
95
96   SUCCEED();
97 }
98
99 TEST_F(GenModelTest, neg_OneOp_Tile)
100 {
101   CircleGen cgen;
102   std::vector<int32_t> mul_data{1, 2, 1, 2};
103   uint32_t mul_buf = cgen.addBuffer(mul_data);
104   int in = cgen.addTensor({{2, 3}, circle::TensorType::TensorType_FLOAT32});
105   // 2D multiples input is not supported
106   int mul = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_INT32, mul_buf});
107   int out = cgen.addTensor({{2, 6}, circle::TensorType::TensorType_FLOAT32});
108   cgen.addOperatorTile({{in, mul}, {out}});
109   cgen.setInputsAndOutputs({in}, {out});
110
111   _context = std::make_unique<GenModelTestContext>(cgen.finish());
112   _context->setBackends({"cpu"});
113   _context->expectFailCompile();
114
115   SUCCEED();
116 }
117
118 TEST_F(GenModelTest, neg_OneOp_Tile_InvalidMulSize)
119 {
120   CircleGen cgen;
121   std::vector<int32_t> multiplies_data{2, 6};
122   uint32_t multiplies_buf = cgen.addBuffer(multiplies_data);
123   int multiplies = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32, multiplies_buf});
124   int in = cgen.addTensor({{1, 2, 3}, circle::TensorType::TensorType_FLOAT32});
125   int out = cgen.addTensor({{2, 6, 3}, circle::TensorType::TensorType_FLOAT32});
126   cgen.addOperatorTile({{in, multiplies}, {out}});
127   cgen.setInputsAndOutputs({in}, {out});
128
129   _context = std::make_unique<GenModelTestContext>(cgen.finish());
130   _context->setBackends({"cpu"});
131   _context->expectFailCompile();
132
133   SUCCEED();
134 }