"tools/generate-unit-test-expect/nist.py" did not exist on "f146feca0a92f7d28335d9c811591de0cb5a42ae"
fuse_ck.cpp 2.18 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <migraphx/gpu/fuse_ck.hpp>
#include <migraphx/matcher.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/register_op.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

struct module;

namespace gpu {

struct ck_gemm
{
    operation op = make_op("dot");

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.op, "op"));
    }

    std::string name() const { return "gpu::ck_gemm"; }
Paul's avatar
Paul committed
25
26
27
28
29
30
31

    void check_gemm_shape(const shape& s) const
    {
        if (contains(s.lens(), 1))
            MIGRAPHX_THROW("Invalid shape for ck_gemm");
    }

Paul's avatar
Paul committed
32
33
    shape compute_shape(std::vector<shape> inputs, const std::vector<module_ref>& mods) const
    {
Paul's avatar
Paul committed
34
        check_shapes{inputs, *this}.not_broadcasted();
Paul's avatar
Paul committed
35
36
37
38
39
40
41
        // if(mods.size() != 1)
        //     MIGRAPHX_THROW("should have one submodule.");
        if(inputs.size() < 2)
            MIGRAPHX_THROW("should have at least two inputs.");
        auto n = inputs.size();
        auto a = inputs[n - 2];
        auto b = inputs[n - 1];
Paul's avatar
Paul committed
42
43
        check_gemm_shape(a);
        check_gemm_shape(b);
Paul's avatar
Paul committed
44
45
46
47
48
49
50
51
52
53
54
55
56
        return op.compute_shape({a, b});
    }
};
MIGRAPHX_REGISTER_OP(ck_gemm);

namespace {

MIGRAPHX_PRED_MATCHER(is_ck_gemm, instruction_ref ins)
{
    if(ins->name() != "dot")
        return false;
    auto a = ins->inputs().front()->get_shape();
    auto b = ins->inputs().back()->get_shape();
Paul's avatar
Paul committed
57
58
    if (a.lens().size() > 2 or b.lens().size() > 2)
        return false;
Paul's avatar
Format  
Paul committed
59
60
    return (a.lens()[0] % 8 == 0 and a.lens()[1] % 8 == 0 and b.lens()[0] % 8 == 0 and
            b.lens()[1] % 8 == 0);
Paul's avatar
Paul committed
61
62
63
64
65
}

struct find_ck_gemm
{
    // Find a convolution followed by a pointwise operation.
Paul's avatar
Format  
Paul committed
66
    auto matcher() const { return match::name("dot")(is_ck_gemm().bind("gemm")); }
Paul's avatar
Paul committed
67
68
69

    void apply(module_pass_manager& mpm, const match::matcher_result& r) const
    {
Paul's avatar
Format  
Paul committed
70
        auto ins = r.result;
Paul's avatar
Paul committed
71
72
73
74
75
76
        mpm.get_module().replace_instruction(ins, ck_gemm{ins->get_operator()}, ins->inputs());
    }
};

} // namespace

Paul's avatar
Format  
Paul committed
77
void fuse_ck::apply(module_pass_manager& mpm) const { match::find_matches(mpm, find_ck_gemm{}); }
Paul's avatar
Paul committed
78
79
80
81
82

} // namespace gpu

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx