2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h"
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"
38 using namespace arm_compute;
40 NEDepthwiseVectorToTensorKernel::NEDepthwiseVectorToTensorKernel()
41 : _input(nullptr), _output(nullptr), _conv_dims()
45 void NEDepthwiseVectorToTensorKernel::configure(const ITensor *input, ITensor *output, size_t conv_w, size_t conv_h)
47 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
48 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
50 TensorShape output_shape = input->info()->tensor_shape();
51 output_shape.set(0, conv_w);
52 output_shape.set(1, conv_h);
53 output_shape.set(2, input->info()->tensor_shape()[0] / (conv_w * conv_h));
55 // Output auto inizialitation if not yet initialized
56 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
59 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
60 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
64 _conv_dims = std::pair<size_t, size_t>(conv_w, conv_h);
66 // Configure kernel window
67 Window win = calculate_max_window(*input->info(), Steps());
68 // The NEDepthwisevectorToTensorKernel doesn't need padding so update_window_and_padding() can be skipped
69 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
71 INEKernel::configure(win);
74 void NEDepthwiseVectorToTensorKernel::run(const Window &window, const ThreadInfo &info)
76 ARM_COMPUTE_UNUSED(info);
77 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
79 // const int input_w = _input->info()->dimension(0);
80 const int output_stride_x = _output->info()->strides_in_bytes().x();
81 const int output_stride_y = _output->info()->strides_in_bytes().y();
82 const int output_stride_z = _output->info()->strides_in_bytes().z();
84 // Setup output window
85 Window window_out(window);
86 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
87 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
88 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
90 Iterator in(_input, window);
91 Iterator out(_output, window_out);
93 const int patch_size = _conv_dims.first * _conv_dims.second;
95 execute_window_loop(window, [&](const Coordinates & id)
97 const int z = id.x() / patch_size;
98 const int index2D = id.x() - z * patch_size;
100 auto input_ptr = reinterpret_cast<float *>(in.ptr());
101 auto output_ptr = reinterpret_cast<float *>(out.ptr() + index2D % _conv_dims.first * output_stride_x + index2D / _conv_dims.first * output_stride_y + z * output_stride_z);
103 *output_ptr = *input_ptr;