Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-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 | #include "arm_gemm.hpp" |
| 25 | #include "gemm_common.hpp" |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame^] | 26 | #include "gemm_implementation.hpp" |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 27 | #include "gemm_interleaved.hpp" |
| 28 | #include "gemm_native.hpp" |
David Mansell | ce8f605 | 2018-05-17 18:51:26 +0100 | [diff] [blame] | 29 | #include "gemv_batched.hpp" |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 30 | #include "gemv_native_transposed.hpp" |
| 31 | #include "gemv_pretransposed.hpp" |
| 32 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 33 | #include "kernels/a64_sgemm_12x8.hpp" |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 34 | #include "kernels/a32_sgemm_8x6.hpp" |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 35 | #include "kernels/a64_sgemv_trans.hpp" |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 36 | #include "kernels/a64_sgemv_pretransposed.hpp" |
| 37 | #include "kernels/a64_sgemm_native_16x4.hpp" |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 38 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 39 | namespace arm_gemm { |
| 40 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 41 | #ifdef __aarch64__ |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame^] | 42 | // SGEMM implementations for AArch64 |
| 43 | |
| 44 | // Pretransposed GEMV |
| 45 | class GemmImpl_sgemm_gemv_pretransposed : public GemmImplementation<float, float> { |
| 46 | public: |
| 47 | bool is_supported(const GemmArgs<float> &args) override { |
| 48 | return (args._Msize==1 && args._alpha==1.0f && args._pretransposed_hint && args._nbatches==1); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 49 | } |
| 50 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame^] | 51 | UniqueGemmCommon<float, float> instantiate(const GemmArgs<float> &args) override { |
| 52 | return UniqueGemmCommon<float, float> (new GemvPretransposed<sgemv_pretransposed, float, float>(args._ci, args._Nsize, args._Ksize, args._nmulti, args._trB, args._beta)); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 53 | } |
| 54 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame^] | 55 | GemmImpl_sgemm_gemv_pretransposed() : GemmImplementation<float, float>(GemmMethod::GEMV_PRETRANSPOSED) { } |
| 56 | }; |
| 57 | |
| 58 | // Native GEMV |
| 59 | class GemmImpl_sgemm_gemv_native_transposed : public GemmImplementation<float, float> { |
| 60 | public: |
| 61 | bool is_supported(const GemmArgs<float> &args) override { |
| 62 | return (args._Msize==1 && args._alpha==1.0f && !args._trA && !args._trB && args._nbatches==1); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 63 | } |
| 64 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame^] | 65 | UniqueGemmCommon<float, float> instantiate(const GemmArgs<float> &args) override { |
| 66 | return UniqueGemmCommon<float, float> (new GemvNativeTransposed<sgemv_trans, float, float>(args._ci, args._Nsize, args._Ksize, args._nmulti, args._beta)); |
| 67 | } |
| 68 | |
| 69 | GemmImpl_sgemm_gemv_native_transposed() : GemmImplementation<float, float>(GemmMethod::GEMV_NATIVE_TRANSPOSED) { } |
| 70 | }; |
| 71 | |
| 72 | // Native GEMM |
| 73 | class GemmImpl_sgemm_gemm_native : public GemmImplementation<float, float> { |
| 74 | public: |
| 75 | bool is_supported(const GemmArgs<float> &args) override { |
| 76 | return (args._Ksize>4 && (args._Nsize % 16)==0 && args._alpha==1.0f && !args._trA && !args._trB); |
| 77 | } |
| 78 | |
| 79 | bool is_recommended(const GemmArgs<float> &args) override { |
| 80 | return ((args._Ksize <= 128) && (args._Nsize <= 128)) || ((args._nmulti > 1) && ((args._Msize / args._maxthreads) < 8)); |
| 81 | } |
| 82 | |
| 83 | UniqueGemmCommon<float, float> instantiate(const GemmArgs<float> &args) override { |
| 84 | return UniqueGemmCommon<float, float> (new GemmNative<sgemm_native_16x4, float, float>(args._ci, args._Msize, args._Nsize, args._Ksize, args._nbatches, args._nmulti, args._beta)); |
| 85 | } |
| 86 | |
| 87 | GemmImpl_sgemm_gemm_native() : GemmImplementation<float, float>(GemmMethod::GEMM_NATIVE) { } |
| 88 | }; |
| 89 | #endif // __aarch64__ |
| 90 | |
| 91 | // Interleaved GEMM |
| 92 | class GemmImpl_sgemm_gemm_interleaved : public GemmImplementation<float, float> { |
| 93 | public: |
| 94 | UniqueGemmCommon<float, float> instantiate(const GemmArgs<float> &args) override { |
| 95 | #ifdef __aarch64__ |
| 96 | return UniqueGemmCommon<float, float> (new GemmInterleaved<sgemm_12x8, float, float>(args)); |
| 97 | #elif defined(__arm__) |
| 98 | return UniqueGemmCommon<float, float> (new GemmInterleaved<sgemm_8x6, float, float>(args)); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 99 | #else |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame^] | 100 | # error Unknown Architecture. |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 101 | #endif |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame^] | 102 | } |
| 103 | |
| 104 | GemmImpl_sgemm_gemm_interleaved() : GemmImplementation<float, float>(GemmMethod::GEMM_INTERLEAVED) { } |
| 105 | }; |
| 106 | |
| 107 | /* List of implementations (order matters) */ |
| 108 | static std::vector<GemmImplementation<float, float> *> SGemmMethods = { |
| 109 | new GemmImpl_gemv_batched<float, float>(), |
| 110 | #ifdef __aarch64__ |
| 111 | new GemmImpl_sgemm_gemv_pretransposed(), |
| 112 | new GemmImpl_sgemm_gemv_native_transposed(), |
| 113 | new GemmImpl_sgemm_gemm_native(), |
| 114 | #endif |
| 115 | new GemmImpl_sgemm_gemm_interleaved() |
| 116 | }; |
| 117 | |
| 118 | /* Templated function to return this list. */ |
| 119 | template<> |
| 120 | std::vector<GemmImplementation<float, float> *> &gemm_implementation_list<float, float>() { |
| 121 | return SGemmMethods; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 122 | } |
| 123 | |
David Mansell | e39334c | 2018-07-06 17:53:35 +0100 | [diff] [blame^] | 124 | /* Explicitly instantiate the external functions for these types. */ |
| 125 | template UniqueGemmCommon<float, float> gemm<float, float>(GemmArgs<float> &args, GemmConfig *cfg); |
| 126 | template GemmMethod get_gemm_method<float, float>(GemmArgs<float> &args); |
| 127 | template bool method_is_compatible<float, float>(GemmMethod method, GemmArgs<float> &args); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 128 | |
| 129 | } // namespace arm_gemm |