xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/UNIT/SubTensorInfo.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2020-2023 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "arm_compute/core/SubTensorInfo.h"
25 #include "arm_compute/core/TensorInfo.h"
26 #include "arm_compute/core/Types.h"
27 #include "tests/framework/Asserts.h"
28 #include "tests/framework/Macros.h"
29 #include "tests/validation/Validation.h"
30 #include "utils/TypePrinter.h"
31 
32 namespace arm_compute
33 {
34 namespace test
35 {
36 namespace validation
37 {
38 TEST_SUITE(UNIT)
TEST_SUITE(SubTensorInfo)39 TEST_SUITE(SubTensorInfo)
40 
41 /** Validate sub-tensor creation
42  *
43  * Test performed:
44  *
45  *  - Negative testing on X indexing
46  *  - Negative testing on Y indexing
47  *  - Positive testing by indexing on X,Y indexing
48  * */
49 TEST_CASE(SubTensorCreation, framework::DatasetMode::ALL)
50 {
51     // Create tensor info
52     TensorInfo info(TensorShape(23U, 17U, 3U), 1, DataType::F32);
53 
54     // Negative testing on X
55     ARM_COMPUTE_EXPECT_THROW(SubTensorInfo(&info, TensorShape(13U, 17U, 3U), Coordinates(24, 0, 0)), framework::LogLevel::ERRORS);
56     ARM_COMPUTE_EXPECT_THROW(SubTensorInfo(&info, TensorShape(13U, 17U, 3U), Coordinates(15, 0, 0)), framework::LogLevel::ERRORS);
57 
58     // Negative testing on Y
59     ARM_COMPUTE_EXPECT_THROW(SubTensorInfo(&info, TensorShape(23U, 8U, 3U), Coordinates(0, 18, 0)), framework::LogLevel::ERRORS);
60     ARM_COMPUTE_EXPECT_THROW(SubTensorInfo(&info, TensorShape(23U, 8U, 3U), Coordinates(0, 13, 0)), framework::LogLevel::ERRORS);
61 
62     // Positive testing on XY indexing
63     ARM_COMPUTE_EXPECT_NO_THROW(SubTensorInfo(&info, TensorShape(4U, 3U, 2U), Coordinates(5, 2, 1)), framework::LogLevel::ERRORS);
64 }
65 
66 /** Validate when extending padding on sub-tensor
67  *
68  * Tests performed:
69  *  - A) Extend padding when SubTensor XY does not match parent tensor should fail
70  *    B) Extend with zero padding when SubTensor XY does not match parent tensor should succeed
71  *  - C) Extend padding when SubTensor XY matches parent tensor should succeed
72  *  - D) Set lock padding to true, so that extend padding would fail
73  */
TEST_CASE(SubTensorPaddingExpansion,framework::DatasetMode::ALL)74 TEST_CASE(SubTensorPaddingExpansion, framework::DatasetMode::ALL)
75 {
76     // Test A
77     {
78         TensorInfo    tensor_info(TensorShape(23U, 17U, 3U), 1, DataType::F32);
79         SubTensorInfo sub_tensor_info(&tensor_info, TensorShape(4U, 3U, 2U), Coordinates(5, 2, 1));
80         ARM_COMPUTE_EXPECT_THROW(sub_tensor_info.extend_padding(PaddingSize(2, 1)), framework::LogLevel::ERRORS);
81     }
82 
83     // Test B
84     {
85         TensorInfo    tensor_info(TensorShape(23U, 17U, 3U), 1, DataType::F32);
86         SubTensorInfo sub_tensor_info(&tensor_info, TensorShape(4U, 3U, 1U), Coordinates(5, 2, 1));
87         ARM_COMPUTE_EXPECT_NO_THROW(sub_tensor_info.extend_padding(PaddingSize(0, 0)), framework::LogLevel::ERRORS);
88         ARM_COMPUTE_EXPECT(tensor_info.padding().uniform(), framework::LogLevel::ERRORS);
89     }
90 
91     // Test C
92     {
93         TensorInfo    tensor_info(TensorShape(23U, 17U, 3U), 1, DataType::F32);
94         SubTensorInfo sub_tensor_info(&tensor_info, TensorShape(23U, 17U, 1U), Coordinates(0, 0, 1));
95         ARM_COMPUTE_EXPECT_NO_THROW(sub_tensor_info.extend_padding(PaddingSize(2, 1)), framework::LogLevel::ERRORS);
96         ARM_COMPUTE_EXPECT(tensor_info.padding().top == 2, framework::LogLevel::ERRORS);
97         ARM_COMPUTE_EXPECT(tensor_info.padding().right == 1, framework::LogLevel::ERRORS);
98     }
99 
100     // Test D
101     {
102         TensorInfo    tensor_info(TensorShape(23U, 17U, 3U), 1, DataType::F32);
103         SubTensorInfo sub_tensor_info(&tensor_info, TensorShape(4U, 3U, 1U), Coordinates(5, 2, 1));
104         sub_tensor_info.set_lock_paddings(true);
105         ARM_COMPUTE_EXPECT_THROW(sub_tensor_info.extend_padding(PaddingSize(2, 1)), framework::LogLevel::ERRORS);
106     }
107 }
108 
109 TEST_SUITE_END() // SubTensorInfo
110 TEST_SUITE_END()
111 } // namespace validation
112 } // namespace test
113 } // namespace arm_compute
114