xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/reference/ChannelShuffle.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2020 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 
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 namespace reference
36 {
37 // Refence implementation for channel shuffle taken from https://github.com/pytorch/pytorch/blob/master/caffe2/operators/channel_shuffle_op.h
38 template <typename T>
channel_shuffle(const SimpleTensor<T> & src,int num_groups)39 SimpleTensor<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.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 #if defined(_OPENMP)
54     #pragma omp parallel for collapse(2)
55 #endif /* _OPENMP */
56     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.
62             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;
64             for(int i = 0; i < channels_in_group; ++i)
65             {
66                 std::copy(src_ptr + i * MxN,
67                           src_ptr + (i + 1) * MxN,
68                           dst_ptr + i * num_groups * MxN);
69             }
70         }
71     }
72 
73     return dst;
74 }
75 
76 template SimpleTensor<uint8_t> channel_shuffle(const SimpleTensor<uint8_t> &src, int num_groups);
77 template SimpleTensor<uint16_t> channel_shuffle(const SimpleTensor<uint16_t> &src, int num_groups);
78 template SimpleTensor<uint32_t> channel_shuffle(const SimpleTensor<uint32_t> &src, int num_groups);
79 template SimpleTensor<half> channel_shuffle(const SimpleTensor<half> &src, int num_groups);
80 template SimpleTensor<float> channel_shuffle(const SimpleTensor<float> &src, int num_groups);
81 } // namespace reference
82 } // namespace validation
83 } // namespace test
84 } // namespace arm_compute
85