blob: db433c99a855ac60b6fc81c630dd95804776a8a1 [file] [log] [blame]
Michele Di Giorgio93b75e02021-06-21 12:00:43 +01001/*
2 * Copyright (c) 2016-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/CpuGemmInterleave4x4Kernel.h"
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010025
26#include "arm_compute/core/ITensor.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010028#include "arm_compute/core/Validate.h"
29#include "arm_compute/core/Window.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010031#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
33
34#include <arm_neon.h>
35
36namespace arm_compute
37{
38namespace cpu
39{
40namespace kernels
41{
42using namespace arm_compute::misc::shape_calculator;
43
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010044void CpuGemmInterleave4x4Kernel::configure(const ITensorInfo *src, ITensorInfo *dst)
45{
46 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
47
48 // dst auto inizialitation if not yet initialized
49 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_interleaved_shape(*src)));
50
51 // Perform validate step
52 ARM_COMPUTE_ERROR_THROW_ON(CpuGemmInterleave4x4Kernel::validate(src, dst));
53
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010054 Window win = calculate_max_window(*src, Steps(1, 4));
55 ICPPKernel::configure(win);
56}
57
58Status CpuGemmInterleave4x4Kernel::validate(const ITensorInfo *src, const ITensorInfo *dst)
59{
60 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
61 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src) is not needed here as this kernel doesn't use CPU FP16 instructions.
62 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
63
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064 if (dst->total_size() != 0)
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010065 {
66 const TensorShape dst_shape = compute_interleaved_shape(*src);
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), dst_shape);
68 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
70 }
71
72 return Status{};
73}
74
75void CpuGemmInterleave4x4Kernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
76{
77 ARM_COMPUTE_UNUSED(info);
78 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
79 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010080 ARM_COMPUTE_ERROR_ON(tensors.empty());
81 /*
82 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
83 * |a00 a01 a02 a03|
84 * |a10 a11 a12 a13|
85 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
86 * |a30 a31 a32 a33|
87 *
88 * After this operation, the dst matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ]
89 */
90 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC);
91 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
92
Michele Di Giorgio2ef59b92021-06-28 18:06:14 +010093 const size_t window_start_x = window.x().start();
94 const size_t window_end_x = window.x().end();
95
96 const size_t in_height = src->info()->dimension(1);
97 const size_t in_stride = src->info()->strides_in_bytes()[1];
98
99 const size_t partial_y = in_height % 4;
100
101 const size_t element_size = src->info()->element_size();
102
103 // Set window for the src tensor
104 Window win = window;
105 win.set(Window::DimX, Window::Dimension(0, 1, 1));
106
107 // Set window for the dst tensor
108 Window win_out(window);
109 win_out.set(Window::DimX, Window::Dimension(0, 1, 1));
110 win_out.scale(Window::DimY, 0.25f);
111
112 Iterator in(src, win);
113 Iterator out(dst, win_out);
114
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100115 execute_window_loop(
116 win,
117 [&](const Coordinates &id)
Michele Di Giorgio2ef59b92021-06-28 18:06:14 +0100118 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 if (id.y() + 4 <= static_cast<int>(in_height))
Michele Di Giorgio2ef59b92021-06-28 18:06:14 +0100120 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 for (size_t x = window_start_x; x < window_end_x; ++x)
Michele Di Giorgio2ef59b92021-06-28 18:06:14 +0100122 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 std::memcpy(out.ptr() + (x * 4 + 0) * element_size, (in.ptr() + 0 * in_stride) + x * element_size,
124 element_size);
125 std::memcpy(out.ptr() + (x * 4 + 1) * element_size, (in.ptr() + 1 * in_stride) + x * element_size,
126 element_size);
127 std::memcpy(out.ptr() + (x * 4 + 2) * element_size, (in.ptr() + 2 * in_stride) + x * element_size,
128 element_size);
129 std::memcpy(out.ptr() + (x * 4 + 3) * element_size, (in.ptr() + 3 * in_stride) + x * element_size,
130 element_size);
Michele Di Giorgio2ef59b92021-06-28 18:06:14 +0100131 }
132 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133 else
134 {
135 for (size_t x = window_start_x; x < window_end_x; ++x)
136 {
137 size_t y = 0;
138 for (; y < partial_y; ++y)
139 {
140 std::memcpy(out.ptr() + (x * 4 + y) * element_size,
141 (in.ptr() + y * in_stride) + x * element_size, element_size);
142 }
143 for (; y < 4; ++y)
144 {
145 std::memset(out.ptr() + (x * 4 + y) * element_size, 0, element_size);
146 }
147 }
148 }
149 },
150 in, out);
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100151}
152
153const char *CpuGemmInterleave4x4Kernel::name() const
154{
155 return "CpuGemmInterleave4x4Kernel";
156}
157} // namespace kernels
158} // namespace cpu
159} // namespace arm_compute