1 /*
2 * Copyright (c) 2017-2018 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/WindowIterator.h"
25 #include "tests/Utils.h"
26 #include "tests/framework/Asserts.h"
27 #include "tests/framework/Macros.h"
28 #include "tests/framework/datasets/Datasets.h"
29 #include "tests/validation/Validation.h"
30 #include "utils/TypePrinter.h"
31
32 #include <stdexcept>
33
34 using namespace arm_compute;
35 using namespace arm_compute::test;
36 using namespace arm_compute::test::validation;
37
38 TEST_SUITE(UNIT)
TEST_SUITE(WindowIterator)39 TEST_SUITE(WindowIterator)
40
41 template <typename Dim, typename... Dims>
42 Window create_window(Dim &&dim0, Dims &&... dims)
43 {
44 Window win;
45 const std::array < Dim, 1 + sizeof...(Dims) > dimensions{ { dim0, std::forward<Dims>(dims)... } };
46 for(size_t i = 0; i < dimensions.size(); i++)
47 {
48 win.set(i, dimensions[i]);
49 }
50 return win;
51 }
52
53 template <typename T>
create_vector(std::initializer_list<T> list_objs)54 std::vector<T> create_vector(std::initializer_list<T> list_objs)
55 {
56 std::vector<T> vec_objs;
57 for(auto it : list_objs)
58 {
59 vec_objs.push_back(it);
60 }
61 return vec_objs;
62 }
63
64 DATA_TEST_CASE(WholeWindow, framework::DatasetMode::ALL, zip(framework::dataset::make("Window", { create_window(Window::Dimension(0, 1)),
65 create_window(Window::Dimension(1, 5, 2), Window::Dimension(3, 5)),
66 create_window(Window::Dimension(4, 16, 4), Window::Dimension(3, 13, 5), Window::Dimension(1, 3, 2))
67 }),
68 framework::dataset::make("Expected", { create_vector({ Coordinates(0, 0) }),
69 create_vector({ Coordinates(1, 3), Coordinates(3, 3), Coordinates(1, 4), Coordinates(3, 4) }),
70 create_vector({ Coordinates(4, 3, 1), Coordinates(8, 3, 1), Coordinates(12, 3, 1), Coordinates(4, 8, 1), Coordinates(8, 8, 1), Coordinates(12, 8, 1) })
71 })),
72 window, expected)
73 {
74 unsigned int i = 0;
75 int row_size = 0;
76 TensorShape window_shape = window.shape();
77 Coordinates start_offset = index2coords(window_shape, 0);
78 Coordinates end_offset = index2coords(window_shape, window.num_iterations_total() - 1);
79 auto window_iterator = create_window_iterator(window, start_offset, end_offset, [&](const Coordinates & id)
__anon38ab53150102(const Coordinates & id) 80 {
81 ARM_COMPUTE_EXPECT_EQUAL(row_size, (window[0].end() - window[0].start()), framework::LogLevel::ERRORS);
82 ARM_COMPUTE_ASSERT(i < expected.size());
83 Coordinates expected_coords = expected[i++];
84 //Set number of dimensions to the maximum (To match the number of dimensions used by the id passed to the lambda function)
85 expected_coords.set_num_dimensions(Coordinates::num_max_dimensions);
86 ARM_COMPUTE_EXPECT_EQUAL(id, expected_coords, framework::LogLevel::ERRORS);
87 });
88 window_iterator.iterate_3D([&](int start, int end)
__anon38ab53150202(int start, int end) 89 {
90 ARM_COMPUTE_EXPECT_EQUAL(window[0].start(), start, framework::LogLevel::ERRORS);
91 ARM_COMPUTE_EXPECT_EQUAL(window[0].end(), end, framework::LogLevel::ERRORS);
92 ARM_COMPUTE_EXPECT(end > start, framework::LogLevel::ERRORS);
93 row_size = end - start;
94 });
95 ARM_COMPUTE_EXPECT_EQUAL(i, expected.size(), framework::LogLevel::ERRORS);
96 }
97
98 DATA_TEST_CASE(PartialWindow2D, framework::DatasetMode::ALL, zip(zip(zip(combine(framework::dataset::make("Window",
99 create_window(Window::Dimension(4, 20, 4), Window::Dimension(3, 32, 5), Window::Dimension(1, 2, 1))),
100 framework::dataset::make("Start", { 0, 1, 3, 2, 4 })),
101 framework::dataset::make("End", { 0, 2, 5, 8, 7 })),
102 framework::dataset::make("RowSize",
103 {
104 create_vector({ 4 }),
105 create_vector({ 8, 8 }),
106 create_vector({ 4, 8, 8 }),
107 create_vector({ 8, 8, 16, 16, 16, 16, 4 }),
108 create_vector({ 16, 16, 16, 16 }),
109 })),
110 framework::dataset::make("Expected", { create_vector({ Coordinates(4, 3, 1) }), create_vector({ Coordinates(8, 3, 1), Coordinates(12, 3, 1) }), create_vector({ Coordinates(16, 3, 1), Coordinates(4, 8, 1), Coordinates(8, 8, 1) }), create_vector({ Coordinates(12, 3, 1), Coordinates(16, 3, 1), Coordinates(4, 8, 1), Coordinates(8, 8, 1), Coordinates(12, 8, 1), Coordinates(16, 8, 1), Coordinates(4, 13, 1) }), create_vector({ Coordinates(4, 8, 1), Coordinates(8, 8, 1), Coordinates(12, 8, 1), Coordinates(16, 8, 1) }) })),
111 window, start, end, expected_row_size, expected)
112 {
113 unsigned int i = 0;
114 int row_size = 0;
115 TensorShape window_shape = window.shape();
116 Coordinates start_offset = index2coords(window_shape, start);
117 Coordinates end_offset = index2coords(window_shape, end);
118 auto window_iterator = create_window_iterator(window, start_offset, end_offset, [&](const Coordinates & id)
__anon38ab53150302(const Coordinates & id) 119 {
120 ARM_COMPUTE_ASSERT(i < expected.size());
121 ARM_COMPUTE_EXPECT_EQUAL(expected_row_size[i], row_size, framework::LogLevel::ERRORS);
122 Coordinates expected_coords = expected[i++];
123 //Set number of dimensions to the maximum (To match the number of dimensions used by the id passed to the lambda function)
124 expected_coords.set_num_dimensions(Coordinates::num_max_dimensions);
125 ARM_COMPUTE_EXPECT_EQUAL(id, expected_coords, framework::LogLevel::ERRORS);
126 });
127 window_iterator.iterate_3D([&](int start, int end)
__anon38ab53150402(int start, int end) 128 {
129 ARM_COMPUTE_EXPECT(start >= window[0].start(), framework::LogLevel::ERRORS);
130 ARM_COMPUTE_EXPECT(end <= window[0].end(), framework::LogLevel::ERRORS);
131 ARM_COMPUTE_EXPECT(end > start, framework::LogLevel::ERRORS);
132 row_size = end - start;
133 });
134 ARM_COMPUTE_EXPECT_EQUAL(i, expected.size(), framework::LogLevel::ERRORS);
135 }
136
137 TEST_SUITE_END()
138 TEST_SUITE_END()
139