2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Copyright (c) 2017-2018 ARM Limited.
20 * SPDX-License-Identifier: MIT
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to
24 * deal in the Software without restriction, including without limitation the
25 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26 * sell copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 #include "arm_compute/core/CL/kernels/CLReduceOperationKernel.h"
43 #include "arm_compute/core/CL/CLHelpers.h"
44 #include "arm_compute/core/CL/CLKernelLibraryEx.h"
45 #include "arm_compute/core/CL/ICLTensor.h"
46 #include "support/StringSupport.h"
48 using namespace arm_compute;
51 // NOTE This is necessary because it is not guaranteed that the axis positions of input and output
53 const TensorShape inferOutputShape(const TensorShape &input_shape, const uint32_t axis)
55 TensorShape out_shape{input_shape};
57 out_shape.set(axis, 1);
65 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const uint32_t axis,
68 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
70 if (output->total_size() != 0)
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
75 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16,
76 DataType::F32, DataType::S32);
77 if (op == ReduceOperation::SUM)
79 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QASYMM8,
80 "Not support QASYMM8, yet");
82 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
84 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->tensor_shape().total_size() == 0,
85 "Inputs are not broadcast compatible");
87 const auto num_dimensions = input->tensor_shape().num_dimensions();
88 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= num_dimensions, "axis must be less than (input's rank).");
90 const TensorShape output_shape = inferOutputShape(input->tensor_shape(), axis);
91 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output_shape.total_size() != output->tensor_shape().total_size(),
92 "output shape's size does not match axis");
98 CLReduceOperationKernel::CLReduceOperationKernel() : _input(nullptr), _output(nullptr), _axis() {}
100 void CLReduceOperationKernel::configure(const ICLTensor *input, ICLTensor *output,
101 const uint32_t axis, ReduceOperation op)
103 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
105 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op));
111 std::unique_ptr<ITensorInfo> output_info = output->info()->clone();
112 output_info->set_tensor_shape(inferOutputShape(input->info()->tensor_shape(), axis));
114 // Construct kernel name
115 std::string kernel_name;
117 if (op == ReduceOperation::MAX)
119 kernel_name = "reduce_min_max";
122 else if (op == ReduceOperation::MIN)
124 kernel_name = "reduce_min_max";
127 else if (op == ReduceOperation::SUM)
129 kernel_name = "reduce_sum_mean";
132 else if (op == ReduceOperation::MEAN)
134 kernel_name = "reduce_sum_mean";
138 throw std::runtime_error("Operation not supported, yet");
140 // Set kernel build options
141 std::set<std::string> build_opts;
142 build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(output_info->data_type()));
143 build_opts.emplace("-DDEPTH_OUT=" + support::cpp11::to_string(output_info->dimension(2)));
144 build_opts.emplace("-DOP_CODE=" + support::cpp11::to_string(op_code));
148 static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel(kernel_name, build_opts));
150 // Configure kernel window
151 Window win = calculate_max_window(*output_info, Steps());
154 coord.set_num_dimensions(output_info->num_dimensions());
155 output->info()->set_valid_region(ValidRegion(coord, output_info->tensor_shape()));
157 ICLKernel::configure_internal(win);
160 Status CLReduceOperationKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
161 const uint32_t axis, ReduceOperation op)
163 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
164 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op));
169 void CLReduceOperationKernel::run(const Window &window, cl::CommandQueue &queue)
171 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
172 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
174 const TensorShape &shape_in = _input->info()->tensor_shape();
176 unsigned int idx = 2 * num_arguments_per_4D_tensor(); // Skip the input and output parameters
178 _kernel.setArg<cl_int>(idx++, _axis);
179 _kernel.setArg<cl_int>(idx++, shape_in[_axis]);
181 // Support dimensions up to 4
182 Window slice_out = window.collapse(ICLKernel::window(), 2, 4);
185 Window slice_in(slice_out);
186 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
187 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
188 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
189 slice_in.set(3, Window::Dimension(0, 0, 0));
191 // Copy output's shape in order to use for recovering at end of this method
192 // TODO Remove changing and recovering output's shape if it is guaranteed that the axis positions
193 // of input and output are the same
194 const TensorShape shape_out = _output->info()->tensor_shape();
195 _output->info()->set_tensor_shape(inferOutputShape(shape_in, _axis));
198 add_4D_tensor_argument(idx, _input, slice_in);
199 add_4D_tensor_argument(idx, _output, slice_out);
200 enqueue(queue, *this, slice_out, lws_hint());
202 // Recover output's shape of output tensor
203 _output->info()->set_tensor_shape(shape_out);