blob: 84eb434bc9a5b4d194775ef8bb7d0dbf6d235d4e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 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/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
43CLPixelWiseMultiplicationKernel::CLPixelWiseMultiplicationKernel()
44 : _input1(nullptr), _input2(nullptr), _output(nullptr)
45{
46}
47
48void CLPixelWiseMultiplicationKernel::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, float scale,
49 ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
50{
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
53 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
54 ARM_COMPUTE_ERROR_ON_MSG(output->info()->data_type() == DataType::U8 && (input1->info()->data_type() != DataType::U8 || input2->info()->data_type() != DataType::U8),
55 "Output can only be U8 if both inputs are U8");
56 ARM_COMPUTE_ERROR_ON_MSG(scale < 0, "Scale cannot be negative. ");
57
58 _input1 = input1;
59 _input2 = input2;
60 _output = output;
61
62 int scale_int = -1;
63 // Extract sign, exponent and mantissa
64 int exponent = 0;
65 float normalized_mantissa = std::frexp(scale, &exponent);
66 // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
67 // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
68 // Moreover, it will be negative as we deal with 1/2^n
69 if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1))
70 {
71 // Store the positive exponent. We know that we compute 1/2^n
72 // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
73 scale_int = std::abs(exponent - 1);
74 }
75
76 std::string data_type;
77 std::string compute_type;
78 // Check if it has float inputs and output
79 if(is_data_type_float(input1->info()->data_type()) || is_data_type_float(input2->info()->data_type()))
80 {
81 scale_int = -1;
82 compute_type = (DataType::F32 == input1->info()->data_type() || DataType::F32 == input2->info()->data_type()) ? "float" : "half";
83 data_type = "DATA_TYPE_FLOAT";
84 }
85 else
86 {
87 compute_type = (DataType::S16 == input1->info()->data_type() || DataType::S16 == input2->info()->data_type()) ? "int" : "ushort";
88 data_type = "DATA_TYPE_INT";
89 }
90
91 // Construct kernel name
92 std::string kernel_name = "pixelwise_mul";
93 kernel_name += (scale_int >= 0) ? "_int" : "_float";
94
95 // Set kernel build options
96 std::set<std::string> build_opts;
97 build_opts.emplace((overflow_policy == ConvertPolicy::WRAP || is_data_type_float(output->info()->data_type())) ? "-DWRAP" : "-DSATURATE");
98 build_opts.emplace((rounding_policy == RoundingPolicy::TO_ZERO) ? "-DROUND=_rtz" : "-DROUND=_rte");
99 build_opts.emplace("-DDATA_TYPE_IN1=" + get_cl_type_from_data_type(input1->info()->data_type()));
100 build_opts.emplace("-DDATA_TYPE_IN2=" + get_cl_type_from_data_type(input2->info()->data_type()));
101 build_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
102 build_opts.emplace("-DDATA_TYPE_RES=" + compute_type);
103 build_opts.emplace("-D" + data_type);
104
105 // Create kernel
106 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
107
108 // Set scale argument
109 unsigned int idx = 3 * num_arguments_per_2D_tensor(); //Skip the inputs and output parameters
110
111 if(scale_int >= 0)
112 {
113 _kernel.setArg(idx++, scale_int);
114 }
115 else
116 {
117 _kernel.setArg(idx++, scale);
118 }
119
120 // Configure kernel window
121 constexpr unsigned int num_elems_processed_per_iteration = 16;
122
123 Window win = calculate_max_window(*input1->info(), Steps(num_elems_processed_per_iteration));
124
125 AccessWindowHorizontal input1_access(input1->info(), 0, num_elems_processed_per_iteration);
126 AccessWindowHorizontal input2_access(input2->info(), 0, num_elems_processed_per_iteration);
127 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
128
129 update_window_and_padding(win, input1_access, input2_access, output_access);
130
131 ValidRegion valid_region = intersect_valid_regions(input1->info()->valid_region(),
132 input2->info()->valid_region());
133 output_access.set_valid_region(win, valid_region);
134
135 ICLKernel::configure(win);
136}
137
138void CLPixelWiseMultiplicationKernel::run(const Window &window, cl::CommandQueue &queue)
139{
140 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
141 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
142
143 Window slice = window.first_slice_window_2D();
144
145 do
146 {
147 unsigned int idx = 0;
148 add_2D_tensor_argument(idx, _input1, slice);
149 add_2D_tensor_argument(idx, _input2, slice);
150 add_2D_tensor_argument(idx, _output, slice);
151 enqueue(queue, *this, slice);
152 }
153 while(window.slide_window_slice_2D(slice));
154}