• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-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/CLROIAlignLayerKernel.h"
25 
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/CL/ICLArray.h"
29 #include "arm_compute/core/CL/ICLTensor.h"
30 #include "arm_compute/core/CL/OpenCL.h"
31 #include "arm_compute/core/Helpers.h"
32 #include "arm_compute/core/TensorInfo.h"
33 #include "arm_compute/core/Utils.h"
34 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
35 #include "src/core/AccessWindowStatic.h"
36 #include "src/core/CL/CLValidate.h"
37 #include "src/core/helpers/AutoConfiguration.h"
38 #include "src/core/helpers/WindowHelpers.h"
39 #include "support/StringSupport.h"
40 
41 using namespace arm_compute::misc::shape_calculator;
42 
43 namespace arm_compute
44 {
45 namespace
46 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * rois,ITensorInfo * output,const ROIPoolingLayerInfo & pool_info)47 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
48 {
49     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output);
50     ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
51     ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
52     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
53     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F32, DataType::F16);
54     ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC, DataLayout::NCHW);
55     ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
56 
57     if(output->total_size() != 0)
58     {
59         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
60         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
61         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), output->tensor_shape());
62     }
63 
64     if(is_data_type_quantized_asymmetric(input->data_type()))
65     {
66         ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::QASYMM16);
67 
68         const UniformQuantizationInfo rois_qinfo = rois->quantization_info().uniform();
69         ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.scale != 0.125f);
70         ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.offset != 0);
71     }
72     else
73     {
74         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois);
75     }
76     return Status{};
77 }
78 
validate_and_configure_window(ITensorInfo * input,ITensorInfo * rois,ITensorInfo * output,const ROIPoolingLayerInfo & pool_info)79 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
80 {
81     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
82 
83     // Output auto inizialitation if not yet initialized
84     const TensorShape output_shape = compute_roi_align_shape(*input, *rois, pool_info);
85     auto_init_if_empty((*output), output_shape, 1, input->data_type());
86     output->set_data_layout(input->data_layout());
87 
88     // Configure kernel window
89     constexpr unsigned int num_elems_processed_per_iteration = 1;
90     Window                 win                               = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
91 
92     AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
93     AccessWindowHorizontal input_access(input, input->valid_region().start(0), num_elems_processed_per_iteration);
94 
95     bool window_changed = update_window_and_padding(win, input_access, output_access);
96     output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
97     Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
98     return std::make_pair(err, win);
99 }
100 } // namespace
101 
CLROIAlignLayerKernel()102 CLROIAlignLayerKernel::CLROIAlignLayerKernel()
103     : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
104 {
105 }
106 
configure(const ICLTensor * input,const ICLTensor * rois,ICLTensor * output,const ROIPoolingLayerInfo & pool_info)107 void CLROIAlignLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
108 {
109     configure(CLKernelLibrary::get().get_compile_context(), input, rois, output, pool_info);
110 }
111 
configure(const CLCompileContext & compile_context,const ICLTensor * input,const ICLTensor * rois,ICLTensor * output,const ROIPoolingLayerInfo & pool_info)112 void CLROIAlignLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
113 {
114     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
115     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info));
116 
117     // Configure kernel window
118     auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info);
119     ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
120 
121     _input     = input;
122     _output    = output;
123     _rois      = rois;
124     _pool_info = pool_info;
125 
126     const DataType data_type = input->info()->data_type();
127     const bool     is_qasymm = is_data_type_quantized_asymmetric(data_type);
128 
129     // Set build options
130     CLBuildOptions build_opts;
131     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
132     build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type()));
133     build_opts.add_option("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH))));
134     build_opts.add_option("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT))));
135     build_opts.add_option("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL))));
136     build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width()));
137     build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height()));
138     build_opts.add_option("-DSPATIAL_SCALE=" + float_to_string_with_full_precision(pool_info.spatial_scale()));
139     build_opts.add_option_if(input->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
140     build_opts.add_option_if(pool_info.sampling_ratio() > 0, "-DSAMPLING_RATIO=" + support::cpp11::to_string(pool_info.sampling_ratio()));
141 
142     if(is_qasymm)
143     {
144         const UniformQuantizationInfo iq_info    = input->info()->quantization_info().uniform();
145         const UniformQuantizationInfo roisq_info = rois->info()->quantization_info().uniform();
146         const UniformQuantizationInfo oq_info    = output->info()->quantization_info().uniform();
147 
148         build_opts.add_option("-DOFFSET_IN=" + float_to_string_with_full_precision(iq_info.offset));
149         build_opts.add_option("-DSCALE_IN=" + float_to_string_with_full_precision(iq_info.scale));
150         build_opts.add_option("-DOFFSET_ROIS=" + float_to_string_with_full_precision(roisq_info.offset));
151         build_opts.add_option("-DSCALE_ROIS=" + float_to_string_with_full_precision(roisq_info.scale));
152         build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
153         build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
154     }
155 
156     // Create kernel
157     const std::string kernel_name = (is_qasymm) ? "roi_align_layer_quantized" : "roi_align_layer";
158     _kernel                       = create_kernel(compile_context, kernel_name, build_opts.options());
159 
160     ICLKernel::configure_internal(win_config.second);
161 }
162 
validate(const ITensorInfo * input,const ITensorInfo * rois,ITensorInfo * output,const ROIPoolingLayerInfo & pool_info)163 Status CLROIAlignLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
164 {
165     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info));
166     return Status{};
167 }
168 
run(const Window & window,cl::CommandQueue & queue)169 void CLROIAlignLayerKernel::run(const Window &window, cl::CommandQueue &queue)
170 {
171     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
172     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
173 
174     Window slice      = window.first_slice_window_3D();
175     Window slice_rois = slice;
176     // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor)
177     slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0));
178     slice.set(get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL), window[3]);
179 
180     // Set arguments
181     unsigned int idx = 0;
182     add_3D_tensor_argument(idx, _input, slice);
183     add_2D_tensor_argument(idx, _rois, slice_rois);
184     add_3D_tensor_argument(idx, _output, slice);
185     add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
186     add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
187 
188     enqueue(queue, *this, slice, lws_hint());
189 }
190 } // namespace arm_compute
191