blob: b0e1fcb3f86b838c51c5d95e381cf3fe1c32fff4 [file] [log] [blame]
Michalis Spyroub7b31532017-11-23 12:10:21 +00001/*
Isabella Gottardie82cb042019-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/NEDepthwiseWeightsReshapeKernel.h"
25
Michalis Spyroub7b31532017-11-23 12:10:21 +000026#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/NEON/INEKernel.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/TensorShape.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37using namespace arm_compute;
38
Georgios Pinitasd05dce42018-01-22 16:29:17 +000039namespace
40{
41template <typename T>
42void weights_reshape(const ITensor *input, const ITensor *bias, ITensor *output, const Window &window)
43{
44 const int input_w = input->info()->dimension(0);
45 const int output_stride_x = output->info()->strides_in_bytes().x();
46 const int output_stride_y = output->info()->strides_in_bytes().y();
47
48 Window window_in(window);
49 // The first three dimensions of the input are increased by the inner loops
50 window_in.set(Window::DimX, Window::Dimension(0, input->info()->dimension(0), input->info()->dimension(0)));
51 window_in.set(Window::DimY, Window::Dimension(0, input->info()->dimension(1), 1));
52 window_in.set(Window::DimZ, Window::Dimension(0, input->info()->dimension(2), 1));
53
54 // Setup output window
55 Window window_out;
56 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
57 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
58
59 Iterator in(input, window_in);
60 Iterator out(output, window_out);
61
62 execute_window_loop(window_in, [&](const Coordinates & id)
63 {
64 auto input_ptr = reinterpret_cast<T *>(in.ptr());
65 auto output_ptr = reinterpret_cast<T *>(out.ptr() + id.y() * input_w * output_stride_x + id.z() * output_stride_y);
66
67 for(int i = 0; i < input_w; ++i, ++input_ptr)
68 {
69 *(output_ptr + i) = *input_ptr;
70 }
71
72 if(bias != nullptr)
73 {
74 *(output_ptr + input_w) = *(reinterpret_cast<T *>(bias->ptr_to_element(Coordinates(id.z()))));
75 }
76 },
77 in, out);
78}
Abe Mbise7784c832018-05-31 16:48:41 +010079
80Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *biases)
81{
Anthony Barbiereaefd002018-07-20 17:49:35 +010082 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Abe Mbise7784c832018-05-31 16:48:41 +010083 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
84 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
85 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(input->data_type()) && (biases != nullptr));
86 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != output->dimension(1));
87 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(0) != (input->dimension(0) * input->dimension(1) + ((biases != nullptr) ? 1 : 0)));
88
89 if(biases != nullptr)
90 {
91 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
92 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != input->dimension(2));
93 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Isabella Gottardie82cb042019-02-14 18:07:36 +000094 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Abe Mbise7784c832018-05-31 16:48:41 +010095 }
96
97 return Status{};
98}
Georgios Pinitasd05dce42018-01-22 16:29:17 +000099} // namespace
100
Michalis Spyroub7b31532017-11-23 12:10:21 +0000101NEDepthwiseWeightsReshapeKernel::NEDepthwiseWeightsReshapeKernel()
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000102 : _func(nullptr), _input(nullptr), _output(nullptr), _biases(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000103{
104}
105
106void NEDepthwiseWeightsReshapeKernel::configure(const ITensor *input, ITensor *output, const ITensor *biases)
107{
Abe Mbise7784c832018-05-31 16:48:41 +0100108 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000109
Abe Mbise7784c832018-05-31 16:48:41 +0100110 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), (biases != nullptr) ? biases->info() : nullptr));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000111
112 _input = input;
113 _output = output;
114 _biases = biases;
115
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000116 switch(_input->info()->element_size())
117 {
118 case 4:
119 {
120 _func = &weights_reshape<uint32_t>;
121 break;
122 }
123 case 2:
124 {
125 _func = &weights_reshape<uint16_t>;
126 break;
127 }
128 case 1:
129 {
130 _func = &weights_reshape<uint8_t>;
131 break;
132 }
133 default:
134 {
135 ARM_COMPUTE_ERROR_ON("Element size not supported");
136 break;
137 }
138 }
139
Michalis Spyroub7b31532017-11-23 12:10:21 +0000140 // Configure kernel window
141 Window win = calculate_max_window(*input->info(), Steps());
142 // The NEDepthwiseWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
143 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
144
145 INEKernel::configure(win);
146}
147
Abe Mbise7784c832018-05-31 16:48:41 +0100148Status NEDepthwiseWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *biases)
149{
150 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
151 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, biases));
152 return Status{};
153}
154
Michalis Spyroub7b31532017-11-23 12:10:21 +0000155void NEDepthwiseWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info)
156{
157 ARM_COMPUTE_UNUSED(info);
158 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000159 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000160
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000161 if(_func != nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000162 {
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000163 (*_func)(_input, _biases, _output, window);
164 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000165}