blob: ecdb5a938c5573387e899677be9e468f32cca4f4 [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
Georgios Pinitas37d080f2019-06-21 18:43:12 +010084 // Handle 3d input re-interpretation
85 if(_gemm_info.reinterpret_input_as_3d())
Anthony Barbierc8e84b52018-07-17 16:48:42 +010086 {
Georgios Pinitas37d080f2019-06-21 18:43:12 +010087 Strides a_strides_as_3d = _a->info()->strides_in_bytes();
88 a_strides_as_3d.remove(Window::DimZ);
89 a.set_strides(a_strides_as_3d);
90 }
91
92 // Handle 3d output re-interpretation
93 if(_gemm_info.depth_output_gemm3d() != 0)
94 {
95 Strides c_strides_as_3d = _c->info()->strides_in_bytes();
96 c_strides_as_3d.remove(Window::DimZ);
97 c.set_strides(c_strides_as_3d);
Anthony Barbierc8e84b52018-07-17 16:48:42 +010098 }
99
100 unsigned int m_end = 0;
101
102 strategy strat(info.cpu_info);
103 auto window_iterator = arm_compute::create_window_iterator(window, start_offset, end_offset, [&](const Coordinates & id)
104 {
105 const unsigned int y0 = id.x();
106 const unsigned int batch = id.y();
107 const unsigned int multi = id.z();
108 const unsigned int ymax = std::min(y0 + strategy::out_height(), m_end);
109
Georgios Pinitasa4658ae2018-07-27 12:20:32 +0100110 // TODO(COMPMID-1424) : Agree on gemm IO layouts
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100111 strat.kernel(a(0, y0, batch, multi), a.stride(Window::DimY),
Georgios Pinitasa4658ae2018-07-27 12:20:32 +0100112 b(0, 0, multi), b.stride(Window::DimY),
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100113 c(0, y0, batch, multi), c.stride(Window::DimY),
114 _beta, (ymax - y0), _params.N, _params.K);
115 });
116
117 auto on_new_row_size = [&](unsigned int start, unsigned int end)
118 {
Michalis Spyrou43c1bf82019-05-24 11:57:38 +0100119 ARM_COMPUTE_UNUSED(start);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100120 m_end = std::min(end, _params.M);
121 };
122
123 window_iterator.iterate_3D(on_new_row_size);
124}
125
126#ifdef __aarch64__
127template class NEGEMMNativeWrapperKernel<float, float>;
128#endif /* __aarch64__ */
129
130} // namespace arm_compute