Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 1 | /* |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 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 "StackLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Types.h" |
| 27 | |
| 28 | #include "tests/validation/Helpers.h" |
| 29 | |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | namespace reference |
| 39 | { |
| 40 | template <typename T> |
| 41 | SimpleTensor<T> stack_layer(const std::vector<SimpleTensor<T>> &in, const TensorShape &output_shape, DataType data_type, unsigned int axis) |
| 42 | { |
| 43 | ARM_COMPUTE_ERROR_ON(output_shape.num_dimensions() > 5); |
| 44 | ARM_COMPUTE_ERROR_ON(in.size() < 2); |
| 45 | ARM_COMPUTE_ERROR_ON(axis > in[0].shape().num_dimensions()); |
| 46 | |
| 47 | SimpleTensor<T> out{ output_shape, data_type }; |
| 48 | |
| 49 | const int width = in[0].shape()[0]; |
| 50 | const int height = in[0].shape()[1]; |
| 51 | const int depth = in[0].shape()[2]; |
| 52 | const int batch_size = in[0].shape()[3]; |
| 53 | const int num_tensors = in.size(); |
| 54 | |
| 55 | // Array to store the input coordinates |
| 56 | // i_coordinates[0] = xi, i_coordinates[1] = yi, i_coordinates[2] = zi |
| 57 | // i_coordinates[3] = bi, i_coordinates[4] = i, i_coordinates[5] = 0 |
| 58 | // i_coordinates[5] will be always zero and used for not incrementing the output when the input has less than 4 dimensions |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 59 | std::array<int, 6> i_coordinates{ 0 }; |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 60 | |
| 61 | // Array of pointers used to map the output coordinates to the input ones accordingly with the axis |
| 62 | // This array is initialized with &i_coordinates[5] since this will be always zero |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 63 | std::array<int *, 5> o_coordinates = { &i_coordinates[5], &i_coordinates[5], &i_coordinates[5], &i_coordinates[5], &i_coordinates[5] }; |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 64 | |
| 65 | // Set the axis coordinate |
| 66 | o_coordinates[axis] = &i_coordinates[4]; |
| 67 | |
| 68 | unsigned int k_shift = 0; |
| 69 | |
| 70 | // Map the output coordinates |
| 71 | for(unsigned int k = 0; k < in[0].shape().num_dimensions(); ++k) |
| 72 | { |
| 73 | if(k == axis) |
| 74 | { |
| 75 | k_shift++; |
| 76 | } |
| 77 | |
| 78 | o_coordinates[k + k_shift] = &i_coordinates[k]; |
| 79 | } |
| 80 | |
| 81 | // Use alias for the input coordinates |
| 82 | int &xi = i_coordinates[0]; |
| 83 | int &yi = i_coordinates[1]; |
| 84 | int &zi = i_coordinates[2]; |
| 85 | int &bi = i_coordinates[3]; |
| 86 | int &i = i_coordinates[4]; |
| 87 | |
| 88 | // Use alias for the output coordinates |
| 89 | int &xo = *(o_coordinates[0]); |
| 90 | int &yo = *(o_coordinates[1]); |
| 91 | int &zo = *(o_coordinates[2]); |
| 92 | int &bo = *(o_coordinates[3]); |
| 93 | int &wo = *(o_coordinates[4]); |
| 94 | |
| 95 | // Stack tensors |
| 96 | for(; i < num_tensors; ++(i)) |
| 97 | { |
| 98 | bi = 0; |
| 99 | for(; bi < batch_size; ++(bi)) |
| 100 | { |
| 101 | zi = 0; |
| 102 | for(; zi < depth; ++(zi)) |
| 103 | { |
| 104 | yi = 0; |
| 105 | for(; yi < height; ++(yi)) |
| 106 | { |
| 107 | xi = 0; |
| 108 | for(; xi < width; ++(xi)) |
| 109 | { |
| 110 | *(reinterpret_cast<T *>(out(Coordinates(xo, yo, zo, bo, wo)))) = *(reinterpret_cast<const T *>(in[i](Coordinates(xi, yi, zi, bi)))); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return out; |
| 118 | } |
| 119 | template SimpleTensor<int> stack_layer(const std::vector<SimpleTensor<int>> &in, const TensorShape &output_shape, DataType data_type, unsigned int axis); |
| 120 | template SimpleTensor<short> stack_layer(const std::vector<SimpleTensor<short>> &in, const TensorShape &output_shape, DataType data_type, unsigned int axis); |
| 121 | template SimpleTensor<char> stack_layer(const std::vector<SimpleTensor<char>> &in, const TensorShape &output_shape, DataType data_type, unsigned int axis); |
| 122 | } // namespace reference |
| 123 | } // namespace validation |
| 124 | } // namespace test |
| 125 | } // namespace arm_compute |