blob: 37269cafaf9c0678ce6eb1151f23df876623058d [file] [log] [blame]
Michalis Spyroub7b31532017-11-23 12:10:21 +00001/*
Isabella Gottardi0a1090a2019-02-14 18:07:36 +00002 * Copyright (c) 2017-2019 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);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000054 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Abe Mbise7784c832018-05-31 16:48:41 +010055 }
56
57 return Status{};
58}
59} // namespace
Michalis Spyroub7b31532017-11-23 12:10:21 +000060
Georgios Pinitasd05dce42018-01-22 16:29:17 +000061template <typename T>
62void NEDepthwiseVectorToTensorKernel::vector_to_tensor(const Window &window)
Michalis Spyroub7b31532017-11-23 12:10:21 +000063{
Michalis Spyroub7b31532017-11-23 12:10:21 +000064 // const int input_w = _input->info()->dimension(0);
65 const int output_stride_x = _output->info()->strides_in_bytes().x();
66 const int output_stride_y = _output->info()->strides_in_bytes().y();
67 const int output_stride_z = _output->info()->strides_in_bytes().z();
68
69 // Setup output window
70 Window window_out(window);
71 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
72 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
73 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
74
75 Iterator in(_input, window);
76 Iterator out(_output, window_out);
77
78 const int patch_size = _conv_dims.first * _conv_dims.second;
79
80 execute_window_loop(window, [&](const Coordinates & id)
81 {
82 const int z = id.x() / patch_size;
83 const int index2D = id.x() - z * patch_size;
84
Georgios Pinitasd05dce42018-01-22 16:29:17 +000085 auto input_ptr = reinterpret_cast<T *>(in.ptr());
86 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 +000087
88 *output_ptr = *input_ptr;
89 },
90 in, out);
91}
Georgios Pinitasd05dce42018-01-22 16:29:17 +000092
93NEDepthwiseVectorToTensorKernel::NEDepthwiseVectorToTensorKernel()
94 : _func(nullptr), _input(nullptr), _output(nullptr), _conv_dims()
95{
96}
97
98void NEDepthwiseVectorToTensorKernel::configure(const ITensor *input, ITensor *output, size_t conv_w, size_t conv_h)
99{
Abe Mbise7784c832018-05-31 16:48:41 +0100100 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000101
102 // Output auto inizialitation if not yet initialized
Abe Mbise7784c832018-05-31 16:48:41 +0100103 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 +0000104 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
105
Abe Mbise7784c832018-05-31 16:48:41 +0100106 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), conv_w, conv_h));
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000107
108 _input = input;
109 _output = output;
110 _conv_dims = std::pair<size_t, size_t>(conv_w, conv_h);
111
112 // Set appropriate function to run
113 switch(input->info()->data_type())
114 {
115 case DataType::QASYMM8:
116 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<uint8_t>;
117 break;
118 case DataType::S32:
119 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<int32_t>;
120 break;
121 case DataType::F16:
122 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<half>;
123 break;
124 case DataType::F32:
125 _func = &NEDepthwiseVectorToTensorKernel::vector_to_tensor<float>;
126 break;
127 default:
128 ARM_COMPUTE_ERROR("Unsupported data type");
129 }
130
131 // Configure kernel window
132 Window win = calculate_max_window(*input->info(), Steps());
133 // The NEDepthwisevectorToTensorKernel doesn't need padding so update_window_and_padding() can be skipped
134 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
135
136 INEKernel::configure(win);
137}
138
Abe Mbise7784c832018-05-31 16:48:41 +0100139Status NEDepthwiseVectorToTensorKernel::validate(const ITensorInfo *input, const ITensorInfo *output, size_t conv_w, size_t conv_h)
140{
141 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
142 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, conv_w, conv_h));
143 return Status{};
144}
145
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000146void NEDepthwiseVectorToTensorKernel::run(const Window &window, const ThreadInfo &info)
147{
148 ARM_COMPUTE_UNUSED(info);
149 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
150 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
151
152 if(_func != nullptr)
153 {
154 (this->*_func)(window);
155 }
156}