blob: b8452fb5fc90b214c402aaa28d73baef5aef5d0d [file] [log] [blame]
Giorgio Arena368e6352018-08-20 15:06:07 +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/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
36#include <arm_neon.h>
37
38using namespace arm_compute;
39using namespace misc::shape_calculator;
40
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
46 DataType::U16, DataType::S16,
47 DataType::U32, DataType::S32,
48 DataType::F16, DataType::F32);
49 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
50
51 // Checks performed when output is configured
52 if(output->total_size() != 0)
53 {
54 const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(compute_flatten_shape(input));
55
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
58 }
59
60 return Status{};
61}
62
63std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
64{
65 // Output tensor auto initialization if not yet initialized
66 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_flatten_shape(input)));
67
68 Window win = calculate_max_window(*input, Steps()); // Flatten does not need paddings
69
70 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
71
72 return std::make_pair(Status{}, win);
73}
74} // namespace
75
76NEFlattenLayerKernel::NEFlattenLayerKernel()
77 : _input(nullptr), _output(nullptr)
78{
79}
80
81void NEFlattenLayerKernel::configure(const ITensor *input, ITensor *output)
82{
83 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
84 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
85
86 _input = input;
87 _output = output;
88
89 // Configure kernel window
90 auto win_config = validate_and_configure_window(input->info(), output->info());
91 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
92 INEKernel::configure(win_config.second);
93}
94
95Status NEFlattenLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
96{
97 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
98 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
99 return Status{};
100}
101
102void NEFlattenLayerKernel::run(const Window &window, const ThreadInfo &info)
103{
104 ARM_COMPUTE_UNUSED(info);
105 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
106 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
107
108 const size_t in_width = _input->info()->dimension(0);
109 const size_t in_height = _input->info()->dimension(1);
110 const size_t out_step_x = in_width * _input->info()->element_size();
111 const size_t out_step_y = out_step_x * in_height;
112
113 Window in_window(window);
114 in_window.set(Window::DimX, Window::Dimension(0, 1, 1));
115
116 Window out_window;
117 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
118 out_window.set(Window::DimX, Window::Dimension(out_window.x().start(), out_window.x().end(), in_width));
119
120 Window in_slice = in_window.first_slice_window_3D();
121 Window out_slice = out_window.first_slice_window_1D();
122
123 do
124 {
125 Iterator in(_input, in_slice);
126 Iterator out(_output, out_slice);
127
128 uint8_t *out_ptr = out.ptr();
129
130 execute_window_loop(in_slice, [&](const Coordinates & id)
131 {
132 memcpy(out_ptr + id.y() * out_step_x + id.z() * out_step_y, in.ptr(), out_step_x);
133 },
134 in);
135 }
136 while(in_window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
137}