blob: 790eff53a0072c7c50ae798e660c5e620911e5d3 [file] [log] [blame]
Michele Di Giorgio72175632018-05-01 16:52:00 +01001/*
2 * Copyright (c) 2018 ARM Limited.
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 "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
42 SimpleTensor<T> dst{ src.shape(), src.data_type(), src.num_channels(), src.fixed_point_position(), src.quantization_info() };
43
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();
53
54 for(int n = 0; n < batches; ++n)
55 {
56 for(int g = 0; g < num_groups; ++g)
57 {
58 // Gather the group g block (of size channels_in_group * MxN) from output channels
59 // g + 0 * G, g + 1 * G, g + 2 * G, g + G * (K - 1) etc.
60 const T *src_ptr = src_ref + g * MxN + n * num_channels * MxN;
61 T *dst_ptr = dst_ref + g * channels_in_group * MxN + n * num_channels * MxN;
62 for(int i = 0; i < channels_in_group; ++i)
63 {
64 std::copy(src_ptr + i * num_groups * MxN,
65 src_ptr + (i * num_groups + 1) * MxN,
66 dst_ptr + i * MxN);
67 }
68 }
69 }
70
71 return dst;
72}
73
74template SimpleTensor<uint8_t> channel_shuffle(const SimpleTensor<uint8_t> &src, int num_groups);
75template SimpleTensor<uint16_t> channel_shuffle(const SimpleTensor<uint16_t> &src, int num_groups);
76template SimpleTensor<uint32_t> channel_shuffle(const SimpleTensor<uint32_t> &src, int num_groups);
77template SimpleTensor<half> channel_shuffle(const SimpleTensor<half> &src, int num_groups);
78template SimpleTensor<float> channel_shuffle(const SimpleTensor<float> &src, int num_groups);
79} // namespace reference
80} // namespace validation
81} // namespace test
82} // namespace arm_compute