From 671a11e1c8e1e4db7bcae9ce97b0c97ebcb97464 Mon Sep 17 00:00:00 2001 From: Anthony Barbier Date: Fri, 6 Jul 2018 15:11:36 +0100 Subject: COMPMID-1379: Created WindowIterator and TensorAccessor - WindowIterator: used to iterate over arbitrary positions of a window. (More flexible than execute_window_loop which only can iterate over entire dimensions) - TensorAccessor: RSH's code uses pointers to specialised types and strides in element sizes, this helps interfacing with their code. Change-Id: I8ded8758d345668804873409f949b8cec694d289 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/139082 Tested-by: Jenkins Reviewed-by: Georgios Pinitas --- arm_compute/core/Window.h | 8 +- arm_compute/core/Window.inl | 20 ++ arm_compute/core/WindowIterator.h | 292 ++++++++++++++++++++++ tests/framework/datasets/InitializerListDataset.h | 5 +- tests/validation/UNIT/WindowIterator.cpp | 138 ++++++++++ utils/TypePrinter.h | 56 ++++- 6 files changed, 514 insertions(+), 5 deletions(-) create mode 100644 arm_compute/core/WindowIterator.h create mode 100644 tests/validation/UNIT/WindowIterator.cpp diff --git a/arm_compute/core/Window.h b/arm_compute/core/Window.h index 6f172ecebf..73c8d4385b 100644 --- a/arm_compute/core/Window.h +++ b/arm_compute/core/Window.h @@ -224,7 +224,13 @@ public: * @return The number of iterations */ constexpr size_t num_iterations(size_t dimension) const; - + /** Return the total number of iterations needed to iterate through the entire window + * + * @return Number of total iterations + */ + size_t num_iterations_total() const; + /** Return the shape of the window in number of steps */ + TensorShape shape() const; /** Split a window into a set of sub windows along a given dimension * * For example to split a window into 3 sub-windows along the Y axis, you would have to do:
diff --git a/arm_compute/core/Window.inl b/arm_compute/core/Window.inl index 8401227eee..c6fc8848aa 100644 --- a/arm_compute/core/Window.inl +++ b/arm_compute/core/Window.inl @@ -247,4 +247,24 @@ inline void Window::use_tensor_dimensions(const TensorShape &shape, size_t first set(n, Window::Dimension(0, std::max(shape[n], static_cast(1)))); } } + +inline TensorShape Window::shape() const +{ + TensorShape shape; + for(size_t d = 0; d < TensorShape::num_max_dimensions; ++d) + { + shape.set(d, (_dims[d].end() - _dims[d].start()) / _dims[d].step()); + } + return shape; +} + +inline size_t Window::num_iterations_total() const +{ + size_t total = 1; + for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d) + { + total *= num_iterations(d); + } + return total; +} } diff --git a/arm_compute/core/WindowIterator.h b/arm_compute/core/WindowIterator.h new file mode 100644 index 0000000000..13e9973506 --- /dev/null +++ b/arm_compute/core/WindowIterator.h @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_WINDOW_ITERATOR_H__ +#define __ARM_COMPUTE_WINDOW_ITERATOR_H__ +#include "arm_compute/core/Coordinates.h" +#include "arm_compute/core/ITensor.h" +#include "arm_compute/core/Window.h" + +//FIXME: Delete the "PRINTF" before the release. In the meantime it's probably going to be useful to debug +//#define PRINTF printf +#define PRINTF(...) + +namespace arm_compute +{ +/** Convert an offset in window steps into absolute coordinates. + * + * @param[in] w Window @p offset is related to. + * @param[in] offset Offset inside the window expressed in number of window steps. + * + * @return Absolute coordinates. + */ +inline Coordinates convert_window_coord_to_position(const Window &w, const Coordinates &offset) +{ + Coordinates position; + for(unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i) + { + position.set(i, w[i].start() + offset[i] * w[i].step()); + } + return position; +} + +/** Tensor accessors to make it easier to interface with arm_gemm */ +template +class TensorAccessor +{ +public: + /** Constructor: + * + * @param[in] tensor Source tensor, must be allocated. + */ + TensorAccessor(const ITensor &tensor) + : _first(tensor.ptr_to_element(Coordinates())), _strides(tensor.info()->strides_in_bytes()) + { + } + /** Get the stride of the dimension dim expressed in number of Ts. + * + * @param[in] dim Dimension of the wanted stride. + * + * @return Stride in number of Ts. + */ + inline size_t stride(size_t dim) const + { + return _strides[dim] / sizeof(T); + } + + /** Returns a pointer to the element at coordinates (x,y,z,w) + * + * @param[in] x X coordinates + * @param[in] y (optional) Y coordinates + * @param[in] z (optional) Z coordinates + * @param[in] w (optional) W coordinates + */ + inline T *get_ptr(unsigned int x, unsigned int y = 0, unsigned int z = 0, unsigned int w = 0) + { + return reinterpret_cast(_first + x * _strides[0] + y * _strides[1] + z * _strides[2] + w * _strides[3]); + } + + /** Returns a pointer to the element at coordinates (x,y,z,w) + * + * @param[in] x X coordinates + * @param[in] y (optional) Y coordinates + * @param[in] z (optional) Z coordinates + * @param[in] w (optional) W coordinates + */ + inline T *operator()(unsigned int x, unsigned int y = 0, unsigned int z = 0, unsigned int w = 0) + { + return get_ptr(x, y, z, w); + } + +private: + uint8_t *_first; /**< Pointer to the first element of the tensor.*/ + const Strides &_strides; /**< Strides in bytes of the tensor */ +}; + +/** Iterate over a portion of a Window */ +template +class WindowIterator +{ +public: + /** Construct a WindowIterator object + * + * @param[in] w Window to use for the iteration + * @param[in] start Where to start iterating from (In Window coordinates) + * @param[in] end Where to stop iterating (In Window coordinates). + * @param[in] lambda_function Lambda function to call for every iteration between start and end. (It will be called last for end - 1) + */ + WindowIterator(const Window &w, const Coordinates &start, const Coordinates &end, L &&lambda_function) + : _lambda_function(std::move(lambda_function)), + _position(convert_window_coord_to_position(w, start)), + _end(convert_window_coord_to_position(w, end)), + _w(w) + { + } + /** Iterate over the lowest 3 dimensions of the window. + * + * @param[in] on_new_row_size Callback to be called before lambda_function every time the width of the row processed changes. + */ + template + void iterate_3D(M &&on_new_row_size) + { + while(_end.z() != _position.z()) + { + PRINTF("New slice %d\n", _position.z()); + iterate_2D_internal(on_new_row_size, _w.x().end() - _w.x().step(), _w.y().end() - _w.y().step()); + _position[2] += _w.z().step(); + _position[1] = _w.y().start(); + _position[0] = _w.x().start(); + } + // Left over: + PRINTF("Left over slice\n"); + iterate_2D(on_new_row_size); + } + + /** Iterate over the lowest 2 dimensions of the window. + * + * @param[in] on_new_row_size Callback to be called before lambda_function every time the width of the row processed changes. + */ + template + void iterate_2D(M &&on_new_row_size) + { + iterate_2D_internal(on_new_row_size, _end.x(), _end.y()); + } + + /** Change the step used for the iteration. + * + * @note Does not affect the start and end points. + * + * @param[in] dim Dimension to change + * @param[in] step New step to use for the given dimension. + */ + inline void set_step(size_t dim, int step) + { + _w.set_dimension_step(dim, step); + } + + /** Returns the coordinates in absolute coordinates of the end position + * + * @return End position coordinates. + */ + const Coordinates &end_position() const + { + return _end; + } + +private: + template + void iterate_2D_internal(M &&on_new_row_size, int end_x, int end_y) + { + //Is there more than one row to process ? + if(end_y == _position.y()) + { + // Single row: + PRINTF("Partial row only\n"); + // Both start and end belong to the same row: + iterate_over_dim0(end_x + _w.x().step(), on_new_row_size); + } + else + { + // Do we start from the beginning of the row ? + if(_w.x().start() != _position.x()) + { + //Start in the middle of a row: process left-over X + PRINTF("Partial row first\n"); + iterate_over_dim0(_w.x().end(), on_new_row_size); + _position[1] += _w.y().step(); + } + + //Middle rows + bool no_leftover = end_x + _w.x().step() == _w.x().end(); + if(no_leftover) + { + PRINTF("no left over\n"); + //Switch to full row size: + on_new_row_size(_w[0].start(), _w.x().end()); + // Shouldn't be possible to reach that point and not have at least one entire row to process + ARM_COMPUTE_ERROR_ON(_w.y().end() == _position.y()); + // No leftover: all the rows lefts to process are full width: + iterate_over_dim1(_w.y().end()); + } + else + { + PRINTF("with left over\n"); + // Are there full rows to process ? + if(_position[1] != end_y) + { + PRINTF("full rows\n"); + //Switch to full row size: + on_new_row_size(_w[0].start(), _w.x().end()); + iterate_over_dim1(_w.y().end() - _w.y().step()); + } + + PRINTF("Final leftover\n"); + //Leftover end x + _position[0] = _w.x().start(); + iterate_over_dim0(end_x + _w.x().step(), on_new_row_size); + } + } + } + + /** Process full rows below 'end' + * + * @param[in] end Y position to stop at. + */ + void iterate_over_dim1(int end) + { + for(; _position[1] != end; _position[1] += _w[1].step()) + { + _position[0] = _w[0].start(); + iterate_over_dim0(_w[0].end()); + } + } + + /** Process elements of a given row up to 'end' + * + * @param[in] end X position to stop at. + * @param[in] on_new_row_size Callback to call before starting iterating + */ + template + void iterate_over_dim0(int end, M &&on_new_row_size) + { + on_new_row_size(_position.x(), end); + iterate_over_dim0(end); + } + + /** Process elements of a given row up to 'end' + * + * @param[in] end X position to stop at. + */ + void iterate_over_dim0(int end) + { + PRINTF("X [%d, %d, %d]\n", _position.x(), end, _w[0].step()); + // Both start and end belong to the same row: + ARM_COMPUTE_ERROR_ON(_position[0] > end); + for(; _position.x() < end; _position[0] += _w[0].step()) + { + _lambda_function(_position); + } + } + + L _lambda_function; /**< Function to call for each iteration */ + Coordinates _position; /**< Absolute coordinates of the current position */ + Coordinates _end; /**< Absolute coordinates of the point after the last iteration */ + Window _w; /**< Window to iterate over */ +}; + +/** Create a WindowIterator object + * + * @param[in] w Window to use for the iteration + * @param[in] start Where to start iterating from (In Window coordinates) + * @param[in] end Where to stop iterating (In Window coordinates). + * @param[in] lambda_function Lambda function to call for every iteration between start and end. (It will be called last for end - 1) + * + * @return A WindowIterator object. + */ +template +WindowIterator create_window_iterator(const Window &w, const Coordinates &start, const Coordinates &end, L &&lambda_function) +{ + return WindowIterator(w, start, end, std::move(lambda_function)); +} +} +#endif /*__ARM_COMPUTE_WINDOW_ITERATOR_H__*/ diff --git a/tests/framework/datasets/InitializerListDataset.h b/tests/framework/datasets/InitializerListDataset.h index f90e0b747a..ec1550df4d 100644 --- a/tests/framework/datasets/InitializerListDataset.h +++ b/tests/framework/datasets/InitializerListDataset.h @@ -25,7 +25,7 @@ #define ARM_COMPUTE_TEST_DATASET_LIST #include "Dataset.h" -#include "support/ToolchainSupport.h" +#include "utils/TypePrinter.h" #include #include @@ -84,8 +84,7 @@ public: */ std::string description() const { - using support::cpp11::to_string; - return _name + "=" + to_string(*_iterator); + return _name + "=" + arm_compute::to_string(*_iterator); } /** Get the current value. diff --git a/tests/validation/UNIT/WindowIterator.cpp b/tests/validation/UNIT/WindowIterator.cpp new file mode 100644 index 0000000000..0c1ab12fe7 --- /dev/null +++ b/tests/validation/UNIT/WindowIterator.cpp @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2017-2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/WindowIterator.h" +#include "tests/Utils.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "utils/TypePrinter.h" + +#include + +using namespace arm_compute; +using namespace arm_compute::test; +using namespace arm_compute::test::validation; + +TEST_SUITE(UNIT) +TEST_SUITE(WindowIterator) + +template +Window create_window(Dim &&dim0, Dims &&... dims) +{ + Window win; + const std::array < Dim, 1 + sizeof...(Dims) > dimensions{ { dim0, std::forward(dims)... } }; + for(size_t i = 0; i < dimensions.size(); i++) + { + win.set(i, dimensions[i]); + } + return win; +} + +template +std::vector create_vector(std::initializer_list list_objs) +{ + std::vector vec_objs; + for(auto it : list_objs) + { + vec_objs.push_back(it); + } + return vec_objs; +} + +DATA_TEST_CASE(WholeWindow, framework::DatasetMode::ALL, zip(framework::dataset::make("Window", { create_window(Window::Dimension(0, 1)), + create_window(Window::Dimension(1, 5, 2), Window::Dimension(3, 5)), + create_window(Window::Dimension(4, 16, 4), Window::Dimension(3, 13, 5), Window::Dimension(1, 3, 2)) + }), + framework::dataset::make("Expected", { create_vector({ Coordinates(0, 0) }), + create_vector({ Coordinates(1, 3), Coordinates(3, 3), Coordinates(1, 4), Coordinates(3, 4) }), + 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) }) + })), + window, expected) +{ + unsigned int i = 0; + int row_size = 0; + TensorShape window_shape = window.shape(); + Coordinates start_offset = index2coords(window_shape, 0); + Coordinates end_offset = index2coords(window_shape, window.num_iterations_total() - 1); + auto window_iterator = create_window_iterator(window, start_offset, end_offset, [&](const Coordinates & id) + { + ARM_COMPUTE_EXPECT_EQUAL(row_size, (window[0].end() - window[0].start()), framework::LogLevel::ERRORS); + ARM_COMPUTE_ASSERT(i < expected.size()); + Coordinates expected_coords = expected[i++]; + //Set number of dimensions to the maximum (To match the number of dimensions used by the id passed to the lambda function) + expected_coords.set_num_dimensions(Coordinates::num_max_dimensions); + ARM_COMPUTE_EXPECT_EQUAL(id, expected_coords, framework::LogLevel::ERRORS); + }); + window_iterator.iterate_3D([&](int start, int end) + { + ARM_COMPUTE_EXPECT_EQUAL(window[0].start(), start, framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT_EQUAL(window[0].end(), end, framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(end > start, framework::LogLevel::ERRORS); + row_size = end - start; + }); + ARM_COMPUTE_EXPECT_EQUAL(i, expected.size(), framework::LogLevel::ERRORS); +} + +DATA_TEST_CASE(PartialWindow2D, framework::DatasetMode::ALL, zip(zip(zip(combine(framework::dataset::make("Window", + create_window(Window::Dimension(4, 20, 4), Window::Dimension(3, 18, 5), Window::Dimension(1, 2, 1))), + framework::dataset::make("Start", { 0, 1, 3, 2, 4 })), + framework::dataset::make("End", { 0, 2, 5, 8, 7 })), + framework::dataset::make("RowSize", +{ + create_vector({ 4 }), + create_vector({ 8, 8 }), + create_vector({ 4, 8, 8 }), + create_vector({ 8, 8, 16, 16, 16, 16, 4 }), + create_vector({ 16, 16, 16, 16 }), +})), +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) }) })), +window, start, end, expected_row_size, expected) +{ + unsigned int i = 0; + int row_size = 0; + TensorShape window_shape = window.shape(); + Coordinates start_offset = index2coords(window_shape, start); + Coordinates end_offset = index2coords(window_shape, end); + auto window_iterator = create_window_iterator(window, start_offset, end_offset, [&](const Coordinates & id) + { + ARM_COMPUTE_ASSERT(i < expected.size()); + ARM_COMPUTE_EXPECT_EQUAL(expected_row_size[i], row_size, framework::LogLevel::ERRORS); + Coordinates expected_coords = expected[i++]; + //Set number of dimensions to the maximum (To match the number of dimensions used by the id passed to the lambda function) + expected_coords.set_num_dimensions(Coordinates::num_max_dimensions); + ARM_COMPUTE_EXPECT_EQUAL(id, expected_coords, framework::LogLevel::ERRORS); + }); + window_iterator.iterate_3D([&](int start, int end) + { + ARM_COMPUTE_EXPECT(start >= window[0].start(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(end <= window[0].end(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(end > start, framework::LogLevel::ERRORS); + row_size = end - start; + }); + ARM_COMPUTE_EXPECT_EQUAL(i, expected.size(), framework::LogLevel::ERRORS); +} + +TEST_SUITE_END() +TEST_SUITE_END() diff --git a/utils/TypePrinter.h b/utils/TypePrinter.h index 49c07938bd..9bd8c2a667 100644 --- a/utils/TypePrinter.h +++ b/utils/TypePrinter.h @@ -60,7 +60,6 @@ std::string to_string_if_not_null(T *arg) return to_string(*arg); } } - /** Formatted output of the Dimensions type. * * @param[out] os Output stream. @@ -1627,6 +1626,48 @@ inline std::string to_string(const Termination &termination) return str.str(); } +/** Formatted output of a vector of objects. + * + * @param[out] os Output stream + * @param[in] args Vector of objects to print + * + * @return Modified output stream. + */ +template +inline ::std::ostream &operator<<(::std::ostream &os, const std::vector &args) +{ + os << "["; + bool first = true; + for(auto &arg : args) + { + if(first) + { + first = false; + } + else + { + os << ", "; + } + os << arg; + } + os << "]"; + return os; +} + +/** Formatted output of a vector of objects. + * + * @param[in] args Vector of objects to print + * + * @return String representing args. + */ +template +std::string to_string(const std::vector &args) +{ + std::stringstream str; + str << args; + return str.str(); +} + /** Formatted output of the WinogradInfo type. */ inline ::std::ostream &operator<<(::std::ostream &os, const WinogradInfo &info) { @@ -1644,5 +1685,18 @@ inline std::string to_string(const WinogradInfo &type) str << type; return str.str(); } + +/** Fallback method: try to use std::to_string: + * + * @param[in] val Value to convert to string + * + * @return String representing val. + */ +template +inline std::string to_string(const T &val) +{ + return support::cpp11::to_string(val); +} + } // namespace arm_compute #endif /* __ARM_COMPUTE_TEST_TYPE_PRINTER_H__ */ -- cgit v1.2.1