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