blob: 1d6ef3d0e8d3f8f82e56c6f8799b1e023adba964 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +01003 *
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/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.h"
25
Anthony Barbier7068f992017-10-26 15:23:08 +010026#include "arm_compute/core/Error.h"
27#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
28#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
29#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
30#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/AccessWindowTranspose.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010039
40#include <cmath>
41
42using namespace arm_compute;
43
44void GCGEMMTranspose1xWKernel::configure(const IGCTensor *input, IGCTensor *output)
45{
Stephen Lie855c232018-01-04 14:13:22 +080046 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Anthony Barbier7068f992017-10-26 15:23:08 +010047 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
48
49 TensorShape output_shape{ input->info()->tensor_shape() };
50 const size_t transpose_w = 16 / input->info()->element_size();
51 output_shape.set(0, input->info()->dimension(1) * transpose_w);
52 output_shape.set(1, static_cast<size_t>(std::ceil((input->info()->dimension(0) / static_cast<float>(transpose_w)))));
53
54 // Output tensor auto inizialitation if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010055 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +010056
57 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
59
60 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
61 const int scale_x = num_elems_processed_per_iteration;
62
63 _input = input;
64 _output = output;
65
66 std::set<std::string> build_opts;
67 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
68 build_opts.emplace(("#define " + dt_name));
69 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
70 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
71 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
72 /*
73 * Following an example of how the transposition1xW works when the input data type is F32
74 *
75 * |a00 a01 a02 a03|
76 * |a10 a11 a12 a13|
77 * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 |
78 * |a30 a31 a32 a33|
79 *
80 * The output matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor)
81 */
82 // Create kernel
83 build_opts.emplace("#define GEMM_TRANSPOSE1xW");
84 _kernel = GCKernelLibrary::get().create_kernel("gemm_transpose1x4", build_opts);
85
86 // Configure window
87 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
88
89 ARM_COMPUTE_ERROR_ON_MSG((win.x().end() / scale_x) == 0, "Transposed shape would be 0 in the second dimension");
90
91 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
92 AccessWindowTranspose output_access(output->info(), 0, 0, num_elems_processed_per_iteration, 1, scale_x, 1.f / scale_x);
93
94 update_window_and_padding(win, input_access, output_access);
95
Gian Marco Iodice1e6c9152019-08-20 11:27:20 +010096 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier7068f992017-10-26 15:23:08 +010097
Anthony Barbier7068f992017-10-26 15:23:08 +010098 IGCKernel::configure(win);
99}
100
101void GCGEMMTranspose1xWKernel::run(const Window &window)
102{
103 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
104 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window);
105
106 _kernel.use();
107
108 // Output is transposed
109 Window out_window(window);
110 out_window.set(Window::DimX, window.y());
111 out_window.set(Window::DimY, window.x());
112
113 Window in_slice = window.first_slice_window_2D();
114 Window out_slice = out_window.first_slice_window_2D();
115
116 do
117 {
118 unsigned int idx = 0;
119 add_2D_tensor_argument(idx, _input, 1, in_slice);
120 add_2D_tensor_argument(idx, _output, 2, out_slice);
121
122 _kernel.update_shader_params();
123
124 enqueue(*this, in_slice);
125 }
126 while(window.slide_window_slice_2D(in_slice) && out_window.slide_window_slice_2D(out_slice));
127}