blob: 6585fdb8b81d266aa9c1c2fd457c72b3444005a2 [file] [log] [blame]
Michalis Spyroub7b31532017-11-23 12:10:21 +00001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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
40NEDepthwiseWeightsReshapeKernel::NEDepthwiseWeightsReshapeKernel()
41 : _input(nullptr), _output(nullptr), _biases(nullptr)
42{
43}
44
45void NEDepthwiseWeightsReshapeKernel::configure(const ITensor *input, ITensor *output, const ITensor *biases)
46{
47 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
48 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
49 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
50 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(1));
51 ARM_COMPUTE_ERROR_ON(output->info()->dimension(0) != (input->info()->dimension(0) * input->info()->dimension(1) + ((biases != nullptr) ? 1 : 0)));
52
53 if(biases != nullptr)
54 {
55 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
56 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
57 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != input->info()->dimension(2));
58 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
59 }
60
61 _input = input;
62 _output = output;
63 _biases = biases;
64
65 // Configure kernel window
66 Window win = calculate_max_window(*input->info(), Steps());
67 // The NEDepthwiseWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
68 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
69
70 INEKernel::configure(win);
71}
72
73void NEDepthwiseWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info)
74{
75 ARM_COMPUTE_UNUSED(info);
76 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
77
78 const int input_w = _input->info()->dimension(0);
79 const int output_stride_x = _output->info()->strides_in_bytes().x();
80 const int output_stride_y = _output->info()->strides_in_bytes().y();
81
82 Window window_in(window);
83 // The first three dimensions of the input are increased by the inner loops
84 window_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0)));
85 window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1));
86 window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), 1));
87
88 // Setup output window
89 Window window_out;
90 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
91 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
92
93 Iterator in(_input, window_in);
94 Iterator out(_output, window_out);
95
96 execute_window_loop(window_in, [&](const Coordinates & id)
97 {
98 auto input_ptr = reinterpret_cast<float *>(in.ptr());
99 auto output_ptr = reinterpret_cast<float *>(out.ptr() + id.y() * input_w * output_stride_x + id.z() * output_stride_y);
100
101 for(int i = 0; i < input_w; ++i, ++input_ptr)
102 {
103 *(output_ptr + i) = *input_ptr;
104 }
105
106 if(_biases != nullptr)
107 {
108 *(output_ptr + input_w) = *(reinterpret_cast<float *>(_biases->ptr_to_element(Coordinates(id.z()))));
109 }
110 },
111 in, out);
112}