Compute Library
 22.08
WindowIterator.cpp
Go to the documentation of this file.
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  */
25 #include "tests/Utils.h"
27 #include "tests/framework/Macros.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)
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>
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 
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)
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);
87  });
88  window_iterator.iterate_3D([&](int start, int end)
89  {
90  ARM_COMPUTE_EXPECT_EQUAL(window[0].start(), start, framework::LogLevel::ERRORS);
93  row_size = end - start;
94  });
96 }
97 
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)
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)
126  });
127  window_iterator.iterate_3D([&](int start, int end)
128  {
129  ARM_COMPUTE_EXPECT(start >= window[0].start(), framework::LogLevel::ERRORS);
132  row_size = end - start;
133  });
135 }
136 
#define ARM_COMPUTE_ASSERT(cond)
Definition: Validate.h:37
Shape of a tensor.
Definition: TensorShape.h:39
Coordinates index2coords(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
Definition: Helpers.inl:156
Iterate over a portion of a Window.
WindowIterator< L > create_window_iterator(const Window &w, const Coordinates &start, const Coordinates &end, L &&lambda_function)
Create a WindowIterator object.
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
Copyright (c) 2017-2022 Arm Limited.
DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), })), framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT), })), framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })), input_info, output_info, act_info, expected)
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
Coordinates of an item.
Definition: Coordinates.h:37
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
std::vector< T > create_vector(std::initializer_list< T > list_objs)
ARM_COMPUTE_EXPECT_EQUAL(expected, actual, framework::LogLevel::ERRORS)
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
zip(zip(framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), }), framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), TensorInfo(TensorShape(2U), 1, DataType::F16), TensorInfo(TensorShape(5U), 1, DataType::F32), })), framework::dataset::make("Expected", { true, false, false}))
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
TEST_SUITE(QASYMM8_to_F32) FIXTURE_DATA_TEST_CASE(RunSmall
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
Describe a multidimensional execution window.
Definition: Window.h:39
Window create_window(Dim &&dim0, Dims &&... dims)