Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +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 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLReshapeLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/CL/ICLTensor.h" |
| 31 | #include "arm_compute/core/CL/OpenCL.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/IAccessWindow.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Utils.h" |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | ab23dd0 | 2020-07-06 14:57:36 +0100 | [diff] [blame] | 37 | #include "arm_compute/core/utils/misc/Cast.h" |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 38 | |
| 39 | #include <string> |
| 40 | |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 41 | /** [CLReshapeLayerKernel Kernel] **/ |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 42 | namespace arm_compute |
| 43 | { |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 44 | namespace |
| 45 | { |
| 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
| 47 | { |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Sang-Hoon Park | 0779fec | 2019-11-13 17:08:12 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 51 | |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size() != output->tensor_shape().total_size()); |
| 55 | |
| 56 | return Status{}; |
| 57 | } |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 58 | } // namespace |
| 59 | |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 60 | void CLReshapeLayerKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 61 | { |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 63 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output)); |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 64 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 65 | // Create kernel |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 66 | std::set<std::string> build_opts = { "-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->element_size()) }; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 67 | _kernel = create_kernel(compile_context, "reshape_layer", build_opts); |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 68 | |
| 69 | // Add static arguments |
| 70 | const cl_int2 input_shape = |
| 71 | { |
| 72 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 73 | static_cast<cl_int>(input->tensor_shape()[0]), |
| 74 | static_cast<cl_int>(input->tensor_shape()[1]) |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 75 | } |
| 76 | }; |
| 77 | const cl_int2 output_shape = |
| 78 | { |
| 79 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 80 | static_cast<cl_int>(output->tensor_shape()[0]), |
| 81 | static_cast<cl_int>(output->tensor_shape()[1]) |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters |
| 85 | _kernel.setArg<cl_int2>(idx++, input_shape); |
| 86 | _kernel.setArg<cl_int2>(idx++, output_shape); |
| 87 | |
| 88 | // Configure kernel window |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 89 | Window win = calculate_max_window(*input); |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 90 | |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 91 | // Set the output valid region |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 92 | output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 93 | ICLKernel::configure_internal(win); |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 96 | Status CLReshapeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 97 | { |
| 98 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 99 | |
| 100 | return Status{}; |
| 101 | } |
| 102 | |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 103 | void CLReshapeLayerKernel::run_op(const InputTensorMap &inputs, const OutputTensorMap &outputs, const Window &window, cl::CommandQueue &queue) |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 104 | { |
| 105 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 106 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 107 | |
| 108 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 109 | Window slice = window_collapsed.first_slice_window_3D(); |
| 110 | |
Georgios Pinitas | ab23dd0 | 2020-07-06 14:57:36 +0100 | [diff] [blame] | 111 | const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(inputs.at(TensorType::ACL_SRC)); |
| 112 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(outputs.at(TensorType::ACL_DST)); |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 113 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 114 | // Set inputs |
| 115 | unsigned int idx = 0; |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 116 | add_3D_tensor_argument(idx, src, window_collapsed); |
| 117 | add_3D_tensor_argument(idx, dst, window_collapsed); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 118 | enqueue(queue, *this, slice, lws_hint()); |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 119 | } |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 120 | } // namespace arm_compute |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 121 | /** [CLReshapeLayerKernel Kernel] **/ |