blob: 35ebc5b70b9752f2bd5cbda2c177cbb4ef5a56f3 [file] [log] [blame]
Giorgio Arena368e6352018-08-20 15:06:07 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Giorgio Arena368e6352018-08-20 15:06:07 +01003 *
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/NEON/kernels/NEFlattenLayerKernel.h"
25
26#include "arm_compute/core/CPP/Validate.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Validate.h"
33
34#include "arm_compute/core/utils/misc/ShapeCalculator.h"
35
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +010036namespace arm_compute
37{
Giorgio Arena368e6352018-08-20 15:06:07 +010038using namespace misc::shape_calculator;
39
40namespace
41{
42Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
43{
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +010044 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas33843562019-12-10 13:33:18 +000045 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000046 // Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Giorgio Arena368e6352018-08-20 15:06:07 +010047
48 // Checks performed when output is configured
49 if(output->total_size() != 0)
50 {
51 const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(compute_flatten_shape(input));
52
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000055 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Giorgio Arena368e6352018-08-20 15:06:07 +010056 }
57
58 return Status{};
59}
60
61std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
62{
63 // Output tensor auto initialization if not yet initialized
64 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_flatten_shape(input)));
65
66 Window win = calculate_max_window(*input, Steps()); // Flatten does not need paddings
67
68 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
69
70 return std::make_pair(Status{}, win);
71}
72} // namespace
73
74NEFlattenLayerKernel::NEFlattenLayerKernel()
75 : _input(nullptr), _output(nullptr)
76{
77}
78
79void NEFlattenLayerKernel::configure(const ITensor *input, ITensor *output)
80{
81 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
82 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
83
84 _input = input;
85 _output = output;
86
87 // Configure kernel window
88 auto win_config = validate_and_configure_window(input->info(), output->info());
89 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
90 INEKernel::configure(win_config.second);
91}
92
93Status NEFlattenLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
94{
95 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
96 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
97 return Status{};
98}
99
100void NEFlattenLayerKernel::run(const Window &window, const ThreadInfo &info)
101{
102 ARM_COMPUTE_UNUSED(info);
103 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
104 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
105
106 const size_t in_width = _input->info()->dimension(0);
107 const size_t in_height = _input->info()->dimension(1);
108 const size_t out_step_x = in_width * _input->info()->element_size();
109 const size_t out_step_y = out_step_x * in_height;
110
111 Window in_window(window);
112 in_window.set(Window::DimX, Window::Dimension(0, 1, 1));
113
114 Window out_window;
115 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
116 out_window.set(Window::DimX, Window::Dimension(out_window.x().start(), out_window.x().end(), in_width));
117
118 Window in_slice = in_window.first_slice_window_3D();
119 Window out_slice = out_window.first_slice_window_1D();
120
121 do
122 {
123 Iterator in(_input, in_slice);
124 Iterator out(_output, out_slice);
125
126 uint8_t *out_ptr = out.ptr();
127
128 execute_window_loop(in_slice, [&](const Coordinates & id)
129 {
130 memcpy(out_ptr + id.y() * out_step_x + id.z() * out_step_y, in.ptr(), out_step_x);
131 },
132 in);
133 }
134 while(in_window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
135}
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100136} // namespace arm_compute