Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 1 | /* |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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" |
| 37 | |
| 38 | #include <string> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
| 45 | { |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, |
| 48 | DataType::U16, DataType::S16, |
| 49 | DataType::U32, DataType::S32, DataType::F16, DataType::F32); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 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 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 61 | CLReshapeLayerKernel::CLReshapeLayerKernel() |
| 62 | : _input(nullptr), _output(nullptr) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | void CLReshapeLayerKernel::configure(const ICLTensor *input, ICLTensor *output) |
| 67 | { |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 68 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 69 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 70 | |
| 71 | _input = input; |
| 72 | _output = output; |
| 73 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 74 | // Create kernel |
| 75 | std::set<std::string> build_opts = { "-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()) }; |
| 76 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reshape_layer", build_opts)); |
| 77 | |
| 78 | // Add static arguments |
| 79 | const cl_int2 input_shape = |
| 80 | { |
| 81 | { |
| 82 | static_cast<cl_int>(_input->info()->tensor_shape()[0]), |
| 83 | static_cast<cl_int>(_input->info()->tensor_shape()[1]) |
| 84 | } |
| 85 | }; |
| 86 | const cl_int2 output_shape = |
| 87 | { |
| 88 | { |
| 89 | static_cast<cl_int>(_output->info()->tensor_shape()[0]), |
| 90 | static_cast<cl_int>(_output->info()->tensor_shape()[1]) |
| 91 | } |
| 92 | }; |
| 93 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters |
| 94 | _kernel.setArg<cl_int2>(idx++, input_shape); |
| 95 | _kernel.setArg<cl_int2>(idx++, output_shape); |
| 96 | |
| 97 | // Configure kernel window |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 98 | Window win = calculate_max_window(*input->info()); |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 99 | |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 100 | // Set the output valid region |
| 101 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 102 | ICLKernel::configure_internal(win); |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Giuseppe Rossini | 8479763 | 2018-08-22 12:07:48 +0100 | [diff] [blame] | 105 | Status CLReshapeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 106 | { |
| 107 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 108 | |
| 109 | return Status{}; |
| 110 | } |
| 111 | |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 112 | void CLReshapeLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 113 | { |
| 114 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 115 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 116 | |
| 117 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 118 | Window slice = window_collapsed.first_slice_window_3D(); |
| 119 | |
| 120 | // Set inputs |
| 121 | unsigned int idx = 0; |
| 122 | add_3D_tensor_argument(idx, _input, window_collapsed); |
| 123 | add_3D_tensor_argument(idx, _output, window_collapsed); |
| 124 | enqueue(queue, *this, slice); |
| 125 | } |