blob: 9b36df3c39b74f0992109342e877213bc6ffddc8 [file] [log] [blame]
Michalis Spyroub7b31532017-11-23 12:10:21 +00001/*
2 * Copyright (c) 2017 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/NEDepthwiseVectorToTensorKernel.h"
25
26#include "arm_compute/core/AccessWindowTranspose.h"
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/NEON/INEKernel.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/TensorShape.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
38using namespace arm_compute;
39
40NEDepthwiseVectorToTensorKernel::NEDepthwiseVectorToTensorKernel()
41 : _input(nullptr), _output(nullptr), _conv_dims()
42{
43}
44
45void NEDepthwiseVectorToTensorKernel::configure(const ITensor *input, ITensor *output, size_t conv_w, size_t conv_h)
46{
47 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Gian Marcobfa3b522017-12-12 10:08:38 +000048 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
49
50 TensorShape output_shape = input->info()->tensor_shape();
51 output_shape.set(0, conv_w);
52 output_shape.set(1, conv_h);
53 output_shape.set(2, input->info()->tensor_shape()[0] / (conv_w * conv_h));
54
55 // Output auto inizialitation if not yet initialized
56 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
57
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Michalis Spyroub7b31532017-11-23 12:10:21 +000059 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
60 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
61
62 _input = input;
63 _output = output;
64 _conv_dims = std::pair<size_t, size_t>(conv_w, conv_h);
65
66 // Configure kernel window
67 Window win = calculate_max_window(*input->info(), Steps());
68 // The NEDepthwisevectorToTensorKernel doesn't need padding so update_window_and_padding() can be skipped
69 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
70
71 INEKernel::configure(win);
72}
73
74void NEDepthwiseVectorToTensorKernel::run(const Window &window, const ThreadInfo &info)
75{
76 ARM_COMPUTE_UNUSED(info);
77 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
78
79 // const int input_w = _input->info()->dimension(0);
80 const int output_stride_x = _output->info()->strides_in_bytes().x();
81 const int output_stride_y = _output->info()->strides_in_bytes().y();
82 const int output_stride_z = _output->info()->strides_in_bytes().z();
83
84 // Setup output window
85 Window window_out(window);
86 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
87 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
88 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
89
90 Iterator in(_input, window);
91 Iterator out(_output, window_out);
92
93 const int patch_size = _conv_dims.first * _conv_dims.second;
94
95 execute_window_loop(window, [&](const Coordinates & id)
96 {
97 const int z = id.x() / patch_size;
98 const int index2D = id.x() - z * patch_size;
99
100 auto input_ptr = reinterpret_cast<float *>(in.ptr());
101 auto output_ptr = reinterpret_cast<float *>(out.ptr() + index2D % _conv_dims.first * output_stride_x + index2D / _conv_dims.first * output_stride_y + z * output_stride_z);
102
103 *output_ptr = *input_ptr;
104 },
105 in, out);
106}