blob: 297ba63826fe39c68a62d9fefa12d0d750abba92 [file] [log] [blame]
Manuel Bottini29599d02021-07-06 15:01:35 +01001/*
2 * Copyright (c) 2017-2021 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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuWeightsReshapeKernel.h"
Manuel Bottini29599d02021-07-06 15:01:35 +010025
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/Validate.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Manuel Bottini29599d02021-07-06 15:01:35 +010029#include "src/core/helpers/AutoConfiguration.h"
30#include "src/core/helpers/WindowHelpers.h"
31
32namespace arm_compute
33{
34namespace cpu
35{
36namespace kernels
37{
38namespace
39{
40TensorShape get_output_shape(const ITensorInfo *src, bool has_bias)
41{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042 TensorShape output_shape{src->tensor_shape()};
Manuel Bottini29599d02021-07-06 15:01:35 +010043
44 output_shape.collapse(3);
45 const size_t tmp_dim = output_shape[0];
46 output_shape.set(0, output_shape[1]);
47 output_shape.set(1, tmp_dim + (has_bias ? 1 : 0));
48
49 return output_shape;
50}
51
52Status validate_arguments(const ITensorInfo *src, const ITensorInfo *biases, const ITensorInfo *dst)
53{
54 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
55 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src) is not needed here as this kernel doesn't use CPU FP16 instructions.
56 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
57
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 if (biases != nullptr)
Manuel Bottini29599d02021-07-06 15:01:35 +010059 {
60 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(src->data_type()));
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases);
62 ARM_COMPUTE_RETURN_ERROR_ON((src->num_dimensions() == 4) && (biases->num_dimensions() != 1));
63 ARM_COMPUTE_RETURN_ERROR_ON((src->num_dimensions() == 5) && (biases->num_dimensions() != 2));
64 ARM_COMPUTE_RETURN_ERROR_ON((src->num_dimensions() == 4) && (biases->dimension(0) != src->tensor_shape()[3]));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 ARM_COMPUTE_RETURN_ERROR_ON((src->num_dimensions() == 5) && (biases->dimension(0) != src->tensor_shape()[3] ||
66 biases->dimension(1) != src->tensor_shape()[4]));
Manuel Bottini29599d02021-07-06 15:01:35 +010067 }
68
69 // Checks performed when output is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 if (dst->total_size() != 0)
Manuel Bottini29599d02021-07-06 15:01:35 +010071 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(),
73 get_output_shape(src, biases != nullptr));
Manuel Bottini29599d02021-07-06 15:01:35 +010074 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
76 }
77
78 return Status{};
79}
80} // namespace
81
82void CpuWeightsReshapeKernel::configure(const ITensorInfo *src, const ITensorInfo *biases, ITensorInfo *dst)
83{
84 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
85
86 // Output tensor auto inizialitation if not yet initialized
87 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(get_output_shape(src, (biases != nullptr))));
88
89 // Perform validation step
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, biases, dst));
Manuel Bottini29599d02021-07-06 15:01:35 +010091
92 // Configure kernel
93 Window window = calculate_max_window(*src, Steps());
94 window.set(Window::DimX, Window::Dimension(0, src->dimension(0), src->dimension(0)));
95 window.set(Window::DimY, Window::Dimension(0, src->dimension(1), src->dimension(1)));
96 window.set(Window::DimZ, Window::Dimension(0, src->dimension(2), src->dimension(2)));
97 ICpuKernel::configure(window);
98}
99
100Status CpuWeightsReshapeKernel::validate(const ITensorInfo *src, const ITensorInfo *biases, const ITensorInfo *dst)
101{
102 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, biases, dst));
103 return Status{};
104}
105
106void CpuWeightsReshapeKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
107{
108 ARM_COMPUTE_UNUSED(info);
109 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
110 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
111
112 auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
113 auto biases = tensors.get_const_tensor(TensorType::ACL_BIAS);
114 auto dst = tensors.get_tensor(TensorType::ACL_DST);
115
116 const unsigned int kernel_size_x = src->info()->dimension(0);
117 const unsigned int kernel_size_y = src->info()->dimension(1);
118 const unsigned int kernel_depth = src->info()->dimension(2);
119 const unsigned int input_stride_x = src->info()->strides_in_bytes().x();
120 const unsigned int input_stride_y = src->info()->strides_in_bytes().y();
121 const unsigned int input_stride_z = src->info()->strides_in_bytes().z();
122 const unsigned int output_stride_y = dst->info()->strides_in_bytes().y();
123
124 // Create iterators
125 Iterator in(src, window);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 execute_window_loop(
127 window,
128 [&](const Coordinates &id)
Manuel Bottini29599d02021-07-06 15:01:35 +0100129 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100130 // Get column index
131 const int kernel_idx = id[3];
132 const int kernel_idz = id[4];
133
134 // Setup pointers
135 const uint8_t *tmp_input_ptr = in.ptr();
136 uint8_t *tmp_output_ptr = dst->ptr_to_element(Coordinates(kernel_idx, 0, kernel_idz));
137 const uint8_t *curr_input_row_ptr = tmp_input_ptr;
138 const uint8_t *curr_input_depth_ptr = tmp_input_ptr;
139
140 // Linearize volume
141 for (unsigned int d = 0; d < kernel_depth; ++d)
Manuel Bottini29599d02021-07-06 15:01:35 +0100142 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 for (unsigned int j = 0; j < kernel_size_y; ++j)
Manuel Bottini29599d02021-07-06 15:01:35 +0100144 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 for (unsigned int i = 0; i < kernel_size_x; ++i)
146 {
147 std::memcpy(tmp_output_ptr, tmp_input_ptr, src->info()->element_size());
148 tmp_input_ptr += input_stride_x;
149 tmp_output_ptr += output_stride_y;
150 }
151 curr_input_row_ptr += input_stride_y;
152 tmp_input_ptr = curr_input_row_ptr;
Manuel Bottini29599d02021-07-06 15:01:35 +0100153 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 curr_input_depth_ptr += input_stride_z;
155 curr_input_row_ptr = curr_input_depth_ptr;
156 tmp_input_ptr = curr_input_depth_ptr;
Manuel Bottini29599d02021-07-06 15:01:35 +0100157 }
Manuel Bottini29599d02021-07-06 15:01:35 +0100158
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 // Add bias
160 if (biases != nullptr)
161 {
162 std::memcpy(tmp_output_ptr, biases->ptr_to_element(Coordinates(kernel_idx, kernel_idz)),
163 src->info()->element_size());
164 }
165 },
166 in);
Manuel Bottini29599d02021-07-06 15:01:35 +0100167}
168const char *CpuWeightsReshapeKernel::name() const
169{
170 return "CpuWeightsReshapeKernel";
171}
172} // namespace kernels
173} // namespace cpu
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100174} // namespace arm_compute