blob: 2ceb39d217aa60a442717fa0f3e7ca407f6a8242 [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/NEDepthwiseIm2ColKernel.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
40NEDepthwiseIm2ColKernel::NEDepthwiseIm2ColKernel()
41 : _input(nullptr), _output(nullptr), _kernel_dims(), _conv_info(), _has_bias()
42{
43}
44
45void NEDepthwiseIm2ColKernel::configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
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(2));
51 ARM_COMPUTE_ERROR_ON(output->info()->dimension(0) != (kernel_dims.width * kernel_dims.height + ((has_bias) ? 1 : 0)));
52
53 _input = input;
54 _output = output;
55 _kernel_dims = kernel_dims;
56 _conv_info = conv_info;
57 _has_bias = has_bias;
58
59 // Configure kernel window
60 Window win = calculate_max_window(*input->info(), Steps());
61
62 // The NEDepthwiseIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped
63 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
64
65 INEKernel::configure(win);
66}
67
68void NEDepthwiseIm2ColKernel::run(const Window &window, const ThreadInfo &info)
69{
70 ARM_COMPUTE_UNUSED(info);
71 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
72
73 //const int kernel_depth = _input->info()->dimension(2);
74 const int input_w = _input->info()->dimension(0);
75 const int input_h = _input->info()->dimension(1);
76 const int input_stride_x = _input->info()->strides_in_bytes().x();
77 const int input_stride_y = _input->info()->strides_in_bytes().y();
78 const int input_stride_z = _input->info()->strides_in_bytes().z();
79 const int stride_x = _conv_info.stride().first;
80 const int stride_y = _conv_info.stride().second;
81
82 const int pad_left = _conv_info.pad_left();
83 const int pad_right = _conv_info.pad_right();
84 const int pad_top = _conv_info.pad_top();
85
86 Window window_in(window);
87 // The first three dimensions of the input are increased by the inner loops
88 window_in.set(Window::DimX, Window::Dimension(0, 0, 0));
89 window_in.set(Window::DimY, Window::Dimension(0, 0, 0));
90 window_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
91
92 // Setup output window
93 Window window_out(window);
94 window_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _output->info()->dimension(0)));
95 window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1));
96 window_out.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), 1));
97
98 Iterator in(_input, window_in);
99 Iterator out(_output, window_out);
100
101 const int full_length = input_w + pad_left + pad_right;
102 const int max_initial_x = stride_x * (((full_length - _kernel_dims.width) / stride_x) + 1);
103
104 execute_window_loop(window_out, [&](const Coordinates & id)
105 {
106 const int src_pixel_linear = id.y() * stride_x;
107
108 const int src_x = -pad_left + src_pixel_linear % max_initial_x;
109 const int src_y = -pad_top + src_pixel_linear / max_initial_x * stride_y;
110
111 // Get pointers
112 const uint8_t *const input_ptr = in.ptr() + id.z() * input_stride_z;
113 auto output_ptr = reinterpret_cast<float *>(out.ptr());
114 const int height = src_y + _kernel_dims.height;
115 const int width = src_x + _kernel_dims.width;
116
117 for(int y = src_y; y < height; ++y)
118 {
119 for(int x = src_x; x < width; ++x, ++output_ptr)
120 {
121 if(x < 0 || x >= input_w || y < 0 || y >= input_h)
122 {
123 *output_ptr = 0;
124 }
125 else
126 {
127 *output_ptr = *(reinterpret_cast<const float *>(input_ptr + x * input_stride_x + y * input_stride_y));
128 }
129 }
130 }
131
132 if(_has_bias)
133 {
134 *output_ptr = static_cast<float>(1);
135 }
136 },
137 in, out);
138}