blob: 9f087d06cb88f8f6c9301336da29462db79af81e [file] [log] [blame]
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +01001/*
Michalis Spyroud1d77222020-04-08 14:10:15 +01002 * Copyright (c) 2018-2020 ARM Limited.
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +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 "ReorgLayer.h"
25
26#include "arm_compute/core/Types.h"
Gian Marco Iodice477531c2018-08-21 17:53:38 +010027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010028
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010037template <typename T>
38SimpleTensor<T> reorg_layer(const SimpleTensor<T> &src, int32_t stride)
39{
Gian Marco Iodice477531c2018-08-21 17:53:38 +010040 ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4);
41 ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NCHW);
42
43 TensorInfo input_info(src.shape(), 1, src.data_type());
44 const TensorShape output_shape = misc::shape_calculator::compute_reorg_output_shape(input_info, stride);
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010045
46 // Create destination tensor
Gian Marco Iodice477531c2018-08-21 17:53:38 +010047 SimpleTensor<T> dst{ output_shape, src.data_type() };
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010048
49 const unsigned int W = dst.shape().x();
50 const unsigned int H = dst.shape().y();
51 const unsigned int C = dst.shape().z();
52 const unsigned int out_c = C / (stride * stride);
53 const unsigned int outer_dims = dst.shape().total_size() / (W * H * C);
54
55 // Calculate layer reorg in NCHW
56 Coordinates map_coords;
Michalis Spyroud1d77222020-04-08 14:10:15 +010057
58#if defined(_OPENMP)
59 #pragma omp parallel for private(map_coords)
60#endif /* _OPENMP */
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010061 for(unsigned int b = 0; b < outer_dims; ++b)
62 {
63 map_coords.set(3, b);
64 for(unsigned int c = 0; c < C; ++c)
65 {
66 map_coords.set(2, c % out_c);
67 const unsigned int offset = c / out_c;
68 for(unsigned int h = 0; h < H; ++h)
69 {
70 map_coords.set(1, h * stride + offset / stride);
71 for(unsigned int w = 0; w < W; ++w)
72 {
73 const unsigned int dst_idx = w + W * (h + H * (c + C * b));
74 map_coords.set(0, w * stride + offset % stride);
75 dst[dst_idx] = *reinterpret_cast<const T *>(src(map_coords));
76 }
77 }
78 }
79 }
80
81 return dst;
82}
83
Gian Marco Iodice477531c2018-08-21 17:53:38 +010084template SimpleTensor<int32_t> reorg_layer(const SimpleTensor<int32_t> &src, int32_t stride);
85template SimpleTensor<int16_t> reorg_layer(const SimpleTensor<int16_t> &src, int32_t stride);
86template SimpleTensor<int8_t> reorg_layer(const SimpleTensor<int8_t> &src, int32_t stride);
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010087} // namespace reference
88} // namespace validation
89} // namespace test
90} // namespace arm_compute