blob: 36b17bfc4ca86aabcf327b49c622690f48d2ba4c [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
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 +000040namespace
41{
42template <typename T>
43void weights_reshape(const ITensor *input, const ITensor *bias, ITensor *output, const Window &window)
44{
45 const int input_w = input->info()->dimension(0);
46 const int output_stride_x = output->info()->strides_in_bytes().x();
47 const int output_stride_y = output->info()->strides_in_bytes().y();
48
49 Window window_in(window);
50 // The first three dimensions of the input are increased by the inner loops
51 window_in.set(Window::DimX, Window::Dimension(0, input->info()->dimension(0), input->info()->dimension(0)));
52 window_in.set(Window::DimY, Window::Dimension(0, input->info()->dimension(1), 1));
53 window_in.set(Window::DimZ, Window::Dimension(0, input->info()->dimension(2), 1));
54
55 // Setup output window
56 Window window_out;
57 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
58 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
59
60 Iterator in(input, window_in);
61 Iterator out(output, window_out);
62
63 execute_window_loop(window_in, [&](const Coordinates & id)
64 {
65 auto input_ptr = reinterpret_cast<T *>(in.ptr());
66 auto output_ptr = reinterpret_cast<T *>(out.ptr() + id.y() * input_w * output_stride_x + id.z() * output_stride_y);
67
68 for(int i = 0; i < input_w; ++i, ++input_ptr)
69 {
70 *(output_ptr + i) = *input_ptr;
71 }
72
73 if(bias != nullptr)
74 {
75 *(output_ptr + input_w) = *(reinterpret_cast<T *>(bias->ptr_to_element(Coordinates(id.z()))));
76 }
77 },
78 in, out);
79}
80} // namespace
81
Michalis Spyroub7b31532017-11-23 12:10:21 +000082NEDepthwiseWeightsReshapeKernel::NEDepthwiseWeightsReshapeKernel()
Georgios Pinitasd05dce42018-01-22 16:29:17 +000083 : _func(nullptr), _input(nullptr), _output(nullptr), _biases(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +000084{
85}
86
87void NEDepthwiseWeightsReshapeKernel::configure(const ITensor *input, ITensor *output, const ITensor *biases)
88{
Georgios Pinitasd05dce42018-01-22 16:29:17 +000089 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Michalis Spyroub7b31532017-11-23 12:10:21 +000090 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
91 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
Georgios Pinitasd05dce42018-01-22 16:29:17 +000092 ARM_COMPUTE_ERROR_ON(is_data_type_quantized_asymmetric(input->info()->data_type()) && (biases != nullptr));
Michalis Spyroub7b31532017-11-23 12:10:21 +000093 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(1));
94 ARM_COMPUTE_ERROR_ON(output->info()->dimension(0) != (input->info()->dimension(0) * input->info()->dimension(1) + ((biases != nullptr) ? 1 : 0)));
95
96 if(biases != nullptr)
97 {
98 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
99 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
100 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != input->info()->dimension(2));
101 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
102 }
103
104 _input = input;
105 _output = output;
106 _biases = biases;
107
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000108 switch(_input->info()->element_size())
109 {
110 case 4:
111 {
112 _func = &weights_reshape<uint32_t>;
113 break;
114 }
115 case 2:
116 {
117 _func = &weights_reshape<uint16_t>;
118 break;
119 }
120 case 1:
121 {
122 _func = &weights_reshape<uint8_t>;
123 break;
124 }
125 default:
126 {
127 ARM_COMPUTE_ERROR_ON("Element size not supported");
128 break;
129 }
130 }
131
Michalis Spyroub7b31532017-11-23 12:10:21 +0000132 // Configure kernel window
133 Window win = calculate_max_window(*input->info(), Steps());
134 // The NEDepthwiseWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
135 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
136
137 INEKernel::configure(win);
138}
139
140void NEDepthwiseWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info)
141{
142 ARM_COMPUTE_UNUSED(info);
143 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000144 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000145
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000146 if(_func != nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000147 {
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000148 (*_func)(_input, _biases, _output, window);
149 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000150}