blob: 77ab5adb14c8439953d97b392e01bda596913fe5 [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/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);
94 }
95
96 return Status{};
97}
Georgios Pinitasd05dce42018-01-22 16:29:17 +000098} // namespace
99
Michalis Spyroub7b31532017-11-23 12:10:21 +0000100NEDepthwiseWeightsReshapeKernel::NEDepthwiseWeightsReshapeKernel()
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000101 : _func(nullptr), _input(nullptr), _output(nullptr), _biases(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000102{
103}
104
105void NEDepthwiseWeightsReshapeKernel::configure(const ITensor *input, ITensor *output, const ITensor *biases)
106{
Abe Mbise7784c832018-05-31 16:48:41 +0100107 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000108
Abe Mbise7784c832018-05-31 16:48:41 +0100109 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), (biases != nullptr) ? biases->info() : nullptr));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000110
111 _input = input;
112 _output = output;
113 _biases = biases;
114
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000115 switch(_input->info()->element_size())
116 {
117 case 4:
118 {
119 _func = &weights_reshape<uint32_t>;
120 break;
121 }
122 case 2:
123 {
124 _func = &weights_reshape<uint16_t>;
125 break;
126 }
127 case 1:
128 {
129 _func = &weights_reshape<uint8_t>;
130 break;
131 }
132 default:
133 {
134 ARM_COMPUTE_ERROR_ON("Element size not supported");
135 break;
136 }
137 }
138
Michalis Spyroub7b31532017-11-23 12:10:21 +0000139 // Configure kernel window
140 Window win = calculate_max_window(*input->info(), Steps());
141 // The NEDepthwiseWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
142 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
143
144 INEKernel::configure(win);
145}
146
Abe Mbise7784c832018-05-31 16:48:41 +0100147Status NEDepthwiseWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *biases)
148{
149 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
150 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, biases));
151 return Status{};
152}
153
Michalis Spyroub7b31532017-11-23 12:10:21 +0000154void NEDepthwiseWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info)
155{
156 ARM_COMPUTE_UNUSED(info);
157 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000158 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000159
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000160 if(_func != nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000161 {
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000162 (*_func)(_input, _biases, _output, window);
163 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000164}