Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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" |
| 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/IAccessWindow.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Utils.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | #include "arm_compute/core/Window.h" |
| 37 | |
| 38 | #include <string> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | CLReshapeLayerKernel::CLReshapeLayerKernel() |
| 43 | : _input(nullptr), _output(nullptr) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void CLReshapeLayerKernel::configure(const ICLTensor *input, ICLTensor *output) |
| 48 | { |
Daniil Efremov | eed841c | 2017-11-09 19:05:25 +0700 | [diff] [blame] | 49 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, |
| 50 | DataType::U16, DataType::S16, DataType::QS16, |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 51 | DataType::U32, DataType::S32, DataType::F16, DataType::F32); |
| 52 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 53 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 54 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
Daniil Efremov | eed841c | 2017-11-09 19:05:25 +0700 | [diff] [blame] | 55 | ARM_COMPUTE_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Georgios Pinitas | 5ee66ea | 2017-09-07 17:29:16 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_ERROR_ON(input->info()->tensor_shape().total_size() != output->info()->tensor_shape().total_size()); |
| 57 | |
| 58 | _input = input; |
| 59 | _output = output; |
| 60 | |
| 61 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 62 | |
| 63 | // Create kernel |
| 64 | std::set<std::string> build_opts = { "-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()) }; |
| 65 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reshape_layer", build_opts)); |
| 66 | |
| 67 | // Add static arguments |
| 68 | const cl_int2 input_shape = |
| 69 | { |
| 70 | { |
| 71 | static_cast<cl_int>(_input->info()->tensor_shape()[0]), |
| 72 | static_cast<cl_int>(_input->info()->tensor_shape()[1]) |
| 73 | } |
| 74 | }; |
| 75 | const cl_int2 output_shape = |
| 76 | { |
| 77 | { |
| 78 | static_cast<cl_int>(_output->info()->tensor_shape()[0]), |
| 79 | static_cast<cl_int>(_output->info()->tensor_shape()[1]) |
| 80 | } |
| 81 | }; |
| 82 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters |
| 83 | _kernel.setArg<cl_int2>(idx++, input_shape); |
| 84 | _kernel.setArg<cl_int2>(idx++, output_shape); |
| 85 | |
| 86 | // Configure kernel window |
| 87 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 88 | |
| 89 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 90 | AccessWindowStatic output_access(output->info(), 0, 0, output->info()->tensor_shape().x(), output->info()->tensor_shape().y()); |
| 91 | update_window_and_padding(win, input_access, output_access); |
| 92 | |
| 93 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 94 | |
| 95 | ICLKernel::configure(win); |
| 96 | } |
| 97 | |
| 98 | void CLReshapeLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 99 | { |
| 100 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 101 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 102 | |
| 103 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 104 | Window slice = window_collapsed.first_slice_window_3D(); |
| 105 | |
| 106 | // Set inputs |
| 107 | unsigned int idx = 0; |
| 108 | add_3D_tensor_argument(idx, _input, window_collapsed); |
| 109 | add_3D_tensor_argument(idx, _output, window_collapsed); |
| 110 | enqueue(queue, *this, slice); |
| 111 | } |