blob: e452dfbcf28400de36180f1f067c8cada12f722f [file] [log] [blame]
Anthony Barbierc8e84b52018-07-17 16:48:42 +01001/*
2 * Copyright (c) 2018 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 */
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
37#include "../arm_gemm/kernels/a32_sgemm_8x6.hpp"
38#include "../arm_gemm/kernels/a64_sgemm_12x8.hpp"
39#include "../arm_gemm/kernels/a64_sgemm_native_16x4.hpp"
40#include "../arm_gemm/kernels/a64_sgemv_pretransposed.hpp"
41#include "../arm_gemm/kernels/a64_sgemv_trans.hpp"
42
43namespace arm_compute
44{
Anthony Barbier3d677cc2018-07-23 16:42:59 +010045namespace
46{
Anthony Barbierc8e84b52018-07-17 16:48:42 +010047template <typename To, typename Tr>
48struct Kernel
49{
50};
51
52#ifdef __aarch64__
53template <>
54struct Kernel<float, float>
55{
56 using strategy = arm_gemm::sgemm_native_16x4;
57};
58#endif /* __aarch64__ */
59
Anthony Barbier3d677cc2018-07-23 16:42:59 +010060} // namespace
61
Anthony Barbierc8e84b52018-07-17 16:48:42 +010062template <typename To, typename Tr>
63Window NEGEMMNativeWrapperKernel<To, Tr>::configure_internal(float alpha, float beta)
64{
65 using strategy = typename Kernel<To, Tr>::strategy;
66
67 _beta = beta;
68
69 //Note: The window is shifted down by 1 dimension compare to the tensors
70 Window window;
71 window.set(Window::DimX, Window::Dimension(0, ceil_to_multiple(_params.M, strategy::out_height()), strategy::out_height()));
72 window.set(Window::DimY, Window::Dimension(0, _params.batches));
73 window.set(Window::DimZ, Window::Dimension(0, _params.multis));
74
75 return window;
76}
77
78template <typename To, typename Tr>
79void NEGEMMNativeWrapperKernel<To, Tr>::run_internal(const Window &window, const Coordinates &start_offset, const Coordinates &end_offset, const ThreadInfo &info)
80{
81 using strategy = typename Kernel<To, Tr>::strategy;
82
83 TensorAccessor<To> a(*_a);
84 TensorAccessor<To> b(*_b);
85 TensorAccessor<Tr> c(*_c);
86
87 if(_a->info()->data_layout() == DataLayout::NHWC)
88 {
89 // In the case of NHWC we want to interpret the output shape as 3D. Thus, the batch stride for A is
90 // the relevant multiple of the row stride.
91 const size_t nhwc_batch_stride = _a->info()->strides_in_bytes().y() * _c->info()->dimension(1);
92 a.set_stride(2, nhwc_batch_stride);
93 }
94
95 unsigned int m_end = 0;
96
97 strategy strat(info.cpu_info);
98 auto window_iterator = arm_compute::create_window_iterator(window, start_offset, end_offset, [&](const Coordinates & id)
99 {
100 const unsigned int y0 = id.x();
101 const unsigned int batch = id.y();
102 const unsigned int multi = id.z();
103 const unsigned int ymax = std::min(y0 + strategy::out_height(), m_end);
104
Georgios Pinitasa4658ae2018-07-27 12:20:32 +0100105 // TODO(COMPMID-1424) : Agree on gemm IO layouts
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100106 strat.kernel(a(0, y0, batch, multi), a.stride(Window::DimY),
Georgios Pinitasa4658ae2018-07-27 12:20:32 +0100107 b(0, 0, multi), b.stride(Window::DimY),
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100108 c(0, y0, batch, multi), c.stride(Window::DimY),
109 _beta, (ymax - y0), _params.N, _params.K);
110 });
111
112 auto on_new_row_size = [&](unsigned int start, unsigned int end)
113 {
114 m_end = std::min(end, _params.M);
115 };
116
117 window_iterator.iterate_3D(on_new_row_size);
118}
119
120#ifdef __aarch64__
121template class NEGEMMNativeWrapperKernel<float, float>;
122#endif /* __aarch64__ */
123
124} // namespace arm_compute