blob: 8960d8a8af59c061d17a9b9f2495aa382e221a17 [file] [log] [blame]
Michalis Spyroub7b31532017-11-23 12:10:21 +00001/*
Georgios Pinitasd05dce42018-01-22 16:29:17 +00002 * Copyright (c) 2017-2018 ARM Limited.
Michalis Spyroub7b31532017-11-23 12:10:21 +00003 *
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
Georgios Pinitasd05dce42018-01-22 16:29:17 +000040template <typename T>
41void NEDepthwiseVectorToTensorKernel::vector_to_tensor(const Window &window)
Michalis Spyroub7b31532017-11-23 12:10:21 +000042{
Michalis Spyroub7b31532017-11-23 12:10:21 +000043 // const int input_w = _input->info()->dimension(0);
44 const int output_stride_x = _output->info()->strides_in_bytes().x();
45 const int output_stride_y = _output->info()->strides_in_bytes().y();
46 const int output_stride_z = _output->info()->strides_in_bytes().z();
47
48 // Setup output window
49 Window window_out(window);
50 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
51 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
52 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
53
54 Iterator in(_input, window);
55 Iterator out(_output, window_out);
56
57 const int patch_size = _conv_dims.first * _conv_dims.second;
58
59 execute_window_loop(window, [&](const Coordinates & id)
60 {
61 const int z = id.x() / patch_size;
62 const int index2D = id.x() - z * patch_size;
63
Georgios Pinitasd05dce42018-01-22 16:29:17 +000064 auto input_ptr = reinterpret_cast<T *>(in.ptr());
65 auto output_ptr = reinterpret_cast<T *>(out.ptr() + index2D % _conv_dims.first * output_stride_x + index2D / _conv_dims.first * output_stride_y + z * output_stride_z);
Michalis Spyroub7b31532017-11-23 12:10:21 +000066
67 *output_ptr = *input_ptr;
68 },
69 in, out);
70}
Georgios Pinitasd05dce42018-01-22 16:29:17 +000071
72NEDepthwiseVectorToTensorKernel::NEDepthwiseVectorToTensorKernel()
73 : _func(nullptr), _input(nullptr), _output(nullptr), _conv_dims()
74{
75}
76
77void NEDepthwiseVectorToTensorKernel::configure(const ITensor *input, ITensor *output, size_t conv_w, size_t conv_h)
78{
79 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32);
80 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
81
82 TensorShape output_shape = input->info()->tensor_shape();
83 output_shape.set(0, conv_w);
84 output_shape.set(1, conv_h);
85 output_shape.set(2, input->info()->tensor_shape()[0] / (conv_w * conv_h));
86
87 // Output auto inizialitation if not yet initialized
88 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
89
90 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
91 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
92 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
93
94 _input = input;
95 _output = output;
96 _conv_dims = std::pair<size_t, size_t>(conv_w, conv_h);
97
98 // Set appropriate function to run
99 switch(input->info()->data_type())
100 {
101 case DataType::QASYMM8:
102 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<uint8_t>;
103 break;
104 case DataType::S32:
105 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<int32_t>;
106 break;
107 case DataType::F16:
108 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<half>;
109 break;
110 case DataType::F32:
111 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<float>;
112 break;
113 default:
114 ARM_COMPUTE_ERROR("Unsupported data type");
115 }
116
117 // Configure kernel window
118 Window win = calculate_max_window(*input->info(), Steps());
119 // The NEDepthwisevectorToTensorKernel doesn't need padding so update_window_and_padding() can be skipped
120 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
121
122 INEKernel::configure(win);
123}
124
125void NEDepthwiseVectorToTensorKernel::run(const Window &window, const ThreadInfo &info)
126{
127 ARM_COMPUTE_UNUSED(info);
128 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
129 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
130
131 if(_func != nullptr)
132 {
133 (this->*_func)(window);
134 }
135}