blob: 89ef080030bee7c92838b9a288bf68964dba2227 [file] [log] [blame]
Michele Di Giorgio72175632018-05-01 16:52:00 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Michele Di Giorgio72175632018-05-01 16:52:00 +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 "ChannelShuffle.h"
25
26#include "arm_compute/core/Types.h"
27#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37// Refence implementation for channel shuffle taken from https://github.com/pytorch/pytorch/blob/master/caffe2/operators/channel_shuffle_op.h
38template <typename T>
39SimpleTensor<T> channel_shuffle(const SimpleTensor<T> &src, int num_groups)
40{
41 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010042 SimpleTensor<T> dst{ src.shape(), src.data_type(), src.num_channels(), src.quantization_info() };
Michele Di Giorgio72175632018-05-01 16:52:00 +010043
44 const int M = src.shape()[0];
45 const int N = src.shape()[1];
46 const int num_channels = src.shape()[2];
47 const int batches = src.shape()[3];
48 const int MxN = M * N;
49 const int channels_in_group = num_channels / num_groups;
50
51 const T *src_ref = src.data();
52 T *dst_ref = dst.data();
Michalis Spyroud1d77222020-04-08 14:10:15 +010053#if defined(_OPENMP)
54 #pragma omp parallel for collapse(2)
55#endif /* _OPENMP */
Michele Di Giorgio72175632018-05-01 16:52:00 +010056 for(int n = 0; n < batches; ++n)
57 {
58 for(int g = 0; g < num_groups; ++g)
59 {
60 // Gather the group g block (of size channels_in_group * MxN) from output channels
61 // g + 0 * G, g + 1 * G, g + 2 * G, g + G * (K - 1) etc.
Michele Di Giorgioefac7c62018-05-16 00:02:35 +010062 const T *src_ptr = src_ref + g * channels_in_group * MxN + n * num_channels * MxN;
63 T *dst_ptr = dst_ref + g * MxN + n * num_channels * MxN;
Michele Di Giorgio72175632018-05-01 16:52:00 +010064 for(int i = 0; i < channels_in_group; ++i)
65 {
Michele Di Giorgioefac7c62018-05-16 00:02:35 +010066 std::copy(src_ptr + i * MxN,
67 src_ptr + (i + 1) * MxN,
68 dst_ptr + i * num_groups * MxN);
Michele Di Giorgio72175632018-05-01 16:52:00 +010069 }
70 }
71 }
72
73 return dst;
74}
75
76template SimpleTensor<uint8_t> channel_shuffle(const SimpleTensor<uint8_t> &src, int num_groups);
77template SimpleTensor<uint16_t> channel_shuffle(const SimpleTensor<uint16_t> &src, int num_groups);
78template SimpleTensor<uint32_t> channel_shuffle(const SimpleTensor<uint32_t> &src, int num_groups);
79template SimpleTensor<half> channel_shuffle(const SimpleTensor<half> &src, int num_groups);
80template SimpleTensor<float> channel_shuffle(const SimpleTensor<float> &src, int num_groups);
81} // namespace reference
82} // namespace validation
83} // namespace test
84} // namespace arm_compute