1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2019-2020 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/core/CL/OpenCL.h"
25*c217d954SCole Faust #include "arm_compute/core/Types.h"
26*c217d954SCole Faust #include "arm_compute/runtime/CL/CLHelpers.h"
27*c217d954SCole Faust #include "arm_compute/runtime/CL/CLScheduler.h"
28*c217d954SCole Faust #include "arm_compute/runtime/CL/Utils.h"
29*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLPermute.h"
30*c217d954SCole Faust #include "utils/Utils.h"
31*c217d954SCole Faust
32*c217d954SCole Faust using namespace arm_compute;
33*c217d954SCole Faust using namespace utils;
34*c217d954SCole Faust
35*c217d954SCole Faust namespace
36*c217d954SCole Faust {
37*c217d954SCole Faust } // namespace
38*c217d954SCole Faust
39*c217d954SCole Faust class CLCacheExample : public Example
40*c217d954SCole Faust {
41*c217d954SCole Faust public:
42*c217d954SCole Faust CLCacheExample() = default;
43*c217d954SCole Faust
do_setup(int argc,char ** argv)44*c217d954SCole Faust bool do_setup(int argc, char **argv) override
45*c217d954SCole Faust {
46*c217d954SCole Faust std::cout << "Once the program has run and created the file cache.bin, rerun with --restore_cache." << std::endl;
47*c217d954SCole Faust CLScheduler::get().default_init();
48*c217d954SCole Faust
49*c217d954SCole Faust if(argc > 1)
50*c217d954SCole Faust {
51*c217d954SCole Faust std::string argv1 = argv[1];
52*c217d954SCole Faust std::transform(argv1.begin(), argv1.end(), argv1.begin(), ::tolower);
53*c217d954SCole Faust if(argv1 == "--restore_cache")
54*c217d954SCole Faust {
55*c217d954SCole Faust // Load the precompiled kernels from a file into the kernel library, in this way the next time they are needed
56*c217d954SCole Faust // compilation won't be required.
57*c217d954SCole Faust restore_program_cache_from_file();
58*c217d954SCole Faust }
59*c217d954SCole Faust else
60*c217d954SCole Faust {
61*c217d954SCole Faust std::cout << "Unkown option " << argv1 << std::endl;
62*c217d954SCole Faust }
63*c217d954SCole Faust }
64*c217d954SCole Faust
65*c217d954SCole Faust // Initialise shapes
66*c217d954SCole Faust init_tensor(TensorShape(8U, 4U, 2U), tensor_nchw, DataType::U8, DataLayout::NCHW);
67*c217d954SCole Faust init_tensor(TensorShape(2U, 8U, 4U), tensor_nhwc, DataType::U8, DataLayout::NHWC);
68*c217d954SCole Faust init_tensor(TensorShape(8U, 4U, 2U), tensor_nchw_result, DataType::U8, DataLayout::NCHW);
69*c217d954SCole Faust
70*c217d954SCole Faust // Create the permutation vector to turn a NCHW tensor to NHWC.
71*c217d954SCole Faust // The input tensor is NCHW, which means that the fastest changing coordinate is W=8U.
72*c217d954SCole Faust // For permutation vectors the fastest changing coordinate is the one on the left too.
73*c217d954SCole Faust // Each element in the permutation vector specifies a mapping from the source tensor to the destination one, thus if we
74*c217d954SCole Faust // use 2U in the permutation vector's first element we are telling the function to move the channels to the fastest
75*c217d954SCole Faust // changing coordinate in the destination tensor.
76*c217d954SCole Faust
77*c217d954SCole Faust const PermutationVector vector_nchw_to_nhwc(2U, 0U, 1U);
78*c217d954SCole Faust permute_nhwc.configure(&tensor_nchw, &tensor_nhwc, vector_nchw_to_nhwc);
79*c217d954SCole Faust
80*c217d954SCole Faust // Allocate and fill tensors
81*c217d954SCole Faust tensor_nhwc.allocator()->allocate();
82*c217d954SCole Faust tensor_nchw.allocator()->allocate();
83*c217d954SCole Faust fill_tensor(tensor_nchw);
84*c217d954SCole Faust
85*c217d954SCole Faust // Demostrate autoconfigure for the output tensor
86*c217d954SCole Faust const PermutationVector vector_nhwc_to_nchw(1U, 2U, 0U);
87*c217d954SCole Faust permute_nchw.configure(&tensor_nhwc, &tensor_nchw_result, vector_nhwc_to_nchw);
88*c217d954SCole Faust tensor_nchw_result.allocator()->allocate();
89*c217d954SCole Faust
90*c217d954SCole Faust // Save the opencl kernels to a file
91*c217d954SCole Faust save_program_cache_to_file();
92*c217d954SCole Faust
93*c217d954SCole Faust return true;
94*c217d954SCole Faust }
do_run()95*c217d954SCole Faust void do_run() override
96*c217d954SCole Faust {
97*c217d954SCole Faust permute_nhwc.run();
98*c217d954SCole Faust permute_nchw.run();
99*c217d954SCole Faust }
do_teardown()100*c217d954SCole Faust void do_teardown() override
101*c217d954SCole Faust {
102*c217d954SCole Faust }
103*c217d954SCole Faust
104*c217d954SCole Faust private:
validate_result(CLTensor & reference,CLTensor & result)105*c217d954SCole Faust void validate_result(CLTensor &reference, CLTensor &result)
106*c217d954SCole Faust {
107*c217d954SCole Faust reference.map();
108*c217d954SCole Faust result.map();
109*c217d954SCole Faust Window window;
110*c217d954SCole Faust window.use_tensor_dimensions(reference.info()->tensor_shape());
111*c217d954SCole Faust Iterator it_ref(&reference, window);
112*c217d954SCole Faust Iterator it_res(&result, window);
113*c217d954SCole Faust execute_window_loop(window, [&](const Coordinates &)
114*c217d954SCole Faust {
115*c217d954SCole Faust assert(*reinterpret_cast<unsigned char *>(it_ref.ptr()) == *reinterpret_cast<unsigned char *>(it_res.ptr()));
116*c217d954SCole Faust },
117*c217d954SCole Faust it_ref, it_res);
118*c217d954SCole Faust reference.unmap();
119*c217d954SCole Faust result.unmap();
120*c217d954SCole Faust }
121*c217d954SCole Faust
fill_tensor(CLTensor & tensor)122*c217d954SCole Faust void fill_tensor(CLTensor &tensor)
123*c217d954SCole Faust {
124*c217d954SCole Faust tensor.map();
125*c217d954SCole Faust Window window;
126*c217d954SCole Faust window.use_tensor_dimensions(tensor.info()->tensor_shape());
127*c217d954SCole Faust Iterator it_tensor(&tensor, window);
128*c217d954SCole Faust unsigned char val(0);
129*c217d954SCole Faust execute_window_loop(window, [&](const Coordinates &)
130*c217d954SCole Faust {
131*c217d954SCole Faust *reinterpret_cast<unsigned char *>(it_tensor.ptr()) = val++;
132*c217d954SCole Faust },
133*c217d954SCole Faust it_tensor);
134*c217d954SCole Faust tensor.unmap();
135*c217d954SCole Faust }
init_tensor(const TensorShape shape,CLTensor & tensor,DataType type,DataLayout layout)136*c217d954SCole Faust void init_tensor(const TensorShape shape, CLTensor &tensor, DataType type, DataLayout layout)
137*c217d954SCole Faust {
138*c217d954SCole Faust tensor.allocator()->init(TensorInfo(shape, 1, type).set_data_layout(layout));
139*c217d954SCole Faust }
140*c217d954SCole Faust
141*c217d954SCole Faust CLTensor tensor_nchw{};
142*c217d954SCole Faust CLTensor tensor_nhwc{};
143*c217d954SCole Faust CLTensor tensor_nchw_result{};
144*c217d954SCole Faust CLPermute permute_nhwc{};
145*c217d954SCole Faust CLPermute permute_nchw{};
146*c217d954SCole Faust };
147*c217d954SCole Faust
148*c217d954SCole Faust /** Main program creating an example that demostrates how to load precompiled kernels from a file.
149*c217d954SCole Faust *
150*c217d954SCole Faust * @param[in] argc Number of arguments
151*c217d954SCole Faust * @param[in] argv Arguments
152*c217d954SCole Faust */
main(int argc,char ** argv)153*c217d954SCole Faust int main(int argc, char **argv)
154*c217d954SCole Faust {
155*c217d954SCole Faust return utils::run_example<CLCacheExample>(argc, argv);
156*c217d954SCole Faust }
157