• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-2020 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 "src/core/CL/kernels/CLArgMinMaxLayerKernel.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/Helpers.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Utils.h"
32 #include "arm_compute/core/Validate.h"
33 #include "src/core/AccessWindowStatic.h"
34 #include "src/core/CL/CLValidate.h"
35 #include "src/core/helpers/AutoConfiguration.h"
36 #include "src/core/helpers/WindowHelpers.h"
37 
38 #include "support/StringSupport.h"
39 
40 namespace arm_compute
41 {
42 namespace
43 {
44 constexpr unsigned int vector_size = 16;
45 
validate_arguments(const ITensorInfo * input,const ITensorInfo * prev_output,const ITensorInfo * output,unsigned int axis,ReductionOperation op)46 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *prev_output, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
47 {
48     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
49     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
50     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S32, DataType::F16, DataType::F32);
51     ARM_COMPUTE_RETURN_ERROR_ON_MSG(op != ReductionOperation::ARG_IDX_MAX && op != ReductionOperation::ARG_IDX_MIN, "Only ARG_IDX_MAX and ARG_IDX_MIN are supported");
52     ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
53     ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
54 
55     if(output->total_size() != 0)
56     {
57         ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32);
58     }
59     if(prev_output != nullptr && prev_output->total_size() != 0)
60     {
61         ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(prev_output, 1, DataType::U32, DataType::S32);
62         if(output->total_size() != 0)
63         {
64             ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(prev_output, output);
65         }
66     }
67 
68     return Status{};
69 }
70 
validate_and_configure_window(ITensorInfo * input,ITensorInfo * prev_output,ITensorInfo * output,unsigned int axis,ReductionOperation op)71 std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *prev_output, ITensorInfo *output, unsigned int axis, ReductionOperation op)
72 {
73     ARM_COMPUTE_UNUSED(op);
74     // Output tensor auto initialization if not yet initialized
75     TensorShape output_shape{ input->tensor_shape() };
76     output_shape.set(axis, 1);
77     DataType output_data_type = DataType::S32;
78     auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape).set_data_type(output_data_type).reset_padding().set_is_resizable(true));
79 
80     Window win            = calculate_max_window((prev_output != nullptr) ? (*prev_output) : (*input), Steps(vector_size));
81     bool   window_changed = false;
82 
83     switch(axis)
84     {
85         case 0:
86         {
87             ITensorInfo           *input_tensor_access = prev_output != nullptr ? prev_output : input;
88             AccessWindowStatic     input_access(input_tensor_access, 0, 0, static_cast<int>(input_tensor_access->dimension(0)), 1);
89             AccessWindowHorizontal output_access(output, 0, 1);
90             window_changed = update_window_and_padding(win, input_access, output_access);
91             output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
92         }
93         break;
94         case 1:
95         case 2:
96         case 3:
97         {
98             AccessWindowHorizontal input_access(input, 0, vector_size);
99             AccessWindowHorizontal output_access(output, 0, vector_size);
100             window_changed = update_window_and_padding(win, input_access, output_access);
101             output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
102         }
103         break;
104         default:
105             ARM_COMPUTE_ERROR("Not supported");
106     }
107 
108     Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
109     return std::make_tuple(err, win);
110 }
111 } // namespace
112 
CLArgMinMaxLayerKernel()113 CLArgMinMaxLayerKernel::CLArgMinMaxLayerKernel()
114     : _input(nullptr), _prev_output(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::ARG_IDX_MAX)
115 {
116 }
117 
configure(const ICLTensor * input,const ICLTensor * prev_output,ICLTensor * output,unsigned int axis,ReductionOperation op)118 void CLArgMinMaxLayerKernel::configure(const ICLTensor *input, const ICLTensor *prev_output, ICLTensor *output, unsigned int axis, ReductionOperation op)
119 {
120     configure(CLKernelLibrary::get().get_compile_context(), input, prev_output, output, axis, op);
121 }
122 
configure(const CLCompileContext & compile_context,const ICLTensor * input,const ICLTensor * prev_output,ICLTensor * output,unsigned int axis,ReductionOperation op)123 void CLArgMinMaxLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *prev_output, ICLTensor *output, unsigned int axis, ReductionOperation op)
124 {
125     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
126     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (prev_output != nullptr) ? prev_output->info() : nullptr, output->info(), axis, op));
127     auto win_config = validate_and_configure_window(input->info(), (prev_output != nullptr) ? prev_output->info() : nullptr, output->info(), axis, op);
128     ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
129 
130     _input          = input;
131     _prev_output    = prev_output;
132     _output         = output;
133     _reduction_axis = axis;
134     _op             = op;
135 
136     // Set build options
137     CLBuildOptions build_opts;
138 
139     build_opts.add_option_if(_prev_output != nullptr, "-DPREV_OUTPUT");
140     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
141     build_opts.add_option_if(is_data_type_float(input->info()->data_type()), "-DFLOAT_DATA_TYPE");
142     build_opts.add_option_if_else(op == ReductionOperation::ARG_IDX_MAX, "-DARG_MAX", "-DARG_MIN");
143     build_opts.add_option("-DDATA_TYPE_OUTPUT=" + get_cl_type_from_data_type(output->info()->data_type()));
144     build_opts.add_option("-DDATA_TYPE_SELECT=" + get_cl_signed_type_from_element_size(input->info()->element_size()));
145 
146     // Create kernel
147     cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange();
148     std::string kernel_axis_name;
149     switch(axis)
150     {
151         case 0:
152         {
153             const ICLTensor *input_for_width = prev_output != nullptr ? _prev_output : _input;
154             build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input_for_width->info()->dimension(0)));
155 
156             kernel_axis_name = "x";
157             lws_hint         = create_lws_hint_parallel_implementations(input_for_width->info()->dimension(0), vector_size);
158         }
159         break;
160         case 1:
161             build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
162             kernel_axis_name = "y";
163             break;
164         case 2:
165             build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
166             kernel_axis_name = "z";
167             break;
168         case 3:
169             build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
170             build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
171             kernel_axis_name = "w";
172             break;
173         default:
174             ARM_COMPUTE_ERROR("Not supported");
175     }
176     _kernel = create_kernel(compile_context, "arg_min_max_" + kernel_axis_name, build_opts.options());
177 
178     // Configure kernel window
179     ICLKernel::configure_internal(std::get<1>(win_config), lws_hint);
180 }
181 
validate(const ITensorInfo * input,const ITensorInfo * prev_output,const ITensorInfo * output,unsigned int axis,ReductionOperation op)182 Status CLArgMinMaxLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *prev_output, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
183 {
184     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, prev_output, output, axis, op));
185     ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), (prev_output != nullptr) ? prev_output->clone().get() : nullptr, output->clone().get(), axis, op)));
186     return Status{};
187 }
188 
run(const Window & window,cl::CommandQueue & queue)189 void CLArgMinMaxLayerKernel::run(const Window &window, cl::CommandQueue &queue)
190 {
191     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
192     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
193 
194     switch(_reduction_axis)
195     {
196         case 0:
197         {
198             // Set out window
199             Window out_window(window);
200             out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
201 
202             // Get first input and output slices
203             Window in_slice  = window.first_slice_window_2D();
204             Window out_slice = out_window.first_slice_window_2D();
205 
206             // Reshape window
207             const unsigned int num_tensors = _prev_output != nullptr ? 3 : 2;
208 
209             // Set local sums buffer
210             unsigned int local_res_size = lws_hint()[0] * _output->info()->element_size();
211             _kernel.setArg(num_arguments_per_2D_tensor() * num_tensors, local_res_size, nullptr);
212             do
213             {
214                 unsigned int idx = 0;
215                 add_2D_tensor_argument(idx, _input, in_slice);
216                 if(_prev_output != nullptr)
217                 {
218                     add_2D_tensor_argument(idx, _prev_output, in_slice);
219                 }
220                 add_2D_tensor_argument(idx, _output, out_slice);
221                 enqueue(queue, *this, in_slice, lws_hint());
222             }
223             while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
224         }
225         break;
226         case 1:
227         {
228             // Get first input and output slices
229             Window window_in{ window };
230             window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1)));
231             Window in_slice  = window_in.first_slice_window_2D();
232             Window out_slice = window.first_slice_window_2D();
233 
234             do
235             {
236                 unsigned int idx = 0;
237                 add_2D_tensor_argument(idx, _input, in_slice);
238                 add_2D_tensor_argument(idx, _output, out_slice);
239                 enqueue(queue, *this, in_slice, lws_hint());
240             }
241             while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
242         }
243         break;
244         case 2:
245         {
246             // Get first input and output slices
247             Window window_in{ window };
248             window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2)));
249             Window in_slice  = window_in.first_slice_window_3D();
250             Window out_slice = window.first_slice_window_3D();
251 
252             do
253             {
254                 unsigned int idx = 0;
255                 add_3D_tensor_argument(idx, _input, in_slice);
256                 add_3D_tensor_argument(idx, _output, out_slice);
257                 enqueue(queue, *this, in_slice, lws_hint());
258             }
259             while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice));
260         }
261         break;
262         case 3:
263         {
264             // Get first input and output slices
265             Window window_in{ window };
266             window_in.set(3, Window::Dimension(0, 1, 1));
267             Window in_slice  = window_in.first_slice_window_4D();
268             Window out_slice = window.first_slice_window_4D();
269 
270             do
271             {
272                 unsigned int idx = 0;
273                 add_4D_tensor_argument(idx, _input, in_slice);
274                 add_4D_tensor_argument(idx, _output, out_slice);
275                 enqueue(queue, *this, in_slice, lws_hint());
276             }
277             while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice));
278         }
279         break;
280         default:
281             ARM_COMPUTE_ERROR("Not supported");
282     }
283 }
284 } // namespace arm_compute
285