aboutsummaryrefslogtreecommitdiff
path: root/src/core/NEON/kernels/arm_gemm/gemm_implementation.hpp
blob: 6734e3cce0853a205bac21e8fbc28cf2dd7466a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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<typename Top, typename Tret>
class GemmImplementation {
public:
    /* Is this implementation compatible with the args as provided? */
    virtual bool is_supported(const GemmArgs<Tret> &args)   { return true; }
    /* Is this implementation "recommended" for these args (heuristic)? */
    virtual bool is_recommended(const GemmArgs<Tret> &args) { return true; }
    /* Instantiate this method please. */
    virtual UniqueGemmCommon<Top, Tret> instantiate(const GemmArgs<Tret> &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<typename Top, typename Tret>
class GemmImpl_gemv_batched : public GemmImplementation<Top, Tret> {
public:
    bool is_supported(const GemmArgs<Tret> &args) override {
        return (args._Msize==1 && args._nbatches > 1);
    }

    UniqueGemmCommon<Top, Tret> instantiate(const GemmArgs<Tret> &args) override {
        return UniqueGemmCommon<Top, Tret> (new GemvBatched<Top, Tret>(args));
    }

    GemmImpl_gemv_batched() : GemmImplementation<Top, Tret>(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<typename Top, typename Tret>
std::vector<GemmImplementation<Top, Tret> *> &gemm_implementation_list();

template<typename Top, typename Tret>
GemmImplementation<Top, Tret> *find_implementation(GemmArgs<Tret> &args, GemmConfig *cfg) {
    auto gemms = gemm_implementation_list<Top, Tret>();

    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<typename Top, typename Tret>
UniqueGemmCommon<Top, Tret> gemm(GemmArgs<Tret> &args, GemmConfig *cfg) {
    auto impl = find_implementation<Top, Tret>(args, cfg);

    if (impl) {
        return impl->instantiate(args);
    }

    return UniqueGemmCommon<Top, Tret>(nullptr);
}

template<typename Top, typename Tret>
GemmMethod get_gemm_method(GemmArgs<Tret> &args) {
    auto impl = find_implementation<Top, Tret>(args, nullptr);

    if (impl) {
        return impl->method;
    }

    /* This shouldn't happen - there should always be at least one valid implementation. */
    return GemmMethod::DEFAULT;
}

template<typename Top, typename Tret>
bool method_is_compatible(GemmMethod method, GemmArgs<Tret> &args) {
    /* Determine if the method is valid by attempting to obtain an implementation specifying this method. */
    GemmConfig cfg(method);

    auto impl = find_implementation<Top, Tret>(args, &cfg);

    if (impl) {
        return true;
    }

    return false;
}

} // namespace arm_gemm