• 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/CLFlattenLayerKernel.h"
25 #include "arm_compute/core/CL/ICLTensor.h"
26 #include "arm_compute/core/TensorInfo.h"
27 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28 #include "src/core/helpers/AutoConfiguration.h"
29 #include "src/core/helpers/WindowHelpers.h"
30 #include "support/StringSupport.h"
31 
32 using namespace arm_compute::misc::shape_calculator;
33 
34 namespace arm_compute
35 {
36 namespace
37 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output)38 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
39 {
40     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
41     ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
42 
43     // Checks performed when output is configured
44     if(output->total_size() != 0)
45     {
46         const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(compute_flatten_shape(input));
47 
48         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
49         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
50         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
51     }
52 
53     return Status{};
54 }
55 } // namespace
56 
CLFlattenLayerKernel()57 CLFlattenLayerKernel::CLFlattenLayerKernel()
58     : _input(nullptr), _output(nullptr)
59 {
60 }
61 
configure(const ICLTensor * input,ICLTensor * output)62 void CLFlattenLayerKernel::configure(const ICLTensor *input, ICLTensor *output)
63 {
64     configure(CLKernelLibrary::get().get_compile_context(), input, output);
65 }
66 
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output)67 void CLFlattenLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output)
68 {
69     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
70 
71     // Output tensor auto initialization if not yet initialized
72     auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_flatten_shape(input->info())));
73 
74     auto padding_info = get_padding_info({ input, output });
75 
76     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
77 
78     _input  = input;
79     _output = output;
80 
81     CLBuildOptions build_opts;
82     build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(input->info()->data_type())));
83     build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
84     build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
85     build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2)));
86     build_opts.add_option_if(output->info()->num_dimensions() > 2, "-DDST_DIM1=" + support::cpp11::to_string(output->info()->dimension(1)));
87 
88     // Create kernel
89     _kernel = create_kernel(compile_context, "flatten", build_opts.options());
90 
91     // Configure kernel window
92     Window win = calculate_max_window(*input->info(), Steps());
93     ICLKernel::configure_internal(win);
94 
95     output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
96 
97     ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
98 
99     // Set config_id for enabling LWS tuning
100     _config_id = "flatten";
101     _config_id += "_";
102     _config_id += lower_string(string_from_data_type(input->info()->data_type()));
103     _config_id += "_";
104     _config_id += support::cpp11::to_string(input->info()->dimension(0));
105     _config_id += "_";
106     _config_id += support::cpp11::to_string(input->info()->dimension(1));
107     _config_id += "_";
108     _config_id += support::cpp11::to_string(input->info()->dimension(2));
109     _config_id += "_";
110     _config_id += support::cpp11::to_string(output->info()->dimension(0));
111     _config_id += "_";
112     _config_id += support::cpp11::to_string(output->info()->dimension(1));
113 }
114 
validate(const ITensorInfo * input,const ITensorInfo * output)115 Status CLFlattenLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
116 {
117     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
118     return Status{};
119 }
120 
run(const Window & window,cl::CommandQueue & queue)121 void CLFlattenLayerKernel::run(const Window &window, cl::CommandQueue &queue)
122 {
123     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
124     ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
125 
126     Window collapsed_window = window.collapse(ICLKernel::window(), Window::DimZ);
127 
128     Window output_window;
129     output_window.use_tensor_dimensions(_output->info()->tensor_shape());
130 
131     // Run kernel
132     unsigned int idx = 0;
133     add_4D_tensor_argument(idx, _input, collapsed_window);
134     add_3D_tensor_argument(idx, _output, output_window);
135     enqueue(queue, *this, collapsed_window, lws_hint());
136 }
137 } // namespace arm_compute
138