Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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/CLGEMMTranspose1xWKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowTranspose.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/Error.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame^] | 36 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | #include <cmath> |
| 39 | |
| 40 | using namespace arm_compute; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame^] | 41 | using namespace arm_compute::misc::shape_calculator; |
| 42 | |
| 43 | namespace |
| 44 | { |
| 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
| 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::U8, DataType::S8, |
| 48 | DataType::QS16, DataType::U16, DataType::S16, DataType::U32, DataType::S32, |
| 49 | DataType::F16, DataType::F32); |
| 50 | |
| 51 | if(output->total_size() != 0) |
| 52 | { |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), |
| 54 | compute_transpose1xW_with_element_size_shape(*input)); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 57 | } |
| 58 | |
| 59 | return Status{}; |
| 60 | } |
| 61 | |
| 62 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, unsigned int &num_elems_processed_per_iteration) |
| 63 | { |
| 64 | num_elems_processed_per_iteration = 16 / input->element_size(); |
| 65 | |
| 66 | const int scale_x = num_elems_processed_per_iteration; |
| 67 | bool window_changed = false; |
| 68 | |
| 69 | // Configure kernel window |
| 70 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 71 | |
| 72 | if((win.x().end() / scale_x) == 0) |
| 73 | { |
| 74 | return std::make_pair(ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Transposed shape would be 0 in the second dimension"), win); |
| 75 | } |
| 76 | |
| 77 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 78 | window_changed = window_changed || update_window_and_padding(win, input_access); |
| 79 | |
| 80 | // Configure window in case of configured output |
| 81 | if(output->total_size() != 0) |
| 82 | { |
| 83 | AccessWindowTranspose output_access(output, 0, 0, num_elems_processed_per_iteration, 1, scale_x, 1.f / scale_x); |
| 84 | window_changed = window_changed || update_window_and_padding(win, output_access); |
| 85 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), input->tensor_shape())); |
| 86 | } |
| 87 | |
| 88 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 89 | return std::make_pair(err, win); |
| 90 | } |
| 91 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | |
| 93 | void CLGEMMTranspose1xWKernel::configure(const ICLTensor *input, ICLTensor *output) |
| 94 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame^] | 95 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
| 97 | // Output tensor auto inizialitation if not yet initialized |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame^] | 98 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*input->info()))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame^] | 100 | // Perform validate step |
| 101 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Georgios Pinitas | 4cbee6e | 2017-06-19 13:02:56 +0100 | [diff] [blame] | 102 | |
| 103 | _input = input; |
| 104 | _output = output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame^] | 106 | // Configure kernel window |
| 107 | unsigned int num_elems_processed_per_iteration = 1; |
| 108 | auto win_config = validate_and_configure_window(input->info(), output->info(), num_elems_processed_per_iteration); |
| 109 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 110 | ICLKernel::configure(win_config.second); |
| 111 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | /* |
| 113 | * Following an example of how the transposition1xW works when the input data type is F32 |
| 114 | * |
| 115 | * |a00 a01 a02 a03| |
| 116 | * |a10 a11 a12 a13| |
| 117 | * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 | |
| 118 | * |a30 a31 a32 a33| |
| 119 | * |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 120 | * The output matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | */ |
| 122 | // Create kernel |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 123 | std::string kernel_name = "gemm_transpose1x" + support::cpp11::to_string(num_elems_processed_per_iteration); |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 124 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame^] | 125 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame^] | 127 | Status CLGEMMTranspose1xWKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 128 | { |
| 129 | unsigned int num_elems_processed_per_iteration = 1; |
| 130 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 131 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), num_elems_processed_per_iteration).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame^] | 133 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void CLGEMMTranspose1xWKernel::run(const Window &window, cl::CommandQueue &queue) |
| 137 | { |
| 138 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 139 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 140 | |
| 141 | // Output is transposed |
| 142 | Window out_window(window); |
| 143 | out_window.set(Window::DimX, window.y()); |
| 144 | out_window.set(Window::DimY, window.x()); |
| 145 | |
| 146 | Window in_slice = window.first_slice_window_2D(); |
| 147 | Window out_slice = out_window.first_slice_window_2D(); |
| 148 | |
| 149 | do |
| 150 | { |
| 151 | unsigned int idx = 0; |
| 152 | add_2D_tensor_argument(idx, _input, in_slice); |
| 153 | add_2D_tensor_argument(idx, _output, out_slice); |
| 154 | enqueue(queue, *this, in_slice, _lws_hint); |
| 155 | } |
| 156 | while(window.slide_window_slice_2D(in_slice) && out_window.slide_window_slice_2D(out_slice)); |
| 157 | } |