blob: f5ba33dd5dc565ebbcf8e88d93a862203d7a9bb0 [file] [log] [blame]
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2019 Arm Limited.
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01003 *
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
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
38template <typename T>
Usama Arif89890c62019-03-19 10:57:05 +000039SimpleTensor<T> pad_layer(const SimpleTensor<T> &src, const PaddingList &paddings, const PixelValue const_value, const PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010040{
Usama Arif89890c62019-03-19 10:57:05 +000041 const DataType dst_data_type = src.data_type();
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010042
Usama Arif89890c62019-03-19 10:57:05 +000043 const TensorShape orig_shape = src.shape();
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010044
45 std::vector<PaddingInfo> paddings_extended = paddings;
46
Usama Arif89890c62019-03-19 10:57:05 +000047 for(size_t i = paddings.size(); i < TensorShape::num_max_dimensions; ++i)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010048 {
49 paddings_extended.emplace_back(PaddingInfo{ 0, 0 });
50 }
51
Usama Arif89890c62019-03-19 10:57:05 +000052 const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(orig_shape, paddings);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010053
54 SimpleTensor<T> dst(padded_shape, dst_data_type);
55
56 // Reference algorithm: loop over the different dimension of the input.
Michalis Spyroufae513c2019-10-16 17:41:33 +010057 const uint32_t num_elements = dst.num_elements();
58 for(uint32_t idx = 0; idx < num_elements; ++idx)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010059 {
Usama Arif89890c62019-03-19 10:57:05 +000060 const Coordinates coord = index2coord(padded_shape, idx);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010061
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 Arif89890c62019-03-19 10:57:05 +000069 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 Rossinid7647d42018-07-17 18:13:13 +010071 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 Arif89890c62019-03-19 10:57:05 +000076 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 Rossinid7647d42018-07-17 18:13:13 +0100109 if(std::any_of(dims.begin(), dims.end(), is_padding_area))
110 {
Usama Arif89890c62019-03-19 10:57:05 +0000111 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 Arena205eed82019-08-14 10:13:50 +0100119 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 Arif89890c62019-03-19 10:57:05 +0000124
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 Arena205eed82019-08-14 10:13:50 +0100132 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 Arif89890c62019-03-19 10:57:05 +0000137
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 Rossinid7647d42018-07-17 18:13:13 +0100146 }
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 Arena205eed82019-08-14 10:13:50 +0100151 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 Rossinid7647d42018-07-17 18:13:13 +0100160
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 Arif89890c62019-03-19 10:57:05 +0000169template SimpleTensor<float> pad_layer(const SimpleTensor<float> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
170template SimpleTensor<half> pad_layer(const SimpleTensor<half> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
Usama Arif89890c62019-03-19 10:57:05 +0000171template SimpleTensor<uint8_t> pad_layer(const SimpleTensor<uint8_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
172template SimpleTensor<int8_t> pad_layer(const SimpleTensor<int8_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
173template SimpleTensor<uint16_t> pad_layer(const SimpleTensor<uint16_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
174template SimpleTensor<int16_t> pad_layer(const SimpleTensor<int16_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
Giorgio Arena205eed82019-08-14 10:13:50 +0100175template SimpleTensor<uint32_t> pad_layer(const SimpleTensor<uint32_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
176template SimpleTensor<int32_t> pad_layer(const SimpleTensor<int32_t> &src, const PaddingList &paddings, const PixelValue const_value = PixelValue(), const PaddingMode mode);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100177} // namespace reference
178} // namespace validation
179} // namespace test
180} // namespace arm_compute