ecfb15931f0ce665546e578b4a8f144c982fcd4c
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / Transpose.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 #include <memory>
20
21 TEST_F(GenModelTest, OneOp_Transpose_PermsToConst)
22 {
23   CircleGen cgen;
24   std::vector<int32_t> perms_data{2, 0, 1, 3};
25   uint32_t perms_buf = cgen.addBuffer(perms_data);
26   int perms = cgen.addTensor({{4}, circle::TensorType::TensorType_INT32, perms_buf});
27   int in = cgen.addTensor({{2, 3, 4, 5}, circle::TensorType::TensorType_FLOAT32});
28   int out = cgen.addTensor({{2, 3, 4, 5}, circle::TensorType::TensorType_FLOAT32});
29   cgen.addOperatorTranspose({{in, perms}, {out}});
30   cgen.setInputsAndOutputs({in}, {out});
31
32   _context = std::make_unique<GenModelTestContext>(cgen.finish());
33   _context->addTestCase(uniformTCD<float>(
34       {{0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   10,  11,  12,  13,  14,  15,  16,  17,
35         18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,
36         36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,
37         54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,
38         72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,
39         90,  91,  92,  93,  94,  95,  96,  97,  98,  99,  100, 101, 102, 103, 104, 105, 106, 107,
40         108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119}},
41       {{0,  1,   2,   3,   4,   20,  21, 22,  23,  24,  40,  41,  42, 43,  44,  60,  61,  62,
42         63, 64,  80,  81,  82,  83,  84, 100, 101, 102, 103, 104, 5,  6,   7,   8,   9,   25,
43         26, 27,  28,  29,  45,  46,  47, 48,  49,  65,  66,  67,  68, 69,  85,  86,  87,  88,
44         89, 105, 106, 107, 108, 109, 10, 11,  12,  13,  14,  30,  31, 32,  33,  34,  50,  51,
45         52, 53,  54,  70,  71,  72,  73, 74,  90,  91,  92,  93,  94, 110, 111, 112, 113, 114,
46         15, 16,  17,  18,  19,  35,  36, 37,  38,  39,  55,  56,  57, 58,  59,  75,  76,  77,
47         78, 79,  95,  96,  97,  98,  99, 115, 116, 117, 118, 119}}));
48   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
49
50   SUCCEED();
51 }
52
53 TEST_F(GenModelTest, OneOp_Transpose_PermsToVar)
54 {
55   CircleGen cgen;
56   int perms = cgen.addTensor({{4}, circle::TensorType::TensorType_INT32});
57   int in = cgen.addTensor({{1, 2, 3, 1}, circle::TensorType::TensorType_FLOAT32});
58   int out = cgen.addTensor({{1, 3, 2, 1}, circle::TensorType::TensorType_FLOAT32});
59   cgen.addOperatorTranspose({{in, perms}, {out}});
60   cgen.setInputsAndOutputs({in, perms}, {out});
61
62   _context = std::make_unique<GenModelTestContext>(cgen.finish());
63   _context->addTestCase(TestCaseData{}
64                             .addInput<float>({1, 2, 3, 4, 5, 6})
65                             .addInput<int32_t>({0, 2, 1, 3})
66                             .addOutput<float>({1, 4, 2, 5, 3, 6}));
67   _context->setBackends({"cpu"});
68
69   SUCCEED();
70 }
71
72 TEST_F(GenModelTest, OneOp_Transpose_RegularTranspose)
73 {
74   CircleGen cgen;
75   int perms = cgen.addTensor({{0}, circle::TensorType::TensorType_INT32});
76   int in = cgen.addTensor({{1, 2, 3, 1}, circle::TensorType::TensorType_FLOAT32});
77   int out = cgen.addTensor({{1, 3, 2, 1}, circle::TensorType::TensorType_FLOAT32});
78   cgen.addOperatorTranspose({{in, perms}, {out}});
79   cgen.setInputsAndOutputs({in, perms}, {out});
80
81   _context = std::make_unique<GenModelTestContext>(cgen.finish());
82   _context->addTestCase(TestCaseData{}
83                             .addInput<float>({1, 2, 3, 4, 5, 6})
84                             .addInput<int32_t>({})
85                             .addOutput<float>({1, 4, 2, 5, 3, 6}));
86   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
87
88   SUCCEED();
89 }
90
91 TEST_F(GenModelTest, neg_OneOp_Transpose_InvalidPermsSize)
92 {
93   CircleGen cgen;
94   std::vector<int32_t> perms_data{0, 1, 2};
95   uint32_t perms_buf = cgen.addBuffer(perms_data);
96   int perms = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32, perms_buf});
97   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
98   int out = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
99   cgen.addOperatorTranspose({{in, perms}, {out}});
100   cgen.setInputsAndOutputs({in}, {out});
101
102   _context = std::make_unique<GenModelTestContext>(cgen.finish());
103   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
104   _context->expectFailCompile();
105
106   SUCCEED();
107 }
108
109 TEST_F(GenModelTest, neg_OneOp_Transpose_InvalidPermsVal)
110 {
111   CircleGen cgen;
112   std::vector<int32_t> perms_data{-3, 3, 1, 2};
113   uint32_t perms_buf = cgen.addBuffer(perms_data);
114   int perms = cgen.addTensor({{4}, circle::TensorType::TensorType_INT32, perms_buf});
115   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
116   int out = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
117   cgen.addOperatorTranspose({{in, perms}, {out}});
118   cgen.setInputsAndOutputs({in}, {out});
119
120   _context = std::make_unique<GenModelTestContext>(cgen.finish());
121   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
122   _context->expectFailCompile();
123
124   SUCCEED();
125 }
126
127 TEST_F(GenModelTest, neg_OneOp_Transpose_DuplicatedPermsVal)
128 {
129   CircleGen cgen;
130   std::vector<int32_t> perms_data{3, 3, 1, 2};
131   uint32_t perms_buf = cgen.addBuffer(perms_data);
132   int perms = cgen.addTensor({{4}, circle::TensorType::TensorType_INT32, perms_buf});
133   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
134   int out = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
135   cgen.addOperatorTranspose({{in, perms}, {out}});
136   cgen.setInputsAndOutputs({in}, {out});
137
138   _context = std::make_unique<GenModelTestContext>(cgen.finish());
139   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
140   _context->expectFailCompile();
141
142   SUCCEED();
143 }