Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [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 "PadLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Types.h" |
| 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 28 | #include "tests/validation/Helpers.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace validation |
| 35 | { |
| 36 | namespace reference |
| 37 | { |
| 38 | template <typename T> |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 39 | SimpleTensor<T> pad_layer(const SimpleTensor<T> &src, const PaddingList &paddings, const PixelValue const_value, const PaddingMode mode) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 40 | { |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 41 | const DataType dst_data_type = src.data_type(); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 42 | |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 43 | const TensorShape orig_shape = src.shape(); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 44 | |
| 45 | std::vector<PaddingInfo> paddings_extended = paddings; |
| 46 | |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 47 | for(size_t i = paddings.size(); i < TensorShape::num_max_dimensions; ++i) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 48 | { |
| 49 | paddings_extended.emplace_back(PaddingInfo{ 0, 0 }); |
| 50 | } |
| 51 | |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 52 | const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(orig_shape, paddings); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 53 | |
| 54 | SimpleTensor<T> dst(padded_shape, dst_data_type); |
| 55 | |
| 56 | // Reference algorithm: loop over the different dimension of the input. |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 57 | const uint32_t num_elements = dst.num_elements(); |
| 58 | for(uint32_t idx = 0; idx < num_elements; ++idx) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 59 | { |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 60 | const Coordinates coord = index2coord(padded_shape, idx); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 61 | |
| 62 | const size_t i = coord.x(); |
| 63 | const size_t j = coord.y(); |
| 64 | const size_t k = coord.z(); |
| 65 | const size_t l = coord[3]; |
| 66 | const size_t m = coord[4]; |
| 67 | const size_t n = coord[5]; |
| 68 | |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 69 | const std::array<size_t, TensorShape::num_max_dimensions> dims = { { 0, 1, 2, 3, 4, 5 } }; |
| 70 | const std::array<size_t, TensorShape::num_max_dimensions> coords = { { i, j, k, l, m, n } }; |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 71 | auto is_padding_area = [&](size_t i) |
| 72 | { |
| 73 | return (coords[i] < paddings_extended[i].first || coords[i] > orig_shape[i] + paddings_extended[i].first - 1); |
| 74 | }; |
| 75 | |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 76 | auto orig_coord_reflect = [&](size_t i) |
| 77 | { |
| 78 | if(is_padding_area(i)) |
| 79 | { |
| 80 | if(coords[i] < paddings_extended[i].first) |
| 81 | { |
| 82 | return paddings_extended[i].first - coords[i]; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | return 2 * orig_shape[i] + paddings_extended[i].first - 2 - coords[i]; |
| 87 | } |
| 88 | } |
| 89 | return coords[i] - paddings_extended[i].first; |
| 90 | }; |
| 91 | |
| 92 | auto orig_coord_symm = [&](size_t i) |
| 93 | { |
| 94 | if(is_padding_area(i)) |
| 95 | { |
| 96 | if(coords[i] < paddings_extended[i].first) |
| 97 | { |
| 98 | return paddings_extended[i].first - coords[i] - 1; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | return 2 * orig_shape[i] + paddings_extended[i].first - 1 - coords[i]; |
| 103 | } |
| 104 | } |
| 105 | return coords[i] - paddings_extended[i].first; |
| 106 | }; |
| 107 | |
| 108 | // If the tuple [i,j,k,l,m] is in the padding area, then simply set the value |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 109 | if(std::any_of(dims.begin(), dims.end(), is_padding_area)) |
| 110 | { |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 111 | switch(mode) |
| 112 | { |
| 113 | case PaddingMode::CONSTANT: |
| 114 | const_value.get(dst[idx]); |
| 115 | break; |
| 116 | case PaddingMode::REFLECT: |
| 117 | { |
| 118 | const Coordinates orig_coords{ orig_coord_reflect(0), |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 119 | orig_coord_reflect(1), |
| 120 | orig_coord_reflect(2), |
| 121 | orig_coord_reflect(3), |
| 122 | orig_coord_reflect(4), |
| 123 | orig_coord_reflect(5) }; |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 124 | |
| 125 | const size_t idx_src = coord2index(orig_shape, orig_coords); |
| 126 | dst[idx] = src[idx_src]; |
| 127 | break; |
| 128 | } |
| 129 | case PaddingMode::SYMMETRIC: |
| 130 | { |
| 131 | const Coordinates orig_coords{ orig_coord_symm(0), |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 132 | orig_coord_symm(1), |
| 133 | orig_coord_symm(2), |
| 134 | orig_coord_symm(3), |
| 135 | orig_coord_symm(4), |
| 136 | orig_coord_symm(5) }; |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 137 | |
| 138 | const size_t idx_src = coord2index(orig_shape, orig_coords); |
| 139 | dst[idx] = src[idx_src]; |
| 140 | break; |
| 141 | } |
| 142 | default: |
| 143 | ARM_COMPUTE_ERROR("Padding mode not supported."); |
| 144 | break; |
| 145 | } |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 146 | } |
| 147 | else |
| 148 | { |
| 149 | // If the tuple[i,j,k,l,m] is not in the padding area, then copy the input into the output |
| 150 | |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 151 | const Coordinates orig_coords |
| 152 | { |
| 153 | i - paddings_extended[0].first, |
| 154 | j - paddings_extended[1].first, |
| 155 | k - paddings_extended[2].first, |
| 156 | l - paddings_extended[3].first, |
| 157 | m - paddings_extended[4].first, |
| 158 | n - paddings_extended[5].first |
| 159 | }; |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 160 | |
| 161 | const size_t idx_src = coord2index(orig_shape, orig_coords); |
| 162 | dst[idx] = src[idx_src]; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return dst; |
| 167 | } |
| 168 | |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 169 | template SimpleTensor<float> pad_layer(const SimpleTensor<float> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode); |
| 170 | template SimpleTensor<half> pad_layer(const SimpleTensor<half> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode); |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 171 | template SimpleTensor<uint8_t> pad_layer(const SimpleTensor<uint8_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode); |
| 172 | template SimpleTensor<int8_t> pad_layer(const SimpleTensor<int8_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode); |
| 173 | template SimpleTensor<uint16_t> pad_layer(const SimpleTensor<uint16_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode); |
| 174 | template SimpleTensor<int16_t> pad_layer(const SimpleTensor<int16_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode); |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 175 | template SimpleTensor<uint32_t> pad_layer(const SimpleTensor<uint32_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode); |
| 176 | template SimpleTensor<int32_t> pad_layer(const SimpleTensor<int32_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 177 | } // namespace reference |
| 178 | } // namespace validation |
| 179 | } // namespace test |
| 180 | } // namespace arm_compute |