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