From e39334c15c7fd141bb8173d5017ea5ca157fca2c Mon Sep 17 00:00:00 2001 From: David Mansell Date: Fri, 6 Jul 2018 17:53:35 +0100 Subject: COMPMID-1271: New system for GEMM heuristics This patch implements a system for separating the "validity" from "preferred" aspect of the current heuristics in gemm_*.cpp. Now, each gemm_*.cpp defines a list of candidate implementations, each of which supplies an is_valid() function (to check for validity), an is_preferred() function (the "heuristic" part), and an instantiate() function which actually produces the GemmCommon object pointer. The actual gemm() function is now templated and uses this list to select an implementation. This patch also implements a mechanism to identify the preferred implementation, and override it via the GemmConfig structure. Change-Id: Id49ab7af8bf2e3e9fd951a9698883ade234d40e1 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/139120 Reviewed-by: Anthony Barbier Tested-by: Jenkins --- .../NEON/kernels/arm_gemm/gemm_implementation.hpp | 131 +++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/core/NEON/kernels/arm_gemm/gemm_implementation.hpp (limited to 'src/core/NEON/kernels/arm_gemm/gemm_implementation.hpp') diff --git a/src/core/NEON/kernels/arm_gemm/gemm_implementation.hpp b/src/core/NEON/kernels/arm_gemm/gemm_implementation.hpp new file mode 100644 index 0000000000..6734e3cce0 --- /dev/null +++ b/src/core/NEON/kernels/arm_gemm/gemm_implementation.hpp @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "gemv_batched.hpp" + +namespace arm_gemm { + +template +class GemmImplementation { +public: + /* Is this implementation compatible with the args as provided? */ + virtual bool is_supported(const GemmArgs &args) { return true; } + /* Is this implementation "recommended" for these args (heuristic)? */ + virtual bool is_recommended(const GemmArgs &args) { return true; } + /* Instantiate this method please. */ + virtual UniqueGemmCommon instantiate(const GemmArgs &args) = 0; + + /* Indicate the "GemmMethod" for use as a selector */ + const GemmMethod method; + + virtual ~GemmImplementation() { } + + GemmImplementation(GemmMethod method) : method(method) { } +}; + +/* "gemv_batched" implementation is type-agnostic, so template it here. */ +template +class GemmImpl_gemv_batched : public GemmImplementation { +public: + bool is_supported(const GemmArgs &args) override { + return (args._Msize==1 && args._nbatches > 1); + } + + UniqueGemmCommon instantiate(const GemmArgs &args) override { + return UniqueGemmCommon (new GemvBatched(args)); + } + + GemmImpl_gemv_batched() : GemmImplementation(GemmMethod::GEMV_BATCHED) { } +}; + +/* "Master" function implemented for each valid combination of types. + * Returns a list of GEMM implementation descriptors for processing by the + * other functions. */ +template +std::vector *> &gemm_implementation_list(); + +template +GemmImplementation *find_implementation(GemmArgs &args, GemmConfig *cfg) { + auto gemms = gemm_implementation_list(); + + for(auto &&i : gemms) { + /* Skip if this implementation doesn't support these args. */ + if (!i->is_supported(args)) { + continue; + } + + /* Skip if a specific method is requested and this is a different one. */ + if (cfg && cfg->method != GemmMethod::DEFAULT && i->method != cfg->method) { + continue; + } + + /* If no specific method is requested, check that this method recommends itself. */ + if ((!cfg || cfg->method == GemmMethod::DEFAULT) && !i->is_recommended(args)) { + continue; + } + + return i; + } + + return nullptr; +} + +template +UniqueGemmCommon gemm(GemmArgs &args, GemmConfig *cfg) { + auto impl = find_implementation(args, cfg); + + if (impl) { + return impl->instantiate(args); + } + + return UniqueGemmCommon(nullptr); +} + +template +GemmMethod get_gemm_method(GemmArgs &args) { + auto impl = find_implementation(args, nullptr); + + if (impl) { + return impl->method; + } + + /* This shouldn't happen - there should always be at least one valid implementation. */ + return GemmMethod::DEFAULT; +} + +template +bool method_is_compatible(GemmMethod method, GemmArgs &args) { + /* Determine if the method is valid by attempting to obtain an implementation specifying this method. */ + GemmConfig cfg(method); + + auto impl = find_implementation(args, &cfg); + + if (impl) { + return true; + } + + return false; +} + +} // namespace arm_gemm -- cgit v1.2.1