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" |
| 36 | |
| 37 | #include <cmath> |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | void CLGEMMTranspose1xWKernel::configure(const ICLTensor *input, ICLTensor *output) |
| 42 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame^] | 43 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, |
| 44 | DataType::U16, DataType::S16, DataType::QS16, |
| 45 | DataType::U32, DataType::S32, DataType::F16, DataType::F32); |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | |
| 48 | TensorShape output_shape{ input->info()->tensor_shape() }; |
| 49 | const size_t transpose_w = 16 / input->info()->element_size(); |
| 50 | output_shape.set(0, input->info()->dimension(1) * transpose_w); |
| 51 | output_shape.set(1, static_cast<size_t>(std::ceil((input->info()->dimension(0) / static_cast<float>(transpose_w))))); |
| 52 | |
| 53 | // Output tensor auto inizialitation if not yet initialized |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame^] | 54 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | |
| 56 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 57 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 60 | const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size(); |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 61 | const int scale_x = num_elems_processed_per_iteration; |
Georgios Pinitas | 4cbee6e | 2017-06-19 13:02:56 +0100 | [diff] [blame] | 62 | |
| 63 | _input = input; |
| 64 | _output = output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Following an example of how the transposition1xW works when the input data type is F32 |
| 68 | * |
| 69 | * |a00 a01 a02 a03| |
| 70 | * |a10 a11 a12 a13| |
| 71 | * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 | |
| 72 | * |a30 a31 a32 a33| |
| 73 | * |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 74 | * 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] | 75 | */ |
| 76 | // Create kernel |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 77 | 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] | 78 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | |
| 80 | // Configure window |
| 81 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 82 | |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 83 | ARM_COMPUTE_ERROR_ON_MSG((win.x().end() / scale_x) == 0, "Transposed shape would be 0 in the second dimension"); |
| 84 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 86 | AccessWindowTranspose output_access(output->info(), 0, 0, num_elems_processed_per_iteration, 1, scale_x, 1.f / scale_x); |
| 87 | |
| 88 | update_window_and_padding(win, input_access, output_access); |
| 89 | |
Georgios Pinitas | 4cbee6e | 2017-06-19 13:02:56 +0100 | [diff] [blame] | 90 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), input->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | |
| 92 | ICLKernel::configure(win); |
| 93 | } |
| 94 | |
| 95 | void CLGEMMTranspose1xWKernel::run(const Window &window, cl::CommandQueue &queue) |
| 96 | { |
| 97 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 98 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 99 | |
| 100 | // Output is transposed |
| 101 | Window out_window(window); |
| 102 | out_window.set(Window::DimX, window.y()); |
| 103 | out_window.set(Window::DimY, window.x()); |
| 104 | |
| 105 | Window in_slice = window.first_slice_window_2D(); |
| 106 | Window out_slice = out_window.first_slice_window_2D(); |
| 107 | |
| 108 | do |
| 109 | { |
| 110 | unsigned int idx = 0; |
| 111 | add_2D_tensor_argument(idx, _input, in_slice); |
| 112 | add_2D_tensor_argument(idx, _output, out_slice); |
| 113 | enqueue(queue, *this, in_slice, _lws_hint); |
| 114 | } |
| 115 | while(window.slide_window_slice_2D(in_slice) && out_window.slide_window_slice_2D(out_slice)); |
| 116 | } |