• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "frameworks/native/ops/relu_builder.h"
17 
18 #include "ops_test.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 using namespace OHOS::NeuralNetworkRuntime::Ops;
23 
24 namespace OHOS {
25 namespace NeuralNetworkRuntime {
26 namespace UnitTest {
27 class ReluBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     ReluBuilder m_builder;
34     std::vector<uint32_t> m_inputs {0};
35     std::vector<uint32_t> m_outputs {1};
36     std::vector<int32_t> m_dim {1, 5, 1, 1};
37 };
38 
SetUp()39 void ReluBuilderTest::SetUp() {}
40 
TearDown()41 void ReluBuilderTest::TearDown() {}
42 
43 /**
44  * @tc.name: relu_build_001
45  * @tc.desc: Provide normal input, output, and parameters to verify the normal behavior of the Build function
46  * @tc.type: FUNC
47  */
48 HWTEST_F(ReluBuilderTest, relu_build_001, TestSize.Level0)
49 {
50     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
51     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
52 
53     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
54     EXPECT_EQ(OH_NN_SUCCESS, ret);
55 }
56 
57 /**
58  * @tc.name: relu_build_002
59  * @tc.desc: Call Build func twice to verify the abnormal behavior of the Build function
60  * @tc.type: FUNC
61  */
62 HWTEST_F(ReluBuilderTest, relu_build_002, TestSize.Level0)
63 {
64     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
65     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
66 
67     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
68     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
69 }
70 
71 /**
72  * @tc.name: relu_build_003
73  * @tc.desc: Provide one more than normal input to verify the abnormal behavior of the Build function
74  * @tc.type: FUNC
75  */
76 HWTEST_F(ReluBuilderTest, relu_build_003, TestSize.Level0)
77 {
78     m_inputs = {0, 1};
79     m_outputs = {2};
80 
81     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
82     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
83 
84     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
85     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
86 }
87 
88 /**
89  * @tc.name: relu_build_004
90  * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
91  * @tc.type: FUNC
92  */
93 HWTEST_F(ReluBuilderTest, relu_build_004, TestSize.Level0)
94 {
95     m_outputs = {1, 2};
96 
97     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
98     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
99 
100     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
101     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
102 }
103 
104 /**
105  * @tc.name: relu_build_005
106  * @tc.desc: Verify that the build function return a failed message with null allTensor
107  * @tc.type: FUNC
108  */
109 HWTEST_F(ReluBuilderTest, relu_build_005, TestSize.Level0)
110 {
111     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputs, m_allTensors);
112     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
113 }
114 
115 /**
116  * @tc.name: relu_build_006
117  * @tc.desc: Verify that the build function return a failed message without output tensor
118  * @tc.type: FUNC
119  */
120 HWTEST_F(ReluBuilderTest, relu_build_006, TestSize.Level0)
121 {
122     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
123 
124     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputs, m_allTensors);
125     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
126 }
127 
128 /**
129  * @tc.name: relu_build_007
130  * @tc.desc: Verify that the build function return a successful message with a virtual parameter
131  * @tc.type: FUNC
132  */
133 HWTEST_F(ReluBuilderTest, relu_build_007, TestSize.Level0)
134 {
135     std::vector<uint32_t> paramsIndex = {2};
136     std::vector<int32_t> paramDim = {};
137 
138     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
139     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
140     std::shared_ptr<NNTensor> reluTensor = TransToNNTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_TENSOR);
141     m_allTensors.emplace_back(reluTensor);
142 
143     OH_NN_ReturnCode ret = m_builder.Build(paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
144     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
145 }
146 
147 /**
148  * @tc.name: relu_get_primitive_001
149  * @tc.desc: Verify the GetPrimitive function return nullptr
150  * @tc.type: FUNC
151  */
152 HWTEST_F(ReluBuilderTest, relu_get_primitive_001, TestSize.Level0)
153 {
154     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
155     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
156     EXPECT_EQ(primitive, expectPrimitive);
157 }
158 
159 /**
160  * @tc.name: relu_get_primitive_002
161  * @tc.desc: Verify the normal params return behavior of the getprimitive function
162  * @tc.type: FUNC
163  */
164 HWTEST_F(ReluBuilderTest, relu_get_primitive_002, TestSize.Level0)
165 {
166     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
167     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
168 
169     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
170     LiteGraphTensorPtr reluPrimitive = m_builder.GetPrimitive();
171     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
172     EXPECT_NE(reluPrimitive, expectPrimitive);
173 
174     mindspore::lite::ActivationType activationType = mindspore::lite::ACTIVATION_TYPE_RELU;
175     auto returnValue = mindspore::lite::MindIR_Activation_GetActivationType(reluPrimitive.get());
176     EXPECT_EQ(returnValue, activationType);
177 }
178 } // namespace UnitTest
179 } // namespace NeuralNetworkRuntime
180 } // namespace OHOS