xref: /aosp_15_r20/external/ComputeLibrary/examples/neon_permute.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2019 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "arm_compute/runtime/NEON/NEFunctions.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/Types.h"
27*c217d954SCole Faust #include "utils/Utils.h"
28*c217d954SCole Faust 
29*c217d954SCole Faust using namespace arm_compute;
30*c217d954SCole Faust using namespace utils;
31*c217d954SCole Faust 
32*c217d954SCole Faust class NeonPermuteExample : public Example
33*c217d954SCole Faust {
34*c217d954SCole Faust public:
35*c217d954SCole Faust     NeonPermuteExample() = default;
36*c217d954SCole Faust 
do_setup(int,char **)37*c217d954SCole Faust     bool do_setup(int, char **) override
38*c217d954SCole Faust     {
39*c217d954SCole Faust         // Initialise shapes
40*c217d954SCole Faust         init_tensor(TensorShape(8U, 4U, 2U), tensor_nchw, DataType::U8, DataLayout::NCHW);
41*c217d954SCole Faust         init_tensor(TensorShape(2U, 8U, 4U), tensor_nhwc, DataType::U8, DataLayout::NHWC);
42*c217d954SCole Faust         init_tensor(TensorShape(8U, 4U, 2U), tensor_nchw_result, DataType::U8, DataLayout::NCHW);
43*c217d954SCole Faust 
44*c217d954SCole Faust         // Create the permutation vector to turn a NCHW tensor to NHWC.
45*c217d954SCole Faust         // The input tensor is NCHW, which means that the fastest changing coordinate is W=8U.
46*c217d954SCole Faust         // For permutation vectors the fastest changing coordinate is the one on the left too.
47*c217d954SCole Faust         // Each element in the permutation vector specifies a mapping from the source tensor to the destination one, thus if we
48*c217d954SCole Faust         // use 2U in the permutation vector's first element we are telling the function to move the channels to the fastest
49*c217d954SCole Faust         // changing coordinate in the destination tensor.
50*c217d954SCole Faust 
51*c217d954SCole Faust         const PermutationVector vector_nchw_to_nhwc(2U, 0U, 1U);
52*c217d954SCole Faust         permute_nhwc.configure(&tensor_nchw, &tensor_nhwc, vector_nchw_to_nhwc);
53*c217d954SCole Faust 
54*c217d954SCole Faust         // Allocate and fill tensors
55*c217d954SCole Faust         tensor_nhwc.allocator()->allocate();
56*c217d954SCole Faust         tensor_nchw.allocator()->allocate();
57*c217d954SCole Faust         fill_tensor(tensor_nchw);
58*c217d954SCole Faust 
59*c217d954SCole Faust         // Demostrate autoconfigure for the output tensor
60*c217d954SCole Faust         const PermutationVector vector_nhwc_to_nchw(1U, 2U, 0U);
61*c217d954SCole Faust         permute_nchw.configure(&tensor_nhwc, &tensor_nchw_result, vector_nhwc_to_nchw);
62*c217d954SCole Faust         tensor_nchw_result.allocator()->allocate();
63*c217d954SCole Faust 
64*c217d954SCole Faust         return true;
65*c217d954SCole Faust     }
do_run()66*c217d954SCole Faust     void do_run() override
67*c217d954SCole Faust     {
68*c217d954SCole Faust         permute_nhwc.run();
69*c217d954SCole Faust         permute_nchw.run();
70*c217d954SCole Faust     }
do_teardown()71*c217d954SCole Faust     void do_teardown() override
72*c217d954SCole Faust     {
73*c217d954SCole Faust #if ARM_COMPUTE_DEBUG_ENABLED
74*c217d954SCole Faust         std::cout << "Tensor NCHW" << std::endl;
75*c217d954SCole Faust         tensor_nchw.print(std::cout);
76*c217d954SCole Faust         std::cout << "Tensor NHWC" << std::endl;
77*c217d954SCole Faust         tensor_nhwc.print(std::cout);
78*c217d954SCole Faust #endif // ARM_COMPUTE_DEBUG_ENABLED
79*c217d954SCole Faust     }
80*c217d954SCole Faust 
81*c217d954SCole Faust private:
validate_result(const Tensor & reference,const Tensor & result)82*c217d954SCole Faust     void validate_result(const Tensor &reference, const Tensor &result)
83*c217d954SCole Faust     {
84*c217d954SCole Faust         Window window;
85*c217d954SCole Faust         window.use_tensor_dimensions(reference.info()->tensor_shape());
86*c217d954SCole Faust         Iterator ref_it(&reference, window);
87*c217d954SCole Faust         Iterator res_it(&result, window);
88*c217d954SCole Faust         execute_window_loop(window, [&](const Coordinates &)
89*c217d954SCole Faust         {
90*c217d954SCole Faust             assert(*reinterpret_cast<unsigned char *>(ref_it.ptr()) == *reinterpret_cast<unsigned char *>(res_it.ptr()));
91*c217d954SCole Faust         },
92*c217d954SCole Faust         ref_it, res_it);
93*c217d954SCole Faust     }
94*c217d954SCole Faust 
fill_tensor(Tensor & tensor)95*c217d954SCole Faust     void fill_tensor(Tensor &tensor)
96*c217d954SCole Faust     {
97*c217d954SCole Faust         Window window;
98*c217d954SCole Faust         window.use_tensor_dimensions(tensor.info()->tensor_shape());
99*c217d954SCole Faust         Iterator      tensor_it(&tensor, window);
100*c217d954SCole Faust         unsigned char val(0);
101*c217d954SCole Faust         execute_window_loop(window, [&](const Coordinates &)
102*c217d954SCole Faust         {
103*c217d954SCole Faust             *reinterpret_cast<unsigned char *>(tensor_it.ptr()) = val++;
104*c217d954SCole Faust         },
105*c217d954SCole Faust         tensor_it);
106*c217d954SCole Faust     }
init_tensor(const TensorShape shape,Tensor & tensor,DataType type,DataLayout layout)107*c217d954SCole Faust     void init_tensor(const TensorShape shape, Tensor &tensor, DataType type, DataLayout layout)
108*c217d954SCole Faust     {
109*c217d954SCole Faust         tensor.allocator()->init(TensorInfo(shape, 1, type).set_data_layout(layout));
110*c217d954SCole Faust     }
111*c217d954SCole Faust 
112*c217d954SCole Faust     Tensor    tensor_nchw{};
113*c217d954SCole Faust     Tensor    tensor_nhwc{};
114*c217d954SCole Faust     Tensor    tensor_nchw_result{};
115*c217d954SCole Faust     NEPermute permute_nhwc{};
116*c217d954SCole Faust     NEPermute permute_nchw{};
117*c217d954SCole Faust };
118*c217d954SCole Faust 
119*c217d954SCole Faust /** Main program that instantiates a permute function example.
120*c217d954SCole Faust  *
121*c217d954SCole Faust  * @param[in] argc Number of arguments
122*c217d954SCole Faust  * @param[in] argv Arguments
123*c217d954SCole Faust  */
main(int argc,char ** argv)124*c217d954SCole Faust int main(int argc, char **argv)
125*c217d954SCole Faust {
126*c217d954SCole Faust     return utils::run_example<NeonPermuteExample>(argc, argv);
127*c217d954SCole Faust }
128