Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | afae756 | 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> |
| 39 | SimpleTensor<T> pad_layer(const SimpleTensor<T> &src, const PaddingList &paddings) |
| 40 | { |
| 41 | DataType dst_data_type = src.data_type(); |
| 42 | |
| 43 | TensorShape orig_shape = src.shape(); |
| 44 | |
| 45 | std::vector<PaddingInfo> paddings_extended = paddings; |
| 46 | |
| 47 | for(size_t i = paddings.size(); i < TensorShape::num_max_dimensions; i++) |
| 48 | { |
| 49 | paddings_extended.emplace_back(PaddingInfo{ 0, 0 }); |
| 50 | } |
| 51 | |
| 52 | TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(orig_shape, paddings); |
| 53 | |
| 54 | SimpleTensor<T> dst(padded_shape, dst_data_type); |
| 55 | |
| 56 | // Reference algorithm: loop over the different dimension of the input. |
| 57 | for(int idx = 0; idx < dst.num_elements(); idx++) |
| 58 | { |
| 59 | Coordinates coord = index2coord(padded_shape, idx); |
| 60 | |
| 61 | const size_t i = coord.x(); |
| 62 | const size_t j = coord.y(); |
| 63 | const size_t k = coord.z(); |
| 64 | const size_t l = coord[3]; |
| 65 | const size_t m = coord[4]; |
| 66 | const size_t n = coord[5]; |
| 67 | |
Georgios Pinitas | afae756 | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 68 | std::array<size_t, TensorShape::num_max_dimensions> dims = { { 0, 1, 2, 3, 4, 5 } }; |
| 69 | 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] | 70 | auto is_padding_area = [&](size_t i) |
| 71 | { |
| 72 | return (coords[i] < paddings_extended[i].first || coords[i] > orig_shape[i] + paddings_extended[i].first - 1); |
| 73 | }; |
| 74 | |
| 75 | // If the tuple [i,j,k,l,m] is in the padding area, then seimply set the value |
| 76 | if(std::any_of(dims.begin(), dims.end(), is_padding_area)) |
| 77 | { |
| 78 | dst[idx] = T(0); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | // If the tuple[i,j,k,l,m] is not in the padding area, then copy the input into the output |
| 83 | |
| 84 | Coordinates orig_coords{ i - paddings_extended[0].first, |
| 85 | j - paddings_extended[1].first, |
| 86 | k - paddings_extended[2].first, |
| 87 | l - paddings_extended[3].first, |
| 88 | m - paddings_extended[4].first, |
| 89 | n - paddings_extended[5].first }; |
| 90 | |
| 91 | const size_t idx_src = coord2index(orig_shape, orig_coords); |
| 92 | dst[idx] = src[idx_src]; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return dst; |
| 97 | } |
| 98 | |
| 99 | template SimpleTensor<float> pad_layer(const SimpleTensor<float> &src, const PaddingList &paddings); |
| 100 | template SimpleTensor<half> pad_layer(const SimpleTensor<half> &src, const PaddingList &paddings); |
| 101 | template SimpleTensor<uint32_t> pad_layer(const SimpleTensor<uint32_t> &src, const PaddingList &paddings); |
| 102 | template SimpleTensor<uint8_t> pad_layer(const SimpleTensor<uint8_t> &src, const PaddingList &paddings); |
| 103 | template SimpleTensor<int8_t> pad_layer(const SimpleTensor<int8_t> &src, const PaddingList &paddings); |
| 104 | template SimpleTensor<uint16_t> pad_layer(const SimpleTensor<uint16_t> &src, const PaddingList &paddings); |
| 105 | template SimpleTensor<int16_t> pad_layer(const SimpleTensor<int16_t> &src, const PaddingList &paddings); |
| 106 | } // namespace reference |
| 107 | } // namespace validation |
| 108 | } // namespace test |
| 109 | } // namespace arm_compute |