blob: 6bf49549e2e2bf6ac0ba2735b2775b1a812f1cb5 [file] [log] [blame]
Michalis Spyrou1d382592018-08-13 17:28:04 +01001/*
Gian Marco Iodicee214fcf2019-03-15 09:12:40 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyrou1d382592018-08-13 17:28:04 +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/NECopyKernel.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/Validate.h"
31#include "arm_compute/core/Window.h"
Pablo Telloc9564cb2019-09-13 10:20:25 +010032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyrou1d382592018-08-13 17:28:04 +010033
Pablo Telloc9564cb2019-09-13 10:20:25 +010034namespace arm_compute
35{
36namespace
37{
38Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding = PaddingList())
39{
40 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas33843562019-12-10 13:33:18 +000041 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Pablo Telloc9564cb2019-09-13 10:20:25 +010042 ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 4);
43
44 // Validate output if initialized
45 if(output->total_size() != 0)
46 {
47 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding), output->tensor_shape());
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
49 }
50
51 return Status{};
52}
53
54std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
55{
56 // Output auto inizialitation if not yet initialized
57 auto_init_if_empty(*output, *input);
58 return std::make_pair(Status{}, calculate_max_window(*output));
59}
60
61std::pair<Status, Window> validate_and_configure_window_with_padding(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding)
62{
63 const TensorShape input_shape = input->tensor_shape();
64 const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input_shape, padding);
65 auto_init_if_empty(*output, input->clone()->set_tensor_shape(padded_shape));
66 // Configure window
67 const Window win = calculate_max_window(*output, output->dimension(0));
68 return std::make_pair(Status{}, win);
69}
70
71} // namespace
Michalis Spyrou1d382592018-08-13 17:28:04 +010072
73NECopyKernel::NECopyKernel()
Pablo Telloc9564cb2019-09-13 10:20:25 +010074 : _input(nullptr), _output(nullptr), _padding()
Michalis Spyrou1d382592018-08-13 17:28:04 +010075{
76}
77
Pablo Telloc9564cb2019-09-13 10:20:25 +010078void NECopyKernel::configure(const ITensor *input, ITensor *output, const PaddingList &padding)
Michalis Spyrou1d382592018-08-13 17:28:04 +010079{
80 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Pablo Telloc9564cb2019-09-13 10:20:25 +010081 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding));
Michalis Spyrou1d382592018-08-13 17:28:04 +010082
Pablo Telloc9564cb2019-09-13 10:20:25 +010083 _input = input;
84 _output = output;
85 _padding = padding;
Michalis Spyrou1d382592018-08-13 17:28:04 +010086
Pablo Telloc9564cb2019-09-13 10:20:25 +010087 std::pair<Status, Window> win_config;
88
89 if(padding.empty())
90 {
91 win_config = validate_and_configure_window(input->info(), output->info());
92 }
93 else
94 {
95 win_config = validate_and_configure_window_with_padding(input->info(), output->info(), padding);
96 }
97
98 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
99 INEKernel::configure(win_config.second);
Michalis Spyrou1d382592018-08-13 17:28:04 +0100100}
101
Pablo Telloc9564cb2019-09-13 10:20:25 +0100102Status NECopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, const PaddingList &padding)
Michalis Spyrou1d382592018-08-13 17:28:04 +0100103{
Pablo Telloc9564cb2019-09-13 10:20:25 +0100104 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding));
105
106 if(padding.empty())
107 {
108 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
109 }
110 else
111 {
112 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_with_padding(input->clone().get(), output->clone().get(), padding).first);
113 }
114
Michalis Spyrou1d382592018-08-13 17:28:04 +0100115 return Status{};
116}
117
118void NECopyKernel::run(const Window &window, const ThreadInfo &info)
119{
120 ARM_COMPUTE_UNUSED(info);
121 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
122 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
123
Pablo Telloc9564cb2019-09-13 10:20:25 +0100124 if(_padding.empty())
Michalis Spyrou1d382592018-08-13 17:28:04 +0100125 {
Pablo Telloc9564cb2019-09-13 10:20:25 +0100126 Window output_window{ window };
127 output_window.set(Window::DimX, Window::Dimension(output_window.x().start(), output_window.x().end(), _input->info()->dimension(0)));
128 Window out_slice = output_window.first_slice_window_1D();
129 do
Michalis Spyrou1d382592018-08-13 17:28:04 +0100130 {
Pablo Telloc9564cb2019-09-13 10:20:25 +0100131 Iterator input_it(_input, out_slice);
132 Iterator output_it(_output, out_slice);
133
134 execute_window_loop(out_slice, [&](const Coordinates &)
135 {
136 memcpy(output_it.ptr(), input_it.ptr(), _output->info()->dimension(0) * _output->info()->element_size());
137 },
138 input_it, output_it);
139 }
140 while(output_window.slide_window_slice_1D(out_slice));
141 }
142 else
143 {
144 Window input_window{ window };
145 input_window.set(Window::DimX, Window::Dimension(0, window.x().end() - _padding[0].first, _input->info()->dimension(0)));
146
147 Iterator input_it(_input, input_window);
148 Iterator output_it(_output, window);
149 const size_t row_size_in_bytes = _input->info()->dimension(0) * _input->info()->element_size();
150 execute_window_loop(window, [&](const Coordinates &)
151 {
152 auto dst_ptr = output_it.ptr() + _padding[0].first * _output->info()->element_size();
153 std::memcpy(dst_ptr, input_it.ptr(), row_size_in_bytes);
Michalis Spyrou1d382592018-08-13 17:28:04 +0100154 },
155 input_it, output_it);
Michalis Spyrou1d382592018-08-13 17:28:04 +0100156 }
Michalis Spyrou1d382592018-08-13 17:28:04 +0100157}
Pablo Telloc9564cb2019-09-13 10:20:25 +0100158} // namespace arm_compute