Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 1 | /* |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TEST_CL_HELPER_H |
| 25 | #define ARM_COMPUTE_TEST_CL_HELPER_H |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 26 | |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/ICLSimpleFunction.h" |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/functions/CLFill.h" |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/IFunction.h" |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 31 | #include "src/core/CL/kernels/CLFillBorderKernel.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 32 | |
| 33 | #include "src/core/CL/ICLKernel.h" |
| 34 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 35 | #include <memory> |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 41 | /** This template synthetizes an ICLSimpleFunction which runs the given kernel K */ |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 42 | template <typename K> |
| 43 | class CLSynthetizeFunction : public ICLSimpleFunction |
| 44 | { |
| 45 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 46 | /** Configure the kernel. |
| 47 | * |
| 48 | * @param[in] args Configuration arguments. |
| 49 | */ |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 50 | template <typename... Args> |
| 51 | void configure(Args &&... args) |
| 52 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 53 | auto k = std::make_unique<K>(); |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 54 | k->configure(std::forward<Args>(args)...); |
| 55 | _kernel = std::move(k); |
| 56 | } |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 57 | /** Configure the kernel setting the GPU target as well |
| 58 | * |
| 59 | * @param[in] gpu_target GPUTarget to set |
| 60 | * @param[in] args Configuration arguments. |
| 61 | */ |
| 62 | template <typename... Args> |
| 63 | void configure(GPUTarget gpu_target, Args &&... args) |
| 64 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 65 | auto k = std::make_unique<K>(); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 66 | k->set_target(gpu_target); |
| 67 | k->configure(std::forward<Args>(args)...); |
| 68 | _kernel = std::move(k); |
| 69 | } |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 70 | /** Validate input arguments |
| 71 | * |
| 72 | * @param[in] args Configuration arguments. |
| 73 | */ |
| 74 | template <typename... Args> |
| 75 | static Status validate(Args &&... args) |
| 76 | { |
| 77 | return K::validate(std::forward<Args>(args)...); |
| 78 | } |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 81 | /** As above but this also setups a Zero border on the input tensor of the specified bordersize */ |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 82 | template <typename K, int bordersize> |
| 83 | class CLSynthetizeFunctionWithZeroConstantBorder : public ICLSimpleFunction |
| 84 | { |
| 85 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 86 | /** Configure the kernel. |
| 87 | * |
| 88 | * @param[in] first First configuration argument. |
| 89 | * @param[in] args Rest of the configuration arguments. |
| 90 | */ |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 91 | template <typename T, typename... Args> |
| 92 | void configure(T first, Args &&... args) |
| 93 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 94 | auto k = std::make_unique<K>(); |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 95 | k->configure(first, std::forward<Args>(args)...); |
| 96 | _kernel = std::move(k); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 97 | _border_handler->configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue()); |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 98 | } |
| 99 | }; |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 100 | |
| 101 | /** As above but this also initializes to zero the input tensor */ |
| 102 | template <typename K, int bordersize> |
| 103 | class CLSynthetizeFunctionInitOutputWithZeroAndWithZeroConstantBorder : public IFunction |
| 104 | { |
| 105 | public: |
| 106 | /** Configure the kernel. |
| 107 | * |
| 108 | * @param[in] first First input argument. |
| 109 | * @param[in] second Second input argument. |
| 110 | * @param[in] args Rest of the configuration arguments. |
| 111 | */ |
| 112 | template <typename T, typename... Args> |
| 113 | void configure(T first, T second, Args &&... args) |
| 114 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 115 | auto k = std::make_unique<K>(); |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 116 | k->set_target(CLScheduler::get().target()); |
| 117 | k->configure(first, second, std::forward<Args>(args)...); |
| 118 | _kernel = std::move(k); |
Manuel Bottini | 55e1678 | 2019-01-15 13:21:57 +0000 | [diff] [blame] | 119 | _border_handler.configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue()); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 120 | _fill.configure(second, PixelValue()); |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Inherited method overridden: |
| 124 | void run() override final |
| 125 | { |
| 126 | ARM_COMPUTE_ERROR_ON_MSG(!_kernel, "The CL kernel or function isn't configured"); |
| 127 | |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 128 | _fill.run(); |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 129 | CLScheduler::get().enqueue(_border_handler, false); |
| 130 | CLScheduler::get().enqueue(*_kernel); |
| 131 | } |
| 132 | |
| 133 | private: |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 134 | CLFill _fill{}; /**< Kernel to initialize the tensor */ |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 135 | CLFillBorderKernel _border_handler{}; /**< Kernel to handle borders */ |
| 136 | std::unique_ptr<ICLKernel> _kernel{}; /**< Kernel to run */ |
| 137 | }; |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 138 | } // namespace test |
| 139 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 140 | #endif /* ARM_COMPUTE_TEST_CL_HELPER_H */ |