blob: 97c20dbd4eee6e57d5797d6b686c438a4bbebae7 [file] [log] [blame]
Anthony Barbierc8e84b52018-07-17 16:48:42 +01001/*
Georgios Pinitas7cd26d42019-01-09 18:35:17 +00002 * Copyright (c) 2018-2019 ARM Limited.
Anthony Barbierc8e84b52018-07-17 16:48:42 +01003 *
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
25#include "arm_compute/core/NEON/kernels/assembly/NEGEMMNativeWrapperKernel.h"
26
27#include "arm_compute/core/ITensor.h"
28#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/WindowIterator.h"
30
31#include "../arm_gemm/utils.hpp"
32#include "arm_gemm.hpp"
33
34#include "../arm_gemm/mergeresults.hpp"
35#include "../arm_gemm/transform.hpp"
36
Anthony Barbierc8e84b52018-07-17 16:48:42 +010037#include "../arm_gemm/kernels/a64_sgemm_native_16x4.hpp"
Anthony Barbierc8e84b52018-07-17 16:48:42 +010038
39namespace arm_compute
40{
Anthony Barbier3d677cc2018-07-23 16:42:59 +010041namespace
42{
Anthony Barbierc8e84b52018-07-17 16:48:42 +010043template <typename To, typename Tr>
44struct Kernel
45{
46};
47
48#ifdef __aarch64__
49template <>
50struct Kernel<float, float>
51{
52 using strategy = arm_gemm::sgemm_native_16x4;
53};
54#endif /* __aarch64__ */
55
Anthony Barbier3d677cc2018-07-23 16:42:59 +010056} // namespace
57
Anthony Barbierc8e84b52018-07-17 16:48:42 +010058template <typename To, typename Tr>
59Window NEGEMMNativeWrapperKernel<To, Tr>::configure_internal(float alpha, float beta)
60{
Michalis Spyrou43c1bf82019-05-24 11:57:38 +010061 ARM_COMPUTE_UNUSED(alpha);
Anthony Barbierc8e84b52018-07-17 16:48:42 +010062 using strategy = typename Kernel<To, Tr>::strategy;
63
64 _beta = beta;
65
66 //Note: The window is shifted down by 1 dimension compare to the tensors
67 Window window;
68 window.set(Window::DimX, Window::Dimension(0, ceil_to_multiple(_params.M, strategy::out_height()), strategy::out_height()));
69 window.set(Window::DimY, Window::Dimension(0, _params.batches));
70 window.set(Window::DimZ, Window::Dimension(0, _params.multis));
71
72 return window;
73}
74
75template <typename To, typename Tr>
76void NEGEMMNativeWrapperKernel<To, Tr>::run_internal(const Window &window, const Coordinates &start_offset, const Coordinates &end_offset, const ThreadInfo &info)
77{
78 using strategy = typename Kernel<To, Tr>::strategy;
79
80 TensorAccessor<To> a(*_a);
81 TensorAccessor<To> b(*_b);
82 TensorAccessor<Tr> c(*_c);
83
84 if(_a->info()->data_layout() == DataLayout::NHWC)
85 {
86 // In the case of NHWC we want to interpret the output shape as 3D. Thus, the batch stride for A is
87 // the relevant multiple of the row stride.
88 const size_t nhwc_batch_stride = _a->info()->strides_in_bytes().y() * _c->info()->dimension(1);
89 a.set_stride(2, nhwc_batch_stride);
90 }
91
92 unsigned int m_end = 0;
93
94 strategy strat(info.cpu_info);
95 auto window_iterator = arm_compute::create_window_iterator(window, start_offset, end_offset, [&](const Coordinates & id)
96 {
97 const unsigned int y0 = id.x();
98 const unsigned int batch = id.y();
99 const unsigned int multi = id.z();
100 const unsigned int ymax = std::min(y0 + strategy::out_height(), m_end);
101
Georgios Pinitasa4658ae2018-07-27 12:20:32 +0100102 // TODO(COMPMID-1424) : Agree on gemm IO layouts
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100103 strat.kernel(a(0, y0, batch, multi), a.stride(Window::DimY),
Georgios Pinitasa4658ae2018-07-27 12:20:32 +0100104 b(0, 0, multi), b.stride(Window::DimY),
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100105 c(0, y0, batch, multi), c.stride(Window::DimY),
106 _beta, (ymax - y0), _params.N, _params.K);
107 });
108
109 auto on_new_row_size = [&](unsigned int start, unsigned int end)
110 {
Michalis Spyrou43c1bf82019-05-24 11:57:38 +0100111 ARM_COMPUTE_UNUSED(start);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100112 m_end = std::min(end, _params.M);
113 };
114
115 window_iterator.iterate_3D(on_new_row_size);
116}
117
118#ifdef __aarch64__
119template class NEGEMMNativeWrapperKernel<float, float>;
120#endif /* __aarch64__ */
121
122} // namespace arm_compute