blob: 17189143ef8e3c13d0ec057a58b7e96e71b82a38 [file] [log] [blame]
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +01001/*
2 * Copyright (c) 2018 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 "arm_compute/core/CL/kernels/CLFlattenLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/CLValidate.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/IAccessWindow.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/core/utils/misc/ShapeCalculator.h"
38#include "support/ToolchainSupport.h"
39
40using namespace arm_compute::misc::shape_calculator;
41
42namespace arm_compute
43{
44namespace
45{
46Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
47{
48 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
50 DataType::U16, DataType::S16,
51 DataType::U32, DataType::S32,
52 DataType::F16, DataType::F32);
53 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
54
55 // Checks performed when output is configured
56 if(output->total_size() != 0)
57 {
58 const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(compute_flatten_shape(input));
59
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
62 }
63
64 return Status{};
65}
66
67std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
68{
69 // Output tensor auto initialization if not yet initialized
70 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_flatten_shape(input)));
71
72 Window win = calculate_max_window(*input, Steps()); // Flatten does not need paddings
73
74 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
75
76 return std::make_pair(Status{}, win);
77}
78} // namespace
79
80CLFlattenLayerKernel::CLFlattenLayerKernel()
81 : _input(nullptr), _output(nullptr)
82{
83}
84
85void CLFlattenLayerKernel::configure(const ICLTensor *input, ICLTensor *output)
86{
87 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
88 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
89
90 _input = input;
91 _output = output;
92
93 CLBuildOptions build_opts;
94 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
95 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
96 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1)));
97
98 // Create kernel
99 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("flatten", build_opts.options()));
100
101 // Configure kernel window
102 auto win_config = validate_and_configure_window(input->info(), output->info());
103 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100104 ICLKernel::configure_internal(win_config.second);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100105
106 // Set config_id for enabling LWS tuning
107 _config_id = "flatten";
108 _config_id += "_";
109 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
110 _config_id += "_";
111 _config_id += support::cpp11::to_string(input->info()->dimension(0));
112 _config_id += "_";
113 _config_id += support::cpp11::to_string(input->info()->dimension(1));
114 _config_id += "_";
115 _config_id += support::cpp11::to_string(input->info()->dimension(2));
116 _config_id += "_";
117 _config_id += support::cpp11::to_string(output->info()->dimension(0));
118 _config_id += "_";
119 _config_id += support::cpp11::to_string(output->info()->dimension(1));
120}
121
122Status CLFlattenLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
123{
124 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
125 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
126 return Status{};
127}
128
129void CLFlattenLayerKernel::run(const Window &window, cl::CommandQueue &queue)
130{
131 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
132 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
133
134 Window out_window;
135 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
136
137 Window out_slice = out_window.first_slice_window_1D();
138 Window in_slice = window.first_slice_window_3D();
139
140 // Run kernel
141 do
142 {
143 // Set arguments
144 unsigned int idx = 0;
145 add_3D_tensor_argument(idx, _input, in_slice);
146 add_1D_tensor_argument(idx, _output, out_slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100147 enqueue(queue, *this, in_slice, lws_hint());
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100148 }
149 while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
150}
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100151} // namespace arm_compute