blob: f30ba61b9a46d25ca141bdf6274247659fca6f15 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/CL/kernels/CLPixelWiseMultiplicationKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
36#include <cmath>
37#include <cstdlib>
38#include <set>
39#include <string>
40
41using namespace arm_compute;
42
Giorgio Arena70623822017-11-27 15:50:10 +000043namespace
44{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000045constexpr unsigned int num_elems_processed_per_iteration = 16;
46
Georgios Pinitas631c41a2017-12-06 11:53:03 +000047Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
48 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
Giorgio Arena70623822017-11-27 15:50:10 +000049{
50 ARM_COMPUTE_UNUSED(overflow_policy);
51 ARM_COMPUTE_UNUSED(rounding_policy);
52
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
54 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
Giorgio Arena70623822017-11-27 15:50:10 +000055 ARM_COMPUTE_RETURN_ERROR_ON_MSG(scale < 0, "Scale cannot be negative.");
56
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000057 const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
58
59 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input1, input2);
61
Giorgio Arena70623822017-11-27 15:50:10 +000062 if(is_data_type_fixed_point(input1->data_type()))
63 {
64 // All data types must be all QS8 or all QS16
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2);
66 ARM_COMPUTE_RETURN_ERROR_ON_MSG(scale != 1, "Unsupported scaling factor for QS8/QS16. Scale must be 1.");
67 }
68
69 // Validate in case of configured output
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000070 if(output->total_size() > 0)
Giorgio Arena70623822017-11-27 15:50:10 +000071 {
72 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
73 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::U8 && (input1->data_type() != DataType::U8 || input2->data_type() != DataType::U8),
74 "Output can only be U8 if both inputs are U8");
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000075 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output");
Giorgio Arena70623822017-11-27 15:50:10 +000076 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input1, output);
77 if(is_data_type_fixed_point(input1->data_type()))
78 {
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, output);
80 }
81 }
82
Georgios Pinitas631c41a2017-12-06 11:53:03 +000083 return Status{};
Giorgio Arena70623822017-11-27 15:50:10 +000084}
85
Georgios Pinitas631c41a2017-12-06 11:53:03 +000086std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output)
Giorgio Arena70623822017-11-27 15:50:10 +000087{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000088 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2);
89 const TensorShape &out_shape = broadcast_pair.first;
90 const ValidRegion &valid_region = broadcast_pair.second;
Giorgio Arena70623822017-11-27 15:50:10 +000091
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000092 // Auto initialize output if not initialized
93 {
94 set_shape_if_empty(*output, out_shape);
95
96 if(input1->data_type() == DataType::S16 || input2->data_type() == DataType::S16)
97 {
98 set_format_if_unknown(*output, Format::S16);
99 }
100 else if(input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32)
101 {
102 set_format_if_unknown(*output, Format::F32);
103 }
104 }
105
106 Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration));
107 Window win_input1 = win.broadcast_if_dimension_le_one(*input1);
108 Window win_input2 = win.broadcast_if_dimension_le_one(*input2);
Giorgio Arena70623822017-11-27 15:50:10 +0000109
110 AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration);
111 AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration);
112 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
113
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000114 bool window_changed = update_window_and_padding(win_input1, input1_access)
115 || update_window_and_padding(win_input2, input2_access)
116 || update_window_and_padding(win, output_access);
Giorgio Arena70623822017-11-27 15:50:10 +0000117
Giorgio Arena70623822017-11-27 15:50:10 +0000118 output_access.set_valid_region(win, valid_region);
119
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000120 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Giorgio Arena70623822017-11-27 15:50:10 +0000121 return std::make_pair(err, win);
122}
123} // namespace
124
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125CLPixelWiseMultiplicationKernel::CLPixelWiseMultiplicationKernel()
126 : _input1(nullptr), _input2(nullptr), _output(nullptr)
127{
128}
129
130void CLPixelWiseMultiplicationKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, float scale,
131 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
132{
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100133 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Giorgio Arenaf6a43c52017-12-01 12:16:25 +0000134 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(),
135 scale, overflow_policy, rounding_policy));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000137 // Configure kernel window
138 auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info());
139 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
140
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 _input1 = input1;
142 _input2 = input2;
143 _output = output;
144
145 int scale_int = -1;
146 // Extract sign, exponent and mantissa
147 int exponent = 0;
148 float normalized_mantissa = std::frexp(scale, &exponent);
149 // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
150 // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
151 // Moreover, it will be negative as we deal with 1/2^n
152 if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1))
153 {
154 // Store the positive exponent. We know that we compute 1/2^n
155 // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
156 scale_int = std::abs(exponent - 1);
157 }
158
159 std::string data_type;
160 std::string compute_type;
161 // Check if it has float inputs and output
162 if(is_data_type_float(input1->info()->data_type()) || is_data_type_float(input2->info()->data_type()))
163 {
164 scale_int = -1;
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100165 compute_type = (input1->info()->data_type() == DataType::F32 || input2->info()->data_type() == DataType::F32) ? "float" : "half";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 data_type = "DATA_TYPE_FLOAT";
167 }
168 else
169 {
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100170 if(input1->info()->data_type() == DataType::S16 || input2->info()->data_type() == DataType::S16)
171 {
172 compute_type = "int";
173 }
174 else if(input1->info()->data_type() == DataType::QS8)
175 {
176 compute_type = "qs8";
177 }
178 else if(input1->info()->data_type() == DataType::QS16)
179 {
180 compute_type = "qs16";
181 }
182 else
183 {
184 compute_type = "ushort";
185 }
186 data_type = "DATA_TYPE_INT";
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 }
188
189 // Construct kernel name
190 std::string kernel_name = "pixelwise_mul";
191 kernel_name += (scale_int >= 0) ? "_int" : "_float";
192
193 // Set kernel build options
194 std::set<std::string> build_opts;
195 build_opts.emplace((overflow_policy == ConvertPolicy::WRAP || is_data_type_float(output->info()->data_type())) ? "-DWRAP" : "-DSATURATE");
196 build_opts.emplace((rounding_policy == RoundingPolicy::TO_ZERO) ? "-DROUND=_rtz" : "-DROUND=_rte");
Michele Di Giorgioab0a77e2017-06-21 15:36:24 +0100197 if(is_data_type_fixed_point(input1->info()->data_type()))
198 {
199 build_opts.emplace("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input1->info()->fixed_point_position()));
200 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 build_opts.emplace("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(input1->info()->data_type()));
202 build_opts.emplace("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(input2->info()->data_type()));
203 build_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
204 build_opts.emplace("-DDATA_TYPE_RES=" + compute_type);
205 build_opts.emplace("-D" + data_type);
206
207 // Create kernel
208 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
209
210 // Set scale argument
Anthony Barbier9a7182e2017-07-11 18:36:40 +0100211 unsigned int idx = 3 * num_arguments_per_3D_tensor(); //Skip the inputs and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212
213 if(scale_int >= 0)
214 {
215 _kernel.setArg(idx++, scale_int);
216 }
217 else
218 {
219 _kernel.setArg(idx++, scale);
220 }
221
Giorgio Arena70623822017-11-27 15:50:10 +0000222 ICLKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223}
224
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000225Status CLPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
226 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000227{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000228 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Giorgio Arena70623822017-11-27 15:50:10 +0000229 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy));
230 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first);
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000231
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000232 return Status{};
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000233}
234
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100235void CLPixelWiseMultiplicationKernel::run(const Window &window, cl::CommandQueue &queue)
236{
237 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
238 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
239
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000240 const TensorShape &in_shape1 = _input1->info()->tensor_shape();
241 const TensorShape &in_shape2 = _input2->info()->tensor_shape();
242 const TensorShape &out_shape = _output->info()->tensor_shape();
243
244 bool can_collapse = true;
245 if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1)
246 {
247 can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ);
248 for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d)
249 {
250 can_collapse = (in_shape1[d] == in_shape2[d]);
251 }
252 }
253
254 bool has_collapsed = false;
255 Window collapsed = can_collapse ? window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &has_collapsed) : window;
256
257 const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1;
258 const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2;
259
260 Window slice = collapsed.first_slice_window_3D();
261 Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed);
262 Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100263
264 do
265 {
266 unsigned int idx = 0;
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000267 add_3D_tensor_argument(idx, _input1, slice_input1);
268 add_3D_tensor_argument(idx, _input2, slice_input2);
Anthony Barbier9a7182e2017-07-11 18:36:40 +0100269 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270 enqueue(queue, *this, slice);
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000271
272 collapsed.slide_window_slice_3D(slice_input1);
273 collapsed.slide_window_slice_3D(slice_input2);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100274 }
Georgios Pinitas1d08a312018-01-03 12:29:22 +0000275 while(collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100276}
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000277
278BorderSize CLPixelWiseMultiplicationKernel::border_size() const
279{
280 const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0));
281 const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize);
282 return BorderSize(0, border, 0, 0);
283}