blob: 7d14971b70eb560ba0003843a567ca99186bb91c [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/*
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 Manselle39334c2018-07-06 17:53:35 +010026#include "gemm_implementation.hpp"
Pablo Telloeb82fd22018-02-23 13:43:50 +000027#include "gemm_interleaved.hpp"
28#include "gemm_native.hpp"
David Mansellce8f6052018-05-17 18:51:26 +010029#include "gemv_batched.hpp"
Pablo Telloeb82fd22018-02-23 13:43:50 +000030#include "gemv_native_transposed.hpp"
31#include "gemv_pretransposed.hpp"
32
Pablo Telloeb82fd22018-02-23 13:43:50 +000033#include "kernels/a64_sgemm_12x8.hpp"
Anthony Barbier5f707732018-07-03 16:22:02 +010034#include "kernels/a32_sgemm_8x6.hpp"
Pablo Telloeb82fd22018-02-23 13:43:50 +000035#include "kernels/a64_sgemv_trans.hpp"
Anthony Barbier5f707732018-07-03 16:22:02 +010036#include "kernels/a64_sgemv_pretransposed.hpp"
37#include "kernels/a64_sgemm_native_16x4.hpp"
Pablo Telloeb82fd22018-02-23 13:43:50 +000038
Georgios Pinitas421405b2018-10-26 19:05:32 +010039#include "kernels/sve_interleaved_fp32_mla_3VLx8.hpp"
40
Anthony Barbier5f707732018-07-03 16:22:02 +010041namespace arm_gemm {
42
Georgios Pinitas421405b2018-10-26 19:05:32 +010043#if defined(__aarch64__) && !defined(__ARM_FEATURE_SVE)
44// SGEMM implementations for AArch64 without SVE
David Manselle39334c2018-07-06 17:53:35 +010045
46// Pretransposed GEMV
47class GemmImpl_sgemm_gemv_pretransposed : public GemmImplementation<float, float> {
48public:
49 bool is_supported(const GemmArgs<float> &args) override {
50 return (args._Msize==1 && args._alpha==1.0f && args._pretransposed_hint && args._nbatches==1);
Pablo Telloeb82fd22018-02-23 13:43:50 +000051 }
52
David Manselle39334c2018-07-06 17:53:35 +010053 UniqueGemmCommon<float, float> instantiate(const GemmArgs<float> &args) override {
54 return UniqueGemmCommon<float, float> (new GemvPretransposed<sgemv_pretransposed, float, float>(args._ci, args._Nsize, args._Ksize, args._nmulti, args._trB, args._beta));
Pablo Telloeb82fd22018-02-23 13:43:50 +000055 }
56
David Manselle39334c2018-07-06 17:53:35 +010057 GemmImpl_sgemm_gemv_pretransposed() : GemmImplementation<float, float>(GemmMethod::GEMV_PRETRANSPOSED) { }
58};
59
60// Native GEMV
61class GemmImpl_sgemm_gemv_native_transposed : public GemmImplementation<float, float> {
62public:
63 bool is_supported(const GemmArgs<float> &args) override {
64 return (args._Msize==1 && args._alpha==1.0f && !args._trA && !args._trB && args._nbatches==1);
Pablo Telloeb82fd22018-02-23 13:43:50 +000065 }
66
David Manselle39334c2018-07-06 17:53:35 +010067 UniqueGemmCommon<float, float> instantiate(const GemmArgs<float> &args) override {
68 return UniqueGemmCommon<float, float> (new GemvNativeTransposed<sgemv_trans, float, float>(args._ci, args._Nsize, args._Ksize, args._nmulti, args._beta));
69 }
70
71 GemmImpl_sgemm_gemv_native_transposed() : GemmImplementation<float, float>(GemmMethod::GEMV_NATIVE_TRANSPOSED) { }
72};
73
74// Native GEMM
75class GemmImpl_sgemm_gemm_native : public GemmImplementation<float, float> {
76public:
77 bool is_supported(const GemmArgs<float> &args) override {
78 return (args._Ksize>4 && (args._Nsize % 16)==0 && args._alpha==1.0f && !args._trA && !args._trB);
79 }
80
81 bool is_recommended(const GemmArgs<float> &args) override {
82 return ((args._Ksize <= 128) && (args._Nsize <= 128)) || ((args._nmulti > 1) && ((args._Msize / args._maxthreads) < 8));
83 }
84
85 UniqueGemmCommon<float, float> instantiate(const GemmArgs<float> &args) override {
86 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));
87 }
88
89 GemmImpl_sgemm_gemm_native() : GemmImplementation<float, float>(GemmMethod::GEMM_NATIVE) { }
90};
91#endif // __aarch64__
92
93// Interleaved GEMM
94class GemmImpl_sgemm_gemm_interleaved : public GemmImplementation<float, float> {
95public:
96 UniqueGemmCommon<float, float> instantiate(const GemmArgs<float> &args) override {
Georgios Pinitas421405b2018-10-26 19:05:32 +010097#ifdef __ARM_FEATURE_SVE
98 return UniqueGemmCommon<float, float> (new GemmInterleaved<interleaved_fp32_mla_3VLx8, float, float>(args));
99#elif defined(__aarch64__)
David Manselle39334c2018-07-06 17:53:35 +0100100 return UniqueGemmCommon<float, float> (new GemmInterleaved<sgemm_12x8, float, float>(args));
101#elif defined(__arm__)
102 return UniqueGemmCommon<float, float> (new GemmInterleaved<sgemm_8x6, float, float>(args));
Pablo Telloeb82fd22018-02-23 13:43:50 +0000103#else
David Manselle39334c2018-07-06 17:53:35 +0100104# error Unknown Architecture.
Pablo Telloeb82fd22018-02-23 13:43:50 +0000105#endif
David Manselle39334c2018-07-06 17:53:35 +0100106 }
107
108 GemmImpl_sgemm_gemm_interleaved() : GemmImplementation<float, float>(GemmMethod::GEMM_INTERLEAVED) { }
109};
110
Anthony Barbier92d20812018-07-13 11:35:30 +0100111static GemmImpl_gemv_batched<float, float> gemv_batched_impl{};
Georgios Pinitas421405b2018-10-26 19:05:32 +0100112#if defined(__aarch64__) && !defined(__ARM_FEATURE_SVE)
Anthony Barbier92d20812018-07-13 11:35:30 +0100113static GemmImpl_sgemm_gemv_pretransposed sgemm_gemv_pretransposed_impl{};
114static GemmImpl_sgemm_gemv_native_transposed sgemm_gemv_native_transposed_impl{};
115static GemmImpl_sgemm_gemm_native sgemm_gemm_native_impl{};
116#endif
117static GemmImpl_sgemm_gemm_interleaved sgemm_gemm_interleaved_impl{};
118
David Manselle39334c2018-07-06 17:53:35 +0100119/* List of implementations (order matters) */
120static std::vector<GemmImplementation<float, float> *> SGemmMethods = {
Anthony Barbier92d20812018-07-13 11:35:30 +0100121 &gemv_batched_impl,
Georgios Pinitas421405b2018-10-26 19:05:32 +0100122#if defined(__aarch64__) && !defined(__ARM_FEATURE_SVE)
Anthony Barbier92d20812018-07-13 11:35:30 +0100123 &sgemm_gemv_pretransposed_impl,
124 &sgemm_gemv_native_transposed_impl,
125 &sgemm_gemm_native_impl,
David Manselle39334c2018-07-06 17:53:35 +0100126#endif
Anthony Barbier92d20812018-07-13 11:35:30 +0100127 &sgemm_gemm_interleaved_impl
David Manselle39334c2018-07-06 17:53:35 +0100128};
129
130/* Templated function to return this list. */
131template<>
132std::vector<GemmImplementation<float, float> *> &gemm_implementation_list<float, float>() {
133 return SGemmMethods;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000134}
135
David Manselle39334c2018-07-06 17:53:35 +0100136/* Explicitly instantiate the external functions for these types. */
137template UniqueGemmCommon<float, float> gemm<float, float>(GemmArgs<float> &args, GemmConfig *cfg);
138template GemmMethod get_gemm_method<float, float>(GemmArgs<float> &args);
139template bool method_is_compatible<float, float>(GemmMethod method, GemmArgs<float> &args);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000140
141} // namespace arm_gemm