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/NEON/kernels/NEFlattenLayerKernel.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/TensorInfo.h"
30 #include "arm_compute/core/Types.h"
31 #include "arm_compute/core/Validate.h"
32 #include "src/core/CPP/Validate.h"
33 #include "src/core/helpers/AutoConfiguration.h"
34 #include "src/core/helpers/WindowHelpers.h"
35
36 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
37
38 namespace arm_compute
39 {
40 using namespace misc::shape_calculator;
41
42 namespace
43 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output)44 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
45 {
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
47 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
48 // Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
49
50 // Checks performed when output is configured
51 if(output->total_size() != 0)
52 {
53 const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(compute_flatten_shape(input));
54
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
58 }
59
60 return Status{};
61 }
62
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output)63 std::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
NEFlattenLayerKernel()76 NEFlattenLayerKernel::NEFlattenLayerKernel()
77 : _input(nullptr), _output(nullptr)
78 {
79 }
80
configure(const ITensor * input,ITensor * output)81 void 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
validate(const ITensorInfo * input,const ITensorInfo * output)95 Status 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
run(const Window & window,const ThreadInfo & info)102 void 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 }
138 } // namespace arm_compute
139