blob: 55bf9b754bf2222879ce2bd492a99471ad51cf27 [file] [log] [blame]
Stephen Lie855c232018-01-04 14:13:22 +08001/*
2 * Copyright (c) 2017-2018 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/GLES_COMPUTE/kernels/GCWeightsReshapeKernel.h"
25
26#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"
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Stephen Lie855c232018-01-04 14:13:22 +080035
36#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
37
38using namespace arm_compute;
39using namespace arm_compute::gles_compute;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010040using namespace arm_compute::misc::shape_calculator;
Stephen Lie855c232018-01-04 14:13:22 +080041
42GCWeightsReshapeKernel::GCWeightsReshapeKernel()
43 : _input(nullptr), _biases(nullptr), _output(nullptr)
44{
45}
46
47void GCWeightsReshapeKernel::configure(const IGCTensor *input, const IGCTensor *biases, IGCTensor *output)
48{
49 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
50 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
51
Stephen Lie855c232018-01-04 14:13:22 +080052 // Output tensor auto inizialitation if not yet initialized
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010053 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_weights_reshaped_shape(*input->info(), (biases != nullptr))));
Stephen Lie855c232018-01-04 14:13:22 +080054
55 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
56
57 if(biases != nullptr)
58 {
59 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
60 ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (biases->info()->num_dimensions() != 1));
61 ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 5) && (biases->info()->num_dimensions() != 2));
62 ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (biases->info()->dimension(0) != input->info()->tensor_shape()[3]));
63 ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 5) && (biases->info()->dimension(0) != input->info()->tensor_shape()[3] || biases->info()->dimension(1) != input->info()->tensor_shape()[4]));
64 }
65
66 _biases = biases;
67 _output = output;
68 _input = input;
69
70 // Create build options
71 std::set<std::string> build_opts;
72 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
73 build_opts.emplace("#define " + dt_name);
74 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
75 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
76 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
77 build_opts.emplace("#define RESHAPE_TO_COLUMNS");
78 if(biases != nullptr)
79 {
80 build_opts.emplace("#define HAS_BIAS");
81 }
82
83 // Create kernel
84 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("reshape_to_columns", build_opts));
85
86 // Set static arguments
87 unsigned int idx = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor();
88 idx += (biases != nullptr) ? num_arguments_per_1D_tensor() : 0;
89 _kernel.set_argument(idx++, _input->info()->dimension(0));
90 _kernel.set_argument(idx++, _input->info()->dimension(1));
91 _kernel.set_argument(idx++, _input->info()->dimension(2));
92 _kernel.set_argument(idx++, _input->info()->dimension(3));
93
94 // Configure window
95 Window win = calculate_max_window(*input->info(), Steps());
96
97 // The GCWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
98 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
99 IGCKernel::configure(win);
100}
101
102void GCWeightsReshapeKernel::run(const Window &window)
103{
104 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
105 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window);
106
107 Window out_window;
108 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
109
110 Window in_slice = window.first_slice_window_3D();
111 Window out_slice = out_window.first_slice_window_2D();
112
113 Window biases_window;
114 Window biases_slice;
115
116 if(_biases != nullptr)
117 {
118 biases_window.use_tensor_dimensions(_biases->info()->tensor_shape());
119 biases_slice = biases_window.first_slice_window_1D();
120 }
121
122 _kernel.use();
123
124 do
125 {
126 // Set arguments
127 unsigned idx = 0;
128 add_3D_tensor_argument(idx, _input, 1, in_slice);
129 add_2D_tensor_argument(idx, _output, 2, out_slice);
130 if(_biases != nullptr)
131 {
132 add_1D_tensor_argument(idx, _biases, 3, biases_slice);
133 biases_window.slide_window_slice_1D(biases_slice);
134 }
135
136 _kernel.update_shader_params();
137 // Run kernel
138 enqueue(*this, in_slice);
139 }
140 while(window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice));
141}