1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2018 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/core/WindowIterator.h"
25*c217d954SCole Faust #include "tests/Utils.h"
26*c217d954SCole Faust #include "tests/framework/Asserts.h"
27*c217d954SCole Faust #include "tests/framework/Macros.h"
28*c217d954SCole Faust #include "tests/framework/datasets/Datasets.h"
29*c217d954SCole Faust #include "tests/validation/Validation.h"
30*c217d954SCole Faust #include "utils/TypePrinter.h"
31*c217d954SCole Faust
32*c217d954SCole Faust #include <stdexcept>
33*c217d954SCole Faust
34*c217d954SCole Faust using namespace arm_compute;
35*c217d954SCole Faust using namespace arm_compute::test;
36*c217d954SCole Faust using namespace arm_compute::test::validation;
37*c217d954SCole Faust
38*c217d954SCole Faust TEST_SUITE(UNIT)
TEST_SUITE(WindowIterator)39*c217d954SCole Faust TEST_SUITE(WindowIterator)
40*c217d954SCole Faust
41*c217d954SCole Faust template <typename Dim, typename... Dims>
42*c217d954SCole Faust Window create_window(Dim &&dim0, Dims &&... dims)
43*c217d954SCole Faust {
44*c217d954SCole Faust Window win;
45*c217d954SCole Faust const std::array < Dim, 1 + sizeof...(Dims) > dimensions{ { dim0, std::forward<Dims>(dims)... } };
46*c217d954SCole Faust for(size_t i = 0; i < dimensions.size(); i++)
47*c217d954SCole Faust {
48*c217d954SCole Faust win.set(i, dimensions[i]);
49*c217d954SCole Faust }
50*c217d954SCole Faust return win;
51*c217d954SCole Faust }
52*c217d954SCole Faust
53*c217d954SCole Faust template <typename T>
create_vector(std::initializer_list<T> list_objs)54*c217d954SCole Faust std::vector<T> create_vector(std::initializer_list<T> list_objs)
55*c217d954SCole Faust {
56*c217d954SCole Faust std::vector<T> vec_objs;
57*c217d954SCole Faust for(auto it : list_objs)
58*c217d954SCole Faust {
59*c217d954SCole Faust vec_objs.push_back(it);
60*c217d954SCole Faust }
61*c217d954SCole Faust return vec_objs;
62*c217d954SCole Faust }
63*c217d954SCole Faust
64*c217d954SCole Faust DATA_TEST_CASE(WholeWindow, framework::DatasetMode::ALL, zip(framework::dataset::make("Window", { create_window(Window::Dimension(0, 1)),
65*c217d954SCole Faust create_window(Window::Dimension(1, 5, 2), Window::Dimension(3, 5)),
66*c217d954SCole Faust create_window(Window::Dimension(4, 16, 4), Window::Dimension(3, 13, 5), Window::Dimension(1, 3, 2))
67*c217d954SCole Faust }),
68*c217d954SCole Faust framework::dataset::make("Expected", { create_vector({ Coordinates(0, 0) }),
69*c217d954SCole Faust create_vector({ Coordinates(1, 3), Coordinates(3, 3), Coordinates(1, 4), Coordinates(3, 4) }),
70*c217d954SCole Faust 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*c217d954SCole Faust })),
72*c217d954SCole Faust window, expected)
73*c217d954SCole Faust {
74*c217d954SCole Faust unsigned int i = 0;
75*c217d954SCole Faust int row_size = 0;
76*c217d954SCole Faust TensorShape window_shape = window.shape();
77*c217d954SCole Faust Coordinates start_offset = index2coords(window_shape, 0);
78*c217d954SCole Faust Coordinates end_offset = index2coords(window_shape, window.num_iterations_total() - 1);
79*c217d954SCole Faust auto window_iterator = create_window_iterator(window, start_offset, end_offset, [&](const Coordinates & id)
__anon7653e3c60102(const Coordinates & id) 80*c217d954SCole Faust {
81*c217d954SCole Faust ARM_COMPUTE_EXPECT_EQUAL(row_size, (window[0].end() - window[0].start()), framework::LogLevel::ERRORS);
82*c217d954SCole Faust ARM_COMPUTE_ASSERT(i < expected.size());
83*c217d954SCole Faust Coordinates expected_coords = expected[i++];
84*c217d954SCole Faust //Set number of dimensions to the maximum (To match the number of dimensions used by the id passed to the lambda function)
85*c217d954SCole Faust expected_coords.set_num_dimensions(Coordinates::num_max_dimensions);
86*c217d954SCole Faust ARM_COMPUTE_EXPECT_EQUAL(id, expected_coords, framework::LogLevel::ERRORS);
87*c217d954SCole Faust });
88*c217d954SCole Faust window_iterator.iterate_3D([&](int start, int end)
__anon7653e3c60202(int start, int end) 89*c217d954SCole Faust {
90*c217d954SCole Faust ARM_COMPUTE_EXPECT_EQUAL(window[0].start(), start, framework::LogLevel::ERRORS);
91*c217d954SCole Faust ARM_COMPUTE_EXPECT_EQUAL(window[0].end(), end, framework::LogLevel::ERRORS);
92*c217d954SCole Faust ARM_COMPUTE_EXPECT(end > start, framework::LogLevel::ERRORS);
93*c217d954SCole Faust row_size = end - start;
94*c217d954SCole Faust });
95*c217d954SCole Faust ARM_COMPUTE_EXPECT_EQUAL(i, expected.size(), framework::LogLevel::ERRORS);
96*c217d954SCole Faust }
97*c217d954SCole Faust
98*c217d954SCole Faust DATA_TEST_CASE(PartialWindow2D, framework::DatasetMode::ALL, zip(zip(zip(combine(framework::dataset::make("Window",
99*c217d954SCole Faust create_window(Window::Dimension(4, 20, 4), Window::Dimension(3, 32, 5), Window::Dimension(1, 2, 1))),
100*c217d954SCole Faust framework::dataset::make("Start", { 0, 1, 3, 2, 4 })),
101*c217d954SCole Faust framework::dataset::make("End", { 0, 2, 5, 8, 7 })),
102*c217d954SCole Faust framework::dataset::make("RowSize",
103*c217d954SCole Faust {
104*c217d954SCole Faust create_vector({ 4 }),
105*c217d954SCole Faust create_vector({ 8, 8 }),
106*c217d954SCole Faust create_vector({ 4, 8, 8 }),
107*c217d954SCole Faust create_vector({ 8, 8, 16, 16, 16, 16, 4 }),
108*c217d954SCole Faust create_vector({ 16, 16, 16, 16 }),
109*c217d954SCole Faust })),
110*c217d954SCole Faust 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*c217d954SCole Faust window, start, end, expected_row_size, expected)
112*c217d954SCole Faust {
113*c217d954SCole Faust unsigned int i = 0;
114*c217d954SCole Faust int row_size = 0;
115*c217d954SCole Faust TensorShape window_shape = window.shape();
116*c217d954SCole Faust Coordinates start_offset = index2coords(window_shape, start);
117*c217d954SCole Faust Coordinates end_offset = index2coords(window_shape, end);
118*c217d954SCole Faust auto window_iterator = create_window_iterator(window, start_offset, end_offset, [&](const Coordinates & id)
__anon7653e3c60302(const Coordinates & id) 119*c217d954SCole Faust {
120*c217d954SCole Faust ARM_COMPUTE_ASSERT(i < expected.size());
121*c217d954SCole Faust ARM_COMPUTE_EXPECT_EQUAL(expected_row_size[i], row_size, framework::LogLevel::ERRORS);
122*c217d954SCole Faust Coordinates expected_coords = expected[i++];
123*c217d954SCole Faust //Set number of dimensions to the maximum (To match the number of dimensions used by the id passed to the lambda function)
124*c217d954SCole Faust expected_coords.set_num_dimensions(Coordinates::num_max_dimensions);
125*c217d954SCole Faust ARM_COMPUTE_EXPECT_EQUAL(id, expected_coords, framework::LogLevel::ERRORS);
126*c217d954SCole Faust });
127*c217d954SCole Faust window_iterator.iterate_3D([&](int start, int end)
__anon7653e3c60402(int start, int end) 128*c217d954SCole Faust {
129*c217d954SCole Faust ARM_COMPUTE_EXPECT(start >= window[0].start(), framework::LogLevel::ERRORS);
130*c217d954SCole Faust ARM_COMPUTE_EXPECT(end <= window[0].end(), framework::LogLevel::ERRORS);
131*c217d954SCole Faust ARM_COMPUTE_EXPECT(end > start, framework::LogLevel::ERRORS);
132*c217d954SCole Faust row_size = end - start;
133*c217d954SCole Faust });
134*c217d954SCole Faust ARM_COMPUTE_EXPECT_EQUAL(i, expected.size(), framework::LogLevel::ERRORS);
135*c217d954SCole Faust }
136*c217d954SCole Faust
137*c217d954SCole Faust TEST_SUITE_END()
138*c217d954SCole Faust TEST_SUITE_END()
139