Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 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 | |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 37 | #include "../arm_gemm/kernels/a64_sgemm_native_16x4.hpp" |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 41 | namespace |
| 42 | { |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 43 | template <typename To, typename Tr> |
| 44 | struct Kernel |
| 45 | { |
| 46 | }; |
| 47 | |
| 48 | #ifdef __aarch64__ |
| 49 | template <> |
| 50 | struct Kernel<float, float> |
| 51 | { |
| 52 | using strategy = arm_gemm::sgemm_native_16x4; |
| 53 | }; |
| 54 | #endif /* __aarch64__ */ |
| 55 | |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 56 | } // namespace |
| 57 | |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 58 | template <typename To, typename Tr> |
| 59 | Window NEGEMMNativeWrapperKernel<To, Tr>::configure_internal(float alpha, float beta) |
| 60 | { |
Michalis Spyrou | 43c1bf8 | 2019-05-24 11:57:38 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_UNUSED(alpha); |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 62 | 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 | |
| 75 | template <typename To, typename Tr> |
| 76 | void 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 Pinitas | a4658ae | 2018-07-27 12:20:32 +0100 | [diff] [blame] | 102 | // TODO(COMPMID-1424) : Agree on gemm IO layouts |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 103 | strat.kernel(a(0, y0, batch, multi), a.stride(Window::DimY), |
Georgios Pinitas | a4658ae | 2018-07-27 12:20:32 +0100 | [diff] [blame] | 104 | b(0, 0, multi), b.stride(Window::DimY), |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 105 | 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 Spyrou | 43c1bf8 | 2019-05-24 11:57:38 +0100 | [diff] [blame] | 111 | ARM_COMPUTE_UNUSED(start); |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 112 | m_end = std::min(end, _params.M); |
| 113 | }; |
| 114 | |
| 115 | window_iterator.iterate_3D(on_new_row_size); |
| 116 | } |
| 117 | |
| 118 | #ifdef __aarch64__ |
| 119 | template class NEGEMMNativeWrapperKernel<float, float>; |
| 120 | #endif /* __aarch64__ */ |
| 121 | |
| 122 | } // namespace arm_compute |