blob: 921582a41d3ef89755a3aba478e2f5f26d279100 [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
Anthony Barbiereaefd002018-07-20 17:49:35 +010026#include "arm_compute/core/CPP/Validate.h"
Michalis Spyroub7b31532017-11-23 12:10:21 +000027#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"
Abe Mbise7784c832018-05-31 16:48:41 +010037#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyroub7b31532017-11-23 12:10:21 +000038
39using namespace arm_compute;
Abe Mbise7784c832018-05-31 16:48:41 +010040using namespace arm_compute::misc::shape_calculator;
41
42namespace
43{
44Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, size_t conv_w, size_t conv_h)
45{
Anthony Barbiereaefd002018-07-20 17:49:35 +010046 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Abe Mbise7784c832018-05-31 16:48:41 +010047 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32);
48
49 if(output->total_size() != 0)
50 {
51 TensorShape output_shape = compute_vector_to_tensor_output_shape(input->tensor_shape(), conv_w, conv_h, output->data_layout());
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
54 }
55
56 return Status{};
57}
58} // namespace
Michalis Spyroub7b31532017-11-23 12:10:21 +000059
Georgios Pinitasd05dce42018-01-22 16:29:17 +000060template <typename T>
61void NEDepthwiseVectorToTensorKernel::vector_to_tensor(const Window &window)
Michalis Spyroub7b31532017-11-23 12:10:21 +000062{
Michalis Spyroub7b31532017-11-23 12:10:21 +000063 // const int input_w = _input->info()->dimension(0);
64 const int output_stride_x = _output->info()->strides_in_bytes().x();
65 const int output_stride_y = _output->info()->strides_in_bytes().y();
66 const int output_stride_z = _output->info()->strides_in_bytes().z();
67
68 // Setup output window
69 Window window_out(window);
70 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
71 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
72 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
73
74 Iterator in(_input, window);
75 Iterator out(_output, window_out);
76
77 const int patch_size = _conv_dims.first * _conv_dims.second;
78
79 execute_window_loop(window, [&](const Coordinates & id)
80 {
81 const int z = id.x() / patch_size;
82 const int index2D = id.x() - z * patch_size;
83
Georgios Pinitasd05dce42018-01-22 16:29:17 +000084 auto input_ptr = reinterpret_cast<T *>(in.ptr());
85 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 +000086
87 *output_ptr = *input_ptr;
88 },
89 in, out);
90}
Georgios Pinitasd05dce42018-01-22 16:29:17 +000091
92NEDepthwiseVectorToTensorKernel::NEDepthwiseVectorToTensorKernel()
93 : _func(nullptr), _input(nullptr), _output(nullptr), _conv_dims()
94{
95}
96
97void NEDepthwiseVectorToTensorKernel::configure(const ITensor *input, ITensor *output, size_t conv_w, size_t conv_h)
98{
Abe Mbise7784c832018-05-31 16:48:41 +010099 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000100
101 // Output auto inizialitation if not yet initialized
Abe Mbise7784c832018-05-31 16:48:41 +0100102 TensorShape output_shape = compute_vector_to_tensor_output_shape(input->info()->tensor_shape(), conv_w, conv_h, output->info()->data_layout());
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000103 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
104
Abe Mbise7784c832018-05-31 16:48:41 +0100105 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), conv_w, conv_h));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000106
107 _input = input;
108 _output = output;
109 _conv_dims = std::pair<size_t, size_t>(conv_w, conv_h);
110
111 // Set appropriate function to run
112 switch(input->info()->data_type())
113 {
114 case DataType::QASYMM8:
115 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<uint8_t>;
116 break;
117 case DataType::S32:
118 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<int32_t>;
119 break;
120 case DataType::F16:
121 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<half>;
122 break;
123 case DataType::F32:
124 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<float>;
125 break;
126 default:
127 ARM_COMPUTE_ERROR("Unsupported data type");
128 }
129
130 // Configure kernel window
131 Window win = calculate_max_window(*input->info(), Steps());
132 // The NEDepthwisevectorToTensorKernel doesn't need padding so update_window_and_padding() can be skipped
133 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
134
135 INEKernel::configure(win);
136}
137
Abe Mbise7784c832018-05-31 16:48:41 +0100138Status NEDepthwiseVectorToTensorKernel::validate(const ITensorInfo *input, const ITensorInfo *output, size_t conv_w, size_t conv_h)
139{
140 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
141 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, conv_w, conv_h));
142 return Status{};
143}
144
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000145void NEDepthwiseVectorToTensorKernel::run(const Window &window, const ThreadInfo &info)
146{
147 ARM_COMPUTE_UNUSED(info);
148 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
149 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
150
151 if(_func != nullptr)
152 {
153 (this->*_func)(window);
154 }
155}