Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +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/NEGEMMInterleavedTransformAWrapper.h" |
| 26 | |
| 27 | #include "NEGEMMInterleavedStrategies.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/Utils.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/WindowIterator.h" |
| 33 | |
| 34 | #include "utils/TypePrinter.h" |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | template <typename To, bool use_dot> |
| 39 | void NEGEMMInterleavedTransformAWrapperTemplate<To, use_dot>::configure(const ITensor *a, ITensor *transformed_a, bool transpose_a, const Window &block_walker, |
| 40 | const INEGEMMWrapperKernel::Params ¶ms) |
| 41 | { |
| 42 | _a = a; |
| 43 | _transformed_a = transformed_a; |
| 44 | _transpose_a = transpose_a; |
| 45 | _Ksize = params.K; |
| 46 | _Msize = params.M; |
| 47 | _k_multi_window = block_walker.shift_dimensions(1); // block_walker contains (M,K,Multi) --> shift by 1 to get rid of the "M" dimension |
| 48 | } |
| 49 | |
| 50 | template <typename To, bool use_dot> |
| 51 | void NEGEMMInterleavedTransformAWrapperTemplate<To, use_dot>::transform(const TransformAWorkload &wl, const ThreadInfo &info, const Window &batch_window, const Coordinates &start_offset, |
| 52 | const Coordinates &end_offset) |
| 53 | { |
| 54 | using strategy = typename Kernel<To, use_dot>::strategy; |
| 55 | |
| 56 | strategy strat(info.cpu_info); |
| 57 | TensorAccessor<To> a(*_a); |
| 58 | TensorAccessor<To> transformed_a(*_transformed_a); |
| 59 | |
| 60 | if(_a->info()->data_layout() == DataLayout::NHWC) |
| 61 | { |
| 62 | // In the case of NHWC we want to interpret the output shape as 3D. Thus, the batch stride for A is |
| 63 | // the relevant multiple of the row stride. |
| 64 | const size_t nhwc_batch_stride = _a->info()->strides_in_bytes().y() * _Msize; |
| 65 | a.set_stride(2, nhwc_batch_stride); |
| 66 | } |
| 67 | |
| 68 | unsigned int last_m = 0; |
| 69 | //TODO: Create a new iterate_1D( DimY); |
| 70 | int last_y = -1; |
| 71 | auto window_iterator = arm_compute::create_window_iterator(batch_window, start_offset, end_offset, [&](const Coordinates & id) |
| 72 | { |
| 73 | if(id.y() != last_y) |
| 74 | { |
| 75 | last_y = id.y(); |
| 76 | unsigned int batch = id.y(); |
| 77 | unsigned int first_m = id.x(); |
| 78 | |
| 79 | if(first_m >= last_m) |
| 80 | return; |
| 81 | |
| 82 | strat.transforms.PrepareA(transformed_a(0, first_m, batch), |
| 83 | a(0, 0, batch, wl._multi), |
| 84 | a.stride(1), first_m, last_m, wl._k0, wl._kmax, _transpose_a); |
| 85 | } |
| 86 | }); |
| 87 | auto on_new_row_size = [&](unsigned int start, unsigned int end) |
| 88 | { |
| 89 | last_m = std::min(end, _Msize); |
| 90 | }; |
| 91 | window_iterator.iterate_2D(on_new_row_size); |
| 92 | } |
| 93 | |
| 94 | template <typename To, bool use_dot> |
| 95 | void NEGEMMInterleavedTransformAWrapperTemplate<To, use_dot>::create_workloads(std::vector<TransformAWorkload> &workloads) |
| 96 | { |
| 97 | execute_window_loop(_k_multi_window, [&](const Coordinates & id) |
| 98 | { |
| 99 | const unsigned int k0 = id.x(); |
| 100 | const unsigned int multi = id.y(); |
| 101 | const unsigned int kmax = std::min(k0 + _k_multi_window.x().step(), _Ksize); |
| 102 | |
| 103 | workloads.push_back(TransformAWorkload(k0, kmax, multi)); |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | template class NEGEMMInterleavedTransformAWrapperTemplate<float>; |
| 108 | #ifdef __aarch64__ |
| 109 | template class NEGEMMInterleavedTransformAWrapperTemplate<uint8_t>; |
| 110 | template class NEGEMMInterleavedTransformAWrapperTemplate<int8_t>; |
| 111 | template class NEGEMMInterleavedTransformAWrapperTemplate<uint8_t, true>; |
| 112 | template class NEGEMMInterleavedTransformAWrapperTemplate<int8_t, true>; |
| 113 | #endif /* __aarch64__ */ |
| 114 | |
| 115 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 116 | template class NEGEMMInterleavedTransformAWrapperTemplate<float16_t>; |
| 117 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 118 | } // namespace arm_compute |