• 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/CLWidthConcatenate4TensorsKernel.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/Utils.h"
31 #include "src/core/CL/CLValidate.h"
32 #include "src/core/helpers/WindowHelpers.h"
33 #include "src/core/utils/helpers/tensor_info.h"
34 #include "support/Cast.h"
35 
36 #include "support/StringSupport.h"
37 
38 namespace arm_compute
39 {
40 namespace
41 {
validate_arguments(const ITensorInfo * input1,const ITensorInfo * input2,const ITensorInfo * input3,const ITensorInfo * input4,const ITensorInfo * output)42 Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *input3, const ITensorInfo *input4, const ITensorInfo *output)
43 {
44     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, input3, input4, output);
45     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input1);
46     ARM_COMPUTE_RETURN_ERROR_ON(input1->data_type() == DataType::UNKNOWN);
47     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2, input3, input4, output);
48     ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(0) + input2->dimension(0) + input3->dimension(0) + input4->dimension(0) > output->dimension(0));
49 
50     for(size_t i = 1; i < Coordinates::num_max_dimensions; ++i)
51     {
52         ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(i) != output->dimension(i));
53         ARM_COMPUTE_RETURN_ERROR_ON(input2->dimension(i) != output->dimension(i));
54         ARM_COMPUTE_RETURN_ERROR_ON(input3->dimension(i) != output->dimension(i));
55         ARM_COMPUTE_RETURN_ERROR_ON(input4->dimension(i) != output->dimension(i));
56     }
57     ARM_COMPUTE_RETURN_ERROR_ON(input1->num_dimensions() > 4);
58 
59     return Status{};
60 }
61 } // namespace
62 
CLWidthConcatenate4TensorsKernel()63 CLWidthConcatenate4TensorsKernel::CLWidthConcatenate4TensorsKernel()
64 {
65 }
66 
validate(const ITensorInfo * input1,const ITensorInfo * input2,const ITensorInfo * input3,const ITensorInfo * input4,const ITensorInfo * output)67 Status CLWidthConcatenate4TensorsKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *input3, const ITensorInfo *input4, const ITensorInfo *output)
68 {
69     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, input3, input4, output));
70     return Status{};
71 }
72 
configure(const CLCompileContext & compile_context,ITensorInfo * input1,ITensorInfo * input2,ITensorInfo * input3,ITensorInfo * input4,ITensorInfo * output)73 void CLWidthConcatenate4TensorsKernel::configure(const CLCompileContext &compile_context,
74                                                  ITensorInfo *input1, ITensorInfo *input2,
75                                                  ITensorInfo *input3, ITensorInfo *input4,
76                                                  ITensorInfo *output)
77 {
78     ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, input3, input4, output);
79     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1, input2, input3, input4, output));
80 
81     auto               padding_info                      = get_padding_info({ input1, input2, input3, input4, output });
82     const unsigned int min_dimension                     = std::min(std::min(input1->dimension(0), input2->dimension(0)), std::min(input3->dimension(0), input4->dimension(0)));
83     const unsigned int num_elems_processed_per_iteration = adjust_vec_size(8, min_dimension);
84     const unsigned int vec_size_leftover                 = output->dimension(0) % num_elems_processed_per_iteration;
85 
86     // Add build options
87     CLBuildOptions build_opts;
88     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input1->data_type()));
89     build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
90     build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_leftover));
91     build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input1->dimension(2)));
92     build_opts.add_option("-DINPUT1_WIDTH=" + support::cpp11::to_string(input1->dimension(0)));
93     build_opts.add_option("-DINPUT2_WIDTH=" + support::cpp11::to_string(input2->dimension(0)));
94     build_opts.add_option("-DINPUT3_WIDTH=" + support::cpp11::to_string(input3->dimension(0)));
95     build_opts.add_option("-DINPUT4_WIDTH=" + support::cpp11::to_string(input4->dimension(0)));
96     build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input1->element_size()));
97     build_opts.add_option("-DINPUT1_ROTATE_N=" + support::cpp11::to_string((input1->dimension(0) - vec_size_leftover) % num_elems_processed_per_iteration));
98     build_opts.add_option("-DINPUT2_ROTATE_N=" + support::cpp11::to_string((input1->dimension(0) + input2->dimension(0) - vec_size_leftover) % num_elems_processed_per_iteration));
99     build_opts.add_option("-DINPUT3_ROTATE_N=" + support::cpp11::to_string((input1->dimension(0) + input2->dimension(0) + input3->dimension(0) - vec_size_leftover) % num_elems_processed_per_iteration));
100 
101     // If input have different quantization info set quantization parameters needed for the re-quantization process
102     const bool have_different_qinfo = helpers::tensor_info::tensors_have_different_quantization_info(output, input1, input2, input3, input4);
103     if(is_data_type_quantized_asymmetric(input1->data_type()) && have_different_qinfo)
104     {
105         const UniformQuantizationInfo iq1_info = input1->quantization_info().uniform();
106         const UniformQuantizationInfo iq2_info = input2->quantization_info().uniform();
107         const UniformQuantizationInfo iq3_info = input3->quantization_info().uniform();
108         const UniformQuantizationInfo iq4_info = input4->quantization_info().uniform();
109         const UniformQuantizationInfo oq_info  = output->quantization_info().uniform();
110 
111         build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq1_info.offset));
112         build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq1_info.scale));
113         build_opts.add_option("-DOFFSET_IN2=" + float_to_string_with_full_precision(iq2_info.offset));
114         build_opts.add_option("-DSCALE_IN2=" + float_to_string_with_full_precision(iq2_info.scale));
115         build_opts.add_option("-DOFFSET_IN3=" + float_to_string_with_full_precision(iq3_info.offset));
116         build_opts.add_option("-DSCALE_IN3=" + float_to_string_with_full_precision(iq3_info.scale));
117         build_opts.add_option("-DOFFSET_IN4=" + float_to_string_with_full_precision(iq4_info.offset));
118         build_opts.add_option("-DSCALE_IN4=" + float_to_string_with_full_precision(iq4_info.scale));
119         build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
120         build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
121     }
122 
123     // Create kernel
124     _kernel = create_kernel(compile_context, "concatenate_width_x4", build_opts.options());
125 
126     // Configure kernel window
127     Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
128     ICLKernel::configure_internal(win.collapse(win, Window::DimZ));
129 
130     // Set output valid region
131     output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
132     ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
133 
134     // Set config_id for enabling LWS tuning
135     _config_id = "concatenate_width_x4_";
136     _config_id += lower_string(string_from_data_type(input1->data_type()));
137     _config_id += "_";
138     _config_id += support::cpp11::to_string(input1->dimension(0));
139     _config_id += "_";
140     _config_id += support::cpp11::to_string(input1->dimension(1));
141     _config_id += "_";
142     _config_id += support::cpp11::to_string(input2->dimension(0));
143     _config_id += "_";
144     _config_id += support::cpp11::to_string(input2->dimension(1));
145     _config_id += "_";
146     _config_id += support::cpp11::to_string(input3->dimension(0));
147     _config_id += "_";
148     _config_id += support::cpp11::to_string(input3->dimension(1));
149     _config_id += "_";
150     _config_id += support::cpp11::to_string(input4->dimension(0));
151     _config_id += "_";
152     _config_id += support::cpp11::to_string(input4->dimension(1));
153 }
154 
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)155 void CLWidthConcatenate4TensorsKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
156 {
157     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
158     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
159 
160     const auto src0 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_VEC));
161     const auto src1 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_VEC + 1));
162     const auto src2 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_VEC + 2));
163     const auto src3 = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_VEC + 3));
164     auto       dst  = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
165 
166     Window slice = window.first_slice_window_4D();
167 
168     do
169     {
170         unsigned int idx = 0;
171         add_4D_tensor_argument(idx, src0, slice);
172         add_4D_tensor_argument(idx, src1, slice);
173         add_4D_tensor_argument(idx, src2, slice);
174         add_4D_tensor_argument(idx, src3, slice);
175         add_4D_tensor_argument(idx, dst, slice);
176         enqueue(queue, *this, window, lws_hint());
177     }
178     while(window.slide_window_slice_4D(slice));
179 }
180 } // namespace arm_compute
181